// a method to mark single or all private messages as read for a given user public static async Task<bool> MarkPrivateMessagesAsRead(bool? markAll, string userName, int? itemId) { using (var db = new whoaverseEntities()) { try { // mark all items as read if (markAll != null && (bool) markAll) { IQueryable<Privatemessage> unreadPrivateMessages = db.Privatemessages .Where(s => s.Recipient.Equals(userName, StringComparison.OrdinalIgnoreCase) && s.Status) .OrderByDescending(s => s.Timestamp) .ThenBy(s => s.Sender); if (!unreadPrivateMessages.Any()) return false; foreach (var singleMessage in unreadPrivateMessages.ToList()) { singleMessage.Status = false; } await db.SaveChangesAsync(); return true; } // mark single item as read if (itemId != null) { var privateMessageToMarkAsread = db.Privatemessages.FirstOrDefault(s => s.Recipient.Equals(userName, StringComparison.OrdinalIgnoreCase) && s.Status && s.Id == itemId); if (privateMessageToMarkAsread == null) return false; var item = db.Privatemessages.Find(itemId); item.Status = false; await db.SaveChangesAsync(); return true; } return false; } catch (Exception) { return false; } } }
public async Task<ActionResult> ToggleNightMode() { // save changes using (var db = new whoaverseEntities()) { var userPreferences = db.Userpreferences.Find(User.Identity.Name); if (userPreferences != null) { // modify existing preferences userPreferences.Night_mode = !userPreferences.Night_mode; await db.SaveChangesAsync(); // 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 Session["UserTheme"] = Utils.User.UserStylePreference(User.Identity.Name); } } Response.StatusCode = 200; return Json("Toggled Night Mode", JsonRequestBehavior.AllowGet); }
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) { // save changes using (var db = new whoaverseEntities()) { 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 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(); // apply theme change Session["UserTheme"] = Utils.User.UserStylePreference(User.Identity.Name); } } //return RedirectToAction("Manage", new { Message = "Your user preferences have been saved." }); return RedirectToAction("Manage"); }
public async Task<ActionResult> UserPreferencesAbout([Bind(Include = "Shortbio, Avatarfile")] UserAboutViewModel model) { // save changes using (var db = new whoaverseEntities()) { 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"); }