Skip to content

Commit 4b12c19

Browse files
authored
Merge pull request #469 from thousandtyone/main
Fix For Issue 458. Comment Now Shows Relevant Errors.
2 parents cf6d274 + 350391f commit 4b12c19

File tree

5 files changed

+74
-13
lines changed

5 files changed

+74
-13
lines changed

source/DasBlog.Web.UI/Controllers/BlogPostController.cs

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
using System.IO;
1919
using System.Linq;
2020
using System.Net;
21-
using DasBlog.Web.Services;
22-
using reCAPTCHA.AspNetCore.Attributes;
2321
using reCAPTCHA.AspNetCore;
2422

2523
namespace DasBlog.Web.Controllers
@@ -331,6 +329,44 @@ public IActionResult Comment(string posttitle, string day, string month, string
331329
return SinglePostView(lpvm);
332330
}
333331

332+
public IActionResult CommentError(AddCommentViewModel comment, List<string> errors)
333+
{
334+
ListPostsViewModel lpvm = null;
335+
NBR.Entry entry = null;
336+
var postguid = Guid.Parse(comment.TargetEntryId);
337+
entry = blogManager.GetBlogPostByGuid(postguid);
338+
if (entry != null)
339+
{
340+
lpvm = new ListPostsViewModel
341+
{
342+
Posts = new List<PostViewModel> { mapper.Map<PostViewModel>(entry) }
343+
};
344+
345+
if (dasBlogSettings.SiteConfiguration.EnableComments)
346+
{
347+
var lcvm = new ListCommentsViewModel
348+
{
349+
Comments = blogManager.GetComments(entry.EntryId, false)
350+
.Select(comment => mapper.Map<CommentViewModel>(comment)).ToList(),
351+
PostId = entry.EntryId,
352+
PostDate = entry.CreatedUtc,
353+
CommentUrl = dasBlogSettings.GetCommentViewUrl(comment.TargetEntryId),
354+
ShowComments = true
355+
};
356+
357+
if(comment != null)
358+
lcvm.CurrentComment = comment;
359+
lpvm.Posts.First().Comments = lcvm;
360+
if(errors != null && errors.Count > 0 )
361+
lpvm.Posts.First().ErrorMessages = errors;
362+
}
363+
}
364+
365+
return SinglePostView(lpvm);
366+
}
367+
368+
369+
334370
private IActionResult Comment(string posttitle)
335371
{
336372
return Comment(posttitle, string.Empty, string.Empty, string.Empty);
@@ -340,6 +376,8 @@ private IActionResult Comment(string posttitle)
340376
[HttpPost("post/comments")]
341377
public IActionResult AddComment(AddCommentViewModel addcomment)
342378
{
379+
List<string> errors = new List<string>();
380+
343381
if (!dasBlogSettings.SiteConfiguration.EnableComments)
344382
{
345383
return BadRequest();
@@ -360,7 +398,7 @@ public IActionResult AddComment(AddCommentViewModel addcomment)
360398
if (string.Compare(addcomment.CheesyQuestionAnswered, dasBlogSettings.SiteConfiguration.CheesySpamA,
361399
StringComparison.OrdinalIgnoreCase) != 0)
362400
{
363-
return Comment(addcomment.TargetEntryId);
401+
errors.Add("Answer to Spam Question is invalid. Please enter a valid answer for Spam Question and try again.");
364402
}
365403
}
366404

@@ -372,15 +410,14 @@ public IActionResult AddComment(AddCommentViewModel addcomment)
372410
if ((!recaptchaResult.success || recaptchaResult.score != 0) &&
373411
recaptchaResult.score < dasBlogSettings.SiteConfiguration.RecaptchaMinimumScore )
374412
{
375-
// Todo: Rajiv Popat: This just redirects to the comment page. Ideally user should be informed that
376-
// the captch is invalid and he should be shown an error page with ability to fix the issue.
377-
// We need to have the ability to show errors and let the user fix typos in Captcha or Cheesy
378-
// Question. For now we are following the sample implementation as Cheesy Spam Question above
379-
// for the sake of consistency but this should be fixed everywhere.
380-
return Comment(addcomment.TargetEntryId);
413+
errors.Add("Unfinished Captcha. Please finish the captcha by clicking 'I'm not a robot' and try again.");
381414
}
382415
}
383416

417+
if(errors.Count > 0)
418+
return CommentError(addcomment, errors);
419+
420+
384421
addcomment.Content = dasBlogSettings.FilterHtml(addcomment.Content);
385422

386423
var commt = mapper.Map<NBR.Comment>(addcomment);

source/DasBlog.Web.UI/Controllers/DasBlogBaseController.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ protected DasBlogBaseController(IDasBlogSettings settings)
2424

2525
protected ViewResult SinglePostView(ListPostsViewModel listPostsViewModel)
2626
{
27-
SinglePost(listPostsViewModel?.Posts?.First());
28-
27+
SinglePost(listPostsViewModel?.Posts?.First());
2928
ViewData[Constants.ShowPageControl] = false;
3029
return View(BLOG_PAGE, listPostsViewModel);
3130
}
@@ -53,14 +52,26 @@ protected void SinglePost(PostViewModel post)
5352
ViewData["Author"] = post.Author;
5453
ViewData["PageImageUrl"] = (post.ImageUrl?.Length > 0) ? post.ImageUrl : dasBlogSettings.MetaTags.TwitterImage;
5554
ViewData["PageVideoUrl"] = (post.VideoUrl?.Length > 0) ? post.VideoUrl : string.Empty;
55+
ShowErrors(post);
5656
}
5757
else
5858
{
5959
DefaultPage();
6060
}
6161
}
6262

63-
protected void DefaultPage(string pageTitle = "")
63+
private void ShowErrors(PostViewModel post)
64+
{
65+
if(post != null && post.Comments.CurrentComment != null && post.ErrorMessages != null && post.ErrorMessages.Count > 0)
66+
{
67+
foreach(string ErrorMessage in post.ErrorMessages)
68+
{
69+
ModelState.AddModelError("", ErrorMessage);
70+
}
71+
}
72+
}
73+
74+
protected void DefaultPage(string pageTitle = "")
6475
{
6576
if (pageTitle.Length > 0)
6677
{

source/DasBlog.Web.UI/Models/BlogViewModels/ListCommentsViewModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ public class ListCommentsViewModel
1616
public string CommentUrl { get; set; }
1717

1818
public bool ShowComments { get; set; }
19+
20+
public AddCommentViewModel CurrentComment {get; set;} = null;
1921
}
2022
}

source/DasBlog.Web.UI/Models/BlogViewModels/PostViewModel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,9 @@ public class PostViewModel
5858
public string VideoUrl { get; set; } = string.Empty;
5959

6060
public int Order { get; set; } = 0;
61+
62+
63+
public List<string> ErrorMessages { get; set; }
64+
6165
}
6266
}

source/DasBlog.Web.UI/Views/Shared/_CommentBlockPartial.cshtml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@
2121
</div>
2222
@if (dasBlogSettings.AreCommentsPermitted(Model.PostDate))
2323
{
24+
@if(Model.CurrentComment != null)
25+
{
26+
<partial name="_CommentAddPartial" model="Model.CurrentComment" />
27+
}
28+
else
29+
{
2430
<partial name="_CommentAddPartial" model="new AddCommentViewModel() { TargetEntryId = Model.PostId }" />
31+
}
2532
}
2633
else
2734
{
2835
<p>Comments are closed.</p>
2936
}
3037
</div>
31-
}
38+
}

0 commit comments

Comments
 (0)