Esempio n. 1
0
        public void AddComment(Area area, Post post, Comment comment, UserBase creator, bool subscribe, out ValidationStateDictionary validationState, out Comment newComment)
        {
            validationState = new ValidationStateDictionary();

            comment.Creator = creator;

            if (comment.State == EntityState.NotSet)
            {
                try
                {
                    comment.State = creator is User
                        ? EntityState.Normal
                        : (EntityState)Enum.Parse(typeof(EntityState), site.CommentStateDefault);
                }
                catch
                {
                    comment.State = EntityState.PendingApproval;
                }
            }

            if (comment.Language == null)
            {
                comment.Language = post.Creator.LanguageDefault;
            }

            validationState.Add(typeof(Comment), validator.Validate(comment));

            //validate anonymous users
            if (!(comment.Creator is User))
            {
                validationState.Add(typeof(UserBase), validator.Validate(comment.Creator));

                comment.Creator.HashedEmail = !string.IsNullOrEmpty(comment.Creator.Email) ? comment.Creator.Email.ComputeHash() : "";
            }

            //validation for subscription
            //todo: (nheskew) moooove and unhack a bit - _feels_ wrong here
            if (subscribe)
            {
                validationState.Add(typeof(PostSubscription), validator.Validate(new PostSubscription {
                    Post = post, User = comment.Creator
                }));
            }

            if (!validationState.IsValid)
            {
                newComment = null;

                return;
            }

            repository.SaveComment(post, comment);

            if (subscribe && !repository.GetSubscriptionExists(post, creator))
            {
                repository.AddSubscription(post, creator);
            }

            newComment = repository.GetComment(comment.ID);
            //avoid sending emails for a comment we've already marked as spam
            if (newComment.State == EntityState.Normal)
            {
                messageOutboundRepository.Save(generateMessages(post, newComment));
            }
        }
 public void Save(MessageOutbound message)
 {
     repository.Save(message);
 }
        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));
        }