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