Exemplo n.º 1
0
        public Task <Response <CommentReplyDetail> > AddReply(Guid commentId, string replyContent, string ipAddress, string userAgent)
        {
            return(TryExecuteAsync <CommentReplyDetail>(async() =>
            {
                if (!_blogConfig.ContentSettings.EnableComments)
                {
                    return new FailedResponse <CommentReplyDetail>((int)ResponseFailureCode.CommentDisabled);
                }

                var cmt = _commentRepository.Get(commentId);

                if (null == cmt)
                {
                    return new FailedResponse <CommentReplyDetail>((int)ResponseFailureCode.CommentNotFound);
                }

                var id = Guid.NewGuid();
                var model = new CommentReplyEntity
                {
                    Id = id,
                    ReplyContent = replyContent,
                    IpAddress = ipAddress,
                    UserAgent = userAgent,
                    ReplyTimeUtc = DateTime.UtcNow,
                    CommentId = commentId
                };

                _commentReplyRepository.Add(model);

                var detail = new CommentReplyDetail
                {
                    CommentContent = cmt.CommentContent,
                    CommentId = commentId,
                    Email = cmt.Email,
                    Id = model.Id,
                    IpAddress = model.IpAddress,
                    PostId = cmt.PostId,
                    PubDateUtc = cmt.Post.PostPublish.PubDateUtc.GetValueOrDefault(),
                    ReplyContent = model.ReplyContent,
                    ReplyContentHtml = Utils.ConvertMarkdownContent(model.ReplyContent, Utils.MarkdownConvertType.Html),
                    ReplyTimeUtc = model.ReplyTimeUtc,
                    Slug = cmt.Post.Slug,
                    Title = cmt.Post.Title,
                    UserAgent = model.UserAgent
                };

                await _moongladeAudit.AddAuditEntry(EventType.Content, EventId.CommentReplied, $"Replied comment id '{commentId}'");
                return new SuccessResponse <CommentReplyDetail>(detail);
            }));
        }
Exemplo n.º 2
0
        public Response <CommentReplyDetail> AddReply(Guid commentId, string replyContent, string ipAddress, string userAgent)
        {
            return(TryExecute <CommentReplyDetail>(() =>
            {
                if (!_blogConfig.ContentSettings.EnableComments)
                {
                    return new FailedResponse <CommentReplyDetail>((int)ResponseFailureCode.CommentDisabled);
                }

                var cmt = _commentRepository.Get(commentId);

                if (null == cmt)
                {
                    return new FailedResponse <CommentReplyDetail>((int)ResponseFailureCode.CommentNotFound);
                }

                var id = Guid.NewGuid();
                var model = new CommentReplyEntity
                {
                    Id = id,
                    ReplyContent = replyContent,
                    IpAddress = ipAddress,
                    UserAgent = userAgent,
                    ReplyTimeUtc = DateTime.UtcNow,
                    CommentId = commentId
                };

                _commentReplyRepository.Add(model);

                var detail = new CommentReplyDetail
                {
                    CommentContent = cmt.CommentContent,
                    CommentId = commentId,
                    Email = cmt.Email,
                    Id = model.Id,
                    IpAddress = model.IpAddress,
                    PostId = cmt.PostId,
                    PubDateUtc = cmt.Post.PostPublish.PubDateUtc.GetValueOrDefault(),
                    ReplyContent = model.ReplyContent,
                    ReplyTimeUtc = model.ReplyTimeUtc,
                    Slug = cmt.Post.Slug,
                    Title = cmt.Post.Title,
                    UserAgent = model.UserAgent
                };

                return new SuccessResponse <CommentReplyDetail>(detail);
            }));
        }
Exemplo n.º 3
0
        public JsonResult DeleteCommentReply([FromForm] string token, [FromForm] int commentReplyId)
        {
            DataResult dr = new DataResult();

            try
            {
                if (commentReplyId < 10000)
                {
                    dr.code = "201";
                    dr.msg  = "参数错误";
                    return(Json(dr));
                }

                UserEntity userEntity = this.GetUserByToken(token);

                CommentReplyBLL    commentReplyBLL    = new CommentReplyBLL();
                CommentReplyEntity commentReplyEntity = commentReplyBLL.GetById(commentReplyId);

                if (commentReplyEntity.fromUserId != userEntity.userId)
                {
                    dr.code = "201";
                    dr.msg  = "不是你的评论";
                    return(Json(dr));
                }
                commentReplyEntity.isDel      = true;
                commentReplyEntity.modifyDate = DateTime.Now;
                int rows = commentReplyBLL.ActionDal.ActionDBAccess.Updateable(commentReplyEntity).ExecuteCommand();
                if (rows > 0)
                {
                    dr.code = "200";
                    dr.msg  = "成功";
                }
                else
                {
                    dr.code = "201";
                    dr.msg  = "失败";
                }
            }
            catch (Exception ex)
            {
                dr.code = "999";
                dr.msg  = ex.Message;
            }

            return(Json(dr));
        }
Exemplo n.º 4
0
        public async Task <CommentReply> AddReply(Guid commentId, string replyContent)
        {
            var cmt = await _commentRepo.GetAsync(commentId);

            if (cmt is null)
            {
                throw new InvalidOperationException($"Comment {commentId} is not found.");
            }

            var id    = Guid.NewGuid();
            var model = new CommentReplyEntity
            {
                Id            = id,
                ReplyContent  = replyContent,
                CreateTimeUtc = DateTime.UtcNow,
                CommentId     = commentId
            };

            await _commentReplyRepo.AddAsync(model);

            var reply = new CommentReply
            {
                CommentContent   = cmt.CommentContent,
                CommentId        = commentId,
                Email            = cmt.Email,
                Id               = model.Id,
                PostId           = cmt.PostId,
                PubDateUtc       = cmt.Post.PubDateUtc.GetValueOrDefault(),
                ReplyContent     = model.ReplyContent,
                ReplyContentHtml = ContentProcessor.MarkdownToContent(model.ReplyContent, ContentProcessor.MarkdownConvertType.Html),
                ReplyTimeUtc     = model.CreateTimeUtc,
                Slug             = cmt.Post.Slug,
                Title            = cmt.Post.Title
            };

            await _audit.AddAuditEntry(EventType.Content, AuditEventId.CommentReplied, $"Replied comment id '{commentId}'");

            return(reply);
        }
Exemplo n.º 5
0
    public async Task <CommentReply> Handle(ReplyCommentCommand request, CancellationToken cancellationToken)
    {
        var cmt = await _commentRepo.GetAsync(request.CommentId);

        if (cmt is null)
        {
            throw new InvalidOperationException($"Comment {request.CommentId} is not found.");
        }

        var id    = Guid.NewGuid();
        var model = new CommentReplyEntity
        {
            Id            = id,
            ReplyContent  = request.ReplyContent,
            CreateTimeUtc = DateTime.UtcNow,
            CommentId     = request.CommentId
        };

        await _commentReplyRepo.AddAsync(model);

        var reply = new CommentReply
        {
            CommentContent   = cmt.CommentContent,
            CommentId        = request.CommentId,
            Email            = cmt.Email,
            Id               = model.Id,
            PostId           = cmt.PostId,
            PubDateUtc       = cmt.Post.PubDateUtc.GetValueOrDefault(),
            ReplyContent     = model.ReplyContent,
            ReplyContentHtml = ContentProcessor.MarkdownToContent(model.ReplyContent, ContentProcessor.MarkdownConvertType.Html),
            ReplyTimeUtc     = model.CreateTimeUtc,
            Slug             = cmt.Post.Slug,
            Title            = cmt.Post.Title
        };

        return(reply);
    }
Exemplo n.º 6
0
        public async Task <CommentReplyDetailDto> Handle(AddReplyCommand request, CancellationToken cancellationToken)
        {
            if (!_blogConfig.ContentSettings.EnableComments)
            {
                throw new BadRequestException(
                          $"{nameof(_blogConfig.ContentSettings.EnableComments)} can not be less than 1, current value: {_blogConfig.ContentSettings.EnableComments}.");
            }

            var cmt = await _context.Comment
                      .Where(c => c.Id == request.CommentId)
                      .Include(c => c.Post)
                      .ThenInclude(d => d.PostPublish)
                      .AsNoTracking()
                      .FirstOrDefaultAsync(cancellationToken: cancellationToken);

            if (null == cmt)
            {
                throw new NotFoundException(nameof(CommentEntity), request.CommentId);
            }

            var id    = Guid.NewGuid();
            var model = new CommentReplyEntity
            {
                Id           = id,
                ReplyContent = request.ReplyContent,
                IpAddress    = request.IpAddress,
                UserAgent    = request.UserAgent,
                ReplyTimeUtc = DateTime.UtcNow,
                CommentId    = request.CommentId
            };

            await _context.CommentReply.AddAsync(model, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            var detail = new CommentReplyDetailDto()
            {
                CommentContent   = cmt.CommentContent,
                CommentId        = request.CommentId,
                Email            = cmt.Email,
                Id               = model.Id,
                IpAddress        = model.IpAddress,
                PostId           = cmt.PostId,
                PubDateUtc       = cmt.Post.PostPublish.PubDateUtc.GetValueOrDefault(),
                ReplyContent     = model.ReplyContent,
                ReplyContentHtml = Utils.ConvertMarkdownContent(model.ReplyContent, Utils.MarkdownConvertType.Html),
                ReplyTimeUtc     = model.ReplyTimeUtc,
                Slug             = cmt.Post.Slug,
                Title            = cmt.Post.Title,
                UserAgent        = model.UserAgent
            };

            if (_blogConfig.NotificationSettings.SendEmailOnCommentReply && !string.IsNullOrWhiteSpace(detail.Email))
            {
                var postLink = "https://" + request.BaseUrl + "/api/Post/Get/" + detail.PostId.ToString();
                _ = Task.Run(async() =>
                {
                    if (!_notificationClientService.IsEnabled)
                    {
                        _logger.LogWarning(
                            "Skipped SendCommentReplyNotification because Email sending is disabled.");
                        await Task.CompletedTask;
                        return;
                    }

                    try
                    {
                        var payload = new CommentReplyNotificationPayload(
                            detail.Email,
                            detail.CommentContent,
                            detail.Title,
                            detail.ReplyContentHtml,
                            postLink);

                        await _notificationClientService.SendNotificationRequest(
                            new NotificationRequest <CommentReplyNotificationPayload>(MailMessageTypes.AdminReplyNotification,
                                                                                      payload));
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e, e.Message);
                    }
                }, cancellationToken);
            }

            return(detail);
        }