summaryrefslogtreecommitdiff
path: root/mastoapi.c
diff options
context:
space:
mode:
authorGravatar default2024-08-09 16:09:45 +0200
committerGravatar default2024-08-09 16:09:45 +0200
commitdb7a68d198178c0232576cd4da6046a0348ae1c9 (patch)
treea9bc47beec03b53b5a35389580cd77bd897aedcb /mastoapi.c
parentVersion 2.57 RELEASED. (diff)
downloadpenes-snac2-db7a68d198178c0232576cd4da6046a0348ae1c9.tar.gz
penes-snac2-db7a68d198178c0232576cd4da6046a0348ae1c9.tar.xz
penes-snac2-db7a68d198178c0232576cd4da6046a0348ae1c9.zip
New function mastoapi_timeline().
Diffstat (limited to 'mastoapi.c')
-rw-r--r--mastoapi.c195
1 files changed, 102 insertions, 93 deletions
diff --git a/mastoapi.c b/mastoapi.c
index c4184b4..7f9ec6c 100644
--- a/mastoapi.c
+++ b/mastoapi.c
@@ -1260,6 +1260,107 @@ void credentials_get(char **body, char **ctype, int *status, snac snac)
1260 *status = HTTP_STATUS_OK; 1260 *status = HTTP_STATUS_OK;
1261} 1261}
1262 1262
1263
1264xs_list *mastoapi_timeline(snac *user, const xs_dict *args, const char *index)
1265{
1266 xs_list *out = xs_list_new();
1267 const char *max_id = xs_dict_get(args, "max_id");
1268 const char *since_id = xs_dict_get(args, "since_id");
1269 const char *min_id = xs_dict_get(args, "min_id");
1270 const char *limit_s = xs_dict_get(args, "limit");
1271 int limit = 0;
1272 int cnt = 0;
1273
1274 if (!xs_is_null(limit_s))
1275 limit = atoi(limit_s);
1276
1277 if (limit == 0)
1278 limit = 20;
1279
1280 xs *timeline = timeline_simple_list(user, index, 0, 2048);
1281
1282 xs_list *p = timeline;
1283 const xs_str *v;
1284
1285 while (xs_list_iter(&p, &v) && cnt < limit) {
1286 xs *msg = NULL;
1287
1288 /* only return entries older that max_id */
1289 if (max_id) {
1290 if (strcmp(v, MID_TO_MD5(max_id)) == 0)
1291 max_id = NULL;
1292
1293 continue;
1294 }
1295
1296 /* only returns entries newer than since_id */
1297 if (since_id) {
1298 if (strcmp(v, MID_TO_MD5(since_id)) == 0)
1299 break;
1300 }
1301
1302 /* only returns entries newer than min_id */
1303 /* what does really "Return results immediately newer than ID" mean? */
1304 if (min_id) {
1305 if (strcmp(v, MID_TO_MD5(min_id)) == 0)
1306 break;
1307 }
1308
1309 /* get the entry */
1310 if (!valid_status(timeline_get_by_md5(user, v, &msg)))
1311 continue;
1312
1313 /* discard non-Notes */
1314 const char *id = xs_dict_get(msg, "id");
1315 const char *type = xs_dict_get(msg, "type");
1316 if (!xs_match(type, POSTLIKE_OBJECT_TYPE))
1317 continue;
1318
1319 const char *from = NULL;
1320 if (strcmp(type, "Page") == 0)
1321 from = xs_dict_get(msg, "audience");
1322
1323 if (from == NULL)
1324 from = get_atto(msg);
1325
1326 if (from == NULL)
1327 continue;
1328
1329 /* is this message from a person we don't follow? */
1330 if (strcmp(from, user->actor) && !following_check(user, from)) {
1331 /* discard if it was not boosted */
1332 xs *idx = object_announces(id);
1333
1334 if (xs_list_len(idx) == 0)
1335 continue;
1336 }
1337
1338 /* discard notes from muted morons */
1339 if (is_muted(user, from))
1340 continue;
1341
1342 /* discard hidden notes */
1343 if (is_hidden(user, id))
1344 continue;
1345
1346 /* if it has a name and it's not a Page or a Video,
1347 it's a poll vote, so discard it */
1348 if (!xs_is_null(xs_dict_get(msg, "name")) && !xs_match(type, "Page|Video"))
1349 continue;
1350
1351 /* convert the Note into a Mastodon status */
1352 xs *st = mastoapi_status(user, msg);
1353
1354 if (st != NULL)
1355 out = xs_list_append(out, st);
1356
1357 cnt++;
1358 }
1359
1360 return out;
1361}
1362
1363
1263int mastoapi_get_handler(const xs_dict *req, const char *q_path, 1364int mastoapi_get_handler(const xs_dict *req, const char *q_path,
1264 char **body, int *b_size, char **ctype) 1365 char **body, int *b_size, char **ctype)
1265{ 1366{
@@ -1518,99 +1619,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
1518 if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/ 1619 if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
1519 /* the private timeline */ 1620 /* the private timeline */
1520 if (logged_in) { 1621 if (logged_in) {
1521 const char *max_id = xs_dict_get(args, "max_id"); 1622 xs *out = mastoapi_timeline(&snac1, args, "private");
1522 const char *since_id = xs_dict_get(args, "since_id");
1523 const char *min_id = xs_dict_get(args, "min_id");
1524 const char *limit_s = xs_dict_get(args, "limit");
1525 int limit = 0;
1526 int cnt = 0;
1527
1528 if (!xs_is_null(limit_s))
1529 limit = atoi(limit_s);
1530
1531 if (limit == 0)
1532 limit = 20;
1533
1534 xs *timeline = timeline_simple_list(&snac1, "private", 0, 2048);
1535
1536 xs *out = xs_list_new();
1537 xs_list *p = timeline;
1538 const xs_str *v;
1539
1540 while (xs_list_iter(&p, &v) && cnt < limit) {
1541 xs *msg = NULL;
1542
1543 /* only return entries older that max_id */
1544 if (max_id) {
1545 if (strcmp(v, MID_TO_MD5(max_id)) == 0)
1546 max_id = NULL;
1547
1548 continue;
1549 }
1550
1551 /* only returns entries newer than since_id */
1552 if (since_id) {
1553 if (strcmp(v, MID_TO_MD5(since_id)) == 0)
1554 break;
1555 }
1556
1557 /* only returns entries newer than min_id */
1558 /* what does really "Return results immediately newer than ID" mean? */
1559 if (min_id) {
1560 if (strcmp(v, MID_TO_MD5(min_id)) == 0)
1561 break;
1562 }
1563
1564 /* get the entry */
1565 if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
1566 continue;
1567
1568 /* discard non-Notes */
1569 const char *id = xs_dict_get(msg, "id");
1570 const char *type = xs_dict_get(msg, "type");
1571 if (!xs_match(type, POSTLIKE_OBJECT_TYPE))
1572 continue;
1573
1574 const char *from = NULL;
1575 if (strcmp(type, "Page") == 0)
1576 from = xs_dict_get(msg, "audience");
1577
1578 if (from == NULL)
1579 from = get_atto(msg);
1580
1581 if (from == NULL)
1582 continue;
1583
1584 /* is this message from a person we don't follow? */
1585 if (strcmp(from, snac1.actor) && !following_check(&snac1, from)) {
1586 /* discard if it was not boosted */
1587 xs *idx = object_announces(id);
1588
1589 if (xs_list_len(idx) == 0)
1590 continue;
1591 }
1592
1593 /* discard notes from muted morons */
1594 if (is_muted(&snac1, from))
1595 continue;
1596
1597 /* discard hidden notes */
1598 if (is_hidden(&snac1, id))
1599 continue;
1600
1601 /* if it has a name and it's not a Page or a Video,
1602 it's a poll vote, so discard it */
1603 if (!xs_is_null(xs_dict_get(msg, "name")) && !xs_match(type, "Page|Video"))
1604 continue;
1605
1606 /* convert the Note into a Mastodon status */
1607 xs *st = mastoapi_status(&snac1, msg);
1608
1609 if (st != NULL)
1610 out = xs_list_append(out, st);
1611
1612 cnt++;
1613 }
1614 1623
1615 *body = xs_json_dumps(out, 4); 1624 *body = xs_json_dumps(out, 4);
1616 *ctype = "application/json"; 1625 *ctype = "application/json";