public EditCommentResponse Handle(EditComment command)
        {
            Debug.WriteLine(DateTime.Now.ToLongTimeString() + ":Got command..");

            var response = new EditCommentResponse();

            try
            {
                var user = _membershipService.GetUserById(command.EditedBy);

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

                var comment = _commentService.GetCommentById(command.CommentId);

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

                if (comment.AuthorUserId != user.Id)
                {
                    response.Error = "You are not allowed to edit another user's comment.";
                    return(response);
                }

                if (comment.Deleted)
                {
                    response.Error = "You cannot edit a deleted comment.";
                    return(response);
                }

                if (string.IsNullOrEmpty(command.Body))
                {
                    response.Error = "A comment is required.";
                    return(response);
                }

                command.Body = command.Body.Trim();

                List <string> oldMentions = null;
                List <string> newMentions = null;

                try
                {
                    _markdownCompiler.Compile(comment.Body, out oldMentions);
                }
                catch (Exception ex)
                {
                    _logger.Error("There was an errror compiling previous mentions for a comment edit.", ex);
                }

                var bodyFormatted = _markdownCompiler.Compile(command.Body, out newMentions);
                _commentService.UpdateCommentBody(comment.Id, command.Body, bodyFormatted, command.DateEdited);

                if (oldMentions != null && oldMentions.Count > 0)
                {
                    // we have some mentions in our previous comment. let's see if they were removed
                    var removed = oldMentions.Except(newMentions).ToList();
                    if (removed.Count > 0)
                    {
                        _eventBus.Publish(new UsersUnmentioned
                        {
                            CommentId = comment.Id,
                            Users     = removed
                        });
                    }
                }

                if (newMentions != null)
                {
                    // the are some mentions in this comment.
                    // let's get only the new mentions that were previously in the comment
                    if (oldMentions != null && oldMentions.Count > 0)
                    {
                        newMentions = newMentions.Except(oldMentions).ToList();
                    }

                    if (newMentions.Count > 0)
                    {
                        _eventBus.Publish(new UsersMentioned
                        {
                            CommentId = comment.Id,
                            Users     = newMentions
                        });
                    }
                }

                response.Body          = command.Body;
                response.FormattedBody = bodyFormatted;
            }
            catch (Exception ex)
            {
                response.Error = ex.Message;
            }

            Debug.WriteLine(DateTime.Now.ToLongTimeString() + ":Finished command..");

            return(response);
        }
Exemple #2
0
        public EditCommentResponse Handle(EditComment command)
        {
            Debug.WriteLine(DateTime.Now.ToLongTimeString() + ": Got command.");

            var response = new EditCommentResponse();

            try
            {
                var user = _membershipService.GetUserById(command.EditedBy);

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

                var comment = _commentService.GetCommentById(command.CommentId);

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


                // todo: permission check for admins?
                if (comment.AuthorUserId != user.Id)
                {
                    response.Error = "You are not allowed to edit another user's comment.";
                    return(response);
                }

                if (comment.Deleted)
                {
                    response.Error = "You cannot edit a deleted comment.";
                    return(response);
                }

                if (string.IsNullOrEmpty(comment.Body))
                {
                    response.Error = "A comment is required";
                    return(response);
                }

                comment.Body = command.Body.Trim();

                List <string> oldMentions = null;
                List <string> newMentions = null;

                // todo: mentions

                var bodyFormatted = _markdownCompiler.Compile(comment.Body);
                _commentService.UpdateCommentBody(comment.Id, comment.Body, bodyFormatted, command.UpdatedAt);

                // todo: mentions

                response.Body          = command.Body;
                response.FormattedBody = bodyFormatted;
            }
            catch (Exception ex)
            {
                // todo: log
                response.Error = ex.Message;
            }

            Debug.WriteLine(DateTime.Now.ToLongTimeString() + ": Finished command.");

            return(response);
        }