commit bcde97c

default  ·  2023-04-13 14:59:17 +0000 UTC
parent 2d5d635
New function notify_add().
4 files changed,  +42, -6
M data.c
M snac.h
+3, -0
 1@@ -935,6 +935,9 @@ void notify(snac *snac, xs_str *type, xs_str *utype, xs_str *actor, xs_dict *msg
 2 
 3     if (!xs_is_null(bot) && !xs_is_null(chat_id) && *bot && *chat_id)
 4         enqueue_telegram(body, bot, chat_id);
 5+
 6+    /* finally, store it in the notification folder */
 7+    notify_add(snac, type, utype, actor, objid);
 8 }
 9 
10 
M data.c
+35, -0
 1@@ -7,6 +7,7 @@
 2 #include "xs_openssl.h"
 3 #include "xs_glob.h"
 4 #include "xs_set.h"
 5+#include "xs_time.h"
 6 
 7 #include "snac.h"
 8 
 9@@ -1474,6 +1475,40 @@ xs_list *inbox_list(void)
10 }
11 
12 
13+/** notifications **/
14+
15+void notify_add(snac *snac, const char *type, const char *utype,
16+                const char *actor, const char *objid)
17+/* adds a new notification */
18+{
19+    xs *ntid = tid(0);
20+    xs *fn   = xs_fmt("%s/notify/", snac->basedir);
21+    xs *date = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
22+    FILE *f;
23+
24+    /* create the directory */
25+    mkdirx(fn);
26+    fn = xs_str_cat(fn, ntid);
27+    fn = xs_str_cat(fn, ".json");
28+
29+    xs *noti = xs_dict_new();
30+
31+    noti = xs_dict_append(noti, "id",    ntid);
32+    noti = xs_dict_append(noti, "type",  type);
33+    noti = xs_dict_append(noti, "utype", utype);
34+    noti = xs_dict_append(noti, "actor", actor);
35+    noti = xs_dict_append(noti, "objid", objid);
36+    noti = xs_dict_append(noti, "date",  date);
37+
38+    if ((f = fopen(fn, "w")) != NULL) {
39+        xs *j = xs_json_dumps_pp(noti, 4);
40+
41+        fwrite(j, strlen(j), 1, f);
42+        fclose(f);
43+    }
44+}
45+
46+
47 /** the queue **/
48 
49 static xs_dict *_enqueue_put(const char *fn, xs_dict *msg)
+0, -5
 1@@ -855,11 +855,6 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
 2     else
 3     if (strcmp(cmd, "/notifications") == 0) {
 4         /* TBD */
 5-        {
 6-            xs *j = xs_json_dumps_pp(args, 4);
 7-            printf("notification args:\n%s\n", j);
 8-        }
 9-
10         *body  = xs_dup("[]");
11         *ctype = "application/json";
12         status = 200;
M snac.h
+4, -1
 1@@ -1,7 +1,7 @@
 2 /* snac - A simple, minimalistic ActivityPub instance */
 3 /* copyright (c) 2022 - 2023 grunfink / MIT license */
 4 
 5-#define VERSION "2.27"
 6+#define VERSION "2.28-dev"
 7 
 8 #define USER_AGENT "snac/" VERSION
 9 
10@@ -137,6 +137,9 @@ d_char *history_list(snac *snac);
11 
12 void lastlog_write(snac *snac);
13 
14+void notify_add(snac *snac, const char *type, const char *utype,
15+                const char *actor, const char *objid);
16+
17 void inbox_add(const char *inbox);
18 void inbox_add_by_actor(const xs_dict *actor);
19 xs_list *inbox_list(void);