From 4e3d596bee1cee2c4146265eb6ac827f09820c53 Mon Sep 17 00:00:00 2001 From: shtrophic Date: Mon, 20 Jan 2025 19:43:42 +0100 Subject: add xs_smtp_request --- data.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'data.c') diff --git a/data.c b/data.c index 33947ff..b148ac7 100644 --- a/data.c +++ b/data.c @@ -3195,7 +3195,7 @@ void enqueue_output_by_actor(snac *snac, const xs_dict *msg, } -void enqueue_email(const xs_str *msg, int retries) +void enqueue_email(const xs_dict *msg, int retries) /* enqueues an email message to be sent */ { xs *qmsg = _new_qmsg("email", msg, retries); -- cgit From 2ca7735779e79dbe6fe62f0111a12c145f428d8f Mon Sep 17 00:00:00 2001 From: shtrophic Date: Wed, 19 Feb 2025 08:56:14 +0100 Subject: fix ownership-problem of mailinfo --- activitypub.c | 2 +- data.c | 5 +++-- snac.h | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) (limited to 'data.c') diff --git a/activitypub.c b/activitypub.c index e2519e6..a91821d 100644 --- a/activitypub.c +++ b/activitypub.c @@ -1025,7 +1025,7 @@ void notify(snac *snac, const char *type, const char *utype, const char *actor, "\n", from, email, subject); - xs *mailinfo = xs_dict_new(); + xs_dict *mailinfo = xs_dict_new(); xs_dict_append(mailinfo, "from", from); xs_dict_append(mailinfo, "to", email); xs_dict_append(mailinfo, "body", xs_fmt("%s%s", header, body)); diff --git a/data.c b/data.c index ae85aaf..d25a5d7 100644 --- a/data.c +++ b/data.c @@ -3174,7 +3174,7 @@ static xs_dict *_enqueue_put(const char *fn, xs_dict *msg) } -static xs_dict *_new_qmsg(const char *type, const xs_val *msg, int retries) +static xs_dict *_new_qmsg(const char *type, xs_dict *msg, int retries) /* creates a queue message */ { int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes")); @@ -3276,9 +3276,10 @@ void enqueue_output_by_actor(snac *snac, const xs_dict *msg, } -void enqueue_email(const xs_dict *msg, int retries) +void enqueue_email(xs_dict *msg, int retries) /* enqueues an email message to be sent */ { + /* qmsg owns msg */ xs *qmsg = _new_qmsg("email", msg, retries); const char *ntid = xs_dict_get(qmsg, "ntid"); xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid); diff --git a/snac.h b/snac.h index 92ffb3f..545df74 100644 --- a/snac.h +++ b/snac.h @@ -274,7 +274,7 @@ void enqueue_output(snac *snac, const xs_dict *msg, const xs_str *inbox, int retries, int p_status); void enqueue_output_by_actor(snac *snac, const xs_dict *msg, const xs_str *actor, int retries); -void enqueue_email(const xs_str *msg, int retries); +void enqueue_email(xs_dict *msg, int retries); void enqueue_telegram(const xs_str *msg, const char *bot, const char *chat_id); void enqueue_ntfy(const xs_str *msg, const char *ntfy_server, const char *ntfy_token); void enqueue_message(snac *snac, const xs_dict *msg); -- cgit From 59a34a9646e28cc9a63194ff55547de0005d0110 Mon Sep 17 00:00:00 2001 From: shtrophic Date: Thu, 20 Feb 2025 17:13:12 +0100 Subject: Revert "fix ownership-problem of mailinfo" This reverts commit 2ca7735779e79dbe6fe62f0111a12c145f428d8f. --- activitypub.c | 2 +- data.c | 5 ++--- snac.h | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) (limited to 'data.c') diff --git a/activitypub.c b/activitypub.c index a91821d..e2519e6 100644 --- a/activitypub.c +++ b/activitypub.c @@ -1025,7 +1025,7 @@ void notify(snac *snac, const char *type, const char *utype, const char *actor, "\n", from, email, subject); - xs_dict *mailinfo = xs_dict_new(); + xs *mailinfo = xs_dict_new(); xs_dict_append(mailinfo, "from", from); xs_dict_append(mailinfo, "to", email); xs_dict_append(mailinfo, "body", xs_fmt("%s%s", header, body)); diff --git a/data.c b/data.c index d25a5d7..ae85aaf 100644 --- a/data.c +++ b/data.c @@ -3174,7 +3174,7 @@ static xs_dict *_enqueue_put(const char *fn, xs_dict *msg) } -static xs_dict *_new_qmsg(const char *type, xs_dict *msg, int retries) +static xs_dict *_new_qmsg(const char *type, const xs_val *msg, int retries) /* creates a queue message */ { int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes")); @@ -3276,10 +3276,9 @@ void enqueue_output_by_actor(snac *snac, const xs_dict *msg, } -void enqueue_email(xs_dict *msg, int retries) +void enqueue_email(const xs_dict *msg, int retries) /* enqueues an email message to be sent */ { - /* qmsg owns msg */ xs *qmsg = _new_qmsg("email", msg, retries); const char *ntid = xs_dict_get(qmsg, "ntid"); xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid); diff --git a/snac.h b/snac.h index 545df74..92ffb3f 100644 --- a/snac.h +++ b/snac.h @@ -274,7 +274,7 @@ void enqueue_output(snac *snac, const xs_dict *msg, const xs_str *inbox, int retries, int p_status); void enqueue_output_by_actor(snac *snac, const xs_dict *msg, const xs_str *actor, int retries); -void enqueue_email(xs_dict *msg, int retries); +void enqueue_email(const xs_str *msg, int retries); void enqueue_telegram(const xs_str *msg, const char *bot, const char *chat_id); void enqueue_ntfy(const xs_str *msg, const char *ntfy_server, const char *ntfy_token); void enqueue_message(snac *snac, const xs_dict *msg); -- cgit From b50631c0f7253e35310a14012e4fa9432aedfb5a Mon Sep 17 00:00:00 2001 From: shtrophic Date: Thu, 20 Feb 2025 17:16:16 +0100 Subject: duplicate dict instead --- data.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'data.c') diff --git a/data.c b/data.c index ae85aaf..a84cb8d 100644 --- a/data.c +++ b/data.c @@ -3279,7 +3279,7 @@ void enqueue_output_by_actor(snac *snac, const xs_dict *msg, void enqueue_email(const xs_dict *msg, int retries) /* enqueues an email message to be sent */ { - xs *qmsg = _new_qmsg("email", msg, retries); + xs *qmsg = _new_qmsg("email", xs_dup(msg), retries); const char *ntid = xs_dict_get(qmsg, "ntid"); xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid); -- cgit From ff06e7e3e019f5f960bc6875d1f027e5e304ee4a Mon Sep 17 00:00:00 2001 From: shtrophic Date: Thu, 20 Feb 2025 18:25:47 +0100 Subject: duping isn't necessary as xs_vals are copied anyway (right?) --- data.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'data.c') diff --git a/data.c b/data.c index a84cb8d..ae85aaf 100644 --- a/data.c +++ b/data.c @@ -3279,7 +3279,7 @@ void enqueue_output_by_actor(snac *snac, const xs_dict *msg, void enqueue_email(const xs_dict *msg, int retries) /* enqueues an email message to be sent */ { - xs *qmsg = _new_qmsg("email", xs_dup(msg), retries); + xs *qmsg = _new_qmsg("email", msg, retries); const char *ntid = xs_dict_get(qmsg, "ntid"); xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid); -- cgit From 770062def6f3e10bf56eae48e274709796fa6df6 Mon Sep 17 00:00:00 2001 From: default Date: Sat, 22 Mar 2025 08:50:08 +0100 Subject: Filter out block instances from inbox_list(). --- data.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'data.c') diff --git a/data.c b/data.c index ce040dd..a192830 100644 --- a/data.c +++ b/data.c @@ -2619,10 +2619,9 @@ xs_list *inbox_list(void) xs_list *ibl = xs_list_new(); xs *spec = xs_fmt("%s/inbox/" "*", srv_basedir); xs *files = xs_glob(spec, 0, 0); - xs_list *p = files; const xs_val *v; - while (xs_list_iter(&p, &v)) { + xs_list_foreach(files, v) { FILE *f; if ((f = fopen(v, "r")) != NULL) { @@ -2630,7 +2629,9 @@ xs_list *inbox_list(void) if (line && *line) { line = xs_strip_i(line); - ibl = xs_list_append(ibl, line); + + if (!is_instance_blocked(line)) + ibl = xs_list_append(ibl, line); } fclose(f); -- cgit From 5090e4e77489d7e4e2d358c417c83be8f76307cb Mon Sep 17 00:00:00 2001 From: default Date: Tue, 1 Apr 2025 06:14:46 +0200 Subject: Added more scheduling code. --- data.c | 37 +++++++++++++++++++++++++++++++++++++ html.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- snac.h | 5 +++++ 3 files changed, 84 insertions(+), 2 deletions(-) (limited to 'data.c') diff --git a/data.c b/data.c index a192830..8f68ee2 100644 --- a/data.c +++ b/data.c @@ -1929,6 +1929,43 @@ xs_list *draft_list(snac *user) } +/** scheduled posts **/ + +int is_scheduled(snac *user, const char *id) +/* returns true if this note is scheduled for future sending */ +{ + return object_user_cache_in(user, id, "sched"); +} + + +void schedule_del(snac *user, const char *id) +/* deletes an scheduled post */ +{ + object_user_cache_del(user, id, "sched"); +} + + +void schedule_add(snac *user, const char *id, const xs_dict *msg) +/* schedules this post for later */ +{ + /* delete from the index, in case it was already there */ + schedule_del(user, id); + + /* overwrite object */ + object_add_ow(id, msg); + + /* [re]add to the index */ + object_user_cache_add(user, id, "sched"); +} + + +xs_list *scheduled_list(snac *user) +/* return the list of scheduled posts */ +{ + return object_user_cache_list(user, "sched", XS_ALL, 1); +} + + /** hiding **/ xs_str *_hidden_fn(snac *snac, const char *id) diff --git a/html.c b/html.c index d4cbb35..bc8a645 100644 --- a/html.c +++ b/html.c @@ -2870,6 +2870,18 @@ xs_str *html_timeline(snac *user, const xs_list *list, int read_only, xs_html_text(L("drafts"))))); } + { + /* show the list of scheduled posts */ + xs *url = xs_fmt("%s/sched", user->actor); + xs_html_add(lol, + xs_html_tag("li", + xs_html_tag("a", + xs_html_attr("href", url), + xs_html_attr("class", "snac-list-link"), + xs_html_attr("title", L("Scheduled posts")), + xs_html_text(L("scheduled posts"))))); + } + /* the list of followed hashtags */ const char *followed_hashtags = xs_dict_get(user->config, "followed_hashtags"); @@ -3919,6 +3931,21 @@ int html_get_handler(const xs_dict *req, const char *q_path, } } else + if (strcmp(p_path, "sched") == 0) { /** list of scheduled posts **/ + if (!login(&snac, req)) { + *body = xs_dup(uid); + status = HTTP_STATUS_UNAUTHORIZED; + } + else { + xs *list = scheduled_list(&snac); + + *body = html_timeline(&snac, list, 0, skip, show, + 0, L("Scheduled posts"), "", 0, error); + *b_size = strlen(*body); + status = HTTP_STATUS_OK; + } + } + else if (xs_startswith(p_path, "list/")) { /** list timelines **/ if (!login(&snac, req)) { *body = xs_dup(uid); @@ -4342,7 +4369,7 @@ int html_post_handler(const xs_dict *req, const char *q_path, } else if (future_post) { - snac_log(&snac, xs_fmt("DUMMY scheduled post 1 %s", id)); + schedule_add(&snac, id, msg); } else { c_msg = msg_create(&snac, msg); @@ -4381,7 +4408,7 @@ int html_post_handler(const xs_dict *req, const char *q_path, object_add_ow(edit_id, msg); if (future_post) { - snac_log(&snac, xs_fmt("DUMMY scheduled post 2 %s", edit_id)); + schedule_add(&snac, edit_id, msg); } else { c_msg = msg_create(&snac, msg); @@ -4390,7 +4417,15 @@ int html_post_handler(const xs_dict *req, const char *q_path, draft_del(&snac, edit_id); } + else + if (is_scheduled(&snac, edit_id)) { + /* editing an scheduled post; just update it */ + schedule_add(&snac, edit_id, msg); + } else { + /* ignore the (possibly changed) published date */ + msg = xs_dict_set(msg, "published", xs_dict_get(p_msg, "published")); + /* set the updated field */ xs *updated = xs_str_utctime(0, ISO_DATE_SPEC); msg = xs_dict_set(msg, "updated", updated); @@ -4474,6 +4509,9 @@ int html_post_handler(const xs_dict *req, const char *q_path, if (strcmp(action, L("Hide")) == 0) { /** **/ if (is_draft(&snac, id)) draft_del(&snac, id); + else + if (is_scheduled(&snac, id)) + schedule_del(&snac, id); else hide(&snac, id); } @@ -4570,6 +4608,8 @@ int html_post_handler(const xs_dict *req, const char *q_path, draft_del(&snac, id); + schedule_del(&snac, id); + snac_log(&snac, xs_fmt("deleted entry %s", id)); } } diff --git a/snac.h b/snac.h index 142ebc1..7e44039 100644 --- a/snac.h +++ b/snac.h @@ -205,6 +205,11 @@ void draft_del(snac *user, const char *id); void draft_add(snac *user, const char *id, const xs_dict *msg); xs_list *draft_list(snac *user); +int is_scheduled(snac *user, const char *id); +void schedule_del(snac *user, const char *id); +void schedule_add(snac *user, const char *id, const xs_dict *msg); +xs_list *scheduled_list(snac *user); + int limited(snac *user, const char *id, int cmd); #define is_limited(user, id) limited((user), (id), 0) #define limit(user, id) limited((user), (id), 1) -- cgit From 9b2d0381ba734102c20d2111f0a2b64a3c438ef7 Mon Sep 17 00:00:00 2001 From: default Date: Tue, 1 Apr 2025 06:32:53 +0200 Subject: More scheduled post code. --- activitypub.c | 2 ++ data.c | 29 ++++++++++++++++++++++++++++- snac.h | 3 ++- 3 files changed, 32 insertions(+), 2 deletions(-) (limited to 'data.c') diff --git a/activitypub.c b/activitypub.c index c00c371..4c22c25 100644 --- a/activitypub.c +++ b/activitypub.c @@ -2759,6 +2759,8 @@ int process_user_queue(snac *snac) cnt++; } + scheduled_process(snac); + return cnt; } diff --git a/data.c b/data.c index 8f68ee2..6661472 100644 --- a/data.c +++ b/data.c @@ -1966,6 +1966,33 @@ xs_list *scheduled_list(snac *user) } +void scheduled_process(snac *user) +/* processes the scheduled list, sending those ready to be sent */ +{ + xs *posts = scheduled_list(user); + const char *md5; + xs *right_now = xs_str_utctime(0, ISO_DATE_SPEC); + + xs_list_foreach(posts, md5) { + xs *msg = NULL; + + if (valid_status(object_get_by_md5(md5, &msg))) { + if (strcmp(xs_dict_get(msg, "published"), right_now) < 0) { + /* due date! */ + const char *id = xs_dict_get(msg, "id"); + + timeline_add(user, id, msg); + + xs *c_msg = msg_create(user, msg); + enqueue_message(user, c_msg); + + schedule_del(user, id); + } + } + } +} + + /** hiding **/ xs_str *_hidden_fn(snac *snac, const char *id) @@ -3734,7 +3761,7 @@ void purge_user(snac *snac) _purge_user_subdir(snac, "public", pub_days); const char *idxs[] = { "followers.idx", "private.idx", "public.idx", - "pinned.idx", "bookmark.idx", "draft.idx", NULL }; + "pinned.idx", "bookmark.idx", "draft.idx", "sched.idx", NULL }; for (n = 0; idxs[n]; n++) { xs *idx = xs_fmt("%s/%s", snac->basedir, idxs[n]); diff --git a/snac.h b/snac.h index 7e44039..0d2aafe 100644 --- a/snac.h +++ b/snac.h @@ -1,7 +1,7 @@ /* snac - A simple, minimalistic ActivityPub instance */ /* copyright (c) 2022 - 2025 grunfink et al. / MIT license */ -#define VERSION "2.74" +#define VERSION "2.75-dev" #define USER_AGENT "snac/" VERSION @@ -209,6 +209,7 @@ int is_scheduled(snac *user, const char *id); void schedule_del(snac *user, const char *id); void schedule_add(snac *user, const char *id, const xs_dict *msg); xs_list *scheduled_list(snac *user); +void scheduled_process(snac *user); int limited(snac *user, const char *id, int cmd); #define is_limited(user, id) limited((user), (id), 0) -- cgit From 848bd3e865fb2daf75d76cbb75a4a39f9b82b516 Mon Sep 17 00:00:00 2001 From: default Date: Sun, 13 Apr 2025 14:30:50 +0200 Subject: Cache the timezone inside the snac struct. --- data.c | 2 ++ html.c | 6 ++---- snac.h | 1 + 3 files changed, 5 insertions(+), 4 deletions(-) (limited to 'data.c') diff --git a/data.c b/data.c index 6661472..440e9df 100644 --- a/data.c +++ b/data.c @@ -282,6 +282,8 @@ int user_open(snac *user, const char *uid) } else srv_log(xs_fmt("error parsing '%s'", cfg_file)); + + user->tz = xs_dict_get_def(user->config, "tz", "UTC"); } else srv_debug(2, xs_fmt("error opening '%s' %d", cfg_file, errno)); diff --git a/html.c b/html.c index a6f79de..2e7f87c 100644 --- a/html.c +++ b/html.c @@ -455,7 +455,7 @@ xs_html *html_note(snac *user, const char *summary, } if (edit_id == NULL || is_draft || is_scheduled(user, edit_id)) { - xs *pdat = xs_fmt(L("Post date and time (timezone: %s):"), xs_dict_get_def(user->config, "tz", "UTC")); + xs *pdat = xs_fmt(L("Post date and time (timezone: %s):"), user->tz); xs_html_add(form, xs_html_tag("p", @@ -4367,9 +4367,7 @@ int html_post_handler(const xs_dict *req, const char *q_path, time_t t = xs_parse_iso_date(post_pubdate, 0); if (t != 0) { - const char *tz = xs_dict_get_def(snac.config, "tz", "UTC"); - - t -= xs_tz_offset(tz); + t -= xs_tz_offset(snac.tz); xs *iso_date = xs_str_iso_date(t); msg = xs_dict_set(msg, "published", iso_date); diff --git a/snac.h b/snac.h index 0d2aafe..5a19467 100644 --- a/snac.h +++ b/snac.h @@ -61,6 +61,7 @@ typedef struct { xs_str *actor; /* actor url */ xs_str *md5; /* actor url md5 */ const xs_dict *lang;/* string translation dict */ + const char *tz; /* configured timezone */ } snac; typedef struct { -- cgit From d11d03787fa5f45926859e780d9c8c0bf56bf5ee Mon Sep 17 00:00:00 2001 From: default Date: Fri, 18 Apr 2025 08:44:47 +0200 Subject: New command-line options 'lists' and 'list_members'. --- data.c | 13 +++++++++++++ main.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) (limited to 'data.c') diff --git a/data.c b/data.c index 440e9df..53f38e9 100644 --- a/data.c +++ b/data.c @@ -2340,6 +2340,19 @@ xs_val *list_maint(snac *user, const char *list, int op) } break; + + case 4: /** find list id by name **/ + if (xs_is_string(list)) { + xs *lol = list_maint(user, NULL, 0); + const xs_list *li; + + xs_list_foreach(lol, li) { + if (strcmp(list, xs_list_get(li, 1)) == 0) { + l = xs_dup(xs_list_get(li, 0)); + break; + } + } + } } return l; diff --git a/main.c b/main.c index 3cd4524..7632032 100644 --- a/main.c +++ b/main.c @@ -59,6 +59,8 @@ int usage(void) printf("import_csv {basedir} {uid} Imports data from CSV files\n"); printf("import_list {basedir} {uid} {file} Imports a Mastodon CSV list file\n"); printf("import_block_list {basedir} {uid} {file} Imports a Mastodon CSV block list file\n"); + printf("lists {basedir} {uid} Returns the names of the lists created by the user\n"); + printf("list_members {basedir} {uid} {name} Returns the list of accounts inside a list\n"); return 1; } @@ -282,9 +284,41 @@ int main(int argc, char *argv[]) return migrate_account(&snac); } + if (strcmp(cmd, "lists") == 0) { /** **/ + xs *lol = list_maint(&snac, NULL, 0); + const xs_list *l; + + xs_list_foreach(lol, l) { + printf("%s (%s)\n", xs_list_get(l, 1), xs_list_get(l, 0)); + } + + return 0; + } + if ((url = GET_ARGV()) == NULL) return usage(); + if (strcmp(cmd, "list_members") == 0) { /** **/ + xs *lid = list_maint(&snac, url, 4); + + if (lid != NULL) { + xs *lcont = list_content(&snac, lid, NULL, 0); + const char *md5; + + xs_list_foreach(lcont, md5) { + xs *actor = NULL; + + if (valid_status(object_get_by_md5(md5, &actor))) { + printf("%s (%s)\n", xs_dict_get(actor, "id"), xs_dict_get_def(actor, "preferredUsername", "")); + } + } + } + else + fprintf(stderr, "Cannot find list named '%s'\n", url); + + return 0; + } + if (strcmp(cmd, "alias") == 0) { /** **/ xs *actor = NULL; xs *uid = NULL; -- cgit From 1ebf2a2d874f3ee589785e8175abdcb33a963d72 Mon Sep 17 00:00:00 2001 From: default Date: Fri, 18 Apr 2025 09:08:48 +0200 Subject: New command-line option 'add_to_list'. --- data.c | 4 ++-- main.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) (limited to 'data.c') diff --git a/data.c b/data.c index 53f38e9..88f1921 100644 --- a/data.c +++ b/data.c @@ -2404,7 +2404,7 @@ xs_val *list_content(snac *user, const char *list, const char *actor_md5, int op break; case 1: /** append actor to list **/ - if (actor_md5 != NULL) { + if (xs_is_string(actor_md5) && xs_is_hex(actor_md5)) { if (!index_in_md5(fn, actor_md5)) index_add_md5(fn, actor_md5); } @@ -2412,7 +2412,7 @@ xs_val *list_content(snac *user, const char *list, const char *actor_md5, int op break; case 2: /** delete actor from list **/ - if (actor_md5 != NULL) + if (xs_is_string(actor_md5) && xs_is_hex(actor_md5)) index_del_md5(fn, actor_md5); break; diff --git a/main.c b/main.c index 54da610..8b8b209 100644 --- a/main.c +++ b/main.c @@ -61,6 +61,9 @@ int usage(void) printf("import_block_list {basedir} {uid} {file} Imports a Mastodon CSV block list file\n"); printf("lists {basedir} {uid} Returns the names of the lists created by the user\n"); printf("list_members {basedir} {uid} {name} Returns the list of accounts inside a list\n"); + printf("create_list {basedir} {uid} {name} Creates a new list\n"); + printf("delete_list {basedir} {uid} {name} Deletes an existing list\n"); + printf("add_to_list {basedir} {uid} {name} {acct} Adds an account (@user@host or actor url) to a list\n"); return 1; } @@ -345,6 +348,33 @@ int main(int argc, char *argv[]) return 0; } + if (strcmp(cmd, "add_to_list") == 0) { /** **/ + const char *account = GET_ARGV(); + + if (account != NULL) { + xs *lid = list_maint(&snac, url, 4); + + if (lid != NULL) { + xs *actor = NULL; + xs *uid = NULL; + + if (valid_status(webfinger_request(account, &actor, &uid))) { + xs *md5 = xs_md5_hex(actor, strlen(actor)); + + list_content(&snac, lid, md5, 1); + printf("Actor %s (%s) added to list %s (%s)\n", actor, uid, url, lid); + } + else + fprintf(stderr, "Cannot resolve account '%s'\n", account); + } + else + fprintf(stderr, "Cannot find a list named '%s'\n", url); + + } + + return 0; + } + if (strcmp(cmd, "alias") == 0) { /** **/ xs *actor = NULL; xs *uid = NULL; -- cgit From ec21e1596ab285813d5774224dfed77cbb30d23e Mon Sep 17 00:00:00 2001 From: green Date: Thu, 24 Apr 2025 15:49:55 +0200 Subject: mastoapi: support lists for users --- data.c | 3 ++- mastoapi.c | 55 ++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 16 deletions(-) (limited to 'data.c') diff --git a/data.c b/data.c index 88f1921..0d1bbc6 100644 --- a/data.c +++ b/data.c @@ -2265,7 +2265,8 @@ xs_val *list_maint(snac *user, const char *list, int op) xs *l2 = xs_split(v2, "/"); /* return [ list_id, list_title ] */ - l = xs_list_append(l, xs_list_append(xs_list_new(), xs_list_get(l2, -1), title)); + xs *tmp_list = xs_list_append(xs_list_new(), xs_list_get(l2, -1), title); + l = xs_list_append(l, tmp_list); } } } diff --git a/mastoapi.c b/mastoapi.c index d93afc5..bea835c 100644 --- a/mastoapi.c +++ b/mastoapi.c @@ -1500,6 +1500,37 @@ xs_str *timeline_link_header(const char *endpoint, xs_list *timeline) } +xs_list *mastoapi_account_lists(snac *user, const char *uid) +/* returns the list of list an user is in */ +{ + xs_list *out = xs_list_new(); + xs *actor_md5 = uid ? xs_md5_hex(uid, strlen(uid)) : NULL; + xs *lol = list_maint(user, NULL, 0); + + const xs_list *li; + xs_list_foreach(lol, li) { + const char *list_id = xs_list_get(li, 0); + const char *list_title = xs_list_get(li, 1); + if (uid) { + xs *users = list_content(user, list_id, NULL, 0); + if (xs_list_in(users, actor_md5) == -1) + continue; + } + + xs *d = xs_dict_new(); + + d = xs_dict_append(d, "id", list_id); + d = xs_dict_append(d, "title", list_title); + d = xs_dict_append(d, "replies_policy", "list"); + d = xs_dict_append(d, "exclusive", xs_stock(XSTYPE_FALSE)); + + out = xs_list_append(out, d); + } + + return out; +} + + int mastoapi_get_handler(const xs_dict *req, const char *q_path, char **body, int *b_size, char **ctype, xs_str **link) { @@ -1723,6 +1754,10 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, if (strcmp(opt, "followers") == 0) { out = xs_list_new(); } + else + if (strcmp(opt, "lists") == 0) { + out = mastoapi_account_lists(&snac1, uid); + } user_free(&snac2); } @@ -1744,6 +1779,10 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, /* implement empty response so apps like Tokodon don't show an error */ out = xs_list_new(); } + else + if (strcmp(opt, "lists") == 0) { + out = mastoapi_account_lists(&snac1, uid); + } } } @@ -1975,21 +2014,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, else if (strcmp(cmd, "/v1/lists") == 0) { /** list of lists **/ if (logged_in) { - xs *lol = list_maint(&snac1, NULL, 0); - xs *l = xs_list_new(); - int c = 0; - const xs_list *li; - - while (xs_list_next(lol, &li, &c)) { - xs *d = xs_dict_new(); - - d = xs_dict_append(d, "id", xs_list_get(li, 0)); - d = xs_dict_append(d, "title", xs_list_get(li, 1)); - d = xs_dict_append(d, "replies_policy", "list"); - d = xs_dict_append(d, "exclusive", xs_stock(XSTYPE_FALSE)); - - l = xs_list_append(l, d); - } + xs *l = mastoapi_account_lists(&snac1, NULL); *body = xs_json_dumps(l, 4); *ctype = "application/json"; -- cgit From 605b60c06ea882cd61df7f2d834c02cce6dd254d Mon Sep 17 00:00:00 2001 From: grunfink Date: Sat, 3 May 2025 19:16:21 +0200 Subject: New function enqueue_webmention(). The q_msg is queued, but nothing is done yet. --- data.c | 13 +++++++++++++ html.c | 4 +++- main.c | 1 + snac.h | 1 + 4 files changed, 18 insertions(+), 1 deletion(-) (limited to 'data.c') diff --git a/data.c b/data.c index 7d33f77..73332ef 100644 --- a/data.c +++ b/data.c @@ -3473,6 +3473,19 @@ void enqueue_actor_refresh(snac *user, const char *actor, int forward_secs) } +void enqueue_webmention(const xs_dict *msg) +/* enqueues a webmention for the post */ +{ + xs *qmsg = _new_qmsg("webmention", msg, 0); + const char *ntid = xs_dict_get(qmsg, "ntid"); + xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid); + + qmsg = _enqueue_put(fn, qmsg); + + srv_debug(1, xs_fmt("enqueue_webmention")); +} + + int was_question_voted(snac *user, const char *id) /* returns true if the user voted in this poll */ { diff --git a/html.c b/html.c index 0652892..e114ea7 100644 --- a/html.c +++ b/html.c @@ -4488,8 +4488,10 @@ int html_post_handler(const xs_dict *req, const char *q_path, snac_log(&snac, xs_fmt("cannot get object '%s' for editing", edit_id)); } - if (c_msg != NULL) + if (c_msg != NULL) { enqueue_message(&snac, c_msg); + enqueue_webmention(msg); + } history_del(&snac, "timeline.html_"); } diff --git a/main.c b/main.c index a6fb6fd..80df2d0 100644 --- a/main.c +++ b/main.c @@ -830,6 +830,7 @@ int main(int argc, char *argv[]) } enqueue_message(&snac, c_msg); + enqueue_webmention(msg); timeline_add(&snac, xs_dict_get(msg, "id"), msg); diff --git a/snac.h b/snac.h index 06f9568..d9d0cfd 100644 --- a/snac.h +++ b/snac.h @@ -289,6 +289,7 @@ void enqueue_close_question(snac *user, const char *id, int end_secs); void enqueue_object_request(snac *user, const char *id, int forward_secs); void enqueue_verify_links(snac *user); void enqueue_actor_refresh(snac *user, const char *actor, int forward_secs); +void enqueue_webmention(const xs_dict *msg); int was_question_voted(snac *user, const char *id); xs_list *user_queue(snac *snac); -- cgit From ee84140ecceb07dad8ea36cc5aa29b91bbb48a56 Mon Sep 17 00:00:00 2001 From: grunfink Date: Sun, 18 May 2025 08:23:48 +0200 Subject: Added a pending follow request count next to the "people" link. --- data.c | 10 ++++++++++ html.c | 12 ++++++++++++ snac.h | 1 + 3 files changed, 23 insertions(+) (limited to 'data.c') diff --git a/data.c b/data.c index 73332ef..e3fa52d 100644 --- a/data.c +++ b/data.c @@ -1335,6 +1335,16 @@ xs_list *pending_list(snac *user) } +int pending_count(snac *user) +/* returns the number of pending follow confirmations */ +{ + xs *spec = xs_fmt("%s/pending/""*.json", user->basedir); + xs *l = xs_glob(spec, 0, 0); + + return xs_list_len(l); +} + + /** timeline **/ double timeline_mtime(snac *snac) diff --git a/html.c b/html.c index aec7d0a..6c7af51 100644 --- a/html.c +++ b/html.c @@ -923,7 +923,9 @@ static xs_html *html_user_body(snac *user, int read_only) } else { int n_len = notify_new_num(user); + int p_len = pending_count(user); xs_html *notify_count = NULL; + xs_html *pending_follow_count = NULL; /* show the number of new notifications, if there are any */ if (n_len) { @@ -935,6 +937,15 @@ static xs_html *html_user_body(snac *user, int read_only) else notify_count = xs_html_text(""); + if (p_len) { + xs *s = xs_fmt(" %d ", p_len); + pending_follow_count = xs_html_tag("sup", + xs_html_attr("style", "background-color: red; color: white;"), + xs_html_text(s)); + } + else + pending_follow_count = xs_html_text(""); + xs *admin_url = xs_fmt("%s/admin", user->actor); xs *notify_url = xs_fmt("%s/notifications", user->actor); xs *people_url = xs_fmt("%s/people", user->actor); @@ -957,6 +968,7 @@ static xs_html *html_user_body(snac *user, int read_only) xs_html_tag("a", xs_html_attr("href", people_url), xs_html_text(L("people"))), + pending_follow_count, xs_html_text(" - "), xs_html_tag("a", xs_html_attr("href", instance_url), diff --git a/snac.h b/snac.h index 64bc2bf..2ab9a67 100644 --- a/snac.h +++ b/snac.h @@ -159,6 +159,7 @@ 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); +int pending_count(snac *user); double timeline_mtime(snac *snac); int timeline_touch(snac *snac); -- cgit From 00fd1a1c3eb99c9e441276a7ce8697a1582152b7 Mon Sep 17 00:00:00 2001 From: green Date: Sat, 29 Mar 2025 01:27:46 +0100 Subject: performance: functions to get the number of followers --- data.c | 17 +++++++++++++++++ html.c | 12 ++---------- snac.h | 2 ++ 3 files changed, 21 insertions(+), 10 deletions(-) (limited to 'data.c') diff --git a/data.c b/data.c index e3fa52d..482a29c 100644 --- a/data.c +++ b/data.c @@ -1215,6 +1215,14 @@ int follower_check(snac *snac, const char *actor) } +int follower_list_len(snac *snac) +/* returns the number followers */ +{ + xs *list = object_user_cache_list(snac, "followers", XS_ALL, 0); + return xs_list_len(list); +} + + xs_list *follower_list(snac *snac) /* returns the list of followers */ { @@ -1709,6 +1717,15 @@ int following_get(snac *snac, const char *actor, xs_dict **data) } +int following_list_len(snac *snac) +/* returns number of people being followed */ +{ + xs *spec = xs_fmt("%s/following/" "*_a.json", snac->basedir); + xs *glist = xs_glob(spec, 0, 0); + return xs_list_len(glist); +} + + xs_list *following_list(snac *snac) /* returns the list of people being followed */ { diff --git a/html.c b/html.c index b27db7a..4483dd7 100644 --- a/html.c +++ b/html.c @@ -821,11 +821,7 @@ xs_html *html_user_head(snac *user, const char *desc, const char *url) /* show metrics in og:description? */ if (xs_is_true(xs_dict_get(user->config, "show_contact_metrics"))) { - xs *fwers = follower_list(user); - xs *fwing = following_list(user); - - xs *s1 = xs_fmt(L("%d following, %d followers"), - xs_list_len(fwing), xs_list_len(fwers)); + xs *s1 = xs_fmt(L("%d following, %d followers"), following_list_len(user), follower_list_len(user)); s1 = xs_str_cat(s1, " ยท "); @@ -1166,11 +1162,7 @@ static xs_html *html_user_body(snac *user, int read_only) } if (xs_is_true(xs_dict_get(user->config, "show_contact_metrics"))) { - xs *fwers = follower_list(user); - xs *fwing = following_list(user); - - xs *s1 = xs_fmt(L("%d following, %d followers"), - xs_list_len(fwing), xs_list_len(fwers)); + xs *s1 = xs_fmt(L("%d following, %d followers"), following_list_len(user), follower_list_len(user)); xs_html_add(top_user, xs_html_tag("p", diff --git a/snac.h b/snac.h index 2ab9a67..256731f 100644 --- a/snac.h +++ b/snac.h @@ -153,6 +153,7 @@ int follower_add(snac *snac, const char *actor); int follower_del(snac *snac, const char *actor); int follower_check(snac *snac, const char *actor); xs_list *follower_list(snac *snac); +int follower_list_len(snac *snac); int pending_add(snac *user, const char *actor, const xs_dict *msg); int pending_check(snac *user, const char *actor); @@ -184,6 +185,7 @@ int following_del(snac *snac, const char *actor); int following_check(snac *snac, const char *actor); int following_get(snac *snac, const char *actor, xs_dict **data); xs_list *following_list(snac *snac); +int following_list_len(snac *snac); void mute(snac *snac, const char *actor); void unmute(snac *snac, const char *actor); -- cgit From 321f64ed70d039dea510c8a7d7aee7dc9577737a Mon Sep 17 00:00:00 2001 From: green Date: Mon, 19 May 2025 15:25:11 +0200 Subject: performance: use following_list_len in more places --- activitypub.c | 6 ++---- data.c | 2 +- mastoapi.c | 16 ++++++++-------- 3 files changed, 11 insertions(+), 13 deletions(-) (limited to 'data.c') diff --git a/activitypub.c b/activitypub.c index a7e133a..120b4a1 100644 --- a/activitypub.c +++ b/activitypub.c @@ -3204,8 +3204,7 @@ int activitypub_get_handler(const xs_dict *req, const char *q_path, int total = 0; if (show_contact_metrics) { - xs *l = follower_list(&snac); - total = xs_list_len(l); + total = follower_list_len(&snac); } xs *id = xs_fmt("%s/%s", snac.actor, p_path); @@ -3216,8 +3215,7 @@ int activitypub_get_handler(const xs_dict *req, const char *q_path, int total = 0; if (show_contact_metrics) { - xs *l = following_list(&snac); - total = xs_list_len(l); + total = following_list_len(&snac); } xs *id = xs_fmt("%s/%s", snac.actor, p_path); diff --git a/data.c b/data.c index 482a29c..f9d27f9 100644 --- a/data.c +++ b/data.c @@ -1216,7 +1216,7 @@ int follower_check(snac *snac, const char *actor) int follower_list_len(snac *snac) -/* returns the number followers */ +/* returns the number of followers */ { xs *list = object_user_cache_list(snac, "followers", XS_ALL, 0); return xs_list_len(list); diff --git a/mastoapi.c b/mastoapi.c index 7fa0078..a7d9c34 100644 --- a/mastoapi.c +++ b/mastoapi.c @@ -680,10 +680,10 @@ xs_dict *mastoapi_account(snac *logged, const xs_dict *actor) /* does this user want to publish their contact metrics? */ if (xs_is_true(xs_dict_get(user.config, "show_contact_metrics"))) { - xs *fwing = following_list(&user); - xs *fwers = follower_list(&user); - xs *ni = xs_number_new(xs_list_len(fwing)); - xs *ne = xs_number_new(xs_list_len(fwers)); + int fwing = following_list_len(&user); + int fwers = follower_list_len(&user); + xs *ni = xs_number_new(fwing); + xs *ne = xs_number_new(fwers); acct = xs_dict_append(acct, "followers_count", ne); acct = xs_dict_append(acct, "following_count", ni); @@ -1309,10 +1309,10 @@ void credentials_get(char **body, char **ctype, int *status, snac snac) /* does this user want to publish their contact metrics? */ if (xs_is_true(xs_dict_get(snac.config, "show_contact_metrics"))) { - xs *fwing = following_list(&snac); - xs *fwers = follower_list(&snac); - xs *ni = xs_number_new(xs_list_len(fwing)); - xs *ne = xs_number_new(xs_list_len(fwers)); + int fwing = following_list_len(&snac); + int fwers = follower_list_len(&snac); + xs *ni = xs_number_new(fwing); + xs *ne = xs_number_new(fwers); acct = xs_dict_append(acct, "followers_count", ne); acct = xs_dict_append(acct, "following_count", ni); -- cgit