示例#1
0
        public void EmailHashingWorks()
        {
            var comment = new CommentModel
            {
                PosterEmail = "  [email protected] "
            };

            comment.SetEmailHash();
            Assert.Equal("0bc83cb571cd1c50ba6f3e8a78ef1346", comment.PosterEmailHash);
        }
示例#2
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);
            }
        }