The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RSSController.cs 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.ServiceModel.Syndication;
  5. using System.Web;
  6. using System.Xml.Linq;
  7. using Teknik.Areas.Blog.Models;
  8. using Teknik.Controllers;
  9. using Teknik.Filters;
  10. using Teknik.Utilities;
  11. using Teknik.Models;
  12. using Teknik.Attributes;
  13. using Teknik.Areas.Users.Utility;
  14. using Microsoft.Extensions.Logging;
  15. using Teknik.Configuration;
  16. using Teknik.Data;
  17. using Microsoft.AspNetCore.Authorization;
  18. using Microsoft.AspNetCore.Mvc;
  19. using Microsoft.SyndicationFeed.Rss;
  20. using System.Xml;
  21. using System.Text;
  22. using System.Threading.Tasks;
  23. using Microsoft.SyndicationFeed;
  24. using Teknik.Logging;
  25. namespace Teknik.Areas.RSS.Controllers
  26. {
  27. [TeknikAuthorize(AuthType.Basic)]
  28. [Area("RSS")]
  29. public class RSSController : DefaultController
  30. {
  31. public RSSController(ILogger<Logger> logger, Config config, TeknikEntities dbContext) : base(logger, config, dbContext) { }
  32. [AllowAnonymous]
  33. public async Task Index()
  34. {
  35. Response.ContentType = "application/rss+xml";
  36. using (var xmlWriter = CreateXmlWriter())
  37. {
  38. var feedWriter = new RssFeedWriter(xmlWriter);
  39. await feedWriter.WriteTitle("Teknik RSS");
  40. await feedWriter.WriteDescription("RSS feeds for the Teknik Services.");
  41. await feedWriter.Write(new SyndicationLink(new Uri(Url.SubRouteUrl("help", "Help.RSS"))));
  42. await xmlWriter.FlushAsync();
  43. }
  44. }
  45. [AllowAnonymous]
  46. public async Task Blog(string username)
  47. {
  48. Response.ContentType = "application/rss+xml";
  49. // If empty, grab the main blog
  50. List<BlogPost> posts = new List<BlogPost>();
  51. string blogUrl = Url.SubRouteUrl("blog", "Blog.Blog");
  52. string title = string.Empty;
  53. string description = string.Empty;
  54. bool isSystem = string.IsNullOrEmpty(username);
  55. bool userExists = false;
  56. if (isSystem)
  57. {
  58. posts = _dbContext.BlogPosts.Where(p => (p.System && p.Published)).ToList();
  59. blogUrl = Url.SubRouteUrl("blog", "Blog.Blog");
  60. }
  61. else
  62. {
  63. Blog.Models.Blog blog = _dbContext.Blogs.Where(p => p.User.Username == username && p.BlogId != _config.BlogConfig.ServerBlogId).FirstOrDefault();
  64. posts = _dbContext.BlogPosts.Where(p => (p.BlogId == blog.BlogId && !p.System) && p.Published).ToList();
  65. blogUrl = Url.SubRouteUrl("blog", "Blog.Blog", new { username = username });
  66. }
  67. if (posts.Any())
  68. {
  69. if (isSystem)
  70. {
  71. userExists = true;
  72. title = _config.BlogConfig.Title;
  73. description = _config.BlogConfig.Description;
  74. }
  75. else
  76. {
  77. Users.Models.User user = UserHelper.GetUser(_dbContext, username);
  78. if (user != null)
  79. {
  80. userExists = true;
  81. title = user.BlogSettings.Title;
  82. description = user.BlogSettings.Description;
  83. }
  84. else
  85. {
  86. userExists = false;
  87. title = "No Blog Available";
  88. description = "The specified user does not exist";
  89. }
  90. }
  91. List<SyndicationItem> items = new List<SyndicationItem>();
  92. if (userExists)
  93. {
  94. foreach (BlogPost post in posts.OrderByDescending(p => p.BlogPostId))
  95. {
  96. if (post.Published && post.System == isSystem)
  97. {
  98. SyndicationItem item = new SyndicationItem()
  99. {
  100. Id = post.BlogPostId.ToString(),
  101. Title = post.Title,
  102. Description = MarkdownHelper.Markdown(post.Article).Value,
  103. Published = post.DatePublished
  104. };
  105. item.AddLink(new SyndicationLink(new Uri(Url.SubRouteUrl("blog", "Blog.Post", new { username = post.Blog.User.Username, id = post.BlogPostId }))));
  106. item.AddContributor(new SyndicationPerson(post.Blog.User.Username, UserHelper.GetUserEmailAddress(_config, post.Blog.User.Username)));
  107. items.Add(item);
  108. }
  109. }
  110. }
  111. using (var xmlWriter = CreateXmlWriter())
  112. {
  113. var feedWriter = new RssFeedWriter(xmlWriter);
  114. await feedWriter.WriteTitle(title);
  115. await feedWriter.WriteDescription(description);
  116. await feedWriter.Write(new SyndicationLink(new Uri(blogUrl)));
  117. foreach (SyndicationItem item in items)
  118. {
  119. await feedWriter.Write(item);
  120. }
  121. await xmlWriter.FlushAsync();
  122. }
  123. }
  124. else
  125. {
  126. using (var xmlWriter = CreateXmlWriter())
  127. {
  128. var feedWriter = new RssFeedWriter(xmlWriter);
  129. await feedWriter.WriteTitle("No Blog Available");
  130. await feedWriter.WriteDescription("The specified blog does not exist");
  131. await feedWriter.Write(new SyndicationLink(new Uri(blogUrl)));
  132. await xmlWriter.FlushAsync();
  133. }
  134. }
  135. }
  136. [AllowAnonymous]
  137. public async Task Podcast()
  138. {
  139. Response.ContentType = "application/rss+xml";
  140. List<SyndicationItem> items = new List<SyndicationItem>();
  141. List<Podcast.Models.Podcast> podcasts = _dbContext.Podcasts.Where(p => p.Published).OrderByDescending(p => p.Episode).ToList();
  142. if (podcasts != null)
  143. {
  144. foreach (Podcast.Models.Podcast podcast in podcasts)
  145. {
  146. SyndicationItem item = new SyndicationItem()
  147. {
  148. Id = podcast.Episode.ToString(),
  149. Title = podcast.Title,
  150. Description = MarkdownHelper.Markdown(podcast.Description).Value,
  151. Published = podcast.DatePublished
  152. };
  153. item.AddLink(new SyndicationLink(new Uri(Url.SubRouteUrl("podcast", "Podcast.View", new { episode = podcast.Episode }))));
  154. foreach (Podcast.Models.PodcastFile file in podcast.Files)
  155. {
  156. SyndicationLink enclosure = new SyndicationLink(new Uri(Url.SubRouteUrl("podcast", "Podcast.Download", new { episode = podcast.Episode, fileName = file.FileName })));
  157. item.AddLink(enclosure);
  158. }
  159. items.Add(item);
  160. }
  161. }
  162. using (var xmlWriter = CreateXmlWriter())
  163. {
  164. var feedWriter = new RssFeedWriter(xmlWriter);
  165. await feedWriter.WriteTitle(_config.PodcastConfig.Title);
  166. await feedWriter.WriteDescription(_config.PodcastConfig.Description);
  167. await feedWriter.Write(new SyndicationLink(new Uri(Url.SubRouteUrl("podcast", "Podcast.Index"))));
  168. foreach (SyndicationItem item in items)
  169. {
  170. await feedWriter.Write(item);
  171. }
  172. await xmlWriter.FlushAsync();
  173. }
  174. }
  175. private XmlWriter CreateXmlWriter()
  176. {
  177. return XmlWriter.Create(Response.Body, new XmlWriterSettings()
  178. {
  179. Async = true,
  180. Encoding = Encoding.UTF8
  181. });
  182. }
  183. }
  184. }