Beispiel #1
0
        public async Task <IActionResult> SubmitCommentAsync(CommentFormViewModel model)
        {
            if (string.IsNullOrEmpty(Request.Form["_guid_"]))
            {
                return(Json(new { result = false, message = "Invalid request." }));
            }

            if (!_commentsSettings.EnableComments)
            {
                return(Json(new { result = false, message = "Comment Not Allowed! " }));
            }

            if (model.PostId <= 0)
            {
                return(Json(new { result = false, message = "Error" }));
            }

            if (_commentsSettings.EnableFormVerificationCode && !_captchaService.Validate(model.CaptchaId, model.CaptchaCode))
            {
                return(Json(new { result = false, message = _stringLocalizer["Invalid Captcha input."].Value }));
            }

            var post = await _postService.GetByIdAsync(model.PostId, new PostIncludeOptions()
            {
                IncludeUser = true,
            });

            if (post == null)
            {
                return(Json(new { result = false, message = "Error" }));
            }

            if (ModelState.IsValid)
            {
                var comment = new Comment()
                {
                    Author   = model.Author,
                    Content  = model.Content,
                    Website  = model.Website,
                    Email    = model.Email,
                    ParentId = model.ParentId,
                    PostId   = post.Id,
                };

                comment.UserId = _httpContextAccessor.HttpContext.User.GetUserId();

                comment.IP      = _httpContextAccessor.GetClientIpAddress();
                comment.Country = await _iPAddressService.GetIPLocationAsync(comment.IP);

                comment.IsApproved = !_commentsSettings.EnableCommentsModeration;

                if (_commentsSettings.TrustAuthenticatedUsers)
                {
                    comment.IsApproved = await _commentService.IsTrustUserAsync(comment.Email);
                }

                await _commentService.InsertAsync(comment);

                var spamProcessContext = new SpamProcessContext()
                {
                    Category = "comment", Comment = comment
                };
                await _spamService.ProcessAsync(spamProcessContext);

                if (spamProcessContext.Passed)
                {
                    comment.IsApproved = true;
                    comment.IsSpam     = false;

                    await _commentService.UpdateAsync(comment);
                }

                if (comment.IsApproved)
                {
                    await _postService.IncreaseCommentsCountAsync(post.Id);
                }

                SaveCommentFormUser(new LastCommentFormUserInfo()
                {
                    Author = model.Author, Email = model.Email, Website = model.Website,
                });

                // event
                var eventData = new CommentApprovedEventData()
                {
                    CommentUrl      = Url.RouteUrl(RouteNames.Post, new { slug = post.Slug }, Request.Scheme, host: Request.Host.Value, fragment: $"comment-{comment.Id}"),
                    Replay          = comment,
                    Post            = post,
                    PostAuthor      = post.User?.GetDisplayName(),
                    PostAuthorEmail = post.User?.Email,
                };

                if (comment.ParentId != null)
                {
                    eventData.SourceComment = await _commentService.GetByIdAsync(comment.ParentId.Value);
                }

                _eventBus.Publish(eventData);


                return(Json(new { result = true, commentId = comment.GuidId, parentId = comment.ParentId, url = Url.RouteUrl("Comment", new { commentId = comment.GuidId }) }));
            }

            return(Json(new { result = false, message = string.Join(" ", ModelState.Values.SelectMany(t => t.Errors.Select(e => e.ErrorMessage))) }));
        }