snac2

Fork of https://codeberg.org/grunfink/snac2
git clone https://git.inz.fi/snac2
Log | Files | Refs | README | LICENSE

commit 732654e73a5df0c9248726df7cf7b9c54ebfce15
parent acff91e0adec8a9a80412e376e77bed392e333c4
Author: default <nobody@localhost>
Date:   Tue, 20 Sep 2022 10:02:00 +0200

Added some timeline functions.

Diffstat:
Mdata.c | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msnac.h | 3+++
2 files changed, 63 insertions(+), 0 deletions(-)

diff --git a/data.c b/data.c @@ -267,3 +267,63 @@ d_char *follower_list(snac *snac) return list; } + + +d_char *_timeline_fn(snac *snac, char *id) +/* returns the file name of a timeline entry by its id */ +{ + xs *md5 = xs_md5_hex(id, strlen(id)); + xs *spec = xs_fmt("%s/timeline/" "*-%s.json", snac->basedir, md5); + glob_t globbuf; + d_char *fn = NULL; + + if (glob(spec, 0, NULL, &globbuf) == 0 && globbuf.gl_matchc) { + /* get just the first file */ + fn = xs_str_new(globbuf.gl_pathv[0]); + } + + globfree(&globbuf); + + return fn; +} + + +d_char *timeline_get(snac *snac, char *id) +/* gets a message from the timeline */ +{ + xs *fn = _timeline_fn(snac, id); + xs *msg = NULL; + + if (fn != NULL) { + FILE *f; + + if ((f = fopen(fn, "r")) != NULL) { + xs *j = xs_readall(f); + + msg = xs_json_loads(j); + fclose(f); + } + } + + return msg; +} + + +void timeline_del(snac *snac, char *id) +/* deletes a message from the timeline */ +{ + xs *fn = _timeline_fn(snac, id); + + if (fn != NULL) { + xs *lfn = NULL; + + unlink(fn); + snac_debug(snac, 1, xs_fmt("timeline_del %s", id)); + + /* try to delete also from the local timeline */ + lfn = xs_replace(fn, "/timeline/", "/local/"); + + if (unlink(lfn) != -1) + snac_debug(snac, 1, xs_fmt("timeline_del (local) %s", id)); + } +} diff --git a/snac.h b/snac.h @@ -44,3 +44,6 @@ int follower_add(snac *snac, char *actor, char *msg); int follower_del(snac *snac, char *actor); int follower_check(snac *snac, char *actor); d_char *follower_list(snac *snac); + +d_char *timeline_get(snac *snac, char *id); +void timeline_del(snac *snac, char *id);