summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar grunfink2025-04-26 05:24:57 +0000
committerGravatar grunfink2025-04-26 05:24:57 +0000
commitce041ee9bc6b7cad7b8207d5af29d1d6de7720dc (patch)
tree28c99928e7b79c4ee1fba9537fba9107c82530d7
parentMerge pull request 'Russian translations for scheduled posts' (#373) from dan... (diff)
parentmastoapi: fix md5 issues (diff)
downloadpenes-snac2-ce041ee9bc6b7cad7b8207d5af29d1d6de7720dc.tar.gz
penes-snac2-ce041ee9bc6b7cad7b8207d5af29d1d6de7720dc.tar.xz
penes-snac2-ce041ee9bc6b7cad7b8207d5af29d1d6de7720dc.zip
Merge pull request 'mastoapi: added `v1/accounts/.../lists` endpoint (issue #371)' (#372) from dandelions/snac2:pr-lists-mastoapi into master
Reviewed-on: https://codeberg.org/grunfink/snac2/pulls/372
-rw-r--r--data.c3
-rw-r--r--mastoapi.c62
2 files changed, 49 insertions, 16 deletions
diff --git a/data.c b/data.c
index 88f1921..0d1bbc6 100644
--- a/data.c
+++ b/data.c
@@ -2265,7 +2265,8 @@ xs_val *list_maint(snac *user, const char *list, int op)
2265 xs *l2 = xs_split(v2, "/"); 2265 xs *l2 = xs_split(v2, "/");
2266 2266
2267 /* return [ list_id, list_title ] */ 2267 /* return [ list_id, list_title ] */
2268 l = xs_list_append(l, xs_list_append(xs_list_new(), xs_list_get(l2, -1), title)); 2268 xs *tmp_list = xs_list_append(xs_list_new(), xs_list_get(l2, -1), title);
2269 l = xs_list_append(l, tmp_list);
2269 } 2270 }
2270 } 2271 }
2271 } 2272 }
diff --git a/mastoapi.c b/mastoapi.c
index d93afc5..705a902 100644
--- a/mastoapi.c
+++ b/mastoapi.c
@@ -1500,6 +1500,44 @@ xs_str *timeline_link_header(const char *endpoint, xs_list *timeline)
1500} 1500}
1501 1501
1502 1502
1503xs_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 = NULL;
1508 xs *lol = list_maint(user, NULL, 0);
1509
1510 if (uid) {
1511 if (!xs_is_hex(uid))
1512 actor_md5 = xs_md5_hex(uid, strlen(uid));
1513 else
1514 actor_md5 = xs_dup(uid);
1515 }
1516
1517 const xs_list *li;
1518 xs_list_foreach(lol, li) {
1519 const char *list_id = xs_list_get(li, 0);
1520 const char *list_title = xs_list_get(li, 1);
1521 if (uid) {
1522 xs *users = list_content(user, list_id, NULL, 0);
1523 if (xs_list_in(users, actor_md5) == -1)
1524 continue;
1525 }
1526
1527 xs *d = xs_dict_new();
1528
1529 d = xs_dict_append(d, "id", list_id);
1530 d = xs_dict_append(d, "title", list_title);
1531 d = xs_dict_append(d, "replies_policy", "list");
1532 d = xs_dict_append(d, "exclusive", xs_stock(XSTYPE_FALSE));
1533
1534 out = xs_list_append(out, d);
1535 }
1536
1537 return out;
1538}
1539
1540
1503int mastoapi_get_handler(const xs_dict *req, const char *q_path, 1541int mastoapi_get_handler(const xs_dict *req, const char *q_path,
1504 char **body, int *b_size, char **ctype, xs_str **link) 1542 char **body, int *b_size, char **ctype, xs_str **link)
1505{ 1543{
@@ -1723,6 +1761,10 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
1723 if (strcmp(opt, "followers") == 0) { 1761 if (strcmp(opt, "followers") == 0) {
1724 out = xs_list_new(); 1762 out = xs_list_new();
1725 } 1763 }
1764 else
1765 if (strcmp(opt, "lists") == 0) {
1766 out = mastoapi_account_lists(&snac1, uid);
1767 }
1726 1768
1727 user_free(&snac2); 1769 user_free(&snac2);
1728 } 1770 }
@@ -1744,6 +1786,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 */ 1786 /* implement empty response so apps like Tokodon don't show an error */
1745 out = xs_list_new(); 1787 out = xs_list_new();
1746 } 1788 }
1789 else
1790 if (strcmp(opt, "lists") == 0) {
1791 out = mastoapi_account_lists(&snac1, uid);
1792 }
1747 } 1793 }
1748 } 1794 }
1749 1795
@@ -1975,21 +2021,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
1975 else 2021 else
1976 if (strcmp(cmd, "/v1/lists") == 0) { /** list of lists **/ 2022 if (strcmp(cmd, "/v1/lists") == 0) { /** list of lists **/
1977 if (logged_in) { 2023 if (logged_in) {
1978 xs *lol = list_maint(&snac1, NULL, 0); 2024 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 2025
1994 *body = xs_json_dumps(l, 4); 2026 *body = xs_json_dumps(l, 4);
1995 *ctype = "application/json"; 2027 *ctype = "application/json";