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) { var newTheme = "light"; // save changes using (var db = new voatEntities()) { var userPreferences = db.Userpreferences.Find(User.Identity.Name); if (userPreferences != null) { // modify existing preferences userPreferences.Disable_custom_css = model.Disable_custom_css; userPreferences.Night_mode = model.Night_mode; userPreferences.Clicking_mode = model.OpenLinksInNewTab; userPreferences.Enable_adult_content = model.Enable_adult_content; userPreferences.Public_subscriptions = model.Public_subscriptions; userPreferences.Topmenu_from_subscriptions = model.Topmenu_from_subscriptions; await db.SaveChangesAsync(); // apply theme change newTheme = userPreferences.Night_mode ? "dark" : "light"; //Session["UserTheme"] = Utils.User.UserStylePreference(User.Identity.Name); } else { // create a new record for this user in userpreferences table var tmpModel = new Userpreference { Disable_custom_css = model.Disable_custom_css, Night_mode = model.Night_mode, Clicking_mode = model.OpenLinksInNewTab, Enable_adult_content = model.Enable_adult_content, Public_subscriptions = model.Public_subscriptions, Topmenu_from_subscriptions = model.Topmenu_from_subscriptions, Username = User.Identity.Name }; db.Userpreferences.Add(tmpModel); await db.SaveChangesAsync(); newTheme = userPreferences.Night_mode ? "dark" : "light"; // apply theme change //Session["UserTheme"] = Utils.User.UserStylePreference(User.Identity.Name); } } //return RedirectToAction("Manage", new { Message = "Your user preferences have been saved." }); Utils.User.SetUserStylePreferenceCookie(newTheme); return RedirectToAction("Manage"); }
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.Night_mode = !userPreferences.Night_mode; await db.SaveChangesAsync(); newTheme = userPreferences.Night_mode ? "dark" : "light"; // apply theme change //Session["UserTheme"] = Utils.User.UserStylePreference(User.Identity.Name); } else { // create a new record for this user in userpreferences table var tmpModel = new Userpreference { Disable_custom_css = false, //Since if user has no pref, they must have been on the light theme Night_mode = true, Clicking_mode = false, Enable_adult_content = false, Public_subscriptions = false, Topmenu_from_subscriptions = false, Username = User.Identity.Name }; db.Userpreferences.Add(tmpModel); await db.SaveChangesAsync(); // apply theme change newTheme = "dark"; //Session["UserTheme"] = Utils.User.UserStylePreference(User.Identity.Name); } } Utils.User.SetUserStylePreferenceCookie(newTheme); Response.StatusCode = 200; return Json("Toggled Night Mode", JsonRequestBehavior.AllowGet); }
public async Task<ActionResult> UserPreferencesAbout([Bind(Include = "Shortbio, 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.Shortbio = model.Shortbio; tmpModel.Username = User.Identity.Name; } 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 = 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.Shortbio = model.Shortbio; userPreferences.Username = User.Identity.Name; await db.SaveChangesAsync(); } } return RedirectToAction("Manage"); }