From 499697258d2c266be69dc13dae96083fc78cd3ef Mon Sep 17 00:00:00 2001 From: default Date: Sat, 12 Aug 2023 09:43:01 +0200 Subject: [PATCH] Rewritten is_msg_public() to not depend on a user. --- activitypub.c | 31 ++++++++++++++++++++++++------- data.c | 2 +- html.c | 6 +++--- mastoapi.c | 2 +- snac.h | 2 +- 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/activitypub.c b/activitypub.c index 7a08ceb..2c20e63 100644 --- a/activitypub.c +++ b/activitypub.c @@ -408,12 +408,29 @@ xs_list *recipient_list(snac *snac, const xs_dict *msg, int expand_public) } -int is_msg_public(snac *snac, const xs_dict *msg) +int is_msg_public(const xs_dict *msg) /* checks if a message is public */ { - xs *rcpts = recipient_list(snac, msg, 0); + const char *to = xs_dict_get(msg, "to"); + const char *cc = xs_dict_get(msg, "cc"); + int n; - return xs_list_in(rcpts, public_address) != -1; + const char *lists[] = { to, cc, NULL }; + for (n = 0; lists[n]; n++) { + const xs_val *l = lists[n]; + + if (xs_type(l) == XSTYPE_STRING) { + if (strcmp(l, public_address) == 0) + return 1; + } + else + if (xs_type(l) == XSTYPE_LIST) { + if (xs_list_in(l, public_address) != -1) + return 1; + } + } + + return 0; } @@ -867,7 +884,7 @@ xs_dict *msg_admiration(snac *snac, char *object, char *type) msg = msg_base(snac, type, "@dummy", snac->actor, "@now", object); - if (is_msg_public(snac, a_msg)) + if (is_msg_public(a_msg)) rcpts = xs_list_append(rcpts, public_address); rcpts = xs_list_append(rcpts, xs_dict_get(a_msg, "attributedTo")); @@ -1097,7 +1114,7 @@ xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts, ctxt = xs_dup(v); /* if this message is public, ours will also be */ - if (!priv && is_msg_public(snac, p_msg) && xs_list_in(to, public_address) == -1) + if (!priv && is_msg_public(p_msg) && xs_list_in(to, public_address) == -1) to = xs_list_append(to, public_address); } @@ -1419,7 +1436,7 @@ int process_input_message(snac *snac, xs_dict *msg, xs_dict *req) /* if it's a DM from someone we don't follow, reject the message */ if (xs_type(xs_dict_get(snac->config, "drop_dm_from_unknown")) == XSTYPE_TRUE) { - if (strcmp(utype, "Note") == 0 && !is_msg_public(snac, msg) && + if (strcmp(utype, "Note") == 0 && !is_msg_public(msg) && !following_check(snac, actor)) { snac_log(snac, xs_fmt("DM rejected from unknown actor %s", actor)); @@ -1726,7 +1743,7 @@ void process_user_queue_item(snac *snac, xs_dict *q_item) } /* if it's public, send to the collected inboxes */ - if (is_msg_public(snac, msg)) { + if (is_msg_public(msg)) { xs *shibx = inbox_list(); xs_str *inbox; diff --git a/data.c b/data.c index 07ea27a..d7643e2 100644 --- a/data.c +++ b/data.c @@ -1041,7 +1041,7 @@ void timeline_update_indexes(snac *snac, const char *id) if (valid_status(object_get(id, &msg))) { /* if its ours and is public, also store in public */ - if (is_msg_public(snac, msg)) { + if (is_msg_public(msg)) { object_user_cache_add(snac, id, "public"); /* also add it to the instance public timeline */ diff --git a/html.c b/html.c index ce0df97..58501f2 100644 --- a/html.c +++ b/html.c @@ -206,7 +206,7 @@ xs_str *html_msg_icon(snac *snac, xs_str *os, const xs_dict *msg) if (strcmp(type, "Note") == 0 || strcmp(type, "Question") == 0 || strcmp(type, "Page") == 0) url = xs_dict_get(msg, "id"); - priv = !is_msg_public(snac, msg); + priv = !is_msg_public(msg); date = xs_dict_get(msg, "published"); udate = xs_dict_get(msg, "updated"); @@ -700,7 +700,7 @@ xs_str *html_entry_controls(snac *snac, xs_str *os, const xs_dict *msg, const ch s = html_button(s, "pin", L("Pin"), L("Pin this post to the top of your timeline")); } - if (is_msg_public(snac, msg)) { + if (is_msg_public(msg)) { if (strcmp(actor, snac->actor) == 0 || xs_list_in(boosts, snac->md5) == -1) { /* not already boosted or us; add button */ s = html_button(s, "boost", L("Boost"), L("Announce this post to your followers")); @@ -845,7 +845,7 @@ xs_str *html_entry(snac *snac, xs_str *os, const xs_dict *msg, int local, xs *boosts = NULL; /* do not show non-public messages in the public timeline */ - if (local && !is_msg_public(snac, msg)) + if (local && !is_msg_public(msg)) return os; /* hidden? do nothing more for this conversation */ diff --git a/mastoapi.c b/mastoapi.c index dbb6e11..25e54d3 100644 --- a/mastoapi.c +++ b/mastoapi.c @@ -658,7 +658,7 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) st = xs_dict_append(st, "content", xs_dict_get(msg, "content")); st = xs_dict_append(st, "visibility", - is_msg_public(snac, msg) ? "public" : "private"); + is_msg_public(msg) ? "public" : "private"); tmp = xs_dict_get(msg, "sensitive"); if (xs_is_null(tmp)) diff --git a/snac.h b/snac.h index 4652827..fed9309 100644 --- a/snac.h +++ b/snac.h @@ -242,7 +242,7 @@ int send_to_inbox(snac *snac, const xs_str *inbox, const xs_dict *msg, xs_str *get_actor_inbox(snac *snac, const char *actor); int send_to_actor(snac *snac, const char *actor, const xs_dict *msg, xs_val **payload, int *p_size, int timeout); -int is_msg_public(snac *snac, const xs_dict *msg); +int is_msg_public(const xs_dict *msg); int is_msg_for_me(snac *snac, const xs_dict *msg); int process_user_queue(snac *snac);