コード例 #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);
            }
        }
コード例 #2
0
        private async Task <bool> CheckIfUserIsAdmin(HttpContext ctx)
        {
            bool isAdmin = _options.IsUserAdminModeratorCheck(ctx);

            if (!isAdmin)
            {
                await ctx.Response.WriteResponse("user is not comments moderator", "text/plain", 403);

                return(false);
            }
            return(true);
        }
コード例 #3
0
        public async Task HandleRequest(HttpContext ctx)
        {
            try
            {
                string       response = null;
                StringValues url;
                StringValues start;
                StringValues count;
                if (ctx.Request.Query.TryGetValue("url", out url))
                {
                    if (!ctx.Request.Query.TryGetValue("start", out start))
                    {
                        start = "0";
                    }
                    if (!ctx.Request.Query.TryGetValue("count", out count))
                    {
                        count = "5000";
                    }
                    string theUrl             = url;
                    bool   includeNotApproved = _options.IsUserAdminModeratorCheck(ctx);
                    var    comments           = _dataAccessFact().GetCommentsForPage(
                        theUrl.NormalizePath(),
                        int.Parse(start),
                        int.Parse(count),
                        includeNotApproved
                        ).ToArray();
                    foreach (var c in comments)
                    {
                        c.CommentContentSource = "";
                    }
                    response = JsonConvert.SerializeObject(comments);
                }

                await ctx.Response.WriteResponse(response, "application/json", 200);
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to get comments", ex);
            }
        }
コード例 #4
0
 public async Task HandleRequest(HttpContext ctx)
 {
     bool isAdmin = _options.IsUserAdminModeratorCheck(ctx);
     int  result  = isAdmin ? 1 : 0;
     await ctx.Response.WriteResponse(result.ToString(), "application/json", 200);
 }