public async Task<ActionResult> CreateSubverse([Bind(Include = "Name, Title, Description, Type, Sidebar, Creation_date, Owner")] AddSubverse subverseTmpModel) { // abort if model state is invalid if (!ModelState.IsValid) return View(); int minimumCcp = MvcApplication.MinimumCcp; int maximumOwnedSubs = MvcApplication.MaximumOwnedSubs; // verify recaptcha if user has less than minimum required CCP if (Karma.CommentKarma(User.Identity.Name) < minimumCcp) { // begin recaptcha check bool isCaptchaCodeValid = await ReCaptchaUtility.Validate(Request); if (!isCaptchaCodeValid) { ModelState.AddModelError("", "Incorrect recaptcha answer."); // TODO // SET PREVENT SPAM DELAY TO 0 return View(); } } // only allow users with less than maximum allowed subverses to create a subverse var amountOfOwnedSubverses = _db.SubverseAdmins .Where(s => s.Username == User.Identity.Name && s.Power == 1) .ToList(); if (amountOfOwnedSubverses.Count >= maximumOwnedSubs) { ModelState.AddModelError(string.Empty, "Sorry, you can not own more than " + maximumOwnedSubs + " subverses."); return View(); } // check if subverse already exists if (SubverseCache.Retrieve(subverseTmpModel.Name) != null) { ModelState.AddModelError(string.Empty, "Sorry, The subverse you are trying to create already exists, but you can try to claim it by submitting a takeover request to /v/subverserequest."); return View(); } try { // setup default values and create the subverse var subverse = new Subverse { name = subverseTmpModel.Name, title = "/v/" + subverseTmpModel.Name, description = subverseTmpModel.Description, sidebar = subverseTmpModel.Sidebar, creation_date = DateTime.Now, type = "link", enable_thumbnails = true, rated_adult = false, private_subverse = false, minimumdownvoteccp = 0, admin_disabled = false }; _db.Subverses.Add(subverse); await _db.SaveChangesAsync(); // subscribe user to the newly created subverse Utils.User.SubscribeToSubverse(subverseTmpModel.Owner, subverse.name); // register user as the owner of the newly created subverse var tmpSubverseAdmin = new SubverseAdmin { SubverseName = subverseTmpModel.Name, Username = User.Identity.Name, Power = 1 }; _db.SubverseAdmins.Add(tmpSubverseAdmin); await _db.SaveChangesAsync(); // go to newly created Subverse return RedirectToAction("SubverseIndex", "Subverses", new { subversetoshow = subverseTmpModel.Name }); } catch (Exception) { ModelState.AddModelError(string.Empty, "Something bad happened, please report this to /v/voatdev. Thank you."); return View(); } }
public ActionResult AcceptModInvitation(int invitationId) { int maximumOwnedSubs = MvcApplication.MaximumOwnedSubs; // check if there is an invitation for this user with this id var userInvitation = _db.Moderatorinvitations.Find(invitationId); if (userInvitation == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } // check if logged in user is actually the invited user if (User.Identity.Name != userInvitation.Sent_to) { return new HttpStatusCodeResult(HttpStatusCode.Unauthorized); } // check if user is over modding limits var amountOfSubsUserModerates = _db.SubverseAdmins.Where(s => s.Username.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase)); if (amountOfSubsUserModerates.Any()) { if (amountOfSubsUserModerates.Count() >= maximumOwnedSubs) { ModelState.AddModelError(string.Empty, "Sorry, you can not own or moderate more than " + maximumOwnedSubs + " subverses."); return RedirectToAction("Index", "Home"); } } // check if subverse exists var subverseToAddModTo = _db.Subverses.FirstOrDefault(s => s.name.Equals(userInvitation.Subverse, StringComparison.OrdinalIgnoreCase)); if (subverseToAddModTo == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } // check if user is already a moderator of this sub var userModerating = _db.SubverseAdmins.Where(s => s.SubverseName.Equals(userInvitation.Subverse, StringComparison.OrdinalIgnoreCase) && s.Username.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase)); if (userModerating.Any()) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } // add user as moderator as specified in invitation var subAdm = new SubverseAdmin { SubverseName = userInvitation.Subverse, Username = userInvitation.Sent_to, Power = userInvitation.Power, Added_by = userInvitation.Sent_by, Added_on = DateTime.Now }; _db.SubverseAdmins.Add(subAdm); // notify sender that user has accepted the invitation StringBuilder confirmation = new StringBuilder(); confirmation.Append("User " + User.Identity.Name + " has accepted your invitation to moderate subverse /v/" + userInvitation.Subverse + "."); confirmation.AppendLine(); MesssagingUtility.SendPrivateMessage("Voat", userInvitation.Sent_by, "Moderator invitation for " + userInvitation.Subverse + " accepted", confirmation.ToString()); // delete the invitation from database _db.Moderatorinvitations.Remove(userInvitation); _db.SaveChanges(); return RedirectToAction("SubverseSettings", "Subverses", new { subversetoshow = userInvitation.Subverse }); }