PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/App_Code/Controls/RecentComments.cs

#
C# | 225 lines | 148 code | 39 blank | 38 comment | 15 complexity | 7a1c42f48dab3a51f4f8239e42e6ca25 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <summary>
  3. // Builds a category list.
  4. // </summary>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. namespace App_Code.Controls
  7. {
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text.RegularExpressions;
  13. using System.Web.UI;
  14. using System.Web.UI.HtmlControls;
  15. using BlogEngine.Core;
  16. using Resources;
  17. /// <summary>
  18. /// Builds a category list.
  19. /// </summary>
  20. public class RecentComments : Control
  21. {
  22. #region Constants and Fields
  23. /// <summary>
  24. /// The sync root.
  25. /// </summary>
  26. private static readonly object syncRoot = new object();
  27. /// <summary>
  28. /// The comments.
  29. /// </summary>
  30. private static readonly Dictionary<Guid, List<Comment>> blogsComments = new Dictionary<Guid, List<Comment>>();
  31. #endregion
  32. #region Constructors and Destructors
  33. /// <summary>
  34. /// Initializes static members of the <see cref="RecentComments"/> class.
  35. /// </summary>
  36. static RecentComments()
  37. {
  38. BindComments();
  39. Post.CommentAdded += (sender, args) => BindComments();
  40. Post.CommentRemoved += (sender, args) => BindComments();
  41. Post.Saved += PostSaved;
  42. Comment.Approved += (sender, args) => BindComments();
  43. BlogSettings.Changed += (sender, args) => BindComments();
  44. }
  45. #endregion
  46. #region Properties
  47. private static List<Comment> Comments
  48. {
  49. get
  50. {
  51. Guid blogId = Blog.CurrentInstance.Id;
  52. if (!blogsComments.ContainsKey(blogId))
  53. {
  54. lock (syncRoot)
  55. {
  56. if (!blogsComments.ContainsKey(blogId))
  57. {
  58. blogsComments[blogId] = new List<Comment>();
  59. }
  60. }
  61. }
  62. return blogsComments[blogId];
  63. }
  64. }
  65. #endregion
  66. #region Public Methods
  67. /// <summary>
  68. /// Outputs server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter"/> object and stores tracing information about the control if tracing is enabled.
  69. /// </summary>
  70. /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the control content.</param>
  71. public override void RenderControl(HtmlTextWriter writer)
  72. {
  73. if (Post.Posts.Count <= 0)
  74. {
  75. return;
  76. }
  77. var html = RenderComments();
  78. writer.Write(html);
  79. writer.Write(Environment.NewLine);
  80. }
  81. #endregion
  82. #region Methods
  83. /// <summary>
  84. /// Binds the comments.
  85. /// </summary>
  86. private static void BindComments()
  87. {
  88. var comments = (from post in Post.Posts
  89. where post.IsVisible
  90. from comment in post.Comments
  91. where comment.IsApproved
  92. select comment).ToList();
  93. comments.Sort();
  94. comments.Reverse();
  95. RecentComments.Comments.Clear();
  96. foreach (var comment in
  97. comments.Where(comment => comment.Email != "pingback" && comment.Email != "trackback").Take(BlogSettings.Instance.NumberOfRecentComments))
  98. {
  99. Comments.Add(comment);
  100. }
  101. }
  102. /// <summary>
  103. /// Handles the Saved event of the Post control.
  104. /// </summary>
  105. /// <param name="sender">The source of the event.</param>
  106. /// <param name="e">The <see cref="BlogEngine.Core.SavedEventArgs"/> instance containing the event data.</param>
  107. private static void PostSaved(object sender, SavedEventArgs e)
  108. {
  109. if (e.Action == SaveAction.Update)
  110. {
  111. return;
  112. }
  113. BindComments();
  114. }
  115. /// <summary>
  116. /// Renders the comments.
  117. /// </summary>
  118. /// <returns>The HTML string.</returns>
  119. private static string RenderComments()
  120. {
  121. if (Comments.Count == 0)
  122. {
  123. return string.Format("<p>{0}</p>", labels.none);
  124. }
  125. using (var ul = new HtmlGenericControl("ul"))
  126. {
  127. ul.Attributes.Add("class", "recentComments");
  128. ul.ID = "recentComments";
  129. foreach (var comment in Comments.Where(comment => comment.IsApproved))
  130. {
  131. var li = new HtmlGenericControl("li");
  132. // The post title
  133. var title = new HtmlAnchor { HRef = comment.Parent.RelativeLink, InnerText = comment.Parent.Title };
  134. title.Attributes.Add("class", "postTitle");
  135. li.Controls.Add(title);
  136. // The comment count on the post
  137. var count =
  138. new LiteralControl(string.Format(" ({0})<br />", ((Post)comment.Parent).ApprovedComments.Count));
  139. li.Controls.Add(count);
  140. // The author
  141. if (comment.Website != null)
  142. {
  143. var author = new HtmlAnchor { HRef = comment.Website.ToString(), InnerHtml = comment.Author };
  144. author.Attributes.Add("rel", "nofollow");
  145. li.Controls.Add(author);
  146. var wrote = new LiteralControl(string.Format(" {0}: ", labels.wrote));
  147. li.Controls.Add(wrote);
  148. }
  149. else
  150. {
  151. var author = new LiteralControl(string.Format("{0} {1}: ", comment.Author, labels.wrote));
  152. li.Controls.Add(author);
  153. }
  154. // The comment body
  155. var commentBody = Regex.Replace(comment.Content, @"\[(.*?)\]", string.Empty);
  156. var bodyLength = Math.Min(commentBody.Length, 50);
  157. commentBody = commentBody.Substring(0, bodyLength);
  158. if (commentBody.Length > 0)
  159. {
  160. if (commentBody[commentBody.Length - 1] == '&')
  161. {
  162. commentBody = commentBody.Substring(0, commentBody.Length - 1);
  163. }
  164. }
  165. commentBody += comment.Content.Length <= 50 ? " " : "? ";
  166. var body = new LiteralControl(commentBody);
  167. li.Controls.Add(body);
  168. // The comment link
  169. var link = new HtmlAnchor
  170. {
  171. HRef = string.Format("{0}#id_{1}", comment.Parent.RelativeLink, comment.Id), InnerHtml = string.Format("[{0}]", labels.more)
  172. };
  173. link.Attributes.Add("class", "moreLink");
  174. li.Controls.Add(link);
  175. ul.Controls.Add(li);
  176. }
  177. using (var sw = new StringWriter())
  178. {
  179. ul.RenderControl(new HtmlTextWriter(sw));
  180. return sw.ToString();
  181. }
  182. }
  183. }
  184. #endregion
  185. }
  186. }