commit 50fa2ae
default
·
2024-11-12 05:34:52 +0000 UTC
parent 4971e54
mastoapi: a lot of proxy code.
1 files changed,
+28,
-19
+28,
-19
1@@ -517,15 +517,19 @@ xs_str *mastoapi_id(const xs_dict *msg)
2 #define MID_TO_MD5(id) (id + 10)
3
4
5-xs_dict *mastoapi_account(const xs_dict *actor)
6+xs_dict *mastoapi_account(snac *logged, const xs_dict *actor)
7 /* converts an ActivityPub actor to a Mastodon account */
8 {
9 const char *id = xs_dict_get(actor, "id");
10 const char *pub = xs_dict_get(actor, "published");
11+ const char *proxy = NULL;
12
13 if (xs_type(id) != XSTYPE_STRING)
14 return NULL;
15
16+ if (logged && xs_is_true(xs_dict_get(srv_config, "proxy_media")))
17+ proxy = logged->actor;
18+
19 const char *prefu = xs_dict_get(actor, "preferredUsername");
20
21 const char *display_name = xs_dict_get(actor, "name");
22@@ -583,7 +587,7 @@ xs_dict *mastoapi_account(const xs_dict *actor)
23 const char *url = xs_dict_get(av, "url");
24
25 if (url != NULL)
26- avatar = xs_dup(url);
27+ avatar = make_url(url, proxy, 1);
28 }
29
30 if (avatar == NULL)
31@@ -596,7 +600,7 @@ xs_dict *mastoapi_account(const xs_dict *actor)
32 const xs_dict *hd = xs_dict_get(actor, "image");
33
34 if (xs_type(hd) == XSTYPE_DICT)
35- header = xs_dup(xs_dict_get(hd, "url"));
36+ header = make_url(xs_dict_get(hd, "url"), proxy, 1);
37
38 if (xs_is_null(header))
39 header = xs_str_new(NULL);
40@@ -619,9 +623,10 @@ xs_dict *mastoapi_account(const xs_dict *actor)
41 const xs_dict *icon = xs_dict_get(v, "icon");
42
43 if (!xs_is_null(name) && !xs_is_null(icon)) {
44- const char *url = xs_dict_get(icon, "url");
45+ const char *o_url = xs_dict_get(icon, "url");
46
47- if (!xs_is_null(url)) {
48+ if (!xs_is_null(o_url)) {
49+ xs *url = make_url(o_url, proxy, 1);
50 xs *nm = xs_strip_chars_i(xs_dup(name), ":");
51 xs *d1 = xs_dict_new();
52
53@@ -790,11 +795,15 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
54 {
55 xs *actor = NULL;
56 actor_get_refresh(snac, get_atto(msg), &actor);
57+ const char *proxy = NULL;
58
59 /* if the author is not here, discard */
60 if (actor == NULL)
61 return NULL;
62
63+ if (snac && xs_is_true(xs_dict_get(srv_config, "proxy_media")))
64+ proxy = snac->actor;
65+
66 const char *type = xs_dict_get(msg, "type");
67 const char *id = xs_dict_get(msg, "id");
68
69@@ -802,7 +811,7 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
70 if (xs_is_null(type) || xs_is_null(id))
71 return NULL;
72
73- xs *acct = mastoapi_account(actor);
74+ xs *acct = mastoapi_account(snac, actor);
75 if (acct == NULL)
76 return NULL;
77
78@@ -871,7 +880,7 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
79
80 if (xs_match(type, "image/*|video/*|Image|Video")) { /* */
81 xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
82- xs *href = make_url(o_href, snac->actor, 1);
83+ xs *href = make_url(o_href, proxy, 1);
84
85 xs *d = xs_dict_new();
86
87@@ -1075,7 +1084,7 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
88 xs *b_actor = NULL;
89
90 if (valid_status(object_get_by_md5(boosted_by_md5, &b_actor))) {
91- xs *b_acct = mastoapi_account(b_actor);
92+ xs *b_acct = mastoapi_account(snac, b_actor);
93 xs *fake_uri = NULL;
94
95 if (snac)
96@@ -1463,7 +1472,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
97
98 if (user_open(&user, uid)) {
99 xs *actor = msg_actor(&user);
100- xs *macct = mastoapi_account(actor);
101+ xs *macct = mastoapi_account(NULL, actor);
102
103 *body = xs_json_dumps(macct, 4);
104 *ctype = "application/json";
105@@ -1521,7 +1530,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
106 xs *luname = xs_tolower_i(xs_dup(uname));
107
108 if (xs_startswith(luname, q)) {
109- xs *acct = mastoapi_account(actor);
110+ xs *acct = mastoapi_account(&snac1, actor);
111
112 out = xs_list_append(out, acct);
113 }
114@@ -1548,7 +1557,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
115 /* if it's not already seen, add it */
116 if (xs_set_add(&seen, user.actor) == 1) {
117 xs *actor = msg_actor(&user);
118- xs *acct = mastoapi_account(actor);
119+ xs *acct = mastoapi_account(&snac1, actor);
120
121 out = xs_list_append(out, acct);
122 }
123@@ -1566,7 +1575,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
124 if (opt == NULL) {
125 /* account information */
126 actor = msg_actor(&snac2);
127- out = mastoapi_account(actor);
128+ out = mastoapi_account(NULL, actor);
129 }
130 else
131 if (strcmp(opt, "statuses") == 0) { /** **/
132@@ -1609,7 +1618,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
133 xs *actor = NULL;
134
135 if (valid_status(object_get(v, &actor))) {
136- xs *acct = mastoapi_account(actor);
137+ xs *acct = mastoapi_account(NULL, actor);
138 out = xs_list_append(out, acct);
139 }
140 }
141@@ -1626,7 +1635,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
142 if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
143 if (opt == NULL) {
144 /* account information */
145- out = mastoapi_account(actor);
146+ out = mastoapi_account(&snac1, actor);
147 }
148 else
149 if (strcmp(opt, "statuses") == 0) {
150@@ -1774,7 +1783,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
151
152 mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
153
154- xs *acct = mastoapi_account(actor);
155+ xs *acct = mastoapi_account(&snac1, actor);
156 mn = xs_dict_append(mn, "account", acct);
157
158 if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
159@@ -1876,7 +1885,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
160 xs *actor = NULL;
161
162 if (valid_status(object_get_by_md5(v, &actor))) {
163- xs *acct = mastoapi_account(actor);
164+ xs *acct = mastoapi_account(&snac1, actor);
165 out = xs_list_append(out, acct);
166 }
167 }
168@@ -2080,7 +2089,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
169
170 if (user_open(&admin, admin_account)) {
171 xs *actor = msg_actor(&admin);
172- xs *acct = mastoapi_account(actor);
173+ xs *acct = mastoapi_account(NULL, actor);
174
175 ins = xs_dict_append(ins, "contact_account", acct);
176
177@@ -2180,7 +2189,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
178 xs *actor2 = NULL;
179
180 if (valid_status(object_get_by_md5(v, &actor2))) {
181- xs *acct2 = mastoapi_account(actor2);
182+ xs *acct2 = mastoapi_account(&snac1, actor2);
183
184 out = xs_list_append(out, acct2);
185 }
186@@ -2266,7 +2275,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
187 xs *actor_o = NULL;
188
189 if (valid_status(actor_request(&snac1, actor, &actor_o))) {
190- xs *acct = mastoapi_account(actor_o);
191+ xs *acct = mastoapi_account(NULL, actor_o);
192
193 acl = xs_list_append(acl, acct);
194