Example #1
0
 public AddCommentTask(IDocumentStore documentStore, BlogConfig config, string postId,
                       PostComments.CommentInput commentInput, RequestValues requestValues)
 {
     _config = config;
     this.commentInput = commentInput;
     this.requestValues = requestValues;
     this.postId = postId;
     this.RavenDocumentStore = documentStore;
 }
Example #2
0
        public static bool IsSpam(string ApiKey, string domain, PostComments.Comment comment)
        {
            #if DEBUG
            return false;
            #endif
            var validator = new AkismetValidator(ApiKey);
            if (!validator.VerifyKey(domain)) throw new Exception("Akismet API key invalid.");

            var akismetComment = new AkismetComment
            {
                blog = domain,
                user_ip= comment.UserHostAddress,
                user_agent = comment.UserAgent,
                comment_content= comment.Content,
                comment_type= "comment",
                comment_author = comment.Author,
                comment_author_email = comment.Email,
                comment_author_url= comment.Website,
            };

            //Check if Akismet thinks this comment is spam. Returns TRUE if spam.
            return validator.IsSpam(akismetComment);
        }
Example #3
0
        public override void Execute()
        {
            var comment = new PostComments.Comment
                              {
                                  Author = commentInput.Author,
                                  Approved = true,
                                  Content = commentInput.Body,
                                  CreatedAt = DateTimeOffset.UtcNow,
                                  Email = commentInput.Email,
                                  Website = commentInput.Website,
                                  UserAgent = requestValues.UserAgent,
                                  UserHostAddress = requestValues.UserHostAddress,
                                  Replies = new List<PostComments.Comment>(),
                              };

            var isSpam = false;
            if (!string.IsNullOrWhiteSpace(_config.AkismetAPIKey) && !string.IsNullOrWhiteSpace(_config.AkismetDomain))
            {
                try
                {
                    isSpam = AkismetValidator.IsSpam(_config.AkismetAPIKey, _config.AkismetDomain, comment);
                }
                catch (Exception ignored_ex)
                {
                    // TODO log
                }
            }

            var post = DocumentSession.Include<BlogPost>(blogPost => blogPost.AuthorId).Load(postId);
            var postAuthor = DocumentSession.Load<User>(post.AuthorId);
            var author = DocumentSession.Load<User>(commentInput.Author);
            if (postAuthor != null) DocumentSession.Advanced.MarkReadOnly(postAuthor);
            if (author != null) DocumentSession.Advanced.MarkReadOnly(author);

            var comments = DocumentSession.Load<PostComments>(postId + "/comments");
            if (comments == null)
            {
                comments = new PostComments();
                DocumentSession.Store(comments, post.Id + "/comments");
            }

            if (isSpam)
            {
                comments.Spam.Add(comment);
            }
            else
            {
                if (commentInput.InReplyTo > 0)
                {
                    foreach (var c in comments.Comments)
                    {
                        if (c.Id != commentInput.InReplyTo) continue;
                        if (c.Replies == null) c.Replies = new List<PostComments.Comment>();
                        c.Replies.Add(comment);
                        break;
                    }
                }
                else
                {
                    comment.Id = comments.GenerateNewCommentId();
                    comments.Comments.Add(comment);
                }
                post.CommentsCount = comments.CommentsCount;
            }

            //			if (requestValues.IsAuthenticated == false && comment.IsSpam)
            //			{
            //				if (commenter.NumberOfSpamComments > 4)
            //					return;
            //				comments.Spam.Add(comment);
            //			}
            //			else
            //			{
            //				post.CommentsCount++;
            //				comments.Comments.Add(comment);
            //			}
            //
            //          // Now send out email notifications
            //			if (requestValues.IsAuthenticated)
            //				return; // we don't send email for authenticated users

            var subject = string.Format("{1} New comment posted: {0}", post.Title, isSpam ? "[Spam] " : string.Empty);
            var commentEmailHTML = string.Format(SendEmailTask.basicEmailHtml,
                                                 string.Format(
                                                     @"<div>New comment on post titled {0} by {1}, click <a href=""{2}"">here</a> to view it</div>",
                                                     post.Title, commentInput.Author, post.ToUrl(""))
                );

            var notify = new HashSet<string> {_config.OwnerEmail};
            if (author != null) notify.Add(author.Email);
            TaskExecutor.ExcuteLater(new SendEmailTask("noreply", subject, notify, commentEmailHTML));
        }