commit d9b94a5

default  ·  2024-11-10 05:59:55 +0000 UTC
parent 5e37c78
RSS feeds are now 2.0 instead of 0.91.
2 files changed,  +35, -5
M html.c
M snac.h
M html.c
+33, -4
 1@@ -3839,11 +3839,14 @@ int html_post_handler(const xs_dict *req, const char *q_path,
 2 }
 3 
 4 
 5-xs_str *timeline_to_rss(snac *user, const xs_list *timeline, char *title, char *link, char *desc)
 6+xs_str *timeline_to_rss(snac *user, const xs_list *timeline,
 7+                        const char *title, const char *link, const char *desc)
 8 /* converts a timeline to rss */
 9 {
10     xs_html *rss = xs_html_tag("rss",
11-        xs_html_attr("version", "0.91"));
12+        xs_html_attr("xmlns:content", "http:/" "/purl.org/rss/1.0/modules/content/"),
13+        xs_html_attr("version",       "2.0"),
14+        xs_html_attr("xmlns:atom",    "http:/" "/www.w3.org/2005/Atom"));
15 
16     xs_html *channel = xs_html_tag("channel",
17         xs_html_tag("title",
18@@ -3852,15 +3855,21 @@ xs_str *timeline_to_rss(snac *user, const xs_list *timeline, char *title, char *
19             xs_html_text("en")),
20         xs_html_tag("link",
21             xs_html_text(link)),
22+        xs_html_sctag("atom:link",
23+            xs_html_attr("href", link),
24+            xs_html_attr("rel", "self"),
25+            xs_html_attr("type", "application/rss+xml")),
26+        xs_html_tag("generator",
27+            xs_html_text(USER_AGENT)),
28         xs_html_tag("description",
29             xs_html_text(desc)));
30 
31     xs_html_add(rss, channel);
32 
33-    int c = 0;
34+    int cnt = 0;
35     const char *v;
36 
37-    while (xs_list_next(timeline, &v, &c)) {
38+    xs_list_foreach(timeline, v) {
39         xs *msg = NULL;
40 
41         if (user) {
42@@ -3874,10 +3883,14 @@ xs_str *timeline_to_rss(snac *user, const xs_list *timeline, char *title, char *
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 
48         if (user && !xs_startswith(id, user->actor))
49             continue;
50 
51+        if (!id || !content || !published)
52+            continue;
53+
54         /* create a title with the first line of the content */
55         xs *title = xs_replace(content, "<br>", "\n");
56         title = xs_regex_replace_i(title, "<[^>]+>", " ");
57@@ -3893,14 +3906,30 @@ xs_str *timeline_to_rss(snac *user, const xs_list *timeline, char *title, char *
58 
59         title = xs_strip_i(title);
60 
61+        /* convert the date */
62+        time_t t = xs_parse_iso_date(published, 0);
63+        xs *rss_date = xs_str_utctime(t, "%a, %d %b %Y %T +0000");
64+
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+
71         xs_html_add(channel,
72             xs_html_tag("item",
73                 xs_html_tag("title",
74                     xs_html_text(title)),
75                 xs_html_tag("link",
76                     xs_html_text(id)),
77+                xs_html_tag("guid",
78+                    xs_html_text(id)),
79+                xs_html_tag("pubDate",
80+                    xs_html_text(rss_date)),
81                 xs_html_tag("description",
82                     xs_html_text(content))));
83+
84+        cnt++;
85     }
86 
87     return xs_html_render_s(rss, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
M snac.h
+2, -1
 1@@ -355,7 +355,8 @@ int html_get_handler(const xs_dict *req, const char *q_path,
 2 int html_post_handler(const xs_dict *req, const char *q_path,
 3                       char *payload, int p_size,
 4                       char **body, int *b_size, char **ctype);
 5-xs_str *timeline_to_rss(snac *user, const xs_list *timeline, char *title, char *link, char *desc);
 6+xs_str *timeline_to_rss(snac *user, const xs_list *timeline,
 7+                        const char *title, const char *link, const char *desc);
 8 
 9 int write_default_css(void);
10 int snac_init(const char *_basedir);