public async Task <ActionResult> ResignAsModeratorPost(string subverse)
        {
            // get moderator name for selected subverse
            var subModerator = _db.SubverseModerator.FirstOrDefault(s => s.Subverse == subverse && s.UserName == User.Identity.Name);

            if (subModerator == null)
            {
                return(HybridError(ErrorViewModel.GetErrorViewModel(ErrorType.NotFound)));
                //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var subverseObject = DataCache.Subverse.Retrieve(subModerator.Subverse);

            if (subverseObject == null)
            {
                return(HybridError(ErrorViewModel.GetErrorViewModel(ErrorType.SubverseNotFound)));
                //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            // execute removal
            _db.SubverseModerator.Remove(subModerator);
            await _db.SaveChangesAsync();

            //clear mod cache
            CacheHandler.Instance.Remove(CachingKey.SubverseModerators(subverseObject.Name));

            return(RedirectToRoute(Models.ROUTE_NAMES.SUBVERSE_INDEX, new { subverse = subverse }));
        }
예제 #2
0
 protected override void UpdateCache(CommandResponse <RemoveModeratorResponse> result)
 {
     if (result.Success)
     {
         CacheHandler.Instance.Remove(CachingKey.SubverseModerators(result.Response.Subverse));
     }
 }
        public async Task <ActionResult> AcceptModeratorInvitation(int invitationId)
        {
            int maximumOwnedSubs = VoatSettings.Instance.MaximumOwnedSubs;

            //TODO: These errors are not friendly - please update to redirect or something
            // check if there is an invitation for this user with this id
            var userInvitation = _db.ModeratorInvitation.Find(invitationId);

            if (userInvitation == null)
            {
                return(ErrorView(new ErrorViewModel()
                {
                    Title = "Moderator Invite Not Found", Description = "The moderator invite is no longer valid", Footer = "Where did it go?"
                }));
                //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            // check if logged in user is actually the invited user
            if (!User.Identity.Name.IsEqual(userInvitation.Recipient))
            {
                return(ErrorView(ErrorViewModel.GetErrorViewModel(HttpStatusCode.Unauthorized)));
                //return new HttpStatusCodeResult(HttpStatusCode.Unauthorized);
            }

            // check if user is over modding limits
            var amountOfSubsUserModerates = _db.SubverseModerator.Where(s => s.UserName.ToLower() == User.Identity.Name.ToLower());

            if (amountOfSubsUserModerates.Any())
            {
                if (amountOfSubsUserModerates.Count() >= maximumOwnedSubs)
                {
                    return(ErrorView(new ErrorViewModel()
                    {
                        Title = "Maximum Moderation Level Exceeded", Description = $"Sorry, you can not own or moderate more than {maximumOwnedSubs} subverses.", Footer = "That's too bad"
                    }));
                }
            }

            // check if subverse exists
            var subverse = _db.Subverse.FirstOrDefault(s => s.Name.ToLower() == userInvitation.Subverse.ToLower());

            if (subverse == null)
            {
                return(ErrorView(ErrorViewModel.GetErrorViewModel(ErrorType.SubverseNotFound)));
                //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            // check if user is already a moderator of this sub
            var userModerating = _db.SubverseModerator.Where(s => s.Subverse.ToLower() == userInvitation.Subverse.ToLower() && s.UserName.ToLower() == User.Identity.Name.ToLower());

            if (userModerating.Any())
            {
                _db.ModeratorInvitation.Remove(userInvitation);
                _db.SaveChanges();
                return(ErrorView(new ErrorViewModel()
                {
                    Title = "You = Moderator * 2?", Description = "You are currently already a moderator of this subverse", Footer = "How much power do you want?"
                }));
                //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            // add user as moderator as specified in invitation
            var subAdm = new SubverseModerator
            {
                Subverse     = subverse.Name,
                UserName     = UserHelper.OriginalUsername(userInvitation.Recipient),
                Power        = userInvitation.Power,
                CreatedBy    = UserHelper.OriginalUsername(userInvitation.CreatedBy),
                CreationDate = Repository.CurrentDate
            };

            _db.SubverseModerator.Add(subAdm);

            // notify sender that user has accepted the invitation
            var message = new Domain.Models.SendMessage()
            {
                Sender    = $"v/{subverse.Name}",
                Subject   = $"Moderator invitation for v/{subverse.Name} accepted",
                Recipient = userInvitation.CreatedBy,
                Message   = $"User {User.Identity.Name} has accepted your invitation to moderate subverse v/{subverse.Name}."
            };
            var cmd = new SendMessageCommand(message).SetUserContext(User);
            await cmd.Execute();

            //clear mod cache
            CacheHandler.Instance.Remove(CachingKey.SubverseModerators(subverse.Name));

            // delete the invitation from database
            _db.ModeratorInvitation.Remove(userInvitation);
            _db.SaveChanges();

            return(RedirectToAction("Update", "SubverseModeration", new { subverse = subverse.Name }));
            //return Update(subverse.Name);
        }