diff options
| author | 2024-11-12 06:34:52 +0100 | |
|---|---|---|
| committer | 2024-11-12 06:35:00 +0100 | |
| commit | 50fa2ae0e620b813e2eab3b6a89efdf171d7856e (patch) | |
| tree | bb9bd9d55a575bbd725fd9d83603ec31e4adf6bf /mastoapi.c | |
| parent | mastoapi: added some proxy code. (diff) | |
| download | snac2-50fa2ae0e620b813e2eab3b6a89efdf171d7856e.tar.gz snac2-50fa2ae0e620b813e2eab3b6a89efdf171d7856e.tar.xz snac2-50fa2ae0e620b813e2eab3b6a89efdf171d7856e.zip | |
mastoapi: a lot of proxy code.
Diffstat (limited to 'mastoapi.c')
| -rw-r--r-- | mastoapi.c | 47 |
1 files changed, 28 insertions, 19 deletions
| @@ -517,15 +517,19 @@ xs_str *mastoapi_id(const xs_dict *msg) | |||
| 517 | #define MID_TO_MD5(id) (id + 10) | 517 | #define MID_TO_MD5(id) (id + 10) |
| 518 | 518 | ||
| 519 | 519 | ||
| 520 | xs_dict *mastoapi_account(const xs_dict *actor) | 520 | xs_dict *mastoapi_account(snac *logged, const xs_dict *actor) |
| 521 | /* converts an ActivityPub actor to a Mastodon account */ | 521 | /* converts an ActivityPub actor to a Mastodon account */ |
| 522 | { | 522 | { |
| 523 | const char *id = xs_dict_get(actor, "id"); | 523 | const char *id = xs_dict_get(actor, "id"); |
| 524 | const char *pub = xs_dict_get(actor, "published"); | 524 | const char *pub = xs_dict_get(actor, "published"); |
| 525 | const char *proxy = NULL; | ||
| 525 | 526 | ||
| 526 | if (xs_type(id) != XSTYPE_STRING) | 527 | if (xs_type(id) != XSTYPE_STRING) |
| 527 | return NULL; | 528 | return NULL; |
| 528 | 529 | ||
| 530 | if (logged && xs_is_true(xs_dict_get(srv_config, "proxy_media"))) | ||
| 531 | proxy = logged->actor; | ||
| 532 | |||
| 529 | const char *prefu = xs_dict_get(actor, "preferredUsername"); | 533 | const char *prefu = xs_dict_get(actor, "preferredUsername"); |
| 530 | 534 | ||
| 531 | const char *display_name = xs_dict_get(actor, "name"); | 535 | const char *display_name = xs_dict_get(actor, "name"); |
| @@ -583,7 +587,7 @@ xs_dict *mastoapi_account(const xs_dict *actor) | |||
| 583 | const char *url = xs_dict_get(av, "url"); | 587 | const char *url = xs_dict_get(av, "url"); |
| 584 | 588 | ||
| 585 | if (url != NULL) | 589 | if (url != NULL) |
| 586 | avatar = xs_dup(url); | 590 | avatar = make_url(url, proxy, 1); |
| 587 | } | 591 | } |
| 588 | 592 | ||
| 589 | if (avatar == NULL) | 593 | if (avatar == NULL) |
| @@ -596,7 +600,7 @@ xs_dict *mastoapi_account(const xs_dict *actor) | |||
| 596 | const xs_dict *hd = xs_dict_get(actor, "image"); | 600 | const xs_dict *hd = xs_dict_get(actor, "image"); |
| 597 | 601 | ||
| 598 | if (xs_type(hd) == XSTYPE_DICT) | 602 | if (xs_type(hd) == XSTYPE_DICT) |
| 599 | header = xs_dup(xs_dict_get(hd, "url")); | 603 | header = make_url(xs_dict_get(hd, "url"), proxy, 1); |
| 600 | 604 | ||
| 601 | if (xs_is_null(header)) | 605 | if (xs_is_null(header)) |
| 602 | header = xs_str_new(NULL); | 606 | header = xs_str_new(NULL); |
| @@ -619,9 +623,10 @@ xs_dict *mastoapi_account(const xs_dict *actor) | |||
| 619 | const xs_dict *icon = xs_dict_get(v, "icon"); | 623 | const xs_dict *icon = xs_dict_get(v, "icon"); |
| 620 | 624 | ||
| 621 | if (!xs_is_null(name) && !xs_is_null(icon)) { | 625 | if (!xs_is_null(name) && !xs_is_null(icon)) { |
| 622 | const char *url = xs_dict_get(icon, "url"); | 626 | const char *o_url = xs_dict_get(icon, "url"); |
| 623 | 627 | ||
| 624 | if (!xs_is_null(url)) { | 628 | if (!xs_is_null(o_url)) { |
| 629 | xs *url = make_url(o_url, proxy, 1); | ||
| 625 | xs *nm = xs_strip_chars_i(xs_dup(name), ":"); | 630 | xs *nm = xs_strip_chars_i(xs_dup(name), ":"); |
| 626 | xs *d1 = xs_dict_new(); | 631 | xs *d1 = xs_dict_new(); |
| 627 | 632 | ||
| @@ -790,11 +795,15 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) | |||
| 790 | { | 795 | { |
| 791 | xs *actor = NULL; | 796 | xs *actor = NULL; |
| 792 | actor_get_refresh(snac, get_atto(msg), &actor); | 797 | actor_get_refresh(snac, get_atto(msg), &actor); |
| 798 | const char *proxy = NULL; | ||
| 793 | 799 | ||
| 794 | /* if the author is not here, discard */ | 800 | /* if the author is not here, discard */ |
| 795 | if (actor == NULL) | 801 | if (actor == NULL) |
| 796 | return NULL; | 802 | return NULL; |
| 797 | 803 | ||
| 804 | if (snac && xs_is_true(xs_dict_get(srv_config, "proxy_media"))) | ||
| 805 | proxy = snac->actor; | ||
| 806 | |||
| 798 | const char *type = xs_dict_get(msg, "type"); | 807 | const char *type = xs_dict_get(msg, "type"); |
| 799 | const char *id = xs_dict_get(msg, "id"); | 808 | const char *id = xs_dict_get(msg, "id"); |
| 800 | 809 | ||
| @@ -802,7 +811,7 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) | |||
| 802 | if (xs_is_null(type) || xs_is_null(id)) | 811 | if (xs_is_null(type) || xs_is_null(id)) |
| 803 | return NULL; | 812 | return NULL; |
| 804 | 813 | ||
| 805 | xs *acct = mastoapi_account(actor); | 814 | xs *acct = mastoapi_account(snac, actor); |
| 806 | if (acct == NULL) | 815 | if (acct == NULL) |
| 807 | return NULL; | 816 | return NULL; |
| 808 | 817 | ||
| @@ -871,7 +880,7 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) | |||
| 871 | 880 | ||
| 872 | if (xs_match(type, "image/*|video/*|Image|Video")) { /* */ | 881 | if (xs_match(type, "image/*|video/*|Image|Video")) { /* */ |
| 873 | xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt)); | 882 | xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt)); |
| 874 | xs *href = make_url(o_href, snac->actor, 1); | 883 | xs *href = make_url(o_href, proxy, 1); |
| 875 | 884 | ||
| 876 | xs *d = xs_dict_new(); | 885 | xs *d = xs_dict_new(); |
| 877 | 886 | ||
| @@ -1075,7 +1084,7 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) | |||
| 1075 | xs *b_actor = NULL; | 1084 | xs *b_actor = NULL; |
| 1076 | 1085 | ||
| 1077 | if (valid_status(object_get_by_md5(boosted_by_md5, &b_actor))) { | 1086 | if (valid_status(object_get_by_md5(boosted_by_md5, &b_actor))) { |
| 1078 | xs *b_acct = mastoapi_account(b_actor); | 1087 | xs *b_acct = mastoapi_account(snac, b_actor); |
| 1079 | xs *fake_uri = NULL; | 1088 | xs *fake_uri = NULL; |
| 1080 | 1089 | ||
| 1081 | if (snac) | 1090 | if (snac) |
| @@ -1463,7 +1472,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 1463 | 1472 | ||
| 1464 | if (user_open(&user, uid)) { | 1473 | if (user_open(&user, uid)) { |
| 1465 | xs *actor = msg_actor(&user); | 1474 | xs *actor = msg_actor(&user); |
| 1466 | xs *macct = mastoapi_account(actor); | 1475 | xs *macct = mastoapi_account(NULL, actor); |
| 1467 | 1476 | ||
| 1468 | *body = xs_json_dumps(macct, 4); | 1477 | *body = xs_json_dumps(macct, 4); |
| 1469 | *ctype = "application/json"; | 1478 | *ctype = "application/json"; |
| @@ -1521,7 +1530,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 1521 | xs *luname = xs_tolower_i(xs_dup(uname)); | 1530 | xs *luname = xs_tolower_i(xs_dup(uname)); |
| 1522 | 1531 | ||
| 1523 | if (xs_startswith(luname, q)) { | 1532 | if (xs_startswith(luname, q)) { |
| 1524 | xs *acct = mastoapi_account(actor); | 1533 | xs *acct = mastoapi_account(&snac1, actor); |
| 1525 | 1534 | ||
| 1526 | out = xs_list_append(out, acct); | 1535 | out = xs_list_append(out, acct); |
| 1527 | } | 1536 | } |
| @@ -1548,7 +1557,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 1548 | /* if it's not already seen, add it */ | 1557 | /* if it's not already seen, add it */ |
| 1549 | if (xs_set_add(&seen, user.actor) == 1) { | 1558 | if (xs_set_add(&seen, user.actor) == 1) { |
| 1550 | xs *actor = msg_actor(&user); | 1559 | xs *actor = msg_actor(&user); |
| 1551 | xs *acct = mastoapi_account(actor); | 1560 | xs *acct = mastoapi_account(&snac1, actor); |
| 1552 | 1561 | ||
| 1553 | out = xs_list_append(out, acct); | 1562 | out = xs_list_append(out, acct); |
| 1554 | } | 1563 | } |
| @@ -1566,7 +1575,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 1566 | if (opt == NULL) { | 1575 | if (opt == NULL) { |
| 1567 | /* account information */ | 1576 | /* account information */ |
| 1568 | actor = msg_actor(&snac2); | 1577 | actor = msg_actor(&snac2); |
| 1569 | out = mastoapi_account(actor); | 1578 | out = mastoapi_account(NULL, actor); |
| 1570 | } | 1579 | } |
| 1571 | else | 1580 | else |
| 1572 | if (strcmp(opt, "statuses") == 0) { /** **/ | 1581 | if (strcmp(opt, "statuses") == 0) { /** **/ |
| @@ -1609,7 +1618,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 1609 | xs *actor = NULL; | 1618 | xs *actor = NULL; |
| 1610 | 1619 | ||
| 1611 | if (valid_status(object_get(v, &actor))) { | 1620 | if (valid_status(object_get(v, &actor))) { |
| 1612 | xs *acct = mastoapi_account(actor); | 1621 | xs *acct = mastoapi_account(NULL, actor); |
| 1613 | out = xs_list_append(out, acct); | 1622 | out = xs_list_append(out, acct); |
| 1614 | } | 1623 | } |
| 1615 | } | 1624 | } |
| @@ -1626,7 +1635,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 1626 | if (logged_in && valid_status(object_get_by_md5(uid, &actor))) { | 1635 | if (logged_in && valid_status(object_get_by_md5(uid, &actor))) { |
| 1627 | if (opt == NULL) { | 1636 | if (opt == NULL) { |
| 1628 | /* account information */ | 1637 | /* account information */ |
| 1629 | out = mastoapi_account(actor); | 1638 | out = mastoapi_account(&snac1, actor); |
| 1630 | } | 1639 | } |
| 1631 | else | 1640 | else |
| 1632 | if (strcmp(opt, "statuses") == 0) { | 1641 | if (strcmp(opt, "statuses") == 0) { |
| @@ -1774,7 +1783,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 1774 | 1783 | ||
| 1775 | mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date")); | 1784 | mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date")); |
| 1776 | 1785 | ||
| 1777 | xs *acct = mastoapi_account(actor); | 1786 | xs *acct = mastoapi_account(&snac1, actor); |
| 1778 | mn = xs_dict_append(mn, "account", acct); | 1787 | mn = xs_dict_append(mn, "account", acct); |
| 1779 | 1788 | ||
| 1780 | if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) { | 1789 | if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) { |
| @@ -1876,7 +1885,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 1876 | xs *actor = NULL; | 1885 | xs *actor = NULL; |
| 1877 | 1886 | ||
| 1878 | if (valid_status(object_get_by_md5(v, &actor))) { | 1887 | if (valid_status(object_get_by_md5(v, &actor))) { |
| 1879 | xs *acct = mastoapi_account(actor); | 1888 | xs *acct = mastoapi_account(&snac1, actor); |
| 1880 | out = xs_list_append(out, acct); | 1889 | out = xs_list_append(out, acct); |
| 1881 | } | 1890 | } |
| 1882 | } | 1891 | } |
| @@ -2080,7 +2089,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 2080 | 2089 | ||
| 2081 | if (user_open(&admin, admin_account)) { | 2090 | if (user_open(&admin, admin_account)) { |
| 2082 | xs *actor = msg_actor(&admin); | 2091 | xs *actor = msg_actor(&admin); |
| 2083 | xs *acct = mastoapi_account(actor); | 2092 | xs *acct = mastoapi_account(NULL, actor); |
| 2084 | 2093 | ||
| 2085 | ins = xs_dict_append(ins, "contact_account", acct); | 2094 | ins = xs_dict_append(ins, "contact_account", acct); |
| 2086 | 2095 | ||
| @@ -2180,7 +2189,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 2180 | xs *actor2 = NULL; | 2189 | xs *actor2 = NULL; |
| 2181 | 2190 | ||
| 2182 | if (valid_status(object_get_by_md5(v, &actor2))) { | 2191 | if (valid_status(object_get_by_md5(v, &actor2))) { |
| 2183 | xs *acct2 = mastoapi_account(actor2); | 2192 | xs *acct2 = mastoapi_account(&snac1, actor2); |
| 2184 | 2193 | ||
| 2185 | out = xs_list_append(out, acct2); | 2194 | out = xs_list_append(out, acct2); |
| 2186 | } | 2195 | } |
| @@ -2266,7 +2275,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 2266 | xs *actor_o = NULL; | 2275 | xs *actor_o = NULL; |
| 2267 | 2276 | ||
| 2268 | if (valid_status(actor_request(&snac1, actor, &actor_o))) { | 2277 | if (valid_status(actor_request(&snac1, actor, &actor_o))) { |
| 2269 | xs *acct = mastoapi_account(actor_o); | 2278 | xs *acct = mastoapi_account(NULL, actor_o); |
| 2270 | 2279 | ||
| 2271 | acl = xs_list_append(acl, acct); | 2280 | acl = xs_list_append(acl, acct); |
| 2272 | 2281 | ||