From 2c00c7b0b585867495a505a9daf3ab48aa1dfa0b Mon Sep 17 00:00:00 2001 From: default Date: Sat, 1 Oct 2022 09:12:33 +0200 Subject: [PATCH] New web action 'Unfollow'. --- activitypub.c | 11 +++++++++++ data.c | 21 +++++++++++++++++++++ html.c | 16 +++++++++++++++- snac.h | 2 ++ 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/activitypub.c b/activitypub.c index 5e26fb6..29ce676 100644 --- a/activitypub.c +++ b/activitypub.c @@ -437,6 +437,17 @@ d_char *msg_create(snac *snac, char *object) } +d_char *msg_undo(snac *snac, char *object) +/* creates an 'Undo' message */ +{ + d_char *msg = msg_base(snac, "Undo", "@object", snac->actor, "@now", object); + + msg = xs_dict_append(msg, "to", xs_dict_get(object, "object")); + + return msg; +} + + d_char *msg_follow(snac *snac, char *actor) /* creates a 'Follow' message */ { diff --git a/data.c b/data.c index 9051810..4ff5a84 100644 --- a/data.c +++ b/data.c @@ -693,6 +693,27 @@ int following_check(snac *snac, char *actor) } +int following_get(snac *snac, char *actor, d_char **data) +/* returns the 'Follow' object */ +{ + xs *fn = _following_fn(snac, actor); + FILE *f; + int status = 200; + + if ((f = fopen(fn, "r")) != NULL) { + xs *j = xs_readall(f); + + fclose(f); + + *data = xs_json_loads(j); + } + else + status = 404; + + return status; +} + + d_char *_muted_fn(snac *snac, char *actor) { xs *md5 = xs_md5_hex(actor, strlen(actor)); diff --git a/html.c b/html.c index 4e60903..45e1963 100644 --- a/html.c +++ b/html.c @@ -867,7 +867,7 @@ int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size, } else if (strcmp(action, L("Follow")) == 0) { - char *msg = msg_follow(&snac, actor); + xs *msg = msg_follow(&snac, actor); /* reload the actor from the message, in may be different */ actor = xs_dict_get(msg, "object"); @@ -878,6 +878,20 @@ int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size, } else if (strcmp(action, L("Unfollow")) == 0) { + /* get the following object */ + xs *object = NULL; + + if (valid_status(following_get(&snac, actor, &object))) { + xs *msg = msg_undo(&snac, xs_dict_get(object, "object")); + + following_del(&snac, actor); + + enqueue_output(&snac, msg, actor, 0); + + snac_log(&snac, xs_fmt("unfollowed actor %s", actor)); + } + else + snac_log(&snac, xs_fmt("actor is not being followed %s", actor)); } else if (strcmp(action, L("Delete")) == 0) { diff --git a/snac.h b/snac.h index 26f153e..afccc65 100644 --- a/snac.h +++ b/snac.h @@ -75,6 +75,7 @@ d_char *local_list(snac *snac, int max); int following_add(snac *snac, char *actor, char *msg); int following_del(snac *snac, char *actor); int following_check(snac *snac, char *actor); +int following_get(snac *snac, char *actor, d_char **data); void mute(snac *snac, char *actor); void unmute(snac *snac, char *actor); @@ -112,6 +113,7 @@ d_char *msg_admiration(snac *snac, char *object, char *type); d_char *msg_create(snac *snac, char *object); d_char *msg_follow(snac *snac, char *actor); d_char *msg_note(snac *snac, char *content, char *rcpts, char *in_reply_to); +d_char *msg_undo(snac *snac, char *object); int activitypub_request(snac *snac, char *url, d_char **data); int actor_request(snac *snac, char *actor, d_char **data);