From 36c3030231ed806c29c182e38cb0957993c835d1 Mon Sep 17 00:00:00 2001 From: default Date: Sat, 23 Nov 2024 17:33:49 +0100 Subject: In user_persist(), only publish the actor to the world if a relevant field has changed. This way, changing user preferences does not trigger an automatic send storm. --- data.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'data.c') diff --git a/data.c b/data.c index f2fa521..bbebdb8 100644 --- a/data.c +++ b/data.c @@ -336,6 +336,32 @@ int user_persist(snac *snac, int publish) xs *bfn = xs_fmt("%s.bak", fn); FILE *f; + if (publish) { + /* check if any of the relevant fields have really changed */ + if ((f = fopen(fn, "r")) != NULL) { + xs *old = xs_json_load(f); + fclose(f); + + if (old != NULL) { + int nw = 0; + const char *fields[] = { "header", "avatar", "name", "bio", "metadata", NULL }; + + for (int n = 0; fields[n]; n++) { + const char *of = xs_dict_get(old, fields[n]); + const char *nf = xs_dict_get(snac->config, fields[n]); + + if (xs_type(of) != XSTYPE_STRING || xs_type(nf) != XSTYPE_STRING || strcmp(of, nf)) { + nw = 1; + break; + } + } + + if (!nw) + publish = 0; + } + } + } + rename(fn, bfn); if ((f = fopen(fn, "w")) != NULL) { -- cgit From 7287776fd1f659619e211ee482ba0d6d64ddbf0b Mon Sep 17 00:00:00 2001 From: default Date: Sun, 24 Nov 2024 08:17:38 +0100 Subject: New function pending_add(). --- activitypub.c | 31 ++++++++++++++++++++----------- data.c | 22 ++++++++++++++++++++++ snac.h | 2 ++ 3 files changed, 44 insertions(+), 11 deletions(-) (limited to 'data.c') diff --git a/activitypub.c b/activitypub.c index 649b1f2..3ab093f 100644 --- a/activitypub.c +++ b/activitypub.c @@ -1927,22 +1927,31 @@ int process_input_message(snac *snac, const xs_dict *msg, const xs_dict *req) object_add(actor, actor_obj); } - xs *f_msg = xs_dup(msg); - xs *reply = msg_accept(snac, f_msg, actor); + if (xs_is_true(xs_dict_get(snac->config, "approve_followers"))) { + pending_add(snac, actor, msg); - post_message(snac, actor, reply); - - if (xs_is_null(xs_dict_get(f_msg, "published"))) { - /* add a date if it doesn't include one (Mastodon) */ - xs *date = xs_str_utctime(0, ISO_DATE_SPEC); - f_msg = xs_dict_set(f_msg, "published", date); + snac_log(snac, xs_fmt("new pending follower approval %s", actor)); } + else { + /* automatic following */ + xs *f_msg = xs_dup(msg); + xs *reply = msg_accept(snac, f_msg, actor); - timeline_add(snac, id, f_msg); + post_message(snac, actor, reply); + + if (xs_is_null(xs_dict_get(f_msg, "published"))) { + /* add a date if it doesn't include one (Mastodon) */ + xs *date = xs_str_utctime(0, ISO_DATE_SPEC); + f_msg = xs_dict_set(f_msg, "published", date); + } - follower_add(snac, actor); + timeline_add(snac, id, f_msg); + + follower_add(snac, actor); + + snac_log(snac, xs_fmt("new follower %s", actor)); + } - snac_log(snac, xs_fmt("new follower %s", actor)); do_notify = 1; } else diff --git a/data.c b/data.c index bbebdb8..fa631e1 100644 --- a/data.c +++ b/data.c @@ -1202,6 +1202,28 @@ xs_list *follower_list(snac *snac) } +/** pending followers **/ + +int pending_add(snac *user, const char *actor, const xs_dict *msg) +/* stores the follow message for later confirmation */ +{ + xs *dir = xs_fmt("%s/pending", user->basedir); + xs *md5 = xs_md5_hex(actor, strlen(actor)); + xs *fn = xs_fmt("%s/%s.json", dir, md5); + FILE *f; + + mkdirx(dir); + + if ((f = fopen(fn, "w")) == NULL) + return -1; + + xs_json_dump(msg, 4, f); + fclose(f); + + return 0; +} + + /** timeline **/ double timeline_mtime(snac *snac) diff --git a/snac.h b/snac.h index 0d62eb2..bcddab4 100644 --- a/snac.h +++ b/snac.h @@ -141,6 +141,8 @@ int follower_del(snac *snac, const char *actor); int follower_check(snac *snac, const char *actor); xs_list *follower_list(snac *snac); +int pending_add(snac *user, const char *actor, const xs_dict *msg); + double timeline_mtime(snac *snac); int timeline_touch(snac *snac); int timeline_here(snac *snac, const char *md5); -- cgit From b91177cb464ffdb118d50569592be3f9789dbef6 Mon Sep 17 00:00:00 2001 From: default Date: Sun, 24 Nov 2024 08:31:01 +0100 Subject: New function pending_get() and pending_list(). --- data.c | 38 ++++++++++++++++++++++++++++++++++++++ snac.h | 2 ++ 2 files changed, 40 insertions(+) (limited to 'data.c') diff --git a/data.c b/data.c index fa631e1..79c7001 100644 --- a/data.c +++ b/data.c @@ -1224,6 +1224,44 @@ int pending_add(snac *user, const char *actor, const xs_dict *msg) } +xs_dict *pending_get(snac *user, const char *actor) +/* returns the pending follow confirmation for the actor */ +{ + xs *md5 = xs_md5_hex(actor, strlen(actor)); + xs *fn = xs_fmt("%s/pending/%s.json", user->basedir, md5); + xs_dict *msg = NULL; + FILE *f; + + if ((f = fopen(fn, "r")) != NULL) { + msg = xs_json_load(f); + fclose(f); + } + + return msg; +} + + +xs_list *pending_list(snac *user) +/* returns a list of pending follow confirmations */ +{ + xs *spec = xs_fmt("%s/pending/""*.json", user->basedir); + xs *l = xs_glob(spec, 0, 0); + xs_list *r = xs_list_new(); + const char *v; + + xs_list_foreach(l, v) { + const char *actor = xs_dict_get(v, "actor"); + + if (xs_type(actor) == XSTYPE_STRING) { + xs *md5 = xs_md5_hex(actor, strlen(actor)); + r = xs_list_append(r, md5); + } + } + + return r; +} + + /** timeline **/ double timeline_mtime(snac *snac) diff --git a/snac.h b/snac.h index bcddab4..25f7b74 100644 --- a/snac.h +++ b/snac.h @@ -142,6 +142,8 @@ int follower_check(snac *snac, const char *actor); xs_list *follower_list(snac *snac); int pending_add(snac *user, const char *actor, const xs_dict *msg); +xs_dict *pending_get(snac *user, const char *actor); +xs_list *pending_list(snac *user); double timeline_mtime(snac *snac); int timeline_touch(snac *snac); -- cgit From 129049edf4752495f768b247253cb7ffc848b0cc Mon Sep 17 00:00:00 2001 From: default Date: Sun, 24 Nov 2024 08:37:19 +0100 Subject: New function pending_check() and pending_del(). --- data.c | 20 ++++++++++++++++++++ snac.h | 2 ++ 2 files changed, 22 insertions(+) (limited to 'data.c') diff --git a/data.c b/data.c index 79c7001..8bc35aa 100644 --- a/data.c +++ b/data.c @@ -1224,6 +1224,16 @@ int pending_add(snac *user, const char *actor, const xs_dict *msg) } +int pending_check(snac *user, const char *actor) +/* checks if there is a pending follow confirmation for the actor */ +{ + xs *md5 = xs_md5_hex(actor, strlen(actor)); + xs *fn = xs_fmt("%s/pending/%s.json", user->basedir, md5); + + return mtime(fn) != 0; +} + + xs_dict *pending_get(snac *user, const char *actor) /* returns the pending follow confirmation for the actor */ { @@ -1241,6 +1251,16 @@ xs_dict *pending_get(snac *user, const char *actor) } +void pending_del(snac *user, const char *actor) +/* deletes a pending follow confirmation for the actor */ +{ + xs *md5 = xs_md5_hex(actor, strlen(actor)); + xs *fn = xs_fmt("%s/pending/%s.json", user->basedir, md5); + + unlink(fn); +} + + xs_list *pending_list(snac *user) /* returns a list of pending follow confirmations */ { diff --git a/snac.h b/snac.h index 25f7b74..b4cb525 100644 --- a/snac.h +++ b/snac.h @@ -142,7 +142,9 @@ int follower_check(snac *snac, const char *actor); xs_list *follower_list(snac *snac); int pending_add(snac *user, const char *actor, const xs_dict *msg); +int pending_check(snac *user, const char *actor); xs_dict *pending_get(snac *user, const char *actor); +void pending_del(snac *user, const char *actor); xs_list *pending_list(snac *user); double timeline_mtime(snac *snac); -- cgit From 9fb84bcb3aa4c6a8061e4b892dc5bd1826b44715 Mon Sep 17 00:00:00 2001 From: default Date: Sun, 24 Nov 2024 08:54:43 +0100 Subject: The people page shows 'Approve' and 'Discard' buttons for pending follows. --- data.c | 20 +++++++++++++++----- html.c | 29 +++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 11 deletions(-) (limited to 'data.c') diff --git a/data.c b/data.c index 8bc35aa..82a1776 100644 --- a/data.c +++ b/data.c @@ -1270,12 +1270,22 @@ xs_list *pending_list(snac *user) const char *v; xs_list_foreach(l, v) { - const char *actor = xs_dict_get(v, "actor"); + FILE *f; + xs *msg = NULL; - if (xs_type(actor) == XSTYPE_STRING) { - xs *md5 = xs_md5_hex(actor, strlen(actor)); - r = xs_list_append(r, md5); - } + if ((f = fopen(v, "r")) == NULL) + continue; + + msg = xs_json_load(f); + fclose(f); + + if (msg == NULL) + continue; + + const char *actor = xs_dict_get(msg, "actor"); + + if (xs_type(actor) == XSTYPE_STRING) + r = xs_list_append(r, actor); } return r; diff --git a/html.c b/html.c index df9e4e3..2d556e8 100644 --- a/html.c +++ b/html.c @@ -2470,10 +2470,9 @@ xs_html *html_people_list(snac *snac, xs_list *list, char *header, char *t, cons xs_html_tag("summary", xs_html_text("...")))); - xs_list *p = list; const char *actor_id; - while (xs_list_iter(&p, &actor_id)) { + xs_list_foreach(list, actor_id) { xs *md5 = xs_md5_hex(actor_id, strlen(actor_id)); xs *actor = NULL; @@ -2542,6 +2541,14 @@ xs_html *html_people_list(snac *snac, xs_list *list, char *header, char *t, cons html_button("limit", L("Limit"), L("Block announces (boosts) from this user"))); } + if (pending_check(snac, actor_id)) { + xs_html_add(form, + html_button("approve", L("Approve"), + L("Approve this follow request"))); + + xs_html_add(form, + html_button("discard", L("Discard"), L("Discard this follow request"))); + } else { xs_html_add(form, html_button("follow", L("Follow"), @@ -2596,13 +2603,23 @@ xs_str *html_people(snac *user) xs *wing = following_list(user); xs *wers = follower_list(user); + xs_html *lists = xs_html_tag("div", + xs_html_attr("class", "snac-posts")); + + if (xs_is_true(xs_dict_get(user->config, "approve_followers"))) { + xs *pending = pending_list(user); + xs_html_add(lists, + html_people_list(user, pending, L("Pending follow confirmations"), "p", proxy)); + } + + xs_html_add(lists, + html_people_list(user, wing, L("People you follow"), "i", proxy), + html_people_list(user, wers, L("People that follow you"), "e", proxy)); + xs_html *html = xs_html_tag("html", html_user_head(user, NULL, NULL), xs_html_add(html_user_body(user, 0), - xs_html_tag("div", - xs_html_attr("class", "snac-posts"), - html_people_list(user, wing, L("People you follow"), "i", proxy), - html_people_list(user, wers, L("People that follow you"), "e", proxy)), + lists, html_footer())); return xs_html_render_s(html, "\n"); -- cgit From a2665c3cc9bb94a7969a904bfcd210e20cf038e7 Mon Sep 17 00:00:00 2001 From: default Date: Sun, 24 Nov 2024 09:37:59 +0100 Subject: Minor tweak to user_persist(). --- data.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'data.c') diff --git a/data.c b/data.c index 82a1776..4e5851a 100644 --- a/data.c +++ b/data.c @@ -350,6 +350,9 @@ int user_persist(snac *snac, int publish) const char *of = xs_dict_get(old, fields[n]); const char *nf = xs_dict_get(snac->config, fields[n]); + if (of == NULL && nf == NULL) + continue; + if (xs_type(of) != XSTYPE_STRING || xs_type(nf) != XSTYPE_STRING || strcmp(of, nf)) { nw = 1; break; -- cgit