Skip to content

Commit

Permalink
Merge pull request #469 from thousandtyone/main
Browse files Browse the repository at this point in the history
Fix For Issue 458. Comment Now Shows Relevant Errors.
  • Loading branch information
poppastring authored Sep 23, 2020
2 parents cf6d274 + 350391f commit 4b12c19
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 13 deletions.
55 changes: 46 additions & 9 deletions source/DasBlog.Web.UI/Controllers/BlogPostController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
using System.IO;
using System.Linq;
using System.Net;
using DasBlog.Web.Services;
using reCAPTCHA.AspNetCore.Attributes;
using reCAPTCHA.AspNetCore;

namespace DasBlog.Web.Controllers
Expand Down Expand Up @@ -331,6 +329,44 @@ public IActionResult Comment(string posttitle, string day, string month, string
return SinglePostView(lpvm);
}

public IActionResult CommentError(AddCommentViewModel comment, List<string> errors)
{
ListPostsViewModel lpvm = null;
NBR.Entry entry = null;
var postguid = Guid.Parse(comment.TargetEntryId);
entry = blogManager.GetBlogPostByGuid(postguid);
if (entry != null)
{
lpvm = new ListPostsViewModel
{
Posts = new List<PostViewModel> { mapper.Map<PostViewModel>(entry) }
};

if (dasBlogSettings.SiteConfiguration.EnableComments)
{
var lcvm = new ListCommentsViewModel
{
Comments = blogManager.GetComments(entry.EntryId, false)
.Select(comment => mapper.Map<CommentViewModel>(comment)).ToList(),
PostId = entry.EntryId,
PostDate = entry.CreatedUtc,
CommentUrl = dasBlogSettings.GetCommentViewUrl(comment.TargetEntryId),
ShowComments = true
};

if(comment != null)
lcvm.CurrentComment = comment;
lpvm.Posts.First().Comments = lcvm;
if(errors != null && errors.Count > 0 )
lpvm.Posts.First().ErrorMessages = errors;
}
}

return SinglePostView(lpvm);
}



private IActionResult Comment(string posttitle)
{
return Comment(posttitle, string.Empty, string.Empty, string.Empty);
Expand All @@ -340,6 +376,8 @@ private IActionResult Comment(string posttitle)
[HttpPost("post/comments")]
public IActionResult AddComment(AddCommentViewModel addcomment)
{
List<string> errors = new List<string>();

if (!dasBlogSettings.SiteConfiguration.EnableComments)
{
return BadRequest();
Expand All @@ -360,7 +398,7 @@ public IActionResult AddComment(AddCommentViewModel addcomment)
if (string.Compare(addcomment.CheesyQuestionAnswered, dasBlogSettings.SiteConfiguration.CheesySpamA,
StringComparison.OrdinalIgnoreCase) != 0)
{
return Comment(addcomment.TargetEntryId);
errors.Add("Answer to Spam Question is invalid. Please enter a valid answer for Spam Question and try again.");
}
}

Expand All @@ -372,15 +410,14 @@ public IActionResult AddComment(AddCommentViewModel addcomment)
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);
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);
Expand Down
17 changes: 14 additions & 3 deletions source/DasBlog.Web.UI/Controllers/DasBlogBaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ protected DasBlogBaseController(IDasBlogSettings settings)

protected ViewResult SinglePostView(ListPostsViewModel listPostsViewModel)
{
SinglePost(listPostsViewModel?.Posts?.First());

SinglePost(listPostsViewModel?.Posts?.First());
ViewData[Constants.ShowPageControl] = false;
return View(BLOG_PAGE, listPostsViewModel);
}
Expand Down Expand Up @@ -53,14 +52,26 @@ protected void SinglePost(PostViewModel post)
ViewData["Author"] = post.Author;
ViewData["PageImageUrl"] = (post.ImageUrl?.Length > 0) ? post.ImageUrl : dasBlogSettings.MetaTags.TwitterImage;
ViewData["PageVideoUrl"] = (post.VideoUrl?.Length > 0) ? post.VideoUrl : string.Empty;
ShowErrors(post);
}
else
{
DefaultPage();
}
}

protected void DefaultPage(string pageTitle = "")
private void ShowErrors(PostViewModel post)
{
if(post != null && post.Comments.CurrentComment != null && post.ErrorMessages != null && post.ErrorMessages.Count > 0)
{
foreach(string ErrorMessage in post.ErrorMessages)
{
ModelState.AddModelError("", ErrorMessage);
}
}
}

protected void DefaultPage(string pageTitle = "")
{
if (pageTitle.Length > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ public class ListCommentsViewModel
public string CommentUrl { get; set; }

public bool ShowComments { get; set; }

public AddCommentViewModel CurrentComment {get; set;} = null;
}
}
4 changes: 4 additions & 0 deletions source/DasBlog.Web.UI/Models/BlogViewModels/PostViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,9 @@ public class PostViewModel
public string VideoUrl { get; set; } = string.Empty;

public int Order { get; set; } = 0;


public List<string> ErrorMessages { get; set; }

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@
</div>
@if (dasBlogSettings.AreCommentsPermitted(Model.PostDate))
{
@if(Model.CurrentComment != null)
{
<partial name="_CommentAddPartial" model="Model.CurrentComment" />
}
else
{
<partial name="_CommentAddPartial" model="new AddCommentViewModel() { TargetEntryId = Model.PostId }" />
}
}
else
{
<p>Comments are closed.</p>
}
</div>
}
}

0 comments on commit 4b12c19

Please sign in to comment.