PageRenderTime 23ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/src/EyeCatch.Umbraco.FeedGenerator/FeedModel.cs

https://bitbucket.org/azzlack/eyecatch.umbraco.rssfeedgenerator
C# | 236 lines | 175 code | 30 blank | 31 comment | 18 complexity | 3b9c9d2fcf61ca06baaca28b47605d4c MD5 | raw file
  1. namespace EyeCatch.Umbraco.FeedGenerator
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Web;
  7. using global::Umbraco.Core.Models;
  8. using global::Umbraco.Web;
  9. using global::Umbraco.Web.Models;
  10. public class FeedModel
  11. {
  12. /// <summary>The source content.</summary>
  13. private IPublishedContent source;
  14. /// <summary>The site base url.</summary>
  15. private string baseUrl;
  16. /// <summary>The feed items.</summary>
  17. private List<FeedItem> items;
  18. public FeedModel(PartialViewMacroModel model)
  19. {
  20. var source = model.MacroParameters["Source"] ?? string.Empty;
  21. var baseUrl = model.MacroParameters["BaseUrl"] ?? string.Empty;
  22. var documentTypeAlias = model.MacroParameters["DocumentTypeAlias"] ?? string.Empty;
  23. var keywordProperty = model.MacroParameters["KeywordProperty"] ?? string.Empty;
  24. var summaryTextProperty = model.MacroParameters["SummaryTextProperty"] ?? string.Empty;
  25. var fullTextProperty = model.MacroParameters["FullTextProperty"] ?? string.Empty;
  26. int maxLevel = 0;
  27. if (model.MacroParameters["MaxLevel"] != null)
  28. {
  29. int.TryParse(model.MacroParameters["MaxLevel"].ToString(), out maxLevel);
  30. }
  31. this.Initialize(
  32. model.Content,
  33. source.ToString(),
  34. baseUrl.ToString(),
  35. documentTypeAlias.ToString().Trim('[', ']').Split(',').Where(x => !string.IsNullOrEmpty(x)).ToArray(),
  36. keywordProperty.ToString().Trim('[', ']'),
  37. summaryTextProperty.ToString().Trim('[', ']'),
  38. fullTextProperty.ToString().Trim('[', ']'),
  39. maxLevel);
  40. }
  41. /// <summary>Gets the title.</summary>
  42. /// <value>The title.</value>
  43. public string Title
  44. {
  45. get
  46. {
  47. return this.source.GetPropertyValue<string>(
  48. "rssTitle",
  49. new Uri(this.source.UrlWithDomain()).Host + " RSS Feed");
  50. }
  51. }
  52. /// <summary>Gets URL of the site.</summary>
  53. /// <value>The site URL.</value>
  54. public string SiteUrl
  55. {
  56. get
  57. {
  58. return this.baseUrl;
  59. }
  60. }
  61. /// <summary>Gets URL of the feed.</summary>
  62. /// <value>The feed URL.</value>
  63. public string SelfUrl
  64. {
  65. get
  66. {
  67. try
  68. {
  69. return HttpContext.Current.Request.Url.AbsoluteUri;
  70. }
  71. catch (Exception)
  72. {
  73. return null;
  74. }
  75. }
  76. }
  77. /// <summary>Gets the publication date.</summary>
  78. /// <value>The publication date.</value>
  79. public DateTime Updated
  80. {
  81. get
  82. {
  83. if (!this.Items.Any())
  84. {
  85. return this.source.UpdateDate.ToUniversalTime();
  86. }
  87. return this.Items.Max(x => x.UpdateDate);
  88. }
  89. }
  90. /// <summary>Gets the feed description.</summary>
  91. /// <value>The feed description.</value>
  92. public string Description
  93. {
  94. get
  95. {
  96. return this.source.GetPropertyValue<string>("rssDescription", true, string.Empty);
  97. }
  98. }
  99. /// <summary>Gets the feed language.</summary>
  100. /// <value>The feed language.</value>
  101. public string Language
  102. {
  103. get
  104. {
  105. var l = this.source.GetPropertyValue<string>("rssLanguage", true, string.Empty);
  106. if (string.IsNullOrEmpty(l))
  107. {
  108. return this.source.GetCulture().Name;
  109. }
  110. return l;
  111. }
  112. }
  113. /// <summary>Gets the feed items.</summary>
  114. /// <value>The feed items.</value>
  115. public List<FeedItem> Items
  116. {
  117. get
  118. {
  119. return this.items ?? (this.items = new List<FeedItem>());
  120. }
  121. }
  122. /// <summary>Gets the Umbraco helper for the current request.</summary>
  123. private UmbracoHelper Umbraco
  124. {
  125. get
  126. {
  127. return new UmbracoHelper(UmbracoContext.Current);
  128. }
  129. }
  130. /// <summary>Initializes this instance.</summary>
  131. /// <param name="content">The current content.</param>
  132. /// <param name="source">The source content.</param>
  133. /// <param name="baseUrl">The base url.</param>
  134. /// <param name="documentTypeAliases">The document type aliases.</param>
  135. /// <param name="keywordProperty">The keyword property.</param>
  136. /// <param name="summaryTextProperty">The summary text property.</param>
  137. /// <param name="fullTextProperty">The full text property.</param>
  138. /// <param name="maxDepth">The maximum number of levels below the source to traverse.</param>
  139. internal void Initialize(IPublishedContent content, string source, string baseUrl, string[] documentTypeAliases, string keywordProperty, string summaryTextProperty, string fullTextProperty, int maxDepth)
  140. {
  141. // Populate Source
  142. if (source != null && !string.IsNullOrEmpty(source))
  143. {
  144. // Check if this is a media item first
  145. var media = this.Umbraco.TypedMedia(source);
  146. this.source = media ?? this.Umbraco.TypedContent(source);
  147. }
  148. else
  149. {
  150. this.source = content;
  151. }
  152. // Populate Base Url
  153. if (baseUrl != null && !string.IsNullOrEmpty(baseUrl))
  154. {
  155. this.baseUrl = baseUrl;
  156. }
  157. else
  158. {
  159. var u = new Uri(content.UrlWithDomain());
  160. this.baseUrl = string.Format("{0}://{1}", u.Scheme, u.Host);
  161. }
  162. // Populate Items
  163. var descendants = this.source.Descendants();
  164. if (maxDepth > 0)
  165. {
  166. descendants = descendants.Where(x => x.Level <= (this.source.Level + maxDepth));
  167. }
  168. if (documentTypeAliases.Any())
  169. {
  170. descendants = descendants.Where(x => documentTypeAliases.Contains(x.DocumentTypeAlias));
  171. }
  172. foreach (var descendant in descendants)
  173. {
  174. var item = new FeedItem()
  175. {
  176. Title = descendant.Name,
  177. PublishDate = descendant.CreateDate.ToUniversalTime(),
  178. UpdateDate = descendant.UpdateDate.ToUniversalTime(),
  179. Url = descendant.UrlWithDomain(),
  180. Author = descendant.CreatorName
  181. };
  182. if (!string.IsNullOrEmpty(keywordProperty) && descendant.HasValue(keywordProperty))
  183. {
  184. item.Tags = descendant.GetPropertyValue<string>(keywordProperty).Split(',');
  185. }
  186. else
  187. {
  188. item.Tags = new string[0];
  189. }
  190. if (!string.IsNullOrEmpty(summaryTextProperty) && descendant.HasValue(summaryTextProperty))
  191. {
  192. var s = descendant.GetPropertyValue<string>(summaryTextProperty);
  193. var processed = new HtmlDocumentProcessor().EncodeUris(descendant, s, baseUrl);
  194. item.Summary = processed.DocumentNode.InnerHtml;
  195. }
  196. if (!string.IsNullOrEmpty(fullTextProperty) && descendant.HasValue(fullTextProperty))
  197. {
  198. var c = descendant.GetPropertyValue<string>(fullTextProperty);
  199. var processed = new HtmlDocumentProcessor().EncodeUris(descendant, c, baseUrl);
  200. item.Content = processed.DocumentNode.InnerHtml;
  201. }
  202. this.Items.Add(item);
  203. }
  204. }
  205. }
  206. }