Пример #1
0
        public BanUserFromSubResponse Handle(BanUserFromSub command)
        {
            var response = new BanUserFromSubResponse();

            try
            {
                var user = command.UserId.HasValue ? _membershipService.GetUserById(command.UserId.Value) : _membershipService.GetUserByUserName(command.UserName);

                if (user == null)
                {
                    response.Error = "Invalid user.";
                    return(response);
                }

                var bannedBy = _membershipService.GetUserById(command.BannedBy);

                if (bannedBy == null)
                {
                    response.Error = "Invalid user.";
                    return(response);
                }

                var sub = command.SubId.HasValue
                    ? _subService.GetSubById(command.SubId.Value)
                    : _subService.GetSubByName(command.SubName);

                if (sub == null)
                {
                    response.Error = "Invalid sub.";
                    return(response);
                }

                if (!_permissionDao.CanUserManageSubAccess(bannedBy, sub.Id))
                {
                    response.Error = "You are not authorized to ban.";
                    return(response);
                }

                _subUserBanService.BanUserFromSub(sub.Id, user.Id, user.UserName, command.DateBanned, command.BannedBy, command.ReasonPrivate, command.ReasonPublic);

                return(response);
            }
            catch (Exception ex)
            {
                // TODO: log error
                response.Error = "An unknown error occured.";
                return(response);
            }
        }
Пример #2
0
        public BanUserFromSubResponse Handle(BanUserFromSub command)
        {
            var response = new BanUserFromSubResponse();

            try
            {
                var user = command.UserId.HasValue ? _membershipService.GetUserById(command.UserId.Value) : _membershipService.GetUserByUserName(command.Username);

                if (user == null)
                {
                    response.Error = "Invalid user.";
                    return(response);
                }

                var bannedBy = _membershipService.GetUserById(command.BannedBy);

                if (bannedBy == null)
                {
                    response.Error = "Invalid user.";
                    return(response);
                }

                var sub = command.SubId.HasValue
                    ? _subService.GetSubById(command.SubId.Value)
                    : _subService.GetSubByName(command.SubName);

                if (sub == null)
                {
                    response.Error = "Invalid sub.";
                    return(response);
                }

                if (!_permissionService.CanUserManageSubAccess(bannedBy, sub.Id))
                {
                    response.Error = "You are not authorized to ban.";
                    return(response);
                }

                _subUserBanService.BanUserFromSub(sub.Id, user.Id, command.DateBanned, command.BannedBy, command.Reason, command.Expires);
            }
            catch (Exception ex)
            {
                // todo: log
                response.Error = ex.Message;
            }

            return(response);
        }
Пример #3
0
        public SubcribeToSubResponse Handle(SubcribeToSub command)
        {
            var response = new SubcribeToSubResponse();

            try
            {
                var user = _membershipService.GetUserByUserName(command.UserName);

                if (user == null)
                {
                    response.Error = "Invalid user.";
                    return(response);
                }

                var sub = command.SubId.HasValue ? _subService.GetSubById(command.SubId.Value) : _subService.GetSubByName(command.SubName);

                if (sub == null)
                {
                    response.Error = "Invalid sub name.";
                    return(response);
                }

                // todo: check for private subs
                // todo: check if the user is subcribed to too many subs

                var subscribedSubs = _subService.GetSubscribedSubsForUser(user.Id);

                if (subscribedSubs.Contains(sub.Id))
                {
                    // already subscribed!
                    response.Success = true;
                    return(response);
                }

                if (subscribedSubs.Count >= _subSettings.Settings.MaximumNumberOfSubscribedSubs)
                {
                    response.Error = "You cannot have more than " + _subSettings.Settings.MaximumNumberOfSubscribedSubs + " subscribed subs.";
                    return(response);
                }

                _subService.SubscribeToSub(user.Id, sub.Id);

                _eventBus.Publish(new SubScriptionChanged
                {
                    Subcribed = true,
                    UserId    = user.Id,
                    SubId     = sub.Id
                });

                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Error = ex.Message;
                return(response);
            }

            return(response);
        }
Пример #4
0
        public EditSubStylesCommandResponse Handle(EditSubStylesCommand command)
        {
            var response = new EditSubStylesCommandResponse();

            try
            {
                var user = _membershipService.GetUserById(command.EditedByUserId);
                if (user == null)
                {
                    response.Error = "Invalid user.";
                    return(response);
                }

                var sub = command.SubId.HasValue
                    ? _subService.GetSubById(command.SubId.Value)
                    : _subService.GetSubByName(command.SubName);
                if (sub == null)
                {
                    response.Error = "Invalid sub.";
                    return(response);
                }

                if (!_permissionService.CanUserManageSubStyles(user, sub.Id))
                {
                    response.Error = "You are not authorized to manage styles for this sub.";
                    return(response);
                }

                var styles = _subStylesService.GetStylesForSub(sub.Id);
                if (styles == null)
                {
                    styles = new SubCss();
                }

                styles.SubId                 = sub.Id;
                styles.CssType               = command.CssType;
                styles.Embedded              = command.Embedded;
                styles.ExternalCss           = command.ExternalCss;
                styles.GitHubCssProjectName  = command.GitHubCssProjectName;
                styles.GitHubCssProjectTag   = command.GitHubCssProjectTag;
                styles.GitHubLessProjectName = command.GitHubLessProjectName;
                styles.GitHubLessProjectTag  = command.GitHubLessProjectTag;

                _subStylesService.UpdateStylesForSub(styles);
            }
            catch (Exception ex)
            {
                _logger.Error("Error trying to edit a sub's styles.", ex);
                response.Error = "An unknown error occured.";
            }

            return(response);
        }
Пример #5
0
        public RemoveModFromSubResponse Handle(RemoveModFromSub command)
        {
            var response = new RemoveModFromSubResponse();

            try
            {
                var requestingUser = _membershipService.GetUserById(command.RequestingUser);
                if (requestingUser == null)
                {
                    response.Error = "Invalid user.";
                    return(response);
                }

                var userToRemove = command.UserIdToRemove.HasValue
                   ? _membershipService.GetUserById(command.UserIdToRemove.Value)
                   : _membershipService.GetUserByUserName(command.UserNameToRemove);
                if (userToRemove == null)
                {
                    response.Error = "Invalid user.";
                    return(response);
                }

                Sub sub = null;

                if (command.SubId.HasValue)
                {
                    sub = _subService.GetSubById(command.SubId.Value);
                }
                else if (!string.IsNullOrEmpty(command.SubName))
                {
                    sub = _subService.GetSubByName(command.SubName);
                }

                if (sub == null)
                {
                    response.Error = "Invalid sub.";
                    return(response);
                }

                var userToRemoveModInfo = _moderationService.GetModeratorInfoForUserInSub(userToRemove.Id, sub.Id);
                if (userToRemoveModInfo == null)
                {
                    response.Error = "The user is not a mod of this sub.";
                    return(response);
                }

                if (requestingUser.IsAdmin)
                {
                    _moderationService.RemoveModFromSub(userToRemove.Id, sub.Id);
                }
                else
                {
                    var requestingUserModInfo = _moderationService.GetModeratorInfoForUserInSub(requestingUser.Id, sub.Id);
                    if (requestingUserModInfo == null)
                    {
                        response.Error = "You are not a mod of this sub.";
                        return(response);
                    }

                    if (
                        // if the user is removing himself (doable)
                        requestingUser.Id == userToRemove.Id ||
                        // or the requesting user is an admin
                        requestingUser.IsAdmin ||
                        // or the user has has "full" access and the user we are changing has is a newby.
                        (requestingUserModInfo.AddedOn <=
                         userToRemoveModInfo.AddedOn &&
                         requestingUserModInfo.Permissions.HasFlag(
                             ModeratorPermissions.All))
                        )
                    {
                        _moderationService.RemoveModFromSub(userToRemove.Id, sub.Id);
                    }
                    else
                    {
                        response.Error = "You are not permitted to remove the mod from this sub.";
                        return(response);
                    }
                }
            }
            catch (Exception ex)
            {
                response.Error = "An unknown error occured.";
                _logger.Error("An error occured removing a user from sub.", ex);
            }

            return(response);
        }
Пример #6
0
        public SendMessageResponse Handle(SendMessage command)
        {
            var response = new SendMessageResponse();

            try
            {
                var author = _membershipService.GetUserById(command.Author);

                if (author == null)
                {
                    response.Error = "No author provided.";
                    return(response);
                }

                Sub  sendAsSub     = null;
                Sub  sendingToSub  = null;
                User sendingToUser = null;

                if (command.SendAsSub.HasValue)
                {
                    var sub = _subService.GetSubById(command.SendAsSub.Value);

                    if (sub == null)
                    {
                        response.Error = "Invalid sub to send as.";
                        return(response);
                    }

                    // the user is trying to send this message as a sub moderator.
                    // let's make sure that this user has this permission for the sub.
                    if (!_permissionService.CanUserManageSubMail(author, command.SendAsSub.Value))
                    {
                        response.Error = "You are not authorized to send a message as a sub moderator.";
                        return(response);
                    }

                    sendAsSub = sub;
                }

                if (string.IsNullOrEmpty(command.To) && !command.ToUserId.HasValue)
                {
                    response.Error = "You must provide a user/sub to send the message to.";
                    return(response);
                }

                if (command.ToUserId.HasValue && !string.IsNullOrEmpty(command.To))
                {
                    throw new Exception("A message with a userid and to was provided. Pick one.");
                }

                if (command.ToUserId.HasValue)
                {
                    sendingToUser = _membershipService.GetUserById(command.ToUserId.Value);
                    if (sendingToUser == null)
                    {
                        response.Error = "No user found to send message to.";
                        return(response);
                    }
                }
                else
                {
                    if (command.To.StartsWith("/u/", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var userName = command.To.Substring(3);
                        sendingToUser = _membershipService.GetUserByUserName(userName);
                        if (sendingToUser == null)
                        {
                            response.Error = string.Format("No user found with the name {0}.", userName);
                            return(response);
                        }
                    }
                    else if (command.To.StartsWith("/s/", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var subName = command.To.Substring(3);
                        sendingToSub = _subService.GetSubByName(subName);
                        if (sendingToSub == null)
                        {
                            response.Error = string.Format("No sub found with the name {0}.", subName);
                            return(response);
                        }
                    }
                    else
                    {
                        // maybe they are trying to send a message to a user
                        sendingToUser = _membershipService.GetUserByUserName(command.To);
                        if (sendingToUser == null)
                        {
                            response.Error = string.Format("No user found with the name {0}.", command.To);
                            return(response);
                        }
                    }
                }

                // let's make sure the message type is correct
                switch (command.Type)
                {
                case MessageType.Private:
                    if (command.CommentId.HasValue)
                    {
                        throw new Exception("A privat message was sent with a reference to a comment id. How?");
                    }
                    break;

                case MessageType.CommentReply:
                    if (!command.CommentId.HasValue)
                    {
                        throw new Exception("The message type was a comment reply, but no comment id was given.");
                    }
                    break;

                case MessageType.PostReply:
                    if (!command.CommentId.HasValue)
                    {
                        throw new Exception("The message type was a post reply, but no comment id was given.");
                    }
                    break;

                case MessageType.Mention:
                    // ensure either 1 comment is given, or 1 post is given
                    if (!command.CommentId.HasValue && !command.PostId.HasValue)
                    {
                        throw new Exception("The message type was a mention, but neither a comment or a post was given.");
                    }
                    if (command.PostId.HasValue && command.CommentId.HasValue)
                    {
                        throw new Exception("The message type was a mention, but both a comment and post were given. Pick one.");
                    }
                    break;

                default:
                    throw new Exception("Unknown message type");
                }

                var message = new Message
                {
                    Id            = GuidUtil.NewSequentialId(),
                    DateCreated   = Common.CurrentTime(),
                    MessageType   = command.Type,
                    ParentId      = null,
                    FirstMessage  = null,
                    AuthorId      = author.Id,
                    AuthorIp      = command.AuthorIp,
                    IsNew         = true,
                    ToUser        = sendingToUser != null ? sendingToUser.Id : (Guid?)null,
                    ToSub         = sendingToSub != null ? sendingToSub.Id : (Guid?)null,
                    FromSub       = sendAsSub != null ? sendAsSub.Id : (Guid?)null,
                    Subject       = command.Subject,
                    Body          = command.Body,
                    BodyFormatted = _markdownCompiler.Compile(command.Body),
                    CommentId     = command.CommentId,
                    PostId        = command.PostId
                };

                _messageService.InsertMessage(message);

                response.MessageId = message.Id;
            }
            catch (Exception ex)
            {
                _logger.Error("Error sending a message.", ex);
                response.Error = "An unknown error occured.";
            }

            return(response);
        }