diff options
| author | 2023-04-13 17:34:48 +0200 | |
|---|---|---|
| committer | 2023-04-13 17:34:48 +0200 | |
| commit | 1a27e67ed9e4c4b12d9ee4d151c7647043b54647 (patch) | |
| tree | ee59fbe612da767a7f21db117d41b711b7a763ad /data.c | |
| parent | New function notify_check_time(). (diff) | |
| download | snac2-1a27e67ed9e4c4b12d9ee4d151c7647043b54647.tar.gz snac2-1a27e67ed9e4c4b12d9ee4d151c7647043b54647.tar.xz snac2-1a27e67ed9e4c4b12d9ee4d151c7647043b54647.zip | |
New functions notify_get() and notify_list().
Diffstat (limited to 'data.c')
| -rw-r--r-- | data.c | 49 |
1 files changed, 49 insertions, 0 deletions
| @@ -1537,6 +1537,55 @@ void notify_add(snac *snac, const char *type, const char *utype, | |||
| 1537 | } | 1537 | } |
| 1538 | 1538 | ||
| 1539 | 1539 | ||
| 1540 | xs_dict *notify_get(snac *snac, const char *id) | ||
| 1541 | /* gets a notification */ | ||
| 1542 | { | ||
| 1543 | xs *fn = xs_fmt("%s/notify/%s.json", snac->basedir); | ||
| 1544 | FILE *f; | ||
| 1545 | xs_dict *out = NULL; | ||
| 1546 | |||
| 1547 | if ((f = fopen(fn, "r")) != NULL) { | ||
| 1548 | xs *j = xs_readall(f); | ||
| 1549 | fclose(f); | ||
| 1550 | |||
| 1551 | out = xs_json_loads(j); | ||
| 1552 | } | ||
| 1553 | |||
| 1554 | return out; | ||
| 1555 | } | ||
| 1556 | |||
| 1557 | |||
| 1558 | xs_list *notify_list(snac *snac, int new_only) | ||
| 1559 | /* returns a list of notifications, optionally only the new ones */ | ||
| 1560 | { | ||
| 1561 | xs *t = NULL; | ||
| 1562 | |||
| 1563 | /* if only new ones are requested, get the last time */ | ||
| 1564 | if (new_only) | ||
| 1565 | t = notify_check_time(snac, 0); | ||
| 1566 | |||
| 1567 | xs *spec = xs_fmt("%s/notify/" "*.json", snac->basedir); | ||
| 1568 | xs *lst = xs_glob(spec, 1, 0); | ||
| 1569 | xs_list *out = xs_list_new(); | ||
| 1570 | xs_list *p = lst; | ||
| 1571 | xs_str *v; | ||
| 1572 | |||
| 1573 | while (xs_list_iter(&p, &v)) { | ||
| 1574 | xs *id = xs_replace(v, ".json", ""); | ||
| 1575 | |||
| 1576 | /* old? */ | ||
| 1577 | if (t != NULL && strcmp(id, t) < 0) | ||
| 1578 | continue; | ||
| 1579 | |||
| 1580 | xs *noti = notify_get(snac, id); | ||
| 1581 | |||
| 1582 | out = xs_list_append(out, noti); | ||
| 1583 | } | ||
| 1584 | |||
| 1585 | return out; | ||
| 1586 | } | ||
| 1587 | |||
| 1588 | |||
| 1540 | /** the queue **/ | 1589 | /** the queue **/ |
| 1541 | 1590 | ||
| 1542 | static xs_dict *_enqueue_put(const char *fn, xs_dict *msg) | 1591 | static xs_dict *_enqueue_put(const char *fn, xs_dict *msg) |