Recent comments macro for DasBlog

One of the issues I had with John Forsythe's Recent Comments macro for DasBlog was that the dasBlog recent comments weren't ordered by date (descending). I found that as people commented on older posts they were getting buried which irritated me as many were very still valid comments.

The fix was actually fairly simple, it was just a matter of adding  a sort and thanks to Lamba expressions, this is something we can do fairly simply. If you want to add recent comments to your dasBlog installation, use the following macro:

Recent Comments Macropublic virtual Control RecentComments(int count, int adminComments, int trimTitle, int trimContent, int trimAuthor, bool showTitle, bool showCommentText, bool showCommentCount) {...}{ int commentsToShow; int totalComments;     CommentCollection allComments = this.requestPage.DataService.GetAllComments();     totalComments = allComments.Count; //Sort the comments in date order (descending)    allComments.Sort((c1, c2) => c1.CreatedUtc.CompareTo(c2.CreatedUtc)); if (!this.requestPage.HideAdminTools && SiteSecurity.IsInRole("admin"))         commentsToShow = totalComments - adminComments; else        commentsToShow = totalComments - count; if (commentsToShow < 0)         commentsToShow = 0;     StringBuilder sb = new StringBuilder();     sb.AppendLine("<div class=\"recentComments\">"); if (showCommentCount)         sb.AppendFormat("<div class=\"totalComments\">Total Comments:{0}</div>", totalComments);     sb.AppendLine("<ul class=\"comments\">"); Loop through the comments#region Loop through the comments for (int i = totalComments - 1; i >= commentsToShow; i--) {...}    {         Comment current = allComments[i]; bool showComment; if (!current.IsPublic || (current.SpamState == SpamState.Spam)) {...}        { if (!this.requestPage.HideAdminTools && SiteSecurity.IsInRole("admin")) {...}            {                 showComment = true;             }else{...}            {                 showComment = false; if (commentsToShow > 0)                     commentsToShow--;             }        }else{...}        {             showComment = true;         }if (showComment) {...}        { if ((current.SpamState == SpamState.Spam))                 sb.Append("<li class=\"spam\">"); else if (!current.IsPublic)                 sb.Append("<li class=\"hidden\">"); else                sb.Append("<li>"); string link = String.Format("{0}{1}{2}", SiteUtilities.GetCommentViewUrl(current.TargetEntryId), "#", current.EntryId); string title = current.TargetTitle; string desc = current.Content; string author = current.Author; if (showTitle) {...}            {                 sb.AppendFormat("<div class=\"recent{0}CommentsTitle\"><a href=\"{1}\">",                     current.SpamState,                     link                     ); if ((title.Length > trimTitle) && (trimTitle > 0))                     sb.AppendFormat("RE: {0}...", title.Substring(0, trimTitle)); else                    sb.AppendFormat("RE: {0}", title);                 sb.Append("</a></div>");             }if (showCommentText) {...}            {                 sb.AppendFormat("<div class=\"recentCommentsContent\"><a href=\"{0}\">",                     link                     ); if ((desc.Length > trimContent) && (trimContent > 0)) {...}                {                     sb.Append(desc.Substring(0, trimContent));                     sb.Append("...");                 }else{...}                {                     sb.Append(desc);                 }                sb.Append("</a></div>");             }            sb.Append("<div class=\"recentCommentsAuthor\">"); if ((author.Length > trimAuthor) && (trimAuthor > 0)) {...}            { int num3 = (trimAuthor > author.Length) ? author.Length : trimAuthor;                 sb.Append("by " + author.Substring(0, num3));                 sb.Append("...");             }else{...}            {                 sb.Append("by " + author);             }            sb.Append("</div></li>");         }    }#endregion    sb.AppendLine("</ul>");     sb.AppendLine("</div>"); return new LiteralControl(sb.ToString()); }

I've since been working on extending it further so you can add a "All Comments" link which I'll post up later as it needs a little more work :)

If you want this wrapped up as a DLL let me know and I'll upload it.

Update 26th Feb 2009: You can download the dll here (it's also got a few other things in there if you want to look around).

Update 27th Feb 2009: I noticed that the above code was messing up everynow and again so I've updated it to use Linq instead which seems to work well. I've updated the DLL but not the source yet.

Subscribe to TSD

Don’t miss out on the latest posts. Sign up now to get access to the library of members-only posts.
jamie@example.com
Subscribe