diff options
| author | 2025-04-24 15:49:55 +0200 | |
|---|---|---|
| committer | 2025-04-26 02:37:13 +0200 | |
| commit | ec21e1596ab285813d5774224dfed77cbb30d23e (patch) | |
| tree | 811b98992389a4110614d491ec46840bbfe33bc5 /mastoapi.c | |
| parent | Updated TODO. (diff) | |
| download | snac2-ec21e1596ab285813d5774224dfed77cbb30d23e.tar.gz snac2-ec21e1596ab285813d5774224dfed77cbb30d23e.tar.xz snac2-ec21e1596ab285813d5774224dfed77cbb30d23e.zip | |
mastoapi: support lists for users
Diffstat (limited to 'mastoapi.c')
| -rw-r--r-- | mastoapi.c | 55 |
1 files changed, 40 insertions, 15 deletions
| @@ -1500,6 +1500,37 @@ xs_str *timeline_link_header(const char *endpoint, xs_list *timeline) | |||
| 1500 | } | 1500 | } |
| 1501 | 1501 | ||
| 1502 | 1502 | ||
| 1503 | xs_list *mastoapi_account_lists(snac *user, const char *uid) | ||
| 1504 | /* returns the list of list an user is in */ | ||
| 1505 | { | ||
| 1506 | xs_list *out = xs_list_new(); | ||
| 1507 | xs *actor_md5 = uid ? xs_md5_hex(uid, strlen(uid)) : NULL; | ||
| 1508 | xs *lol = list_maint(user, NULL, 0); | ||
| 1509 | |||
| 1510 | const xs_list *li; | ||
| 1511 | xs_list_foreach(lol, li) { | ||
| 1512 | const char *list_id = xs_list_get(li, 0); | ||
| 1513 | const char *list_title = xs_list_get(li, 1); | ||
| 1514 | if (uid) { | ||
| 1515 | xs *users = list_content(user, list_id, NULL, 0); | ||
| 1516 | if (xs_list_in(users, actor_md5) == -1) | ||
| 1517 | continue; | ||
| 1518 | } | ||
| 1519 | |||
| 1520 | xs *d = xs_dict_new(); | ||
| 1521 | |||
| 1522 | d = xs_dict_append(d, "id", list_id); | ||
| 1523 | d = xs_dict_append(d, "title", list_title); | ||
| 1524 | d = xs_dict_append(d, "replies_policy", "list"); | ||
| 1525 | d = xs_dict_append(d, "exclusive", xs_stock(XSTYPE_FALSE)); | ||
| 1526 | |||
| 1527 | out = xs_list_append(out, d); | ||
| 1528 | } | ||
| 1529 | |||
| 1530 | return out; | ||
| 1531 | } | ||
| 1532 | |||
| 1533 | |||
| 1503 | int mastoapi_get_handler(const xs_dict *req, const char *q_path, | 1534 | int mastoapi_get_handler(const xs_dict *req, const char *q_path, |
| 1504 | char **body, int *b_size, char **ctype, xs_str **link) | 1535 | char **body, int *b_size, char **ctype, xs_str **link) |
| 1505 | { | 1536 | { |
| @@ -1723,6 +1754,10 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 1723 | if (strcmp(opt, "followers") == 0) { | 1754 | if (strcmp(opt, "followers") == 0) { |
| 1724 | out = xs_list_new(); | 1755 | out = xs_list_new(); |
| 1725 | } | 1756 | } |
| 1757 | else | ||
| 1758 | if (strcmp(opt, "lists") == 0) { | ||
| 1759 | out = mastoapi_account_lists(&snac1, uid); | ||
| 1760 | } | ||
| 1726 | 1761 | ||
| 1727 | user_free(&snac2); | 1762 | user_free(&snac2); |
| 1728 | } | 1763 | } |
| @@ -1744,6 +1779,10 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 1744 | /* implement empty response so apps like Tokodon don't show an error */ | 1779 | /* implement empty response so apps like Tokodon don't show an error */ |
| 1745 | out = xs_list_new(); | 1780 | out = xs_list_new(); |
| 1746 | } | 1781 | } |
| 1782 | else | ||
| 1783 | if (strcmp(opt, "lists") == 0) { | ||
| 1784 | out = mastoapi_account_lists(&snac1, uid); | ||
| 1785 | } | ||
| 1747 | } | 1786 | } |
| 1748 | } | 1787 | } |
| 1749 | 1788 | ||
| @@ -1975,21 +2014,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 1975 | else | 2014 | else |
| 1976 | if (strcmp(cmd, "/v1/lists") == 0) { /** list of lists **/ | 2015 | if (strcmp(cmd, "/v1/lists") == 0) { /** list of lists **/ |
| 1977 | if (logged_in) { | 2016 | if (logged_in) { |
| 1978 | xs *lol = list_maint(&snac1, NULL, 0); | 2017 | xs *l = mastoapi_account_lists(&snac1, NULL); |
| 1979 | xs *l = xs_list_new(); | ||
| 1980 | int c = 0; | ||
| 1981 | const xs_list *li; | ||
| 1982 | |||
| 1983 | while (xs_list_next(lol, &li, &c)) { | ||
| 1984 | xs *d = xs_dict_new(); | ||
| 1985 | |||
| 1986 | d = xs_dict_append(d, "id", xs_list_get(li, 0)); | ||
| 1987 | d = xs_dict_append(d, "title", xs_list_get(li, 1)); | ||
| 1988 | d = xs_dict_append(d, "replies_policy", "list"); | ||
| 1989 | d = xs_dict_append(d, "exclusive", xs_stock(XSTYPE_FALSE)); | ||
| 1990 | |||
| 1991 | l = xs_list_append(l, d); | ||
| 1992 | } | ||
| 1993 | 2018 | ||
| 1994 | *body = xs_json_dumps(l, 4); | 2019 | *body = xs_json_dumps(l, 4); |
| 1995 | *ctype = "application/json"; | 2020 | *ctype = "application/json"; |