1818using System . IO ;
1919using System . Linq ;
2020using System . Net ;
21- using DasBlog . Web . Services ;
22- using reCAPTCHA . AspNetCore . Attributes ;
2321using reCAPTCHA . AspNetCore ;
2422
2523namespace 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 ) ;
0 commit comments