commit 1cfa00e
default
·
2023-04-10 10:14:16 +0000 UTC
parent 862dafd
New function mastoapi_id(), that creates mostly-compatible Ids. Most apps (all?) sort entries by status Id, so they must be time-sorted (somewhat).
1 files changed,
+50,
-18
+50,
-18
1@@ -366,6 +366,30 @@ int oauth_post_handler(const xs_dict *req, const char *q_path,
2 }
3
4
5+xs_str *mastoapi_id(const xs_dict *msg)
6+/* returns a somewhat Mastodon-compatible status id */
7+{
8+ char tmp[256] = "";
9+ int n = 0;
10+ const char *id = xs_dict_get(msg, "id");
11+ const char *published = xs_dict_get(msg, "published");
12+
13+ if (!xs_is_null(published)) {
14+ /* transfer all numbers from the published date */
15+ while (*published && n < sizeof(tmp) - 1) {
16+ if (*published >= '0' && *published <= '9')
17+ tmp[n++] = *published;
18+ published++;
19+ }
20+ tmp[n] = '\0';
21+ }
22+
23+ xs *md5 = xs_md5_hex(id, strlen(id));
24+
25+ return xs_str_cat(xs_str_new(tmp), md5);
26+}
27+
28+
29 int mastoapi_get_handler(const xs_dict *req, const char *q_path,
30 char **body, int *b_size, char **ctype)
31 {
32@@ -441,7 +465,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
33 if (logged_in) {
34 const char *max_id = xs_dict_get(args, "max_id");
35 const char *since_id = xs_dict_get(args, "since_id");
36-// const char *min_id = xs_dict_get(args, "min_id");
37+ const char *min_id = xs_dict_get(args, "min_id");
38 const char *limit_s = xs_dict_get(args, "limit");
39 int limit = 0;
40 int cnt = 0;
41@@ -475,6 +499,13 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
42 break;
43 }
44
45+ /* only returns entries newer than min_id */
46+ /* what does really "Return results immediately newer than ID" mean? */
47+ if (min_id) {
48+ if (strcmp(v, min_id) == 0)
49+ break;
50+ }
51+
52 /* get the entry */
53 if (!valid_status(timeline_get_by_md5(&snac, v, &msg)))
54 continue;
55@@ -531,10 +562,11 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
56
57 char *tmp;
58 id = xs_dict_get(msg, "id");
59+ xs *mid = mastoapi_id(msg);
60
61 xs *st = xs_dict_new();
62
63- st = xs_dict_append(st, "id", v);
64+ st = xs_dict_append(st, "id", mid);
65 st = xs_dict_append(st, "uri", id);
66 st = xs_dict_append(st, "url", id);
67 st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
68@@ -586,24 +618,24 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
69
70 st = xs_dict_append(st, "replies_count", ixc);
71
72- tmp = xs_dict_get(msg, "inReplyTo");
73- if (xs_is_null(tmp)) {
74- st = xs_dict_append(st, "in_reply_to_id", n);
75- st = xs_dict_append(st, "in_reply_to_account_id", n);
76- }
77- else {
78- xs *irt_md5 = xs_md5_hex(tmp, strlen(tmp));
79- st = xs_dict_append(st, "in_reply_to_id", irt_md5);
80+ /* default in_reply_to values */
81+ st = xs_dict_append(st, "in_reply_to_id", n);
82+ st = xs_dict_append(st, "in_reply_to_account_id", n);
83
84+ tmp = xs_dict_get(msg, "inReplyTo");
85+ if (!xs_is_null(tmp)) {
86 xs *irto = NULL;
87- char *at = NULL;
88- if (valid_status(object_get(tmp, &irto)) &&
89- !xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
90- xs *at_md5 = xs_md5_hex(at, strlen(at));
91- st = xs_dict_append(st, "in_reply_to_account_id", at_md5);
92+
93+ if (valid_status(object_get(tmp, &irto))) {
94+ xs *irt_mid = mastoapi_id(irto);
95+ st = xs_dict_set(st, "in_reply_to_id", irt_mid);
96+
97+ char *at = NULL;
98+ if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
99+ xs *at_md5 = xs_md5_hex(at, strlen(at));
100+ st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
101+ }
102 }
103- else
104- st = xs_dict_append(st, "in_reply_to_account_id", n);
105 }
106
107 st = xs_dict_append(st, "reblog", n);
108@@ -632,7 +664,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
109 *ctype = "application/json";
110 status = 200;
111
112-// printf("%s\n", *body);
113+ srv_debug(0, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
114 }
115 else {
116 status = 401; // unauthorized