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(-) 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 v1.2.3 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(-) 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 v1.2.3