rss.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2025 grunfink et al. / MIT license */
  3. #include "xs.h"
  4. #include "xs_html.h"
  5. #include "xs_regex.h"
  6. #include "xs_time.h"
  7. #include "snac.h"
  8. xs_str *rss_from_timeline(snac *user, const xs_list *timeline,
  9. const char *title, const char *link, const char *desc)
  10. /* converts a timeline to rss */
  11. {
  12. xs_html *rss = xs_html_tag("rss",
  13. xs_html_attr("xmlns:content", "http:/" "/purl.org/rss/1.0/modules/content/"),
  14. xs_html_attr("version", "2.0"),
  15. xs_html_attr("xmlns:atom", "http:/" "/www.w3.org/2005/Atom"));
  16. xs_html *channel = xs_html_tag("channel",
  17. xs_html_tag("title",
  18. xs_html_text(title)),
  19. xs_html_tag("language",
  20. xs_html_text("en")),
  21. xs_html_tag("link",
  22. xs_html_text(link)),
  23. xs_html_sctag("atom:link",
  24. xs_html_attr("href", link),
  25. xs_html_attr("rel", "self"),
  26. xs_html_attr("type", "application/rss+xml")),
  27. xs_html_tag("generator",
  28. xs_html_text(USER_AGENT)),
  29. xs_html_tag("description",
  30. xs_html_text(desc)));
  31. xs_html_add(rss, channel);
  32. int cnt = 0;
  33. const char *v;
  34. xs_list_foreach(timeline, v) {
  35. xs *msg = NULL;
  36. if (user) {
  37. if (!valid_status(timeline_get_by_md5(user, v, &msg)))
  38. continue;
  39. }
  40. else {
  41. if (!valid_status(object_get_by_md5(v, &msg)))
  42. continue;
  43. }
  44. const char *id = xs_dict_get(msg, "id");
  45. const char *content = xs_dict_get(msg, "content");
  46. const char *published = xs_dict_get(msg, "published");
  47. if (user && !xs_startswith(id, user->actor))
  48. continue;
  49. if (!id || !content || !published)
  50. continue;
  51. /* create a title with the first line of the content */
  52. xs *title = xs_replace(content, "<br>", "\n");
  53. title = xs_regex_replace_i(title, "<[^>]+>", " ");
  54. title = xs_regex_replace_i(title, "&[^;]+;", " ");
  55. int i;
  56. for (i = 0; title[i] && title[i] != '\n' && i < 50; i++);
  57. if (title[i] != '\0') {
  58. title[i] = '\0';
  59. title = xs_str_cat(title, "...");
  60. }
  61. title = xs_strip_i(title);
  62. /* convert the date */
  63. time_t t = xs_parse_iso_date(published, 0);
  64. xs *rss_date = xs_str_utctime(t, "%a, %d %b %Y %T +0000");
  65. /* if it's the first one, add it to the header */
  66. if (cnt == 0)
  67. xs_html_add(channel,
  68. xs_html_tag("lastBuildDate",
  69. xs_html_text(rss_date)));
  70. xs_html_add(channel,
  71. xs_html_tag("item",
  72. xs_html_tag("title",
  73. xs_html_text(title)),
  74. xs_html_tag("link",
  75. xs_html_text(id)),
  76. xs_html_tag("guid",
  77. xs_html_text(id)),
  78. xs_html_tag("pubDate",
  79. xs_html_text(rss_date)),
  80. xs_html_tag("description",
  81. xs_html_text(content))));
  82. cnt++;
  83. }
  84. return xs_html_render_s(rss, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  85. }