mirror of
https://codeberg.org/grunfink/snac2.git
synced 2024-11-22 13:25:04 +00:00
Improved activitypub_request().
This commit is contained in:
parent
02b0df78c6
commit
a0bcc4e6c0
2 changed files with 47 additions and 19 deletions
|
@ -15,8 +15,9 @@ int activitypub_request(snac *snac, char *url, d_char **data)
|
|||
{
|
||||
int status;
|
||||
xs *response = NULL;
|
||||
xs *payload;
|
||||
xs *payload = NULL;
|
||||
int p_size;
|
||||
char *ctype;
|
||||
|
||||
/* check if it's an url for this same site */
|
||||
/* ... */
|
||||
|
@ -25,41 +26,51 @@ int activitypub_request(snac *snac, char *url, d_char **data)
|
|||
response = http_signed_request(snac, "GET", url,
|
||||
NULL, NULL, 0, &status, &payload, &p_size);
|
||||
|
||||
{
|
||||
xs *j = xs_json_loads(response);
|
||||
printf("%s\n", j);
|
||||
if (valid_status(status)) {
|
||||
if (dbglevel >= 3) {
|
||||
xs *j = xs_json_dumps_pp(response, 4);
|
||||
fprintf(stderr, "%s\n", j);
|
||||
}
|
||||
|
||||
if (valid_status(status)) {
|
||||
/* ensure it's ActivityPub data */
|
||||
ctype = xs_dict_get(response, "content-type");
|
||||
|
||||
if (xs_str_in(ctype, "application/activity+json") != -1)
|
||||
*data = xs_json_loads(payload);
|
||||
else
|
||||
status = 500;
|
||||
}
|
||||
|
||||
if (!valid_status(status))
|
||||
*data = NULL;
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
#if 0
|
||||
int actor_request(snac *snac, char *actor, d_char **data)
|
||||
/* request an actor */
|
||||
{
|
||||
int status;
|
||||
xs *response = NULL;
|
||||
xs *payload;
|
||||
int p_size;
|
||||
int status, status2;
|
||||
xs *payload = NULL;
|
||||
|
||||
/* get from disk first */
|
||||
status = actor_get(snac, actor, data);
|
||||
|
||||
if (status == 200)
|
||||
return;
|
||||
return status;
|
||||
|
||||
/* get from the net */
|
||||
response = http_signed_request(snac, "GET", actor,
|
||||
NULL, NULL, 0, &status, &payload, &p_size);
|
||||
/* actor data non-existent or stale: get from the net */
|
||||
status2 = activitypub_request(snac, actor, &payload);
|
||||
|
||||
// response = http_signed_request(&snac, "GET", "https://mastodon.social/users/VictorMoral",
|
||||
// headers, NULL, 0, &status, &payload, &p_size);
|
||||
if (valid_status(status2)) {
|
||||
/* renew data */
|
||||
xs *j = xs_json_dumps_pp(payload, 4);
|
||||
status = actor_add(snac, actor, j);
|
||||
|
||||
*data = payload;
|
||||
payload = NULL;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
#endif
|
||||
|
|
17
main.c
17
main.c
|
@ -21,6 +21,7 @@ int main(int argc, char *argv[])
|
|||
char *user;
|
||||
char *url;
|
||||
int argi = 1;
|
||||
snac snac;
|
||||
|
||||
argc--;
|
||||
if (argc < argi)
|
||||
|
@ -73,7 +74,23 @@ int main(int argc, char *argv[])
|
|||
|
||||
url = argv[argi++];
|
||||
|
||||
if (!user_open(&snac, user)) {
|
||||
printf("error in user '%s'\n", user);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (strcmp(cmd, "request") == 0) {
|
||||
int status;
|
||||
xs *data = NULL;
|
||||
|
||||
status = activitypub_request(&snac, url, &data);
|
||||
|
||||
printf("status: %d\n", status);
|
||||
if (valid_status(status)) {
|
||||
|
||||
xs *j = xs_json_dumps_pp(data, 4);
|
||||
printf("%s\n", j);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
Loading…
Reference in a new issue