commit 1a27e67

default  ·  2023-04-13 15:34:48 +0000 UTC
parent 211bedd
New functions notify_get() and notify_list().
3 files changed,  +60, -3
M data.c
M snac.h
M data.c
+49, -0
 1@@ -1537,6 +1537,55 @@ void notify_add(snac *snac, const char *type, const char *utype,
 2 }
 3 
 4 
 5+xs_dict *notify_get(snac *snac, const char *id)
 6+/* gets a notification */
 7+{
 8+    xs *fn = xs_fmt("%s/notify/%s.json", snac->basedir);
 9+    FILE *f;
10+    xs_dict *out = NULL;
11+
12+    if ((f = fopen(fn, "r")) != NULL) {
13+        xs *j = xs_readall(f);
14+        fclose(f);
15+
16+        out = xs_json_loads(j);
17+    }
18+
19+    return out;
20+}
21+
22+
23+xs_list *notify_list(snac *snac, int new_only)
24+/* returns a list of notifications, optionally only the new ones */
25+{
26+    xs *t = NULL;
27+
28+    /* if only new ones are requested, get the last time */
29+    if (new_only)
30+        t = notify_check_time(snac, 0);
31+
32+    xs *spec     = xs_fmt("%s/notify/" "*.json", snac->basedir);
33+    xs *lst      = xs_glob(spec, 1, 0);
34+    xs_list *out = xs_list_new();
35+    xs_list *p   = lst;
36+    xs_str *v;
37+
38+    while (xs_list_iter(&p, &v)) {
39+        xs *id = xs_replace(v, ".json", "");
40+
41+        /* old? */
42+        if (t != NULL && strcmp(id, t) < 0)
43+            continue;
44+
45+        xs *noti = notify_get(snac, id);
46+
47+        out = xs_list_append(out, noti);
48+    }
49+
50+    return out;
51+}
52+
53+
54 /** the queue **/
55 
56 static xs_dict *_enqueue_put(const char *fn, xs_dict *msg)
+9, -3
 1@@ -855,9 +855,15 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
 2     else
 3     if (strcmp(cmd, "/notifications") == 0) {
 4         /* TBD */
 5-        *body  = xs_dup("[]");
 6-        *ctype = "application/json";
 7-        status = 200;
 8+        if (logged_in) {
 9+            xs *l = notify_list(&snac1, 0);
10+
11+            *body  = xs_dup("[]");
12+            *ctype = "application/json";
13+            status = 200;
14+        }
15+        else
16+            status = 401;
17     }
18     else
19     if (strcmp(cmd, "/filters") == 0) {
M snac.h
+2, -0
1@@ -140,6 +140,8 @@ void lastlog_write(snac *snac);
2 xs_str *notify_check_time(snac *snac, int reset);
3 void notify_add(snac *snac, const char *type, const char *utype,
4                 const char *actor, const char *objid);
5+xs_dict *notify_get(snac *snac, const char *id);
6+xs_list *notify_list(snac *snac, int new_only);
7 
8 void inbox_add(const char *inbox);
9 void inbox_add_by_actor(const xs_dict *actor);