From 07fec7127326cf02d50bd148581908d51cc697f6 Mon Sep 17 00:00:00 2001 From: default Date: Mon, 28 Nov 2022 10:46:42 +0100 Subject: [PATCH] Upgraded followers to the new db. --- activitypub.c | 8 ++--- data.c | 97 ++++++++++++++++++++------------------------------- html.c | 7 ++-- snac.h | 6 ++-- upgrade.c | 45 ++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 71 deletions(-) diff --git a/activitypub.c b/activitypub.c index 7360af1..d22261d 100644 --- a/activitypub.c +++ b/activitypub.c @@ -192,12 +192,10 @@ d_char *recipient_list(snac *snac, char *msg, int expand_public) if (expand_public && strcmp(v, public_address) == 0) { /* iterate the followers and add them */ xs *fwers = follower_list(snac); - char *fw; + char *actor; char *p = fwers; - while (xs_list_iter(&p, &fw)) { - char *actor = xs_dict_get(fw, "actor"); - + while (xs_list_iter(&p, &actor)) { if (xs_list_in(list, actor) == -1) list = xs_list_append(list, actor); } @@ -832,7 +830,7 @@ int process_message(snac *snac, char *msg, char *req) timeline_add(snac, xs_dict_get(f_msg, "id"), f_msg, NULL, NULL); - follower_add(snac, actor, f_msg); + follower_add(snac, actor); snac_log(snac, xs_fmt("New follower %s", actor)); do_notify = 1; diff --git a/data.c b/data.c index b3f124e..1b9be2d 100644 --- a/data.c +++ b/data.c @@ -15,7 +15,7 @@ #include #include -double db_layout = 2.4; +double db_layout = 2.5; int db_upgrade(d_char **error); @@ -641,94 +641,73 @@ int object_user_cache_in(snac *snac, const char *id, const char *cachedir) } +d_char *object_user_cache_list(snac *snac, const char *cachedir, int max) +/* returns the objects in a cache as a list */ +{ + xs *idx = xs_fmt("%s/%s.idx", snac->basedir, cachedir); + return index_list(idx, max); +} + + /** specialized functions **/ -d_char *_follower_fn(snac *snac, char *actor) -{ - xs *md5 = xs_md5_hex(actor, strlen(actor)); - return xs_fmt("%s/followers/%s.json", snac->basedir, md5); -} +/** followers **/ - -int follower_add(snac *snac, char *actor, char *msg) +int follower_add(snac *snac, const char *actor) /* adds a follower */ { - int ret = 201; /* created */ - xs *fn = _follower_fn(snac, actor); - FILE *f; + int status = object_user_cache_add(snac, actor, "followers"); - if ((f = fopen(fn, "w")) != NULL) { - xs *j = xs_json_dumps_pp(msg, 4); - - fwrite(j, 1, strlen(j), f); - fclose(f); - } - else - ret = 500; - - snac_debug(snac, 2, xs_fmt("follower_add %s %s", actor, fn)); - - return ret; -} - - -int follower_del(snac *snac, char *actor) -/* deletes a follower */ -{ - int status = 200; - xs *fn = _follower_fn(snac, actor); - - if (fn != NULL) - unlink(fn); - else - status = 404; - - snac_debug(snac, 2, xs_fmt("follower_del %s %s", actor, fn)); + snac_debug(snac, 2, xs_fmt("follower_add %s %s", actor)); return status; } -int follower_check(snac *snac, char *actor) +int follower_del(snac *snac, const char *actor) +/* deletes a follower */ +{ + int status = object_user_cache_del(snac, actor, "followers"); + + snac_debug(snac, 2, xs_fmt("follower_del %s %s", actor)); + + return status; +} + + +int follower_check(snac *snac, const char *actor) /* checks if someone is a follower */ { - xs *fn = _follower_fn(snac, actor); - - return !!(mtime(fn) != 0.0); + return object_user_cache_in(snac, actor, "followers"); } d_char *follower_list(snac *snac) /* returns the list of followers */ { - xs *spec = xs_fmt("%s/followers/" "*.json", snac->basedir); - xs *glist = xs_glob(spec, 0, 0); + xs *list = object_user_cache_list(snac, "followers", XS_ALL); + d_char *fwers = xs_list_new(); char *p, *v; - d_char *list = xs_list_new(); - /* iterate the list of files */ - p = glist; + /* resolve the list of md5 to be a list of actors */ + p = list; while (xs_list_iter(&p, &v)) { - FILE *f; + xs *a_obj = NULL; - /* load the follower data */ - if ((f = fopen(v, "r")) != NULL) { - xs *j = xs_readall(f); - fclose(f); + if (valid_status(object_get_by_md5(v, &a_obj, NULL))) { + char *actor = xs_dict_get(a_obj, "id"); - if (j != NULL) { - xs *o = xs_json_loads(j); - - if (o != NULL) - list = xs_list_append(list, o); - } + if (!xs_is_null(actor)) + fwers = xs_list_append(fwers, actor); } } - return list; + return fwers; } +/** timeline **/ + double timeline_mtime(snac *snac) { xs *fn = xs_fmt("%s/timeline", snac->basedir); diff --git a/html.c b/html.c index b854717..ea154dd 100644 --- a/html.c +++ b/html.c @@ -840,13 +840,12 @@ d_char *html_people_list(snac *snac, d_char *os, d_char *list, const char *heade { xs *s = xs_str_new(NULL); xs *h = xs_fmt("

%s

\n", header); - char *p, *v; + char *p, *actor_id; s = xs_str_cat(s, h); p = list; - while (xs_list_iter(&p, &v)) { - char *actor_id = xs_dict_get(v, "actor"); + while (xs_list_iter(&p, &actor_id)) { xs *md5 = xs_md5_hex(actor_id, strlen(actor_id)); xs *actor = NULL; @@ -939,7 +938,7 @@ d_char *html_people(snac *snac) s = html_user_header(snac, s, 0); - s = html_people_list(snac, s, wing, L("People you follow"), "i"); +// s = html_people_list(snac, s, wing, L("People you follow"), "i"); s = html_people_list(snac, s, wers, L("People that follows you"), "e"); diff --git a/snac.h b/snac.h index 5abf2d1..b8b5215 100644 --- a/snac.h +++ b/snac.h @@ -62,9 +62,9 @@ d_char *index_list_desc(const char *fn, int max); int object_del(const char *id); int object_del_if_unref(const char *id); -int follower_add(snac *snac, char *actor, char *msg); -int follower_del(snac *snac, char *actor); -int follower_check(snac *snac, char *actor); +int follower_add(snac *snac, const char *actor); +int follower_del(snac *snac, const char *actor); +int follower_check(snac *snac, const char *actor); d_char *follower_list(snac *snac); double timeline_mtime(snac *snac); diff --git a/upgrade.c b/upgrade.c index 786e0f9..3e6351a 100644 --- a/upgrade.c +++ b/upgrade.c @@ -128,6 +128,51 @@ int db_upgrade(d_char **error) nf = 2.4; } + else + if (f < 2.5) { + /* upgrade followers */ + xs *users = user_list(); + char *p, *v; + + xs_debug(); + + p = users; + while (xs_list_iter(&p, &v)) { + snac snac; + + if (user_open(&snac, v)) { + xs *spec = xs_fmt("%s/followers/" "*.json", snac.basedir); + xs *dir = xs_glob(spec, 0, 0); + char *p, *v; + + p = dir; + while (xs_list_iter(&p, &v)) { + FILE *f; + + if ((f = fopen(v, "r")) != NULL) { + xs *s = xs_readall(f); + xs *o = xs_json_loads(s); + fclose(f); + + char *type = xs_dict_get(o, "type"); + + if (!xs_is_null(type) && strcmp(type, "Follow") == 0) { + unlink(v); + + char *actor = xs_dict_get(o, "actor"); + + if (!xs_is_null(actor)) + follower_add(&snac, actor); + } + } + } + + user_free(&snac); + } + } + + nf = 2.5; + } if (f < nf) { f = nf;