Пример #1
0
        public void MdContentToHtml()
        {
            var md     = "A quick brown **fox** jumped over the lazy dog.";
            var result = ContentProcessor.MarkdownToContent(md, ContentProcessor.MarkdownConvertType.Html);

            Assert.IsTrue(result == "<p>A quick brown <strong>fox</strong> jumped over the lazy dog.</p>\n");
        }
Пример #2
0
        public void MdContentToNone()
        {
            var md     = "A quick brown **fox** jumped over the lazy dog.";
            var result = ContentProcessor.MarkdownToContent(md, ContentProcessor.MarkdownConvertType.None);

            Assert.IsTrue(result == "A quick brown **fox** jumped over the lazy dog.");
        }
Пример #3
0
        public void MdContentToException()
        {
            var md = "A quick brown **fox** jumped over the lazy dog.";

            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                var result = ContentProcessor.MarkdownToContent(md, (ContentProcessor.MarkdownConvertType) 4);
            });
        }
Пример #4
0
        public async Task <IActionResult> NewComment(Guid postId, NewCommentModel model, [FromServices] ISessionBasedCaptcha captcha)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (!_blogConfig.ContentSettings.EnableComments)
            {
                return(Forbid());
            }

            if (!captcha.ValidateCaptchaCode(model.CaptchaCode, HttpContext.Session))
            {
                ModelState.AddModelError(nameof(model.CaptchaCode), "Wrong Captcha Code");
                return(Conflict(ModelState));
            }

            var response = await _commentService.CreateAsync(new CommentRequest(postId)
            {
                Username  = model.Username,
                Content   = model.Content,
                Email     = model.Email,
                IpAddress = DNT ? "N/A" : HttpContext.Connection.RemoteIpAddress.ToString()
            });

            if (_blogConfig.NotificationSettings.SendEmailOnNewComment && _notificationClient is not null)
            {
                _ = Task.Run(async() =>
                {
                    await _notificationClient.NotifyCommentAsync(response,
                                                                 s => ContentProcessor.MarkdownToContent(s, ContentProcessor.MarkdownConvertType.Html));
                });
            }

            if (_blogConfig.ContentSettings.RequireCommentReview)
            {
                return(Created("moonglade://empty", response));
            }

            return(Ok());
        }
Пример #5
0
        public async Task NotifyCommentAsync(
            string username, string email, string ipAddress, string postTitle, string commentContent, DateTime createTimeUtc)
        {
            var payload = new CommentPayload(
                username,
                email,
                ipAddress,
                postTitle,
                ContentProcessor.MarkdownToContent(commentContent, ContentProcessor.MarkdownConvertType.Html),
                createTimeUtc
                );

            try
            {
                await SendAsync(new NotificationRequest <CommentPayload>(MailMesageTypes.NewCommentNotification, payload));
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
            }
        }
Пример #6
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);
        }
Пример #7
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);
    }
Пример #8
0
    public async Task Handle(CommentNotification notification, CancellationToken cancellationToken)
    {
        var payload = new CommentPayload(
            notification.Username,
            notification.Email,
            notification.IPAddress,
            notification.PostTitle,
            ContentProcessor.MarkdownToContent(notification.CommentContent, ContentProcessor.MarkdownConvertType.Html),
            notification.CreateTimeUtc
            );

        var response = await _client.SendNotification(MailMesageTypes.NewCommentNotification, payload);

        var respBody = await response.Content.ReadAsStringAsync(cancellationToken);

        if (response.IsSuccessStatusCode)
        {
            _logger.LogInformation($"Email is sent, server response: '{respBody}'");
        }
        else
        {
            throw new($"Email sending failed, response code: '{response.StatusCode}', response body: '{respBody}'");
        }
    }
 private string FormatPostContent(string rawContent)
 {
     return(_settings.Editor == EditorChoice.Markdown ?
            ContentProcessor.MarkdownToContent(rawContent, ContentProcessor.MarkdownConvertType.Html, false) :
            rawContent);
 }