commit 339dc1a
default
·
2024-08-09 14:57:14 +0000 UTC
parent f5332ec
Use index_desc_first() / index_desc_next() in mastoapi_timeline().
1 files changed,
+75,
-62
+75,
-62
1@@ -1261,9 +1261,20 @@ void credentials_get(char **body, char **ctype, int *status, snac snac)
2 }
3
4
5-xs_list *mastoapi_timeline(snac *user, const xs_dict *args, const char *index)
6+xs_list *mastoapi_timeline(snac *user, const xs_dict *args, int skip, const char *index_fn)
7 {
8- xs_list *out = xs_list_new();
9+ xs_list *out = xs_list_new();
10+ FILE *f;
11+ char md5[MD5_HEX_SIZE];
12+
13+ if (dbglevel) {
14+ xs *js = xs_json_dumps(args, 0);
15+ srv_log(xs_fmt("mastoapi_timeline args: %s", js));
16+ }
17+
18+ if ((f = fopen(index_fn, "r")) == NULL)
19+ return out;
20+
21 const char *max_id = xs_dict_get(args, "max_id");
22 const char *since_id = xs_dict_get(args, "since_id");
23 const char *min_id = xs_dict_get(args, "min_id");
24@@ -1277,85 +1288,86 @@ xs_list *mastoapi_timeline(snac *user, const xs_dict *args, const char *index)
25 if (limit == 0)
26 limit = 20;
27
28- xs *timeline = timeline_simple_list(user, index, 0, 2048);
29+ if (index_desc_first(f, md5, skip)) {
30+ do {
31+ xs *msg = NULL;
32
33- xs_list *p = timeline;
34- const xs_str *v;
35+ /* only return entries older that max_id */
36+ if (max_id) {
37+ if (strcmp(md5, MID_TO_MD5(max_id)) == 0)
38+ max_id = NULL;
39
40- while (xs_list_iter(&p, &v) && cnt < limit) {
41- xs *msg = NULL;
42+ continue;
43+ }
44
45- /* only return entries older that max_id */
46- if (max_id) {
47- if (strcmp(v, MID_TO_MD5(max_id)) == 0)
48- max_id = NULL;
49+ /* only returns entries newer than since_id */
50+ if (since_id) {
51+ if (strcmp(md5, MID_TO_MD5(since_id)) == 0)
52+ break;
53+ }
54
55- continue;
56- }
57+ /* only returns entries newer than min_id */
58+ /* what does really "Return results immediately newer than ID" mean? */
59+ if (min_id) {
60+ if (strcmp(md5, MID_TO_MD5(min_id)) == 0)
61+ break;
62+ }
63
64- /* only returns entries newer than since_id */
65- if (since_id) {
66- if (strcmp(v, MID_TO_MD5(since_id)) == 0)
67- break;
68- }
69+ /* get the entry */
70+ if (!valid_status(timeline_get_by_md5(user, md5, &msg)))
71+ continue;
72
73- /* only returns entries newer than min_id */
74- /* what does really "Return results immediately newer than ID" mean? */
75- if (min_id) {
76- if (strcmp(v, MID_TO_MD5(min_id)) == 0)
77- break;
78- }
79+ /* discard non-Notes */
80+ const char *id = xs_dict_get(msg, "id");
81+ const char *type = xs_dict_get(msg, "type");
82+ if (!xs_match(type, POSTLIKE_OBJECT_TYPE))
83+ continue;
84
85- /* get the entry */
86- if (!valid_status(timeline_get_by_md5(user, v, &msg)))
87- continue;
88+ const char *from = NULL;
89+ if (strcmp(type, "Page") == 0)
90+ from = xs_dict_get(msg, "audience");
91
92- /* discard non-Notes */
93- const char *id = xs_dict_get(msg, "id");
94- const char *type = xs_dict_get(msg, "type");
95- if (!xs_match(type, POSTLIKE_OBJECT_TYPE))
96- continue;
97+ if (from == NULL)
98+ from = get_atto(msg);
99
100- const char *from = NULL;
101- if (strcmp(type, "Page") == 0)
102- from = xs_dict_get(msg, "audience");
103+ if (from == NULL)
104+ continue;
105
106- if (from == NULL)
107- from = get_atto(msg);
108+ /* is this message from a person we don't follow? */
109+ if (strcmp(from, user->actor) && !following_check(user, from)) {
110+ /* discard if it was not boosted */
111+ xs *idx = object_announces(id);
112
113- if (from == NULL)
114- continue;
115+ if (xs_list_len(idx) == 0)
116+ continue;
117+ }
118
119- /* is this message from a person we don't follow? */
120- if (strcmp(from, user->actor) && !following_check(user, from)) {
121- /* discard if it was not boosted */
122- xs *idx = object_announces(id);
123+ /* discard notes from muted morons */
124+ if (is_muted(user, from))
125+ continue;
126
127- if (xs_list_len(idx) == 0)
128+ /* discard hidden notes */
129+ if (is_hidden(user, id))
130 continue;
131- }
132
133- /* discard notes from muted morons */
134- if (is_muted(user, from))
135- continue;
136+ /* if it has a name and it's not a Page or a Video,
137+ it's a poll vote, so discard it */
138+ if (!xs_is_null(xs_dict_get(msg, "name")) && !xs_match(type, "Page|Video"))
139+ continue;
140
141- /* discard hidden notes */
142- if (is_hidden(user, id))
143- continue;
144+ /* convert the Note into a Mastodon status */
145+ xs *st = mastoapi_status(user, msg);
146
147- /* if it has a name and it's not a Page or a Video,
148- it's a poll vote, so discard it */
149- if (!xs_is_null(xs_dict_get(msg, "name")) && !xs_match(type, "Page|Video"))
150- continue;
151+ if (st != NULL)
152+ out = xs_list_append(out, st);
153
154- /* convert the Note into a Mastodon status */
155- xs *st = mastoapi_status(user, msg);
156+ cnt++;
157+ } while (cnt < limit && index_desc_next(f, md5));
158+ }
159
160- if (st != NULL)
161- out = xs_list_append(out, st);
162+ fclose(f);
163
164- cnt++;
165- }
166+ srv_debug(1, xs_fmt("mastoapi_timeline: %d %d", cnt, xs_list_len(out)));
167
168 return out;
169 }
170@@ -1619,7 +1631,8 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
171 if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
172 /* the private timeline */
173 if (logged_in) {
174- xs *out = mastoapi_timeline(&snac1, args, "private");
175+ xs *ifn = user_index_fn(&snac1, "private");
176+ xs *out = mastoapi_timeline(&snac1, args, 0, ifn);
177
178 *body = xs_json_dumps(out, 4);
179 *ctype = "application/json";