commit 8d4d702

default  ·  2023-04-23 07:41:43 +0000 UTC
parent 82cd5fd
Added an instance timeline.
1 files changed,  +55, -2
+55, -2
 1@@ -912,8 +912,61 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
 2     else
 3     if (strcmp(cmd, "/v1/timelines/public") == 0) {
 4         /* the public timeline (public timelines for all users) */
 5-        /* TBD */
 6-        *body  = xs_dup("[]");
 7+
 8+        /* this is a kludge: first users in the list get all the fame */
 9+
10+        const char *limit_s  = xs_dict_get(args, "limit");
11+        int limit = 0;
12+        int cnt   = 0;
13+
14+        if (!xs_is_null(limit_s))
15+            limit = atoi(limit_s);
16+
17+        if (limit == 0)
18+            limit = 20;
19+
20+        xs *out    = xs_list_new();
21+        xs *users  = user_list();
22+        xs_list *p = users;
23+        xs_str *uid;
24+
25+        while (xs_list_iter(&p, &uid) && cnt < limit) {
26+            snac user;
27+
28+            if (user_open(&user, uid)) {
29+                xs *timeline = timeline_simple_list(&snac1, "public", 0, 4);
30+                xs_list *p2  = timeline;
31+                xs_str *v;
32+
33+                while (xs_list_iter(&p2, &v) && cnt < limit) {
34+                    xs *msg = NULL;
35+
36+                    /* get the entry */
37+                    if (!valid_status(timeline_get_by_md5(&user, v, &msg)))
38+                        continue;
39+
40+                    /* discard non-Notes */
41+                    if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
42+                        continue;
43+
44+                    /* discard entries not by this user */
45+                    if (!xs_startswith(xs_dict_get(msg, "id"), user.actor))
46+                        continue;
47+
48+                    /* convert the Note into a Mastodon status */
49+                    xs *st = mastoapi_status(&snac1, msg);
50+
51+                    if (st != NULL) {
52+                        out = xs_list_append(out, st);
53+                        cnt++;
54+                    }
55+                }
56+
57+                user_free(&user);
58+            }
59+        }
60+
61+        *body  = xs_json_dumps_pp(out, 4);
62         *ctype = "application/json";
63         status = 200;
64     }