Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public async Task <CommentListItemDto> Handle(AddCommentCommand request, CancellationToken cancellationToken)
        {
            // 1. Check comment enabled or not
            if (!_blogConfig.ContentSettings.EnableComments)
            {
                throw new BadRequestException(
                          $"{nameof(_blogConfig.ContentSettings.EnableComments)} can not be less than 1, current value: {_blogConfig.ContentSettings.EnableComments}.");
            }

            // 2. Harmonize banned keywords
            if (_blogConfig.ContentSettings.EnableWordFilter)
            {
                request.Username = _wordFilterService.FilterContent(request.Username);
                request.Content  = _wordFilterService.FilterContent(request.Content);
            }

            var model = new CommentEntity
            {
                Id             = Guid.NewGuid(),
                Username       = request.Username,
                CommentContent = request.Content,
                PostId         = request.PostId,
                CreateOnUtc    = DateTime.UtcNow,
                Email          = request.Email,
                IPAddress      = request.IpAddress,
                IsApproved     = !_blogConfig.ContentSettings.RequireCommentReview,
                UserAgent      = request.UserAgent
            };

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

            await _context.SaveChangesAsync(cancellationToken);

            var postTitle = await _context.Post.Select(p => p.Title).FirstOrDefaultAsync(cancellationToken: cancellationToken);

            var item = new CommentListItemDto()
            {
                Id             = model.Id,
                CommentContent = model.CommentContent,
                CreateOnUtc    = model.CreateOnUtc,
                Email          = model.Email,
                IpAddress      = model.IPAddress,
                IsApproved     = model.IsApproved,
                PostTitle      = postTitle,
                Username       = model.Username
            };

            if (_blogConfig.NotificationSettings.SendEmailOnNewComment && null != _notificationClientService)
            {
                _ = Task.Run(async() =>
                {
                    if (!_notificationClientService.IsEnabled)
                    {
                        _logger.LogWarning(
                            "Skipped SendNewCommentNotification because Email sending is disabled.");
                        await Task.CompletedTask;

                        return;
                    }

                    try
                    {
                        var payload = new NewCommentNotificationPayload(
                            request.Username,
                            request.Email,
                            request.IpAddress,
                            postTitle,
                            Utils.ConvertMarkdownContent(request.Content, Utils.MarkdownConvertType.Html),
                            model.CreateOnUtc
                            );

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

            return(item);
        }