From b77ef13d2cb9894a7b7814d47405b2c34b9f70f9 Mon Sep 17 00:00:00 2001 From: default Date: Wed, 7 Jun 2023 13:09:19 +0200 Subject: [PATCH] Don't enqueue a request_replies message if it's already there. --- data.c | 44 ++++++++++++++++++++++++++++++++++++++------ main.c | 2 +- snac.h | 1 + 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/data.c b/data.c index 8da0888..3a28989 100644 --- a/data.c +++ b/data.c @@ -1938,10 +1938,33 @@ void enqueue_close_question(snac *user, const char *id, int end_secs) void enqueue_request_replies(snac *user, const char *id) /* enqueues a request for the replies of a message */ { + /* test first if this precise request is already in the queue */ + xs *queue = user_queue(user); + xs_list *p = queue; + xs_str *v; + + while (xs_list_iter(&p, &v)) { + xs *q_item = queue_get(v); + + if (q_item != NULL) { + const char *type = xs_dict_get(q_item, "type"); + const char *msg = xs_dict_get(q_item, "message"); + + if (type && msg && strcmp(type, "request_replies") == 0 && strcmp(msg, id) == 0) { + /* don't requeue */ + snac_debug(user, 0, xs_fmt("enqueue_request_replies already here %s", id)); + return; + } + } + } + + /* not there; enqueue the request with a small delay */ xs *qmsg = _new_qmsg("request_replies", id, 0); - char *ntid = xs_dict_get(qmsg, "ntid"); + xs *ntid = tid(10); xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid); + qmsg = xs_dict_set(qmsg, "ntid", ntid); + qmsg = _enqueue_put(fn, qmsg); snac_debug(user, 0, xs_fmt("enqueue_request_replies %s", id)); @@ -2030,16 +2053,13 @@ xs_list *queue(void) } -xs_dict *dequeue(const char *fn) -/* dequeues a message */ +xs_dict *queue_get(const char *fn) +/* gets a file from a queue */ { FILE *f; xs_dict *obj = NULL; if ((f = fopen(fn, "r")) != NULL) { - /* delete right now */ - unlink(fn); - xs *j = xs_readall(f); obj = xs_json_loads(j); @@ -2050,6 +2070,18 @@ xs_dict *dequeue(const char *fn) } +xs_dict *dequeue(const char *fn) +/* dequeues a message */ +{ + xs_dict *obj = queue_get(fn); + + if (obj != NULL) + unlink(fn); + + return obj; +} + + /** the purge **/ static int _purge_file(const char *fn, time_t mt) diff --git a/main.c b/main.c index e8309c4..e2de58e 100644 --- a/main.c +++ b/main.c @@ -279,8 +279,8 @@ int main(int argc, char *argv[]) status = activitypub_request(&snac, url, &data); printf("status: %d\n", status); - if (valid_status(status)) { + if (data != NULL) { xs *j = xs_json_dumps_pp(data, 4); printf("%s\n", j); } diff --git a/snac.h b/snac.h index f3ed48c..adbd6c9 100644 --- a/snac.h +++ b/snac.h @@ -170,6 +170,7 @@ int was_question_voted(snac *user, const char *id); xs_list *user_queue(snac *snac); xs_list *queue(void); +xs_dict *queue_get(const char *fn); xs_dict *dequeue(const char *fn); void purge(snac *snac);