PageRenderTime 34ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Comment.cs

#
C# | 423 lines | 200 code | 60 blank | 163 comment | 26 complexity | 93ad1283cf9bea8f1423918bb20b59d5 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace BlogEngine.Core
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Xml.Serialization;
  7. /// <summary>
  8. /// Represents a comment to a blog post.
  9. /// </summary>
  10. [Serializable]
  11. public sealed class Comment : IComparable<Comment>, IPublishable
  12. {
  13. #region Constants and Fields
  14. /// <summary>
  15. /// The comments.
  16. /// </summary>
  17. private List<Comment> comments;
  18. /// <summary>
  19. /// The date created.
  20. /// </summary>
  21. private DateTime dateCreated = DateTime.MinValue;
  22. #endregion
  23. #region Events
  24. /// <summary>
  25. /// Occurs after a comment is approved by the comment moderator.
  26. /// </summary>
  27. public static event EventHandler<EventArgs> Approved;
  28. /// <summary>
  29. /// Occurs just before a comment is approved by the comment moderator.
  30. /// </summary>
  31. public static event EventHandler<CancelEventArgs> Approving;
  32. /// <summary>
  33. /// Occurs after a comment is disapproved by the comment moderator.
  34. /// </summary>
  35. public static event EventHandler<EventArgs> Disapproved;
  36. /// <summary>
  37. /// Occurs just before a comment is disapproved by the comment moderator.
  38. /// </summary>
  39. public static event EventHandler<CancelEventArgs> Disapproving;
  40. /// <summary>
  41. /// Occurs when the post is being served to the output stream.
  42. /// </summary>
  43. public static event EventHandler<ServingEventArgs> Serving;
  44. /// <summary>
  45. /// Occurs when the page is being attacked by robot spam.
  46. /// </summary>
  47. public static event EventHandler<EventArgs> SpamAttack;
  48. #endregion
  49. #region Properties
  50. /// <summary>
  51. /// Gets the absolute link.
  52. /// </summary>
  53. /// <value>The absolute link.</value>
  54. public Uri AbsoluteLink
  55. {
  56. get
  57. {
  58. return new Uri(string.Format("{0}#id_{1}", Parent.AbsoluteLink, Id));
  59. }
  60. }
  61. /// <summary>
  62. /// Gets or sets the author.
  63. /// </summary>
  64. /// <value>The author.</value>
  65. public string Author { get; set; }
  66. /// <summary>
  67. /// Gets or sets the Avatar of the comment.
  68. /// </summary>
  69. public string Avatar { get; set; }
  70. /// <summary>
  71. /// Gets a list of Comments.
  72. /// </summary>
  73. public List<Comment> Comments
  74. {
  75. get
  76. {
  77. return comments ?? (comments = new List<Comment>());
  78. }
  79. }
  80. /// <summary>
  81. /// Gets or sets the content.
  82. /// </summary>
  83. /// <value>The content.</value>
  84. public string Content { get; set; } // Do Not HtmlEncode, leads to double, triple, etc encoding.
  85. /// <summary>
  86. /// Gets or sets the country.
  87. /// </summary>
  88. /// <value>The country.</value>
  89. public string Country { get; set; }
  90. /// <summary>
  91. /// Gets or sets when the comment was created.
  92. /// </summary>
  93. public DateTime DateCreated
  94. {
  95. get
  96. {
  97. return dateCreated == DateTime.MinValue ? dateCreated : dateCreated.AddHours(BlogSettings.Instance.Timezone);
  98. }
  99. set
  100. {
  101. dateCreated = value;
  102. }
  103. }
  104. /// <summary>
  105. /// Gets the description. Returns always string.empty.
  106. /// </summary>
  107. /// <value>The description.</value>
  108. public string Description
  109. {
  110. get
  111. {
  112. return string.Empty;
  113. }
  114. }
  115. /// <summary>
  116. /// Gets or sets the email.
  117. /// </summary>
  118. /// <value>The email.</value>
  119. public string Email { get; set; }
  120. /// <summary>
  121. /// Gets or sets the IP address.
  122. /// </summary>
  123. /// <value>The IP address.</value>
  124. public string IP { get; set; }
  125. /// <summary>
  126. /// Gets or sets the Id of the comment.
  127. /// </summary>
  128. public Guid Id { get; set; }
  129. /// <summary>
  130. /// Gets or sets a value indicating whether the Comment is approved.
  131. /// </summary>
  132. public bool IsApproved { get; set; }
  133. /// <summary>
  134. /// Indicate if comment is spam
  135. /// </summary>
  136. public bool IsSpam { get; set; }
  137. /// <summary>
  138. /// indicate if comment is deleted
  139. /// </summary>
  140. public bool IsDeleted { get; set; }
  141. /// <summary>
  142. /// Gets a value indicating whether or not this comment has been published
  143. /// </summary>
  144. public bool IsPublished
  145. {
  146. get
  147. {
  148. return IsApproved && !IsSpam && !IsDeleted;
  149. }
  150. }
  151. /// <summary>
  152. /// Gets a value indicating whether or not this comment should be shown
  153. /// </summary>
  154. /// <value></value>
  155. public bool IsVisible
  156. {
  157. get
  158. {
  159. return IsApproved && !IsSpam && !IsDeleted;
  160. }
  161. }
  162. /// <summary>
  163. /// Gets a value indicating whether or not this comment is visible to visitors not logged into the blog.
  164. /// </summary>
  165. /// <value></value>
  166. public bool IsVisibleToPublic
  167. {
  168. get
  169. {
  170. return IsApproved && !IsSpam && !IsDeleted;
  171. }
  172. }
  173. /// <summary>
  174. /// Gets or sets process that approved or rejected comment
  175. /// </summary>
  176. [XmlElement]
  177. public string ModeratedBy { get; set; }
  178. /// <summary>
  179. /// Gets or sets the parent.
  180. /// </summary>
  181. /// <value>The parent.</value>
  182. public IPublishable Parent { get; set; }
  183. /// <summary>
  184. /// Gets or sets the Id of the parent comment.
  185. /// </summary>
  186. public Guid ParentId { get; set; }
  187. /// <summary>
  188. /// Gets the relative link of the comment.
  189. /// </summary>
  190. /// <value>The relative link.</value>
  191. public string RelativeLink
  192. {
  193. get
  194. {
  195. return string.Format("{0}#id_{1}", Parent.RelativeLink, Id);
  196. }
  197. }
  198. /// <summary>
  199. /// Gets abbreviated content
  200. /// </summary>
  201. public string Teaser
  202. {
  203. get
  204. {
  205. var ret = Utils.StripHtml(Content).Trim();
  206. return ret.Length > 120 ? string.Format("{0} ...", ret.Substring(0, 116)) : ret;
  207. }
  208. }
  209. /// <summary>
  210. /// Pingbacks and trackbacks are identified by email
  211. /// </summary>
  212. public bool IsPingbackOrTrackback
  213. {
  214. get { return (Email.ToLowerInvariant() == "pingback" || Email.ToLowerInvariant() == "trackback") ? true : false; }
  215. }
  216. /// <summary>
  217. /// Gets the title of the object
  218. /// </summary>
  219. /// <value></value>
  220. public string Title
  221. {
  222. get
  223. {
  224. if (Website != null)
  225. {
  226. return string.Format("<a class=\"comment_auth\" href=\"{2}\" alt=\"{2}\" title=\"{2}\">{0}</a> on <a href=\"{3}\" alt=\"{3}\">{1}</a>", Author, Parent.Title, Website.ToString(), RelativeLink);
  227. }
  228. return string.Format("{0} on <a href=\"{2}\" alt=\"{2}\">{1}</a>", Author, Parent.Title, RelativeLink);
  229. }
  230. }
  231. /// <summary>
  232. /// Gets or sets the website.
  233. /// </summary>
  234. /// <value>The website.</value>
  235. public Uri Website { get; set; }
  236. /// <summary>
  237. /// Gets Categories.
  238. /// </summary>
  239. StateList<Category> IPublishable.Categories
  240. {
  241. get
  242. {
  243. return null;
  244. }
  245. }
  246. /// <summary>
  247. /// Gets DateModified.
  248. /// </summary>
  249. DateTime IPublishable.DateModified
  250. {
  251. get
  252. {
  253. return DateCreated;
  254. }
  255. }
  256. #endregion
  257. #region Public Methods
  258. /// <summary>
  259. /// Called when [serving].
  260. /// </summary>
  261. /// <param name="comment">The comment.</param>
  262. /// <param name="arg">The <see cref="BlogEngine.Core.ServingEventArgs"/> instance containing the event data.</param>
  263. public static void OnServing(Comment comment, ServingEventArgs arg)
  264. {
  265. if (Serving != null)
  266. {
  267. Serving(comment, arg);
  268. }
  269. }
  270. /// <summary>
  271. /// Called when [spam attack].
  272. /// </summary>
  273. public static void OnSpamAttack()
  274. {
  275. if (SpamAttack != null)
  276. {
  277. SpamAttack(null, new EventArgs());
  278. }
  279. }
  280. #endregion
  281. #region Implemented Interfaces
  282. #region IComparable<Comment>
  283. /// <summary>
  284. /// Compares the current object with another object of the same type.
  285. /// </summary>
  286. /// <param name="other">
  287. /// An object to compare with this object.
  288. /// </param>
  289. /// <returns>
  290. /// A 32-bit signed integer that indicates the relative order of the
  291. /// objects being compared. The return value has the following meanings:
  292. /// Value Meaning Less than zero This object is less than the other parameter.
  293. /// Zero This object is equal to other. Greater than zero This object is greater than other.
  294. /// </returns>
  295. public int CompareTo(Comment other)
  296. {
  297. return DateCreated.CompareTo(other.DateCreated);
  298. }
  299. #endregion
  300. #region IPublishable
  301. /// <summary>
  302. /// Raises the <see cref="Serving"/> event.
  303. /// </summary>
  304. /// <param name="eventArgs">The <see cref="BlogEngine.Core.ServingEventArgs"/> instance containing the event data.</param>
  305. public void OnServing(ServingEventArgs eventArgs)
  306. {
  307. if (Serving != null)
  308. {
  309. Serving(this, eventArgs);
  310. }
  311. }
  312. #endregion
  313. #endregion
  314. #region Methods
  315. /// <summary>
  316. /// Called when [approved].
  317. /// </summary>
  318. /// <param name="comment">The comment.</param>
  319. internal static void OnApproved(Comment comment)
  320. {
  321. if (Approved != null)
  322. {
  323. Approved(comment, EventArgs.Empty);
  324. }
  325. }
  326. /// <summary>
  327. /// Called when [approving].
  328. /// </summary>
  329. /// <param name="comment">The comment.</param>
  330. /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
  331. internal static void OnApproving(Comment comment, CancelEventArgs e)
  332. {
  333. if (Approving != null)
  334. {
  335. Approving(comment, e);
  336. }
  337. }
  338. /// <summary>
  339. /// Called when [disapproved].
  340. /// </summary>
  341. /// <param name="comment">The comment.</param>
  342. internal static void OnDisapproved(Comment comment)
  343. {
  344. if (Disapproved != null)
  345. {
  346. Disapproved(comment, EventArgs.Empty);
  347. }
  348. }
  349. /// <summary>
  350. /// Called when [disapproving].
  351. /// </summary>
  352. /// <param name="comment">The comment.</param>
  353. /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
  354. internal static void OnDisapproving(Comment comment, CancelEventArgs e)
  355. {
  356. if (Disapproving != null)
  357. {
  358. Disapproving(comment, e);
  359. }
  360. }
  361. #endregion
  362. }
  363. }