public async Task <ActionResult> Update(SubverseSettingsViewModel updatedModel)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    SetNavigationViewModel(updatedModel.Name);
                    return(View("~/Views/Subverses/Admin/SubverseSettings.cshtml", updatedModel));
                }
                var existingSubverse = _db.Subverse.FirstOrDefault(x => x.Name.ToUpper() == updatedModel.Name.ToUpper());

                // check if subverse exists before attempting to edit it
                if (existingSubverse != null)
                {
                    SetNavigationViewModel(existingSubverse.Name);

                    // check if user requesting edit is authorized to do so for current subverse
                    if (!ModeratorPermission.HasPermission(User, updatedModel.Name, Domain.Models.ModeratorAction.ModifySettings))
                    {
                        return(new EmptyResult());
                    }
                    //check description for banned domains
                    if (BanningUtility.ContentContainsBannedDomain(existingSubverse.Name, updatedModel.Description))
                    {
                        ModelState.AddModelError(string.Empty, "Sorry, description text contains banned domains.");
                        return(View("~/Views/Subverses/Admin/SubverseSettings.cshtml", updatedModel));
                    }
                    //check sidebar for banned domains
                    if (BanningUtility.ContentContainsBannedDomain(existingSubverse.Name, updatedModel.SideBar))
                    {
                        ModelState.AddModelError(string.Empty, "Sorry, sidebar text contains banned domains.");
                        return(View("~/Views/Subverses/Admin/SubverseSettings.cshtml", updatedModel));
                    }

                    // TODO investigate if EntityState is applicable here and use that instead
                    // db.Entry(updatedModel).State = EntityState.Modified;

                    existingSubverse.Title       = updatedModel.Title;
                    existingSubverse.Description = updatedModel.Description;
                    existingSubverse.SideBar     = updatedModel.SideBar;

                    //if (updatedModel.Stylesheet != null)
                    //{
                    //    if (updatedModel.Stylesheet.Length < 50001)
                    //    {
                    //        existingSubverse.Stylesheet = updatedModel.Stylesheet;
                    //    }
                    //    else
                    //    {
                    //        ModelState.AddModelError(string.Empty, "Sorry, custom CSS limit is set to 50000 characters.");
                    //        return View("~/Views/Subverses/Admin/SubverseSettings.cshtml", updatedModel);
                    //    }
                    //}
                    //else
                    //{
                    //    existingSubverse.Stylesheet = updatedModel.Stylesheet;
                    //}

                    existingSubverse.IsAdult = updatedModel.IsAdult;

                    existingSubverse.IsThumbnailEnabled  = updatedModel.IsThumbnailEnabled;
                    existingSubverse.IsAuthorizedOnly    = updatedModel.IsAuthorizedOnly;
                    existingSubverse.ExcludeSitewideBans = updatedModel.ExcludeSitewideBans;

                    //Only update if time lock has expired
                    if (existingSubverse.LastUpdateDate == null || (Repository.CurrentDate.Subtract(existingSubverse.LastUpdateDate.Value) > TimeSpan.FromHours(VoatSettings.Instance.SubverseUpdateTimeLockInHours)))
                    {
                        existingSubverse.MinCCPForDownvote = updatedModel.MinCCPForDownvote;
                        existingSubverse.IsPrivate         = updatedModel.IsPrivate;
                    }

                    // these properties are currently not implemented but they can be saved and edited for future use
                    //existingSubverse.Type = updatedModel.Type;
                    //existingSubverse.SubmitLinkLabel = updatedModel.SubmitLinkLabel;
                    //existingSubverse.SubmitPostLabel = updatedModel.SubmitPostLabel;
                    //existingSubverse.SubmissionText = updatedModel.SubmissionText;
                    //existingSubverse.IsDefaultAllowed = updatedModel.IsDefaultAllowed;

                    //if (existingSubverse.IsAnonymized == true && updatedModel.IsAnonymized == false)
                    //{
                    //    ModelState.AddModelError(string.Empty, "Sorry, this subverse is permanently locked to anonymized mode.");
                    //    return View("~/Views/Subverses/Admin/SubverseSettings.cshtml", updatedModel);
                    //}

                    // only subverse owners should be able to convert a sub to anonymized mode
                    if (ModeratorPermission.IsLevel(User, updatedModel.Name, Domain.Models.ModeratorLevel.Owner))
                    {
                        existingSubverse.IsAnonymized = updatedModel.IsAnonymized;
                    }

                    existingSubverse.LastUpdateDate = Repository.CurrentDate;
                    await _db.SaveChangesAsync();

                    //purge new minified CSS
                    CacheHandler.Instance.Remove(CachingKey.SubverseStylesheet(existingSubverse.Name));

                    //purge subvere
                    CacheHandler.Instance.Remove(CachingKey.Subverse(existingSubverse.Name));

                    // go back to this subverse
                    return(RedirectToRoute(Models.ROUTE_NAMES.SUBVERSE_INDEX, new { subverse = updatedModel.Name }));

                    // user was not authorized to commit the changes, drop attempt
                }
                ModelState.AddModelError(string.Empty, "Sorry, The subverse you are trying to edit does not exist.");
                return(View("~/Views/Subverses/Admin/SubverseSettings.cshtml", updatedModel));
            }
            catch (Exception ex)
            {
                EventLogger.Instance.Log(ex);
                ModelState.AddModelError(string.Empty, "Something bad happened.");
                return(View("~/Views/Subverses/Admin/SubverseSettings.cshtml", updatedModel));
            }
        }
        public async Task <ActionResult> UserPreferencesAbout([Bind("Bio, Avatarfile")] UserAboutViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("Manage", model));
            }

            ViewBag.UserName = User.Identity.Name;

            string avatarKey = null;

            //ThumbGenerator.GenerateThumbnail
            if (model.Avatarfile != null)
            {
                try
                {
                    var stream = model.Avatarfile.OpenReadStream();
                    var result = await ThumbGenerator.GenerateAvatar(stream, model.Avatarfile.FileName, model.Avatarfile.ContentType);

                    if (result.Success)
                    {
                        avatarKey = result.Response;
                    }
                    else
                    {
                        ModelState.AddModelError("Avatarfile", result.Message);
                        return(View("Manage", model));
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Avatarfile", "Uploaded file is not recognized as a valid image.");
                    return(RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat }));
                }
            }

            var bio = model.Bio.TrimSafe();

            //This is a hack
            var context = new VoatOutOfRepositoryDataContextAccessor();

            using (var repo = new Repository(User, context))
            {
                var p = await repo.GetUserPreferences(User.Identity.Name);

                if (bio != p.Bio)
                {
                    if (String.IsNullOrEmpty(bio))
                    {
                        p.Bio = "I tried to delete my bio but they gave me this instead";
                    }
                    else if (bio == STRINGS.DEFAULT_BIO)
                    {
                        p.Bio = null;
                    }
                    else
                    {
                        p.Bio = bio;
                    }
                }
                if (!String.IsNullOrEmpty(avatarKey))
                {
                    p.Avatar = avatarKey;
                }
                await context.SaveChangesAsync();
            }

            /*
             * using (var db = new VoatUIDataContextAccessor())
             * {
             *  var userPreferences = GetUserPreference(db);
             *
             *  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)
             *                      {
             *                          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 });
             *      }
             *  }
             *
             *  var bio = model.Bio.TrimSafe();
             *
             *  if (String.IsNullOrEmpty(bio))
             *  {
             *      userPreferences.Bio = "I tried to delete my bio but they gave me this instead";
             *  }
             *  else if (bio == STRINGS.DEFAULT_BIO)
             *  {
             *      userPreferences.Bio = null;
             *  }
             *  else
             *  {
             *      userPreferences.Bio = bio;
             *  }
             *  await db.SaveChangesAsync();
             * }
             */
            ClearUserCache();

            return(RedirectToAction("Manage"));
        }