Ejemplo n.º 1
0
        public async Task HandleRequest(HttpContext ctx)
        {
            try
            {
                string json = await ctx.Request.ReadBodyAsString();

                CommentModel comment = JsonConvert.DeserializeObject <CommentModel>(json);

                comment.SetEmailHash();
                comment.PostTime = DateTime.UtcNow;
                comment.PageUrl  = comment.PageUrl.NormalizePath();
                if (comment.CommentContentSource.Length > _options.CommentSourceMaxLength)
                {
                    await ctx.Response.WriteResponse($"Comment has exceeded maximum length of {_options.CommentSourceMaxLength} characters.", "text/plain", 400);

                    return;
                }
                comment.Approved = !_options.RequireCommentApproval;
                if (!comment.Approved)
                {
                    comment.Approved = _options.IsUserAdminModeratorCheck(ctx); // admins don't require approval for comments
                }
                comment.PostedByMod = _options.IsUserAdminModeratorCheck(ctx);
                if (comment.IsMarkdown)
                {
                    comment.CommentContentRendered = _mardownParser.ConvertToHtml(comment.CommentContentSource);
                }
                else
                {
                    comment.CommentContentSource   = WebUtility.HtmlEncode(comment.CommentContentSource);
                    comment.CommentContentSource   = comment.CommentContentSource.Replace("\n", " <br /> ");
                    comment.CommentContentRendered = comment.CommentContentSource;
                }
                CommentModel response = null;
                using (var dataAccess = _dataAccessFact())
                {
                    response = dataAccess.PostComment(comment);
                }
                string responseJson = JsonConvert.SerializeObject(response);
                await ctx.Response.WriteResponse(responseJson, "application/json", 201);
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to post comment", ex);
            }
        }
Ejemplo n.º 2
0
        public async Task HandleRequest(HttpContext ctx)
        {
            try
            {
                string text = ctx.Request.ReadBodyAsString();
                if (text.Length > _options.CommentSourceMaxLength)
                {
                    await ctx.Response.WriteResponse($"Comment has exceeded maximum length of {_options.CommentSourceMaxLength} characters.", "text/plain", 400);

                    return;
                }
                var rendered = _mardownParser.ConvertToHtml(text);
                await ctx.Response.WriteResponse(rendered, "text/html", 200);
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to preview comment", ex);
            }
        }