commit 668f69d
default
·
2023-04-12 16:44:15 +0000 UTC
parent a2cab2b
Added the mastodon personal timeline.
1 files changed,
+60,
-21
+60,
-21
1@@ -643,23 +643,23 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
2 xs_dict *args = xs_dict_get(req, "q_vars");
3 xs *cmd = xs_replace(q_path, "/api/v1", "");
4
5- snac snac = {0};
6- int logged_in = process_auth_token(&snac, req);;
7+ snac snac1 = {0};
8+ int logged_in = process_auth_token(&snac1, req);
9
10 if (strcmp(cmd, "/accounts/verify_credentials") == 0) {
11 if (logged_in) {
12 xs *acct = xs_dict_new();
13
14- acct = xs_dict_append(acct, "id", xs_dict_get(snac.config, "uid"));
15- acct = xs_dict_append(acct, "username", xs_dict_get(snac.config, "uid"));
16- acct = xs_dict_append(acct, "acct", xs_dict_get(snac.config, "uid"));
17- acct = xs_dict_append(acct, "display_name", xs_dict_get(snac.config, "name"));
18- acct = xs_dict_append(acct, "created_at", xs_dict_get(snac.config, "published"));
19- acct = xs_dict_append(acct, "note", xs_dict_get(snac.config, "bio"));
20- acct = xs_dict_append(acct, "url", snac.actor);
21+ acct = xs_dict_append(acct, "id", xs_dict_get(snac1.config, "uid"));
22+ acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
23+ acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
24+ acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
25+ acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
26+ acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
27+ acct = xs_dict_append(acct, "url", snac1.actor);
28
29 xs *avatar = NULL;
30- char *av = xs_dict_get(snac.config, "avatar");
31+ char *av = xs_dict_get(snac1.config, "avatar");
32
33 if (xs_is_null(av) || *av == '\0')
34 avatar = xs_fmt("%s/susie.png", srv_baseurl);
35@@ -677,6 +677,45 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
36 }
37 }
38 else
39+ if (xs_startswith(cmd, "/accounts/") && xs_endswith(cmd, "/statuses")) {
40+ /* the public list of posts of a user */
41+ xs *l = xs_split(cmd, "/");
42+
43+ if (xs_list_len(l) == 4) {
44+ snac snac2;
45+ const char *uid = xs_list_get(l, 2);
46+
47+ if (user_open(&snac2, uid)) {
48+ xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
49+ xs *out = xs_list_new();
50+ xs_list *p = timeline;
51+ xs_str *v;
52+
53+ while (xs_list_iter(&p, &v)) {
54+ xs *msg = NULL;
55+
56+ if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
57+ /* add only posts by the author */
58+ if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
59+ xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
60+ xs *st = mastoapi_status(&snac2, msg);
61+
62+ out = xs_list_append(out, st);
63+ }
64+ }
65+ }
66+
67+ *body = xs_json_dumps_pp(out, 4);
68+ *ctype = "application/json";
69+ status = 200;
70+
71+ user_free(&snac2);
72+ }
73+ else
74+ srv_debug(0, xs_fmt("mastoapi account statuses: bad user '%s'", uid));
75+ }
76+ }
77+ else
78 if (strcmp(cmd, "/timelines/home") == 0) {
79 /* the private timeline */
80 if (logged_in) {
81@@ -693,7 +732,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
82 if (limit == 0)
83 limit = 20;
84
85- xs *timeline = timeline_simple_list(&snac, "private", 0, XS_ALL);
86+ xs *timeline = timeline_simple_list(&snac1, "private", 0, XS_ALL);
87
88 xs *out = xs_list_new();
89 xs_list *p = timeline;
90@@ -724,7 +763,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
91 }
92
93 /* get the entry */
94- if (!valid_status(timeline_get_by_md5(&snac, v, &msg)))
95+ if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
96 continue;
97
98 /* discard non-Notes */
99@@ -732,7 +771,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
100 continue;
101
102 /* convert the Note into a Mastodon status */
103- xs *st = mastoapi_status(&snac, msg);
104+ xs *st = mastoapi_status(&snac1, msg);
105
106 if (st != NULL)
107 out = xs_list_append(out, st);
108@@ -879,10 +918,10 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
109 /* skip the 'fake' part of the id */
110 id = MID_TO_MD5(id);
111
112- if (valid_status(timeline_get_by_md5(&snac, id, &msg))) {
113+ if (valid_status(timeline_get_by_md5(&snac1, id, &msg))) {
114 if (op == NULL) {
115 /* return the status itself */
116- out = mastoapi_status(&snac, msg);
117+ out = mastoapi_status(&snac1, msg);
118 }
119 else
120 if (strcmp(op, "context") == 0) {
121@@ -899,8 +938,8 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
122 while (object_parent(pid, pid, sizeof(pid))) {
123 xs *m2 = NULL;
124
125- if (valid_status(timeline_get_by_md5(&snac, pid, &m2))) {
126- xs *st = mastoapi_status(&snac, m2);
127+ if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
128+ xs *st = mastoapi_status(&snac1, m2);
129 anc = xs_list_append(anc, st);
130 }
131 else
132@@ -914,8 +953,8 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
133 while (xs_list_iter(&p, &v)) {
134 xs *m2 = NULL;
135
136- if (valid_status(timeline_get_by_md5(&snac, v, &m2))) {
137- xs *st = mastoapi_status(&snac, m2);
138+ if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
139+ xs *st = mastoapi_status(&snac1, m2);
140 des = xs_list_append(des, st);
141 }
142 }
143@@ -944,7 +983,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
144 xs *actor2 = NULL;
145
146 if (valid_status(object_get_by_md5(v, &actor2))) {
147- xs *acct2 = mastoapi_account(&snac, actor2);
148+ xs *acct2 = mastoapi_account(&snac1, actor2);
149
150 out = xs_list_append(out, acct2);
151 }
152@@ -964,7 +1003,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
153
154 /* user cleanup */
155 if (logged_in)
156- user_free(&snac);
157+ user_free(&snac1);
158
159 return status;
160 }