/
ArticleModel.cs
266 lines (232 loc) · 11.3 KB
/
ArticleModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using TheDailyWtf.Data;
using TheDailyWtf.Discourse;
namespace TheDailyWtf.Models
{
public sealed class ArticleModel
{
public ArticleModel()
{
this.Author = new AuthorModel();
this.Series = new SeriesModel();
}
public int Id { get; set; }
public AuthorModel Author { get; set; }
public string Status { get; set; }
public string Summary { get; set; }
[AllowHtml]
public string BodyHtml { get; set; }
public string Title { get; set; }
public string RssTitle
{
get
{
if (this.Series.Title.Equals("Feature Articles", StringComparison.OrdinalIgnoreCase))
return HttpUtility.HtmlEncode(this.Title);
else
return HttpUtility.HtmlEncode(string.Format("{0}: {1}", this.Series.Title, this.Title));
}
}
public int DiscourseCommentCount { get; set; }
public int CachedCommentCount { get; set; }
public int CoalescedCommentCount { get { return Math.Max(this.DiscourseCommentCount, this.CachedCommentCount); } }
public DateTime? LastCommentDate { get; set; }
public string LastCommentDateDescription
{
get
{
if (LastCommentDate == null)
return "-none-";
if (LastCommentDate.Value.Date == DateTime.Now.Date)
return this.LastCommentDate.Value.ToShortTimeString();
return this.LastCommentDate.Value.ToShortDateString();
}
}
public int? DiscourseTopicId { get; set; }
public string DiscourseTopicSlug { get; set; }
public string DiscourseThreadUrl { get { return string.Format("http://what.thedailywtf.com/t/{0}/{1}", this.DiscourseTopicSlug, this.DiscourseTopicId); } }
public DateTime? PublishedDate { get; set; }
public SeriesModel Series { get; set; }
public string Url { get { return string.Format("http://{0}/articles/{1}", Config.Wtf.Host, this.Slug); } }
public string CommentsUrl { get { return string.Format("http://{0}/articles/comments/{1}", Config.Wtf.Host, this.Slug); } }
public string Slug { get; set; }
public string TwitterUrl { get { return string.Format("//www.twitter.com/home?status=http:{0}+-+{1}+-+The+Daily+WTF", HttpUtility.UrlEncode(this.Url), HttpUtility.UrlEncode(this.Title)); } }
public string FacebookUrl { get { return string.Format("//www.facebook.com/sharer.php?u=http:{0}&t={1}+-+The+Daily+WTF", HttpUtility.UrlEncode(this.Url), HttpUtility.UrlEncode(this.Title)); } }
public string EmailUrl
{
get
{
return string.Format(
"mailto:%20?subject={0}&body={1}",
HttpUtility.UrlPathEncode("Check out this article on The Daily WTF..."),
HttpUtility.UrlPathEncode(string.Format("{0}: {1}", this.Title, this.Url))
);
}
}
public string GooglePlusUrl { get { return string.Format("//plus.google.com/share?url={0}", HttpUtility.UrlEncode(this.Url)); } }
public int? PreviousArticleId { get; set; }
public string PreviousArticleTitle { get; set; }
public string PreviousArticleSlug { get; set; }
public string PreviousArticleUrl { get { return string.Format("//{0}/articles/{1}", Config.Wtf.Host, this.PreviousArticleSlug); } }
public int? NextArticleId { get; set; }
public string NextArticleTitle { get; set; }
public string NextArticleSlug { get; set; }
public string NextArticleUrl { get { return string.Format("//{0}/articles/{1}", Config.Wtf.Host, this.NextArticleSlug); } }
public static IEnumerable<ArticleModel> GetAllArticlesBySeries(string series)
{
var articles = StoredProcs.Articles_GetArticles(series, Domains.PublishedStatus.Published, null, null).Execute();
return articles.Select(a => ArticleModel.FromTable(a));
}
public static IEnumerable<ArticleModel> GetAllArticlesByMonth(DateTime month)
{
return GetSeriesArticlesByMonth(null, month);
}
public static IEnumerable<ArticleModel> GetSeriesArticlesByMonth(string series, DateTime month)
{
var monthStart = new DateTime(month.Year, month.Month, 1);
var articles = StoredProcs.Articles_GetArticles(
series,
Domains.PublishedStatus.Published,
monthStart,
monthStart.AddMonths(1).AddSeconds(-1.0)
).Execute();
return articles.Select(a => ArticleModel.FromTable(a));
}
public static IEnumerable<ArticleModel> GetRecentArticles()
{
return GetRecentArticles(8);
}
public static IEnumerable<ArticleModel> GetRecentArticles(int count)
{
var articles = StoredProcs.Articles_GetRecentArticles(Domains.PublishedStatus.Published, Article_Count: count).Execute();
return articles.Select(a => ArticleModel.FromTable(a));
}
public static IEnumerable<ArticleModel> GetRecentArticlesBySeries(string slug)
{
var articles = StoredProcs.Articles_GetRecentArticles(Domains.PublishedStatus.Published, Series_Slug: slug, Article_Count: 8).Execute();
return articles.Select(a => ArticleModel.FromTable(a));
}
public static IEnumerable<ArticleModel> GetRecentArticlesByAuthor(string slug)
{
var articles = StoredProcs.Articles_GetRecentArticles(Domains.PublishedStatus.Published, Author_Slug: slug, Article_Count: 8).Execute();
return articles.Select(a => ArticleModel.FromTable(a));
}
public IEnumerable<ArticleModel> GetSimilarArticles()
{
yield break;
}
public static IEnumerable<ArticleModel> GetUnpublishedArticles()
{
var articles = StoredProcs.Articles_GetUnpublishedArticles().Execute();
return articles.Select(a => ArticleModel.FromTable(a));
}
public IEnumerable<CommentModel> GetFeaturedComments()
{
return CommentModel.GetFeaturedCommentsForArticle(this);
}
public static ArticleModel GetArticleById(int id)
{
var article = StoredProcs.Articles_GetArticleById(id).Execute();
if (article == null)
return null;
return ArticleModel.FromTable(article);
}
public static ArticleModel GetArticleBySlug(string slug)
{
var article = StoredProcs.Articles_GetArticleBySlug(slug).Execute();
if (article == null)
return null;
return ArticleModel.FromTable(article);
}
public static ArticleModel GetRandomArticle()
{
var article = StoredProcs.Articles_GetRandomArticle().Execute();
return ArticleModel.FromTable(article);
}
public static ArticleModel FromTable(Tables.Articles_Extended article)
{
DateTime lastCommentDate = DateTime.Now;
var model = new ArticleModel()
{
Id = article.Article_Id,
Slug = article.Article_Slug,
Author = AuthorModel.FromTable(article),
BodyHtml = article.Body_Html,
DiscourseCommentCount = (int)article.Cached_Comment_Count,
CachedCommentCount = (int)article.Cached_Comment_Count,
DiscourseTopicId = article.Discourse_Topic_Id,
LastCommentDate = lastCommentDate,
PublishedDate = article.Published_Date,
Series = SeriesModel.FromTable(article),
Status = article.PublishedStatus_Name,
Summary = ArticleModel.ExtractAndStripFirstParagram(article.Body_Html),
Title = article.Title_Text,
PreviousArticleId = article.Previous_Article_Id,
PreviousArticleSlug = article.Previous_Article_Slug,
PreviousArticleTitle = article.Previous_Title_Text,
NextArticleId = article.Next_Article_Id,
NextArticleSlug = article.Next_Article_Slug,
NextArticleTitle = article.Next_Title_Text
};
if (article.Discourse_Topic_Id != null)
{
var topic = DiscourseHelper.GetDiscussionTopic((int)article.Discourse_Topic_Id);
model.LastCommentDate = topic.LastPostedAt;
model.DiscourseCommentCount = topic.PostsCount;
model.DiscourseTopicSlug = topic.Slug;
}
return model;
}
private static string ExtractAndStripFirstParagram(string articleText)
{
string extracted = ExtractSummary(articleText, 1, false);
return StripHtml(extracted);
}
private static string ExtractSummary(string articleText, int paragraphCount, bool skipRule)
{
string summary = string.Empty;
int index = 0;
//Skip past first HR
if (skipRule)
{
var hrMatch = Regexes.Hr.Match(articleText);
if (hrMatch != null)
index += hrMatch.Index + hrMatch.Length;
}
//Skip past the first paragraph
for (int i = 0; i <= paragraphCount; i++)
{
var pMatch = Regexes.P.Match(articleText.Substring(index));
if (pMatch == null)
break;
index += pMatch.Index;
if (i < paragraphCount)
index += pMatch.Length;
}
summary += (index == 0) ? articleText : articleText.Substring(0, index);
//Close Blockquotes
var quotMatches = Regexes.BlockQuoteStart.Matches(summary);
var quotClosMatches = Regexes.BlockQuoteEnd.Matches(summary);
for (int i = 0; i < quotMatches.Count - quotClosMatches.Count; i++)
summary += "</blockquote>";
return summary;
}
private static string StripHtml(string text)
{
return Regex.Replace(text, @"<(.|\n)*?>", string.Empty);
}
private static class Regexes
{
private static readonly RegexOptions Options = RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant;
public static readonly Regex Hr = new Regex(@"\<hr\s*\/?\s*\>", Regexes.Options);
public static readonly Regex P = new Regex(@"\<(p)[^>]*\>", Regexes.Options);
public static readonly Regex BlockQuoteStart = new Regex(@"\<blockquote[^>]*\>", Regexes.Options);
public static readonly Regex BlockQuoteEnd = new Regex(@"\<\/blockquote[^>]*\>", Regexes.Options);
}
}
}