Esempio n. 1
0
        public async Task<ActionResult> CreateSubverse([Bind(Include = "Name, Title, Description, Type, Sidebar, CreationDate, Owner")] AddSubverse subverseTmpModel)
        {
            // abort if model state is invalid
            if (!ModelState.IsValid) return View();

            int minimumCcp = Settings.MinimumCcp;
            int maximumOwnedSubs = Settings.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.SubverseModerators
                .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 (DataCache.Subverse.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,
                    CreationDate = DateTime.Now,
                    Type = "link",
                    IsThumbnailEnabled = true,
                    IsAdult = false,
                    IsPrivate = false,
                    MinCCPForDownvote = 0,
                    IsAdminDisabled = false,
                    CreatedBy = User.Identity.Name
                };

                _db.Subverses.Add(subverse);
                await _db.SaveChangesAsync();

                // subscribe user to the newly created subverse
                UserHelper.SubscribeToSubverse(subverseTmpModel.Owner, subverse.Name);

                // register user as the owner of the newly created subverse
                var tmpSubverseAdmin = new SubverseModerator
                {
                    Subverse = subverseTmpModel.Name,
                    UserName = User.Identity.Name,
                    Power = 1
                };
                _db.SubverseModerators.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();
            }
        }
Esempio n. 2
0
        public ActionResult AcceptModInvitation(int invitationId)
        {
            int maximumOwnedSubs = Settings.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.Equals(userInvitation.Recipient, StringComparison.OrdinalIgnoreCase))
            {
                return new HttpStatusCodeResult(HttpStatusCode.Unauthorized);
            }

            // check if user is over modding limits
            var amountOfSubsUserModerates = _db.SubverseModerators.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.SubverseModerators.Where(s => s.Subverse.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 SubverseModerator
            {
                Subverse = userInvitation.Subverse,
                UserName = userInvitation.Recipient,
                Power = userInvitation.Power,
                CreatedBy = userInvitation.CreatedBy,
                CreationDate = DateTime.Now
            };

            _db.SubverseModerators.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.CreatedBy, "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 });
        }