Upgraded followers to the new db.

This commit is contained in:
default 2022-11-28 10:46:42 +01:00
parent 9c136dacdf
commit 07fec71273
5 changed files with 92 additions and 71 deletions

View file

@ -192,12 +192,10 @@ d_char *recipient_list(snac *snac, char *msg, int expand_public)
if (expand_public && strcmp(v, public_address) == 0) { if (expand_public && strcmp(v, public_address) == 0) {
/* iterate the followers and add them */ /* iterate the followers and add them */
xs *fwers = follower_list(snac); xs *fwers = follower_list(snac);
char *fw; char *actor;
char *p = fwers; char *p = fwers;
while (xs_list_iter(&p, &fw)) { while (xs_list_iter(&p, &actor)) {
char *actor = xs_dict_get(fw, "actor");
if (xs_list_in(list, actor) == -1) if (xs_list_in(list, actor) == -1)
list = xs_list_append(list, actor); 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); 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)); snac_log(snac, xs_fmt("New follower %s", actor));
do_notify = 1; do_notify = 1;

97
data.c
View file

@ -15,7 +15,7 @@
#include <sys/file.h> #include <sys/file.h>
#include <fcntl.h> #include <fcntl.h>
double db_layout = 2.4; double db_layout = 2.5;
int db_upgrade(d_char **error); 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 **/ /** specialized functions **/
d_char *_follower_fn(snac *snac, char *actor) /** followers **/
{
xs *md5 = xs_md5_hex(actor, strlen(actor));
return xs_fmt("%s/followers/%s.json", snac->basedir, md5);
}
int follower_add(snac *snac, const char *actor)
int follower_add(snac *snac, char *actor, char *msg)
/* adds a follower */ /* adds a follower */
{ {
int ret = 201; /* created */ int status = object_user_cache_add(snac, actor, "followers");
xs *fn = _follower_fn(snac, actor);
FILE *f;
if ((f = fopen(fn, "w")) != NULL) { snac_debug(snac, 2, xs_fmt("follower_add %s %s", actor));
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));
return status; 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 */ /* checks if someone is a follower */
{ {
xs *fn = _follower_fn(snac, actor); return object_user_cache_in(snac, actor, "followers");
return !!(mtime(fn) != 0.0);
} }
d_char *follower_list(snac *snac) d_char *follower_list(snac *snac)
/* returns the list of followers */ /* returns the list of followers */
{ {
xs *spec = xs_fmt("%s/followers/" "*.json", snac->basedir); xs *list = object_user_cache_list(snac, "followers", XS_ALL);
xs *glist = xs_glob(spec, 0, 0); d_char *fwers = xs_list_new();
char *p, *v; char *p, *v;
d_char *list = xs_list_new();
/* iterate the list of files */ /* resolve the list of md5 to be a list of actors */
p = glist; p = list;
while (xs_list_iter(&p, &v)) { while (xs_list_iter(&p, &v)) {
FILE *f; xs *a_obj = NULL;
/* load the follower data */ if (valid_status(object_get_by_md5(v, &a_obj, NULL))) {
if ((f = fopen(v, "r")) != NULL) { char *actor = xs_dict_get(a_obj, "id");
xs *j = xs_readall(f);
fclose(f);
if (j != NULL) { if (!xs_is_null(actor))
xs *o = xs_json_loads(j); fwers = xs_list_append(fwers, actor);
if (o != NULL)
list = xs_list_append(list, o);
}
} }
} }
return list; return fwers;
} }
/** timeline **/
double timeline_mtime(snac *snac) double timeline_mtime(snac *snac)
{ {
xs *fn = xs_fmt("%s/timeline", snac->basedir); xs *fn = xs_fmt("%s/timeline", snac->basedir);

7
html.c
View file

@ -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 *s = xs_str_new(NULL);
xs *h = xs_fmt("<h2>%s</h2>\n", header); xs *h = xs_fmt("<h2>%s</h2>\n", header);
char *p, *v; char *p, *actor_id;
s = xs_str_cat(s, h); s = xs_str_cat(s, h);
p = list; p = list;
while (xs_list_iter(&p, &v)) { while (xs_list_iter(&p, &actor_id)) {
char *actor_id = xs_dict_get(v, "actor");
xs *md5 = xs_md5_hex(actor_id, strlen(actor_id)); xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
xs *actor = NULL; xs *actor = NULL;
@ -939,7 +938,7 @@ d_char *html_people(snac *snac)
s = html_user_header(snac, s, 0); 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"); s = html_people_list(snac, s, wers, L("People that follows you"), "e");

6
snac.h
View file

@ -62,9 +62,9 @@ d_char *index_list_desc(const char *fn, int max);
int object_del(const char *id); int object_del(const char *id);
int object_del_if_unref(const char *id); int object_del_if_unref(const char *id);
int follower_add(snac *snac, char *actor, char *msg); int follower_add(snac *snac, const char *actor);
int follower_del(snac *snac, char *actor); int follower_del(snac *snac, const char *actor);
int follower_check(snac *snac, char *actor); int follower_check(snac *snac, const char *actor);
d_char *follower_list(snac *snac); d_char *follower_list(snac *snac);
double timeline_mtime(snac *snac); double timeline_mtime(snac *snac);

View file

@ -128,6 +128,51 @@ int db_upgrade(d_char **error)
nf = 2.4; 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) { if (f < nf) {
f = nf; f = nf;