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)); }
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)); }
public ModelResult <ScheduleItemComment> AddComment(ScheduleItemAddress scheduleItemAddress, CommentInput commentInput) { CommentIn commentIn = new CommentIn(commentInput); pluginEngine.ExecuteAll("ProcessInputOfComment", new { context, comment = commentIn }); commentInput = commentIn.ToCommentInput(); commentInput = pluginEngine.Process <CommentIn>("ProcessInputOfCommentOnAdd", new CommentIn(commentInput)).ToCommentInput(); if (pluginEngine.AnyTrue("IsCommentSpam", new { context, comment = commentIn })) { return(new ModelResult <ScheduleItemComment>(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 <ScheduleItemComment>(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"); ScheduleItemComment comment; using (TransactionScope transaction = new TransactionScope()) { string commentSlug = generateUniqueCommentSlug(scheduleItemAddress); comment = commentInput.ToComment(context.User.Cast <UserAuthenticated>(), context.HttpContext.Request.GetUserIPAddress().ToLong(), context.HttpContext.Request.UserAgent, language, commentSlug, commentState); comment = conferencesCommentRepository.Save(comment, context.Site.ID, scheduleItemAddress.EventName, scheduleItemAddress.ScheduleItemSlug); 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) { scheduleItemRepository.AddSubscription(context.Site.ID, comment.ScheduleItem, comment.CreatorUserID); } else { scheduleItemRepository.AddSubscription(context.Site.ID, comment); } } //TODO: (erikpo) Move into a module messageOutboundRepository.Save(generateMessages(comment.ScheduleItem, comment)); ScheduleItemSmallReadOnly scheduleItemProxy = new ScheduleItemSmallReadOnly(comment.ScheduleItem); CommentReadOnly commentProxy = new CommentReadOnly(comment, ""); pluginEngine.ExecuteAll("CommentAdded", new { context, parent = scheduleItemProxy, comment = commentProxy }); if (comment.State == EntityState.Normal) { pluginEngine.ExecuteAll("CommentApproved", new { context, parent = scheduleItemProxy, comment = commentProxy }); } return(new ModelResult <ScheduleItemComment>(comment, validationState)); }