diff options
| author | 2024-08-09 16:57:14 +0200 | |
|---|---|---|
| committer | 2024-08-09 16:57:14 +0200 | |
| commit | 339dc1a3255f39087476fa511e94d98016b76338 (patch) | |
| tree | 579b64294af9d701b7c6e4acc9f6127311245762 /mastoapi.c | |
| parent | Added some functions that return index filenames. (diff) | |
| download | snac2-339dc1a3255f39087476fa511e94d98016b76338.tar.gz snac2-339dc1a3255f39087476fa511e94d98016b76338.tar.xz snac2-339dc1a3255f39087476fa511e94d98016b76338.zip | |
Use index_desc_first() / index_desc_next() in mastoapi_timeline().
Diffstat (limited to 'mastoapi.c')
| -rw-r--r-- | mastoapi.c | 137 |
1 files changed, 75 insertions, 62 deletions
| @@ -1261,9 +1261,20 @@ void credentials_get(char **body, char **ctype, int *status, snac snac) | |||
| 1261 | } | 1261 | } |
| 1262 | 1262 | ||
| 1263 | 1263 | ||
| 1264 | xs_list *mastoapi_timeline(snac *user, const xs_dict *args, const char *index) | 1264 | xs_list *mastoapi_timeline(snac *user, const xs_dict *args, int skip, const char *index_fn) |
| 1265 | { | 1265 | { |
| 1266 | xs_list *out = xs_list_new(); | 1266 | xs_list *out = xs_list_new(); |
| 1267 | FILE *f; | ||
| 1268 | char md5[MD5_HEX_SIZE]; | ||
| 1269 | |||
| 1270 | if (dbglevel) { | ||
| 1271 | xs *js = xs_json_dumps(args, 0); | ||
| 1272 | srv_log(xs_fmt("mastoapi_timeline args: %s", js)); | ||
| 1273 | } | ||
| 1274 | |||
| 1275 | if ((f = fopen(index_fn, "r")) == NULL) | ||
| 1276 | return out; | ||
| 1277 | |||
| 1267 | const char *max_id = xs_dict_get(args, "max_id"); | 1278 | const char *max_id = xs_dict_get(args, "max_id"); |
| 1268 | const char *since_id = xs_dict_get(args, "since_id"); | 1279 | const char *since_id = xs_dict_get(args, "since_id"); |
| 1269 | const char *min_id = xs_dict_get(args, "min_id"); | 1280 | const char *min_id = xs_dict_get(args, "min_id"); |
| @@ -1277,85 +1288,86 @@ xs_list *mastoapi_timeline(snac *user, const xs_dict *args, const char *index) | |||
| 1277 | if (limit == 0) | 1288 | if (limit == 0) |
| 1278 | limit = 20; | 1289 | limit = 20; |
| 1279 | 1290 | ||
| 1280 | xs *timeline = timeline_simple_list(user, index, 0, 2048); | 1291 | if (index_desc_first(f, md5, skip)) { |
| 1292 | do { | ||
| 1293 | xs *msg = NULL; | ||
| 1281 | 1294 | ||
| 1282 | xs_list *p = timeline; | 1295 | /* only return entries older that max_id */ |
| 1283 | const xs_str *v; | 1296 | if (max_id) { |
| 1297 | if (strcmp(md5, MID_TO_MD5(max_id)) == 0) | ||
| 1298 | max_id = NULL; | ||
| 1284 | 1299 | ||
| 1285 | while (xs_list_iter(&p, &v) && cnt < limit) { | 1300 | continue; |
| 1286 | xs *msg = NULL; | 1301 | } |
| 1287 | 1302 | ||
| 1288 | /* only return entries older that max_id */ | 1303 | /* only returns entries newer than since_id */ |
| 1289 | if (max_id) { | 1304 | if (since_id) { |
| 1290 | if (strcmp(v, MID_TO_MD5(max_id)) == 0) | 1305 | if (strcmp(md5, MID_TO_MD5(since_id)) == 0) |
| 1291 | max_id = NULL; | 1306 | break; |
| 1307 | } | ||
| 1292 | 1308 | ||
| 1293 | continue; | 1309 | /* only returns entries newer than min_id */ |
| 1294 | } | 1310 | /* what does really "Return results immediately newer than ID" mean? */ |
| 1311 | if (min_id) { | ||
| 1312 | if (strcmp(md5, MID_TO_MD5(min_id)) == 0) | ||
| 1313 | break; | ||
| 1314 | } | ||
| 1295 | 1315 | ||
| 1296 | /* only returns entries newer than since_id */ | 1316 | /* get the entry */ |
| 1297 | if (since_id) { | 1317 | if (!valid_status(timeline_get_by_md5(user, md5, &msg))) |
| 1298 | if (strcmp(v, MID_TO_MD5(since_id)) == 0) | 1318 | continue; |
| 1299 | break; | ||
| 1300 | } | ||
| 1301 | 1319 | ||
| 1302 | /* only returns entries newer than min_id */ | 1320 | /* discard non-Notes */ |
| 1303 | /* what does really "Return results immediately newer than ID" mean? */ | 1321 | const char *id = xs_dict_get(msg, "id"); |
| 1304 | if (min_id) { | 1322 | const char *type = xs_dict_get(msg, "type"); |
| 1305 | if (strcmp(v, MID_TO_MD5(min_id)) == 0) | 1323 | if (!xs_match(type, POSTLIKE_OBJECT_TYPE)) |
| 1306 | break; | 1324 | continue; |
| 1307 | } | ||
| 1308 | 1325 | ||
| 1309 | /* get the entry */ | 1326 | const char *from = NULL; |
| 1310 | if (!valid_status(timeline_get_by_md5(user, v, &msg))) | 1327 | if (strcmp(type, "Page") == 0) |
| 1311 | continue; | 1328 | from = xs_dict_get(msg, "audience"); |
| 1312 | 1329 | ||
| 1313 | /* discard non-Notes */ | 1330 | if (from == NULL) |
| 1314 | const char *id = xs_dict_get(msg, "id"); | 1331 | from = get_atto(msg); |
| 1315 | const char *type = xs_dict_get(msg, "type"); | ||
| 1316 | if (!xs_match(type, POSTLIKE_OBJECT_TYPE)) | ||
| 1317 | continue; | ||
| 1318 | 1332 | ||
| 1319 | const char *from = NULL; | 1333 | if (from == NULL) |
| 1320 | if (strcmp(type, "Page") == 0) | 1334 | continue; |
| 1321 | from = xs_dict_get(msg, "audience"); | ||
| 1322 | 1335 | ||
| 1323 | if (from == NULL) | 1336 | /* is this message from a person we don't follow? */ |
| 1324 | from = get_atto(msg); | 1337 | if (strcmp(from, user->actor) && !following_check(user, from)) { |
| 1338 | /* discard if it was not boosted */ | ||
| 1339 | xs *idx = object_announces(id); | ||
| 1325 | 1340 | ||
| 1326 | if (from == NULL) | 1341 | if (xs_list_len(idx) == 0) |
| 1327 | continue; | 1342 | continue; |
| 1343 | } | ||
| 1328 | 1344 | ||
| 1329 | /* is this message from a person we don't follow? */ | 1345 | /* discard notes from muted morons */ |
| 1330 | if (strcmp(from, user->actor) && !following_check(user, from)) { | 1346 | if (is_muted(user, from)) |
| 1331 | /* discard if it was not boosted */ | 1347 | continue; |
| 1332 | xs *idx = object_announces(id); | ||
| 1333 | 1348 | ||
| 1334 | if (xs_list_len(idx) == 0) | 1349 | /* discard hidden notes */ |
| 1350 | if (is_hidden(user, id)) | ||
| 1335 | continue; | 1351 | continue; |
| 1336 | } | ||
| 1337 | 1352 | ||
| 1338 | /* discard notes from muted morons */ | 1353 | /* if it has a name and it's not a Page or a Video, |
| 1339 | if (is_muted(user, from)) | 1354 | it's a poll vote, so discard it */ |
| 1340 | continue; | 1355 | if (!xs_is_null(xs_dict_get(msg, "name")) && !xs_match(type, "Page|Video")) |
| 1356 | continue; | ||
| 1341 | 1357 | ||
| 1342 | /* discard hidden notes */ | 1358 | /* convert the Note into a Mastodon status */ |
| 1343 | if (is_hidden(user, id)) | 1359 | xs *st = mastoapi_status(user, msg); |
| 1344 | continue; | ||
| 1345 | 1360 | ||
| 1346 | /* if it has a name and it's not a Page or a Video, | 1361 | if (st != NULL) |
| 1347 | it's a poll vote, so discard it */ | 1362 | out = xs_list_append(out, st); |
| 1348 | if (!xs_is_null(xs_dict_get(msg, "name")) && !xs_match(type, "Page|Video")) | ||
| 1349 | continue; | ||
| 1350 | 1363 | ||
| 1351 | /* convert the Note into a Mastodon status */ | 1364 | cnt++; |
| 1352 | xs *st = mastoapi_status(user, msg); | 1365 | } while (cnt < limit && index_desc_next(f, md5)); |
| 1366 | } | ||
| 1353 | 1367 | ||
| 1354 | if (st != NULL) | 1368 | fclose(f); |
| 1355 | out = xs_list_append(out, st); | ||
| 1356 | 1369 | ||
| 1357 | cnt++; | 1370 | srv_debug(1, xs_fmt("mastoapi_timeline: %d %d", cnt, xs_list_len(out))); |
| 1358 | } | ||
| 1359 | 1371 | ||
| 1360 | return out; | 1372 | return out; |
| 1361 | } | 1373 | } |
| @@ -1619,7 +1631,8 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 1619 | if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/ | 1631 | if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/ |
| 1620 | /* the private timeline */ | 1632 | /* the private timeline */ |
| 1621 | if (logged_in) { | 1633 | if (logged_in) { |
| 1622 | xs *out = mastoapi_timeline(&snac1, args, "private"); | 1634 | xs *ifn = user_index_fn(&snac1, "private"); |
| 1635 | xs *out = mastoapi_timeline(&snac1, args, 0, ifn); | ||
| 1623 | 1636 | ||
| 1624 | *body = xs_json_dumps(out, 4); | 1637 | *body = xs_json_dumps(out, 4); |
| 1625 | *ctype = "application/json"; | 1638 | *ctype = "application/json"; |