The next generation of the Teknik Services. Written in ASP.NET. Fork for blog tags.
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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using MarkdownDeep;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.ServiceModel.Syndication;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using System.Xml.Linq;
  9. using Teknik.Areas.Blog.Models;
  10. using Teknik.Controllers;
  11. using Teknik.Filters;
  12. using Teknik.Utilities;
  13. using Teknik.Models;
  14. using Teknik.Attributes;
  15. using Teknik.Areas.Users.Utility;
  16. namespace Teknik.Areas.RSS.Controllers
  17. {
  18. [TeknikAuthorize(AuthType.Basic)]
  19. public class RSSController : DefaultController
  20. {
  21. [AllowAnonymous]
  22. public ActionResult Index()
  23. {
  24. SyndicationFeed feed = new SyndicationFeed("Teknik RSS", "RSS feeds for the Teknik Services.", new Uri(Url.SubRouteUrl("help", "Help.RSS")));
  25. return new RssResult(feed);
  26. }
  27. [TrackDownload]
  28. [AllowAnonymous]
  29. public ActionResult Blog(string username)
  30. {
  31. using (TeknikEntities db = new TeknikEntities())
  32. {
  33. // If empty, grab the main blog
  34. List<BlogPost> posts = new List<BlogPost>();
  35. string blogUrl = Url.SubRouteUrl("blog", "Blog.Blog");
  36. string title = string.Empty;
  37. string description = string.Empty;
  38. bool isSystem = string.IsNullOrEmpty(username);
  39. if (isSystem)
  40. {
  41. posts = db.BlogPosts.Where(p => (p.System && p.Published)).ToList();
  42. blogUrl = Url.SubRouteUrl("blog", "Blog.Blog");
  43. }
  44. else
  45. {
  46. Blog.Models.Blog blog = db.Blogs.Where(p => p.User.Username == username && p.BlogId != Config.BlogConfig.ServerBlogId).FirstOrDefault();
  47. posts = db.BlogPosts.Where(p => (p.BlogId == blog.BlogId && !p.System) && p.Published).ToList();
  48. blogUrl = Url.SubRouteUrl("blog", "Blog.Blog", new { username = username });
  49. }
  50. if (posts.Any())
  51. {
  52. if (isSystem)
  53. {
  54. title = Config.BlogConfig.Title;
  55. description = Config.BlogConfig.Description;
  56. }
  57. else
  58. {
  59. Users.Models.User user = UserHelper.GetUser(db, username);
  60. if (user != null)
  61. {
  62. title = user.BlogSettings.Title;
  63. description = user.BlogSettings.Description;
  64. }
  65. else
  66. {
  67. SyndicationFeed badUserFeed = new SyndicationFeed("No Blog Available", "The specified user does not exist", new Uri(blogUrl));
  68. return new RssResult(badUserFeed);
  69. }
  70. }
  71. List<SyndicationItem> items = new List<SyndicationItem>();
  72. foreach (BlogPost post in posts.OrderByDescending(p => p.BlogPostId))
  73. {
  74. if (post.Published && post.System == isSystem)
  75. {
  76. items.Add(new SyndicationItem(
  77. post.Title,
  78. MarkdownHelper.Markdown(post.Article).ToHtmlString(),
  79. new Uri(Url.SubRouteUrl("blog", "Blog.Post", new { username = post.Blog.User.Username, id = post.BlogPostId })),
  80. post.BlogPostId.ToString(),
  81. post.DateEdited
  82. ));
  83. }
  84. }
  85. SyndicationFeed feed = new SyndicationFeed(title, description, new Uri(blogUrl), items);
  86. return new RssResult(feed);
  87. }
  88. SyndicationFeed badFeed = new SyndicationFeed("No Blog Available", "The specified blog does not exist", new Uri(blogUrl));
  89. return new RssResult(badFeed);
  90. }
  91. }
  92. [TrackDownload]
  93. [AllowAnonymous]
  94. public ActionResult Podcast()
  95. {
  96. using (TeknikEntities db = new TeknikEntities())
  97. {
  98. List<SyndicationItem> items = new List<SyndicationItem>();
  99. List<Podcast.Models.Podcast> podcasts = db.Podcasts.Where(p => p.Published).OrderByDescending(p => p.Episode).ToList();
  100. if (podcasts != null)
  101. {
  102. foreach (Podcast.Models.Podcast podcast in podcasts)
  103. {
  104. SyndicationItem item = new SyndicationItem(
  105. podcast.Title,
  106. MarkdownHelper.Markdown(podcast.Description).ToHtmlString(),
  107. new Uri(Url.SubRouteUrl("podcast", "Podcast.View", new { episode = podcast.Episode })),
  108. podcast.Episode.ToString(),
  109. podcast.DateEdited
  110. );
  111. foreach (Podcast.Models.PodcastFile file in podcast.Files)
  112. {
  113. SyndicationLink enclosure = SyndicationLink.CreateMediaEnclosureLink(new Uri(Url.SubRouteUrl("podcast", "Podcast.Download", new { episode = podcast.Episode, fileName = file.FileName })), file.ContentType, file.ContentLength);
  114. item.Links.Add(enclosure);
  115. }
  116. items.Add(item);
  117. }
  118. }
  119. SyndicationFeed feed = new SyndicationFeed(Config.PodcastConfig.Title, Config.PodcastConfig.Description, new Uri(Url.SubRouteUrl("podcast", "Podcast.Index")), items);
  120. return new RssResult(feed);
  121. }
  122. }
  123. }
  124. }