commit 1a27e67ed9e4c4b12d9ee4d151c7647043b54647
parent 211bedd4978c4711f0e2399b38ccb6e76e39856d
Author: default <nobody@localhost>
Date:   Thu, 13 Apr 2023 17:34:48 +0200
New functions notify_get() and notify_list().
Diffstat:
3 files changed, 60 insertions(+), 3 deletions(-)
diff --git a/data.c b/data.c
@@ -1537,6 +1537,55 @@ void notify_add(snac *snac, const char *type, const char *utype,
 }
 
 
+xs_dict *notify_get(snac *snac, const char *id)
+/* gets a notification */
+{
+    xs *fn = xs_fmt("%s/notify/%s.json", snac->basedir);
+    FILE *f;
+    xs_dict *out = NULL;
+
+    if ((f = fopen(fn, "r")) != NULL) {
+        xs *j = xs_readall(f);
+        fclose(f);
+
+        out = xs_json_loads(j);
+    }
+
+    return out;
+}
+
+
+xs_list *notify_list(snac *snac, int new_only)
+/* returns a list of notifications, optionally only the new ones */
+{
+    xs *t = NULL;
+
+    /* if only new ones are requested, get the last time */
+    if (new_only)
+        t = notify_check_time(snac, 0);
+
+    xs *spec     = xs_fmt("%s/notify/" "*.json", snac->basedir);
+    xs *lst      = xs_glob(spec, 1, 0);
+    xs_list *out = xs_list_new();
+    xs_list *p   = lst;
+    xs_str *v;
+
+    while (xs_list_iter(&p, &v)) {
+        xs *id = xs_replace(v, ".json", "");
+
+        /* old? */
+        if (t != NULL && strcmp(id, t) < 0)
+            continue;
+
+        xs *noti = notify_get(snac, id);
+
+        out = xs_list_append(out, noti);
+    }
+
+    return out;
+}
+
+
 /** the queue **/
 
 static xs_dict *_enqueue_put(const char *fn, xs_dict *msg)
diff --git a/mastoapi.c b/mastoapi.c
@@ -855,9 +855,15 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
     else
     if (strcmp(cmd, "/notifications") == 0) {
         /* TBD */
-        *body  = xs_dup("[]");
-        *ctype = "application/json";
-        status = 200;
+        if (logged_in) {
+            xs *l = notify_list(&snac1, 0);
+
+            *body  = xs_dup("[]");
+            *ctype = "application/json";
+            status = 200;
+        }
+        else
+            status = 401;
     }
     else
     if (strcmp(cmd, "/filters") == 0) {
diff --git a/snac.h b/snac.h
@@ -140,6 +140,8 @@ void lastlog_write(snac *snac);
 xs_str *notify_check_time(snac *snac, int reset);
 void notify_add(snac *snac, const char *type, const char *utype,
                 const char *actor, const char *objid);
+xs_dict *notify_get(snac *snac, const char *id);
+xs_list *notify_list(snac *snac, int new_only);
 
 void inbox_add(const char *inbox);
 void inbox_add_by_actor(const xs_dict *actor);