diff --git a/activitypub.c b/activitypub.c index bdee395..4a7c247 100644 --- a/activitypub.c +++ b/activitypub.c @@ -165,6 +165,9 @@ int activitypub_get_handler(d_char *req, char *q_path, snac snac; xs *msg = xs_dict_new(); + if (accept == NULL) + return 400; + if (xs_str_in(accept, "application/activity+json") == -1 && xs_str_in(accept, "application/ld+json") == -1) return 0; @@ -222,11 +225,14 @@ int activitypub_post_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype) /* processes an input message */ { - int status = 200; + int status = 202; /* accepted */ char *headers = xs_dict_get(req, "headers"); char *i_ctype = xs_dict_get(headers, "content-type"); snac snac; + if (i_ctype == NULL) + return 400; + if (xs_str_in(i_ctype, "application/activity+json") == -1 && xs_str_in(i_ctype, "application/ld+json") == -1) return 0; @@ -247,6 +253,16 @@ int activitypub_post_handler(d_char *req, char *q_path, return 404; } + /* decode */ + xs *msg = xs_json_loads(payload); + + if (msg && xs_dict_get(msg, "actor") && xs_dict_get(msg, "type")) + enqueue_input(&snac, msg); + else { + srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path)); + status = 400; + } + user_free(&snac); return status; diff --git a/data.c b/data.c index 3f5d5e0..67b1802 100644 --- a/data.c +++ b/data.c @@ -709,6 +709,33 @@ int actor_get(snac *snac, char *actor, d_char **data) } +void enqueue_input(snac *snac, char *msg) +/* enqueues an input message */ +{ + xs *ntid = tid(0); + xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid); + xs *tfn = xs_str_cat(fn, ".tmp"); + FILE *f; + + if ((f = fopen(tfn, "w")) != NULL) { + xs *qmsg = xs_dict_new(); + xs *j; + + qmsg = xs_dict_append(qmsg, "type", "input"); + qmsg = xs_dict_append(qmsg, "object", msg); + + j = xs_json_dumps_pp(qmsg, 4); + + fwrite(j, strlen(j), 1, f); + fclose(f); + + rename(tfn, fn); + + snac_debug(snac, 2, xs_fmt("enqueue_input %s", fn)); + } +} + + void enqueue_output(snac *snac, char *actor, char *msg, int retries) /* enqueues an output message for an actor */ { @@ -740,7 +767,7 @@ void enqueue_output(snac *snac, char *actor, char *msg, int retries) rename(tfn, fn); - snac_debug(snac, 2, xs_fmt("enqueue %s %s %d", actor, fn, retries)); + snac_debug(snac, 2, xs_fmt("enqueue_output %s %s %d", actor, fn, retries)); } } diff --git a/snac.h b/snac.h index 149889a..c750c75 100644 --- a/snac.h +++ b/snac.h @@ -64,6 +64,7 @@ int is_muted(snac *snac, char *actor); int actor_add(snac *snac, char *actor, char *msg); int actor_get(snac *snac, char *actor, d_char **data); +void enqueue_input(snac *snac, char *msg); void enqueue_output(snac *snac, char *actor, char *msg, int retries); d_char *queue(snac *snac);