commit c827b26

default  ·  2022-11-18 10:08:20 +0000 UTC
parent f49deb9
Added rss on output.
1 files changed,  +87, -4
M html.c
M html.c
+87, -4
  1@@ -948,27 +948,47 @@ d_char *html_people(snac *snac)
  2 
  3 int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype)
  4 {
  5+    char *accept = xs_dict_get(req, "accept");
  6     int status = 404;
  7     snac snac;
  8-    char *uid, *p_path;
  9+    xs *uid = NULL;
 10+    char *p_path;
 11     int cache = 1;
 12     char *v;
 13 
 14     xs *l = xs_split_n(q_path, "/", 2);
 15+    v = xs_list_get(l, 1);
 16+
 17+    if (xs_is_null(v)) {
 18+        srv_log(xs_fmt("html_get_handler bad query '%s'", q_path));
 19+        return 404;
 20+    }
 21+
 22+    uid = xs_dup(v);
 23+
 24+    /* rss? */
 25+    if (xs_endswith(uid, ".rss")) {
 26+        uid = xs_crop(uid, 0, -4);
 27+        p_path = ".rss";
 28+    }
 29+    else
 30+        p_path = xs_list_get(l, 2);
 31 
 32-    uid = xs_list_get(l, 1);
 33     if (!uid || !user_open(&snac, uid)) {
 34         /* invalid user */
 35         srv_log(xs_fmt("html_get_handler bad user %s", uid));
 36         return 404;
 37     }
 38 
 39+    /* return the RSS if requested by Accept header */
 40+    if (xs_str_in(accept, "text/xml") != -1 ||
 41+        xs_str_in(accept, "application/rss+xml") != -1)
 42+        p_path = ".rss";
 43+
 44     /* check if server config variable 'disable_cache' is set */
 45     if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
 46         cache = 0;
 47 
 48-    p_path = xs_list_get(l, 2);
 49-
 50     if (p_path == NULL) {
 51         /* public timeline */
 52         xs *h = xs_str_localtime(0, "%Y-%m.html");
 53@@ -1068,6 +1088,69 @@ int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char *
 54             status  = 200;
 55         }
 56     }
 57+    else
 58+    if (strcmp(p_path, ".rss") == 0) {
 59+        /* public timeline in RSS format */
 60+        d_char *rss;
 61+        xs *elems = local_list(&snac, 20);
 62+        char *p, *v;
 63+
 64+        rss = xs_fmt(
 65+            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
 66+            "<rss version=\"0.91\">\n"
 67+            "<channel>\n"
 68+            "<title>%s</title>\n"
 69+            "<language>en</language>\n"
 70+            "<link>%s.rss</link>\n"
 71+            "<description>%s</description>\n",
 72+            snac.actor,
 73+            snac.actor,
 74+            snac.actor
 75+        );
 76+
 77+        p = elems;
 78+        while (xs_list_iter(&p, &v)) {
 79+            xs *msg  = timeline_get(&snac, v);
 80+            char *id = xs_dict_get(msg, "id");
 81+
 82+            if (!xs_startswith(id, snac.actor))
 83+                continue;
 84+
 85+            xs *content = sanitize(xs_dict_get(msg, "content"));
 86+            xs *title   = xs_dup(content);
 87+
 88+            /* escape tags */
 89+            content = xs_replace_i(content, "<", "&lt;");
 90+            content = xs_replace_i(content, ">", "&gt;");
 91+
 92+            /* cut title in the first tag start */
 93+            if ((v = strchr(title, '<')) != NULL)
 94+                *v = '\0';
 95+
 96+            if (strlen(title) > 40) {
 97+                title = xs_crop(title, 0, 40);
 98+                title = xs_str_cat(title, "...");
 99+            }
100+
101+            xs *s = xs_fmt(
102+                "<item>\n"
103+                "<title>%s</title>\n"
104+                "<link>%s</link>\n"
105+                "<description>%s</description>\n"
106+                "</item>\n",
107+                *title ? title : "...", id, content
108+            );
109+
110+            rss = xs_str_cat(rss, s);
111+        }
112+
113+        rss = xs_str_cat(rss, "</channel>\n</rss>\n");
114+
115+        *body   = rss;
116+        *b_size = strlen(rss);
117+        *ctype  = "application/rss+xml; charset=utf-8";
118+        status  = 200;
119+    }
120     else
121         status = 404;
122