public IActionResult AddComment(Guid postid, CommentViewModel comment) { if (!ModelState.IsValid) { return(BadRequest()); } Comment commt = _mapper.Map <Comment>(comment); CommentSaveState state = _blogManager.AddComment(postid.ToString(), commt); if (state == CommentSaveState.Failed) { return(StatusCode(500)); } if (state == CommentSaveState.NotFound) { return(NotFound()); } return(Ok()); }
public IActionResult AddComment(AddCommentViewModel addcomment) { if (!_dasBlogSettings.SiteConfiguration.EnableComments) { return(BadRequest()); } if (!ModelState.IsValid) { Comment(new Guid(addcomment.TargetEntryId)); } Comment commt = _mapper.Map <Comment>(addcomment); commt.AuthorIPAddress = HttpContext.Connection.RemoteIpAddress.ToString(); commt.AuthorUserAgent = HttpContext.Request.Headers["User-Agent"].ToString(); commt.CreatedUtc = commt.ModifiedUtc = DateTime.UtcNow; commt.EntryId = Guid.NewGuid().ToString(); commt.IsPublic = !_dasBlogSettings.SiteConfiguration.CommentsRequireApproval; CommentSaveState state = _blogManager.AddComment(addcomment.TargetEntryId, commt); if (state == CommentSaveState.Failed) { ModelState.AddModelError("", "Comment failed"); return(StatusCode(500)); } if (state == CommentSaveState.NotFound) { ModelState.AddModelError("", "Invalid comment attempt"); return(NotFound()); } return(Comment(new Guid(addcomment.TargetEntryId))); }
public IActionResult AddComment(AddCommentViewModel addcomment) { List <string> errors = new List <string>(); if (!dasBlogSettings.SiteConfiguration.EnableComments) { return(BadRequest()); } if (!ModelState.IsValid) { return(Comment(addcomment.TargetEntryId)); } // Optional in case of Captcha. Commenting the settings in the config file // Will disable this check. People will typically disable this when using captcha. if (!String.IsNullOrEmpty(dasBlogSettings.SiteConfiguration.CheesySpamQ) && !String.IsNullOrEmpty(dasBlogSettings.SiteConfiguration.CheesySpamA) && dasBlogSettings.SiteConfiguration.CheesySpamQ.Trim().Length > 0 && dasBlogSettings.SiteConfiguration.CheesySpamA.Trim().Length > 0) { if (string.Compare(addcomment.CheesyQuestionAnswered, dasBlogSettings.SiteConfiguration.CheesySpamA, StringComparison.OrdinalIgnoreCase) != 0) { errors.Add("Answer to Spam Question is invalid. Please enter a valid answer for Spam Question and try again."); } } if (dasBlogSettings.SiteConfiguration.EnableCaptcha) { var recaptchaTask = recaptcha.Validate(Request); recaptchaTask.Wait(); var recaptchaResult = recaptchaTask.Result; if ((!recaptchaResult.success || recaptchaResult.score != 0) && recaptchaResult.score < dasBlogSettings.SiteConfiguration.RecaptchaMinimumScore) { errors.Add("Unfinished Captcha. Please finish the captcha by clicking 'I'm not a robot' and try again."); } } if (errors.Count > 0) { return(CommentError(addcomment, errors)); } addcomment.Content = dasBlogSettings.FilterHtml(addcomment.Content); var commt = mapper.Map <NBR.Comment>(addcomment); commt.AuthorIPAddress = HttpContext.Connection.RemoteIpAddress.ToString(); commt.AuthorUserAgent = HttpContext.Request.Headers["User-Agent"].ToString(); commt.CreatedUtc = commt.ModifiedUtc = DateTime.UtcNow; commt.EntryId = Guid.NewGuid().ToString(); commt.IsPublic = !dasBlogSettings.SiteConfiguration.CommentsRequireApproval; var state = blogManager.AddComment(addcomment.TargetEntryId, commt); if (state == NBR.CommentSaveState.Failed) { ModelState.AddModelError("", "Comment failed"); return(StatusCode(500)); } if (state == NBR.CommentSaveState.SiteCommentsDisabled) { ModelState.AddModelError("", "Comments are closed for this post"); return(StatusCode(403)); } if (state == NBR.CommentSaveState.PostCommentsDisabled) { ModelState.AddModelError("", "Comment are currently disabled"); return(StatusCode(403)); } if (state == NBR.CommentSaveState.NotFound) { ModelState.AddModelError("", "Invalid Target Post Id"); return(NotFound()); } logger.LogInformation(new EventDataItem(EventCodes.CommentAdded, null, "Comment created on: {0}", commt.TargetTitle)); BreakSiteCache(); return(Comment(addcomment.TargetEntryId)); }
public IActionResult AddComment(AddCommentViewModel addcomment) { if (!dasBlogSettings.SiteConfiguration.EnableComments) { return(BadRequest()); } if (!ModelState.IsValid) { return(Comment(addcomment.TargetEntryId)); } if (dasBlogSettings.SiteConfiguration.CheesySpamQ.Trim().Length > 0 && dasBlogSettings.SiteConfiguration.CheesySpamA.Trim().Length > 0) { if (string.Compare(addcomment.CheesyQuestionAnswered, dasBlogSettings.SiteConfiguration.CheesySpamA, StringComparison.OrdinalIgnoreCase) != 0) { return(Comment(addcomment.TargetEntryId)); } } addcomment.Content = dasBlogSettings.FilterHtml(addcomment.Content); var commt = mapper.Map <NBR.Comment>(addcomment); commt.AuthorIPAddress = HttpContext.Connection.RemoteIpAddress.ToString(); commt.AuthorUserAgent = HttpContext.Request.Headers["User-Agent"].ToString(); commt.CreatedUtc = commt.ModifiedUtc = DateTime.UtcNow; commt.EntryId = Guid.NewGuid().ToString(); commt.IsPublic = !dasBlogSettings.SiteConfiguration.CommentsRequireApproval; var state = blogManager.AddComment(addcomment.TargetEntryId, commt); if (state == NBR.CommentSaveState.Failed) { ModelState.AddModelError("", "Comment failed"); return(StatusCode(500)); } if (state == NBR.CommentSaveState.SiteCommentsDisabled) { ModelState.AddModelError("", "Comments are closed for this post"); return(StatusCode(403)); } if (state == NBR.CommentSaveState.PostCommentsDisabled) { ModelState.AddModelError("", "Comment are currently disabled"); return(StatusCode(403)); } if (state == NBR.CommentSaveState.NotFound) { ModelState.AddModelError("", "Invalid Target Post Id"); return(NotFound()); } logger.LogInformation(new EventDataItem(EventCodes.CommentAdded, null, "Comment created on: {0}", commt.TargetTitle)); BreakSiteCache(); return(Comment(addcomment.TargetEntryId)); }
public IActionResult AddComment(AddCommentViewModel addcomment) { if (!dasBlogSettings.SiteConfiguration.EnableComments) { return(BadRequest()); } if (!ModelState.IsValid) { return(Comment(addcomment.TargetEntryId)); } // Optional in case of Captcha. Commenting the settings in the config file // Will disable this check. People will typically disable this when using captcha. if (!String.IsNullOrEmpty(dasBlogSettings.SiteConfiguration.CheesySpamQ) && !String.IsNullOrEmpty(dasBlogSettings.SiteConfiguration.CheesySpamA) && dasBlogSettings.SiteConfiguration.CheesySpamQ.Trim().Length > 0 && dasBlogSettings.SiteConfiguration.CheesySpamA.Trim().Length > 0) { if (string.Compare(addcomment.CheesyQuestionAnswered, dasBlogSettings.SiteConfiguration.CheesySpamA, StringComparison.OrdinalIgnoreCase) != 0) { return(Comment(addcomment.TargetEntryId)); } } if (dasBlogSettings.SiteConfiguration.EnableCaptcha) { var recaptchaTask = recaptcha.Validate(Request); recaptchaTask.Wait(); var recaptchaResult = recaptchaTask.Result; if ((!recaptchaResult.success || recaptchaResult.score != 0) && recaptchaResult.score < dasBlogSettings.SiteConfiguration.RecaptchaMinimumScore) { // Todo: Rajiv Popat: This just redirects to the comment page. Ideally user should be informed that // the captch is invalid and he should be shown an error page with ability to fix the issue. // We need to have the ability to show errors and let the user fix typos in Captcha or Cheesy // Question. For now we are following the sample implementation as Cheesy Spam Question above // for the sake of consistency but this should be fixed everywhere. return(Comment(addcomment.TargetEntryId)); } } addcomment.Content = dasBlogSettings.FilterHtml(addcomment.Content); var commt = mapper.Map <NBR.Comment>(addcomment); commt.AuthorIPAddress = HttpContext.Connection.RemoteIpAddress.ToString(); commt.AuthorUserAgent = HttpContext.Request.Headers["User-Agent"].ToString(); commt.CreatedUtc = commt.ModifiedUtc = DateTime.UtcNow; commt.EntryId = Guid.NewGuid().ToString(); commt.IsPublic = !dasBlogSettings.SiteConfiguration.CommentsRequireApproval; var state = blogManager.AddComment(addcomment.TargetEntryId, commt); if (state == NBR.CommentSaveState.Failed) { ModelState.AddModelError("", "Comment failed"); return(StatusCode(500)); } if (state == NBR.CommentSaveState.SiteCommentsDisabled) { ModelState.AddModelError("", "Comments are closed for this post"); return(StatusCode(403)); } if (state == NBR.CommentSaveState.PostCommentsDisabled) { ModelState.AddModelError("", "Comment are currently disabled"); return(StatusCode(403)); } if (state == NBR.CommentSaveState.NotFound) { ModelState.AddModelError("", "Invalid Target Post Id"); return(NotFound()); } logger.LogInformation(new EventDataItem(EventCodes.CommentAdded, null, "Comment created on: {0}", commt.TargetTitle)); BreakSiteCache(); return(Comment(addcomment.TargetEntryId)); }
public IActionResult AddComment(AddCommentViewModel addcomment) { List <string> errors = new List <string>(); if (!ModelState.IsValid) { errors.Add("[Some of your entries are invalid]"); } if (!dasBlogSettings.SiteConfiguration.EnableComments) { errors.Add("Comments are disabled on the site."); } // Optional in case of Captcha. Commenting the settings in the config file // Will disable this check. People will typically disable this when using captcha. if (!string.IsNullOrEmpty(dasBlogSettings.SiteConfiguration.CheesySpamQ) && !string.IsNullOrEmpty(dasBlogSettings.SiteConfiguration.CheesySpamA) && dasBlogSettings.SiteConfiguration.CheesySpamQ.Trim().Length > 0 && dasBlogSettings.SiteConfiguration.CheesySpamA.Trim().Length > 0) { if (string.Compare(addcomment.CheesyQuestionAnswered, dasBlogSettings.SiteConfiguration.CheesySpamA, StringComparison.OrdinalIgnoreCase) != 0) { errors.Add("Answer to Spam Question is invalid. Please enter a valid answer for Spam Question and try again."); } } if (dasBlogSettings.SiteConfiguration.EnableCaptcha) { var recaptchaTask = recaptcha.Validate(Request); recaptchaTask.Wait(); var recaptchaResult = recaptchaTask.Result; if ((!recaptchaResult.success || recaptchaResult.score != 0) && recaptchaResult.score < dasBlogSettings.SiteConfiguration.RecaptchaMinimumScore) { errors.Add("Unfinished Captcha. Please finish the captcha by clicking 'I'm not a robot' and try again."); } } var commt = mapper.Map <NBR.Comment>(addcomment); commt.AuthorIPAddress = HttpContext.Connection.RemoteIpAddress.ToString(); commt.AuthorUserAgent = HttpContext.Request.Headers["User-Agent"].ToString(); commt.EntryId = Guid.NewGuid().ToString(); commt.IsPublic = !dasBlogSettings.SiteConfiguration.CommentsRequireApproval; commt.CreatedUtc = commt.ModifiedUtc = DateTime.Now.ToUniversalTime(); if (dasBlogSettings.SiteConfiguration.EnableSpamBlockingService) { commt = CheckForSpam(commt, dasBlogSettings.SiteConfiguration); // Spam Moderation is Disabled and the comment is spam. Let's show an error! // TODO: Discuss what are the pros and cons of showing error vs just silently deleting the // comment. if (!dasBlogSettings.SiteConfiguration.EnableSpamModeration && commt.SpamState == NBR.SpamState.Spam) { errors.Add("Spam Comment Detected. Please enter a legitimate comment that is not spam to post it."); } } if (errors.Count > 0) { return(CommentError(addcomment, errors)); } logger.LogInformation(new EventDataItem(EventCodes.CommentAdded, null, "Comment CONTENT DUMP", commt.Content)); var state = blogManager.AddComment(addcomment.TargetEntryId, commt); if (state == NBR.CommentSaveState.Failed) { logger.LogError(new EventDataItem(EventCodes.CommentBlocked, null, "Failed to save comment: {0}", commt.TargetTitle)); errors.Add("Failed to save comment."); } if (state == NBR.CommentSaveState.SiteCommentsDisabled) { logger.LogError(new EventDataItem(EventCodes.CommentBlocked, null, "Comments are closed for this post: {0}", commt.TargetTitle)); errors.Add("Comments are closed for this post."); } if (state == NBR.CommentSaveState.PostCommentsDisabled) { logger.LogError(new EventDataItem(EventCodes.CommentBlocked, null, "Comment are currently disabled: {0}", commt.TargetTitle)); errors.Add("Comment are currently disabled."); } if (state == NBR.CommentSaveState.NotFound) { logger.LogError(new EventDataItem(EventCodes.CommentBlocked, null, "Invalid Post Id: {0}", commt.TargetTitle)); errors.Add("Invalid Post Id."); } if (errors.Count > 0) { return(CommentError(addcomment, errors)); } logger.LogInformation(new EventDataItem(EventCodes.CommentAdded, null, "Comment created on: {0}", commt.TargetTitle)); BreakSiteCache(); return(Comment(addcomment.TargetEntryId)); }