commit 45357b8
default
·
2024-05-05 09:00:29 +0000 UTC
parent 580299b
New function timeline_to_rss().
M
html.c
+62,
-49
1@@ -2728,55 +2728,7 @@ int html_get_handler(const xs_dict *req, const char *q_path,
2 xs_dict_get(srv_config, "host"));
3 xs *rss_link = xs_fmt("%s.rss", snac.actor);
4
5- xs_html *rss = xs_html_tag("rss",
6- xs_html_attr("version", "0.91"));
7-
8- xs_html *channel = xs_html_tag("channel",
9- xs_html_tag("title",
10- xs_html_text(rss_title)),
11- xs_html_tag("language",
12- xs_html_text("en")),
13- xs_html_tag("link",
14- xs_html_text(rss_link)),
15- xs_html_tag("description",
16- xs_html_text(bio)));
17-
18- xs_html_add(rss, channel);
19-
20- xs_list *p = elems;
21- char *v;
22-
23- while (xs_list_iter(&p, &v)) {
24- xs *msg = NULL;
25-
26- if (!valid_status(timeline_get_by_md5(&snac, v, &msg)))
27- continue;
28-
29- char *id = xs_dict_get(msg, "id");
30- char *content = xs_dict_get(msg, "content");
31-
32- if (!xs_startswith(id, snac.actor))
33- continue;
34-
35- /* create a title with the first line of the content */
36- xs *es_title = xs_replace(content, "<br>", "\n");
37- xs *title = xs_str_new(NULL);
38- int i;
39-
40- for (i = 0; es_title[i] && es_title[i] != '\n' && es_title[i] != '&' && i < 50; i++)
41- title = xs_append_m(title, &es_title[i], 1);
42-
43- xs_html_add(channel,
44- xs_html_tag("item",
45- xs_html_tag("title",
46- xs_html_text(title)),
47- xs_html_tag("link",
48- xs_html_text(id)),
49- xs_html_tag("description",
50- xs_html_text(content))));
51- }
52-
53- *body = xs_html_render_s(rss, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
54+ *body = timeline_to_rss(&snac, elems, rss_title, rss_link, bio);
55 *b_size = strlen(*body);
56 *ctype = "application/rss+xml; charset=utf-8";
57 status = 200;
58@@ -3336,3 +3288,64 @@ int html_post_handler(const xs_dict *req, const char *q_path,
59
60 return status;
61 }
62+
63+
64+xs_str *timeline_to_rss(snac *user, const xs_list *timeline, char *title, char *link, char *desc)
65+/* converts a timeline to rss */
66+{
67+ xs_html *rss = xs_html_tag("rss",
68+ xs_html_attr("version", "0.91"));
69+
70+ xs_html *channel = xs_html_tag("channel",
71+ xs_html_tag("title",
72+ xs_html_text(title)),
73+ xs_html_tag("language",
74+ xs_html_text("en")),
75+ xs_html_tag("link",
76+ xs_html_text(link)),
77+ xs_html_tag("description",
78+ xs_html_text(desc)));
79+
80+ xs_html_add(rss, channel);
81+
82+ int c = 0;
83+ char *v;
84+
85+ while (xs_list_next(timeline, &v, &c)) {
86+ xs *msg = NULL;
87+
88+ if (user) {
89+ if (!valid_status(timeline_get_by_md5(user, v, &msg)))
90+ continue;
91+ }
92+ else {
93+ if (!valid_status(object_get_by_md5(v, &msg)))
94+ continue;
95+ }
96+
97+ char *id = xs_dict_get(msg, "id");
98+ char *content = xs_dict_get(msg, "content");
99+
100+ if (user && !xs_startswith(id, user->actor))
101+ continue;
102+
103+ /* create a title with the first line of the content */
104+ xs *es_title = xs_replace(content, "<br>", "\n");
105+ xs *title = xs_str_new(NULL);
106+ int i;
107+
108+ for (i = 0; es_title[i] && es_title[i] != '\n' && es_title[i] != '&' && i < 50; i++)
109+ title = xs_append_m(title, &es_title[i], 1);
110+
111+ xs_html_add(channel,
112+ xs_html_tag("item",
113+ xs_html_tag("title",
114+ xs_html_text(title)),
115+ xs_html_tag("link",
116+ xs_html_text(id)),
117+ xs_html_tag("description",
118+ xs_html_text(content))));
119+ }
120+
121+ return xs_html_render_s(rss, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
122+}
M
snac.h
+1,
-0
1@@ -324,6 +324,7 @@ 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
7 int snac_init(const char *_basedir);
8 int adduser(const char *uid);