Ejemplo n.º 1
0
        public ModelResult <PostComment> AddComment(Post post, CommentInputForImport commentInput)
        {
            ValidationStateDictionary validationState = new ValidationStateDictionary();

            validationState.Add(typeof(CommentInputForImport), validator.Validate(commentInput));

            if (!validationState.IsValid)
            {
                return(new ModelResult <PostComment>(validationState));
            }

            PostComment comment;

            using (TransactionScope transaction = new TransactionScope())
            {
                string commentSlug = generateUniqueCommentSlug(post.ToPostAddress());

                comment = commentInput.ToComment(commentSlug);

                comment = blogsCommentRepository.Save(comment, context.Site.ID, post.Blog.Name, post.Slug);

                invalidateCachedCommentDependencies(comment);

                transaction.Complete();
            }

            PostSmallReadOnly postProxy    = new PostSmallReadOnly(comment.Post);
            CommentReadOnly   commentProxy = new CommentReadOnly(comment, absolutePathHelper.GetAbsolutePath(comment));

            pluginEngine.ExecuteAll("CommentAddedFromImport", new { context, parent = postProxy, comment = commentProxy });

            return(new ModelResult <PostComment>(comment, validationState));
        }
Ejemplo n.º 2
0
        public ModelResult <PostComment> EditComment(PostCommentAddress commentAddress, CommentInput commentInput)
        {
            commentInput = pluginEngine.Process("ProcessInputOfComment", new CommentIn(commentInput)).ToCommentInput();
            commentInput = pluginEngine.Process("ProcessInputOfCommentOnEdit", new CommentIn(commentInput)).ToCommentInput();

            if (pluginEngine.AnyTrue("IsCommentSpam", commentInput))
            {
                return(new ModelResult <PostComment>(new ValidationStateDictionary(typeof(CommentInput), new ValidationState(new[] { new ValidationError("Comment.IsSpam", commentInput, "The supplied comment was considered to be spam and was not added") }))));
            }

            ValidationStateDictionary validationState = ValidateCommentInput(commentInput);

            if (!validationState.IsValid)
            {
                return(new ModelResult <PostComment>(validationState));
            }

            PostComment newComment;
            PostComment originalComment;

            using (TransactionScope transaction = new TransactionScope())
            {
                originalComment = getComment(commentAddress);
                newComment      = originalComment.Apply(commentInput, context.User.Cast <UserAuthenticated>());

                newComment = blogsCommentRepository.Save(newComment, context.Site.ID, newComment.Post.BlogName, newComment.Post.Slug);

                invalidateCachedCommentForEdit(newComment, originalComment);

                transaction.Complete();
            }

            PostSmallReadOnly postProxy            = new PostSmallReadOnly(newComment.Post);
            CommentReadOnly   newCommentProxy      = new CommentReadOnly(newComment, absolutePathHelper.GetAbsolutePath(newComment));
            CommentReadOnly   originalCommentProxy = new CommentReadOnly(originalComment, absolutePathHelper.GetAbsolutePath(originalComment));

            pluginEngine.ExecuteAll("CommentEdited", new { context, parent = postProxy, comment = newCommentProxy, commentOriginal = originalCommentProxy });

            return(new ModelResult <PostComment>(newComment, validationState));
        }
Ejemplo n.º 3
0
        public ModelResult <PostComment> AddComment(PostAddress postAddress, CommentInput commentInput)
        {
            CommentIn commentIn = new CommentIn(commentInput);

            pluginEngine.ExecuteAll("ProcessInputOfComment", new { context, comment = commentIn });
            commentInput = commentIn.ToCommentInput();

            commentInput = pluginEngine.Process("ProcessInputOfCommentOnAdd", new CommentIn(commentInput)).ToCommentInput();

            if (pluginEngine.AnyTrue("IsCommentSpam", new { context, comment = commentIn }))
            {
                return(new ModelResult <PostComment>(new ValidationStateDictionary(typeof(CommentInput), new ValidationState(new[] { new ValidationError("Comment.IsSpam", commentInput, "The supplied comment was considered to be spam and was not added") }))));
            }

            ValidationStateDictionary validationState = ValidateCommentInput(commentInput);

            if (!validationState.IsValid)
            {
                return(new ModelResult <PostComment>(validationState));
            }

            EntityState commentState;

            try
            {
                commentState = context.User.IsAuthenticated ? EntityState.Normal : (EntityState)Enum.Parse(typeof(EntityState), context.Site.CommentStateDefault);
            }
            catch
            {
                commentState = EntityState.PendingApproval;
            }

            //TODO: (erikpo) Replace with some logic to set the language from the user's browser or from a dropdown list
            Language    language = languageRepository.GetLanguage(context.Site.LanguageDefault ?? "en");
            PostComment comment;

            using (TransactionScope transaction = new TransactionScope())
            {
                string commentSlug = generateUniqueCommentSlug(postAddress);

                comment = commentInput.ToComment(context.User.Cast <UserAuthenticated>(), context.HttpContext.Request.GetUserIPAddress().ToLong(), context.HttpContext.Request.UserAgent, language, commentSlug, commentState);

                comment = blogsCommentRepository.Save(comment, context.Site.ID, postAddress.BlogName, postAddress.PostSlug);

                if (comment.State == EntityState.Normal)
                {
                    invalidateCachedCommentDependencies(comment);
                }

                transaction.Complete();
            }

            //TODO: (erikpo) The following calls to setup the subscription and send out emails for those subscribed needs to happen in the transaction (but can't currently because of issues with them being in different repositories

            //TODO: (erikpo) Move into a module
            if (commentInput.Subscribe)
            {
                if (context.User.IsAuthenticated)
                {
                    postRepository.AddSubscription(context.Site.ID, comment.Post, comment.CreatorUserID);
                }
                else
                {
                    postRepository.AddSubscription(context.Site.ID, comment);
                }
            }

            //TODO: (erikpo) Move into a module
            messageOutboundRepository.Save(generateMessages(comment.Post, comment));

            PostSmallReadOnly postProxy    = new PostSmallReadOnly(comment.Post);
            CommentReadOnly   commentProxy = new CommentReadOnly(comment, absolutePathHelper.GetAbsolutePath(comment));

            pluginEngine.ExecuteAll("CommentAdded", new { context, parent = postProxy, comment = commentProxy });

            if (comment.State == EntityState.Normal)
            {
                pluginEngine.ExecuteAll("CommentApproved", new { context, parent = postProxy, comment = commentProxy });
            }

            return(new ModelResult <PostComment>(comment, validationState));
        }