From a0bcc4e6c022fc55956920fd49303abc6ca9b4db Mon Sep 17 00:00:00 2001 From: default Date: Fri, 23 Sep 2022 18:15:59 +0200 Subject: [PATCH] Improved activitypub_request(). --- activitypub.c | 49 ++++++++++++++++++++++++++++++------------------- main.c | 17 +++++++++++++++++ 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/activitypub.c b/activitypub.c index de99b75..7cbf4db 100644 --- a/activitypub.c +++ b/activitypub.c @@ -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); + } + + /* 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 = xs_json_loads(payload); - } + 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 diff --git a/main.c b/main.c index 101c549..7d48fa4 100644 --- a/main.c +++ b/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;