commit a6a5190

default  ·  2023-11-20 18:25:53 +0000 UTC
parent 7dd1c8a
The RSS is created using xs_html.
1 files changed,  +36, -41
M html.c
M html.c
+36, -41
  1@@ -2136,31 +2136,33 @@ int html_get_handler(const xs_dict *req, const char *q_path,
  2         if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
  3             return 403;
  4 
  5-        xs_str *rss;
  6         xs *elems = timeline_simple_list(&snac, "public", 0, 20);
  7         xs *bio   = not_really_markdown(xs_dict_get(snac.config, "bio"), NULL);
  8-        char *p, *v;
  9 
 10-        xs *es1 = xs_html_encode(xs_dict_get(snac.config, "name"));
 11-        xs *es2 = xs_html_encode(snac.uid);
 12-        xs *es3 = xs_html_encode(xs_dict_get(srv_config, "host"));
 13-        xs *es4 = xs_html_encode(bio);
 14-        rss = xs_fmt(
 15-            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
 16-            "<rss version=\"0.91\">\n"
 17-            "<channel>\n"
 18-            "<title>%s (@%s@%s)</title>\n"
 19-            "<language>en</language>\n"
 20-            "<link>%s.rss</link>\n"
 21-            "<description>%s</description>\n",
 22-            es1,
 23-            es2,
 24-            es3,
 25-            snac.actor,
 26-            es4
 27-        );
 28+        xs *rss_title = xs_fmt("%s (@%s@%s)",
 29+            xs_dict_get(snac.config, "name"),
 30+            snac.uid,
 31+            xs_dict_get(srv_config, "host"));
 32+        xs *rss_link = xs_fmt("%s.rss", snac.actor);
 33+
 34+        xs_html *rss = xs_html_tag("rss",
 35+            xs_html_attr("version", "0.91"));
 36+
 37+        xs_html *channel = xs_html_tag("channel",
 38+            xs_html_tag("title",
 39+                xs_html_text(rss_title)),
 40+            xs_html_tag("language",
 41+                xs_html_text("en")),
 42+            xs_html_tag("link",
 43+                xs_html_text(rss_link)),
 44+            xs_html_tag("description",
 45+                xs_html_text(bio)));
 46+
 47+        xs_html_add(rss, channel);
 48+
 49+        xs_list *p = elems;
 50+        char *v;
 51 
 52-        p = elems;
 53         while (xs_list_iter(&p, &v)) {
 54             xs *msg  = NULL;
 55 
 56@@ -2168,38 +2170,31 @@ int html_get_handler(const xs_dict *req, const char *q_path,
 57                 continue;
 58 
 59             char *id = xs_dict_get(msg, "id");
 60+            char *content = xs_dict_get(msg, "content");
 61 
 62             if (!xs_startswith(id, snac.actor))
 63                 continue;
 64 
 65-            xs *content = xs_html_encode(xs_dict_get(msg, "content"));
 66-
 67-            // We SHOULD only use sanitized one for description.
 68-            // So, only encode for feed title, while the description just keep it sanitized as is.
 69-            xs *es_title_enc = encode_html(xs_dict_get(msg, "content"));
 70-            xs *es_title = xs_replace(es_title_enc, "<br>", "\n");
 71+            /* create a title with the first line of the content */
 72+            xs *es_title = xs_replace(content, "<br>", "\n");
 73             xs *title   = xs_str_new(NULL);
 74             int i;
 75 
 76             for (i = 0; es_title[i] && es_title[i] != '\n' && es_title[i] != '&' && i < 50; i++)
 77                 title = xs_append_m(title, &es_title[i], 1);
 78 
 79-            xs *s = xs_fmt(
 80-                "<item>\n"
 81-                "<title>%s...</title>\n"
 82-                "<link>%s</link>\n"
 83-                "<description>%s</description>\n"
 84-                "</item>\n",
 85-                title, id, content
 86-            );
 87-
 88-            rss = xs_str_cat(rss, s);
 89+            xs_html_add(channel,
 90+                xs_html_tag("item",
 91+                    xs_html_tag("title",
 92+                        xs_html_text(title)),
 93+                    xs_html_tag("link",
 94+                        xs_html_text(id)),
 95+                    xs_html_tag("description",
 96+                        xs_html_text(content))));
 97         }
 98 
 99-        rss = xs_str_cat(rss, "</channel>\n</rss>\n");
100-
101-        *body   = rss;
102-        *b_size = strlen(rss);
103+        *body   = _xs_html_render(rss, xs_dup("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"));
104+        *b_size = strlen(*body);
105         *ctype  = "application/rss+xml; charset=utf-8";
106         status  = 200;
107