commit db7a68d

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