示例#1
0
        public async Task<ActionResult> UserPreferences([Bind(Include = "Disable_custom_css, Night_mode, OpenLinksInNewTab, Enable_adult_content, Public_subscriptions, Topmenu_from_subscriptions, Shortbio, Avatar")] UserPreferencesViewModel model)
        {
            if (!ModelState.IsValid) return View("Manage", model);

            // save changes
            string newTheme;
            using (var db = new voatEntities())
            {
                var userPreferences = db.UserPreferences.Find(User.Identity.Name);

                if (userPreferences != null)
                {
                    // modify existing preferences
                    userPreferences.DisableCSS = model.Disable_custom_css;
                    userPreferences.NightMode = model.Night_mode;
                    userPreferences.OpenInNewWindow = model.OpenLinksInNewTab;
                    userPreferences.EnableAdultContent = model.Enable_adult_content;
                    userPreferences.DisplaySubscriptions = model.Public_subscriptions;
                    userPreferences.UseSubscriptionsMenu = model.Topmenu_from_subscriptions;

                    await db.SaveChangesAsync();
                    newTheme = userPreferences.NightMode ? "dark" : "light";
                }
                else
                {
                    // create a new record for this user in userpreferences table
                    var tmpModel = new UserPreference
                    {
                        DisableCSS = model.Disable_custom_css ? true : false,
                        NightMode = model.Night_mode ? true : false,
                        Language = "en",
                        OpenInNewWindow = model.OpenLinksInNewTab ? true : false,
                        EnableAdultContent = model.Enable_adult_content ? true : false,
                        DisplayVotes = false,
                        DisplaySubscriptions = model.Public_subscriptions ? true : false,
                        UseSubscriptionsMenu = model.Topmenu_from_subscriptions,
                        UserName = User.Identity.Name
                    };
                    db.UserPreferences.Add(tmpModel);

                    await db.SaveChangesAsync();
                    newTheme = tmpModel.NightMode ? "dark" : "light";
                }
            }

            UserHelper.SetUserStylePreferenceCookie(newTheme);
            return RedirectToAction("Manage");
        }
示例#2
0
        public async Task<ActionResult> ToggleNightMode()
        {
            string newTheme = "light";
            // save changes
            using (var db = new voatEntities())
            {
                var userPreferences = db.UserPreferences.Find(User.Identity.Name);

                if (userPreferences != null)
                {
                    // modify existing preferences
                    userPreferences.NightMode = !userPreferences.NightMode;
                    await db.SaveChangesAsync();
                    newTheme = userPreferences.NightMode ? "dark" : "light";
                    // apply theme change
                    //Session["UserTheme"] = UserHelper.UserStylePreference(User.Identity.Name);
                }
                else
                {
                    // create a new record for this user in userpreferences table
                    var tmpModel = new UserPreference
                    {
                        DisableCSS = false,
                        //Since if user has no pref, they must have been on the light theme
                        NightMode = true,
                        OpenInNewWindow = false,
                        EnableAdultContent = false,
                        DisplaySubscriptions = false,
                        UseSubscriptionsMenu = false,
                        UserName = User.Identity.Name
                    };
                    db.UserPreferences.Add(tmpModel);

                    await db.SaveChangesAsync();
                    // apply theme change
                    newTheme = "dark";
                    //Session["UserTheme"] = UserHelper.UserStylePreference(User.Identity.Name);
                }
            }

            UserHelper.SetUserStylePreferenceCookie(newTheme);
            Response.StatusCode = 200;
            return Json("Toggled Night Mode", JsonRequestBehavior.AllowGet);
        }
示例#3
0
        public async Task<ActionResult> UserPreferencesAbout([Bind(Include = "Bio, Avatarfile")] UserAboutViewModel model)
        {
            // save changes
            using (var db = new voatEntities())
            {
                var userPreferences = db.UserPreferences.Find(User.Identity.Name);
                var tmpModel = new UserPreference();

                if (userPreferences == null)
                {
                    // create a new record for this user in userpreferences table
                    tmpModel.DisableCSS = false;
                    tmpModel.NightMode = false;
                    tmpModel.Language = "en";
                    tmpModel.OpenInNewWindow = false;
                    tmpModel.EnableAdultContent = false;
                    tmpModel.DisplayVotes = false;
                    tmpModel.DisplaySubscriptions = false;
                    tmpModel.UseSubscriptionsMenu = false;
                    tmpModel.UserName = User.Identity.Name;
                    tmpModel.Bio = model.Bio;
                }

                if (model.Avatarfile != null && model.Avatarfile.ContentLength > 0)
                {
                    // check uploaded file size is < 300000 bytes (300 kilobytes)
                    if (model.Avatarfile.ContentLength < 300000)
                    {
                        try
                        {
                            using (var img = Image.FromStream(model.Avatarfile.InputStream))
                            {
                                if (img.RawFormat.Equals(ImageFormat.Jpeg) || img.RawFormat.Equals(ImageFormat.Png))
                                {
                                    // resize uploaded file
                                    var thumbnailResult = await ThumbGenerator.GenerateAvatar(img, User.Identity.Name, model.Avatarfile.ContentType);
                                    if (thumbnailResult)
                                    {
                                        if (userPreferences == null)
                                        {
                                            tmpModel.Avatar = User.Identity.Name + ".jpg";
                                        }
                                        else
                                        {
                                            userPreferences.Avatar = User.Identity.Name + ".jpg";
                                        }
                                    }
                                    else
                                    {
                                        // unable to generate thumbnail
                                        ModelState.AddModelError("", "Uploaded file is not recognized as a valid image.");
                                        return RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat });
                                    }
                                }
                                else
                                {
                                    // uploaded file was invalid
                                    ModelState.AddModelError("", "Uploaded file is not recognized as an image.");
                                    return RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat });
                                }
                            }
                        }
                        catch (Exception)
                        {
                            // uploaded file was invalid
                            ModelState.AddModelError("", "Uploaded file is not recognized as an image.");
                            return RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat });
                        }
                    }
                    else
                    {
                        // refuse to save the file and explain why
                        ModelState.AddModelError("", "Uploaded image may not exceed 300 kb, please upload a smaller image.");
                        return RedirectToAction("Manage", new { Message = ManageMessageId.UploadedFileToolarge });
                    }
                }

                if (userPreferences == null)
                {
                    db.UserPreferences.Add(tmpModel);
                    await db.SaveChangesAsync();
                }
                else
                {
                    userPreferences.Bio = model.Bio;
                    userPreferences.UserName = User.Identity.Name;
                    await db.SaveChangesAsync();
                }

            }

            return RedirectToAction("Manage");
        }