From 4f4f321a535126c5f9602a152a5b9e395e8f918e Mon Sep 17 00:00:00 2001 From: default Date: Thu, 22 Sep 2022 18:50:39 +0200 Subject: Added some actor functions. --- data.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) (limited to 'data.c') diff --git a/data.c b/data.c index c5f68de..8a5b4a1 100644 --- a/data.c +++ b/data.c @@ -636,6 +636,83 @@ void enqueue_output(snac *snac, char *actor, char *msg, int retries) } +d_char *_actor_fn(snac *snac, char *actor) +/* returns the file name for an actor */ +{ + xs *md5 = xs_md5_hex(actor, strlen(actor)); + return xs_fmt("%s/actors/%s.json", snac->basedir, md5); +} + + +int actor_add(snac *snac, char *actor, char *msg) +/* adds a follower */ +{ + int ret = 201; /* created */ + xs *fn = _actor_fn(snac, actor); + FILE *f; + + 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("actor_add %s %s", actor, fn)); + + return ret; +} + + +int actor_get(snac *snac, char *actor, char **data) +/* returns an already downloaded actor */ +{ + xs *fn = _actor_fn(snac, actor); + float t; + float max_time; + int status; + FILE *f; + + t = mtime(fn); + + /* no mtime? there is nothing here */ + if (t == 0.0) + return 404; + + /* maximum time for the actor data to be considered stale */ + max_time = 3600.0 * 36.0; + + if (t + max_time < (float) time(NULL)) { + /* actor data exists but also stinks */ + status = 202; + + if ((f = fopen(fn, "a")) != NULL) { + /* write a blank at the end to 'touch' the file */ + fwrite(" ", 1, 1, f); + fclose(f); + } + } + else { + /* it's still valid */ + status = 200; + } + + if ((f = fopen(fn, "r")) != NULL) { + xs *j = xs_readall(f); + + fclose(f); + + *data = xs_json_loads(j); + } + else + status = 500; + + return status; +} + + d_char *queue(snac *snac) /* returns a list with filenames that can be dequeued */ { -- cgit v1.2.3