commit 85bf347
default
·
2024-02-05 13:53:45 +0000 UTC
parent 3f2c2a9
Added a notification index.
1 files changed,
+66,
-23
M
data.c
M
data.c
+66,
-23
1@@ -2045,13 +2045,33 @@ void notify_add(snac *snac, const char *type, const char *utype,
2 xs_json_dump(noti, 4, f);
3 fclose(f);
4 }
5+
6+ /* add it to the index if it already exists */
7+ xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
8+
9+ if (mtime(idx) != 0.0) {
10+ pthread_mutex_lock(&data_mutex);
11+
12+ if ((f = fopen(idx, "a")) != NULL) {
13+ fprintf(f, "%-32s\n", ntid);
14+ fclose(f);
15+ }
16+
17+ pthread_mutex_unlock(&data_mutex);
18+ }
19 }
20
21
22 xs_dict *notify_get(snac *snac, const char *id)
23 /* gets a notification */
24 {
25- xs *fn = xs_fmt("%s/notify/%s.json", snac->basedir, id);
26+ /* base file */
27+ xs *fn = xs_fmt("%s/notify/%s", snac->basedir, id);
28+
29+ /* strip spaces and add extension */
30+ fn = xs_strip_i(fn);
31+ fn = xs_str_cat(fn, ".json");
32+
33 FILE *f;
34 xs_dict *out = NULL;
35
36@@ -2064,19 +2084,53 @@ xs_dict *notify_get(snac *snac, const char *id)
37 }
38
39
40+xs_list *notify_list(snac *snac)
41+/* returns a list of notification ids */
42+{
43+ xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
44+
45+ if (mtime(idx) == 0.0) {
46+ /* create the index from scratch */
47+ FILE *f;
48+
49+ pthread_mutex_lock(&data_mutex);
50+
51+ if ((f = fopen(idx, "w")) != NULL) {
52+ xs *spec = xs_fmt("%s/notify/" "*.json", snac->basedir);
53+ xs *lst = xs_glob(spec, 1, 0);
54+ xs_list *p = lst;
55+ char *v;
56+
57+ while (xs_list_iter(&p, &v)) {
58+ char *p = strrchr(v, '.');
59+ if (p) {
60+ *p = '\0';
61+ fprintf(f, "%-32s\n", v);
62+ }
63+ }
64+
65+ fclose(f);
66+ }
67+
68+ pthread_mutex_unlock(&data_mutex);
69+ }
70+
71+ return index_list_desc(idx, 0, 64);
72+}
73+
74+
75 int notify_new_num(snac *snac)
76 /* counts the number of new notifications */
77 {
78 xs *t = notify_check_time(snac, 0);
79- xs *spec = xs_fmt("%s/notify/" "*.json", snac->basedir);
80- xs *lst = xs_glob(spec, 1, 1);
81+ xs *lst = notify_list(snac);
82 int cnt = 0;
83
84 xs_list *p = lst;
85 xs_str *v;
86
87 while (xs_list_iter(&p, &v)) {
88- xs *id = xs_replace(v, ".json", "");
89+ xs *id = xs_strip_i(xs_dup(v));
90
91 /* old? count no more */
92 if (strcmp(id, t) < 0)
93@@ -2089,25 +2143,6 @@ int notify_new_num(snac *snac)
94 }
95
96
97-xs_list *notify_list(snac *snac)
98-/* returns a list of notification ids, optionally only the new ones */
99-{
100- xs *spec = xs_fmt("%s/notify/" "*.json", snac->basedir);
101- xs *lst = xs_glob(spec, 1, 1);
102- xs_list *out = xs_list_new();
103- xs_list *p = lst;
104- xs_str *v;
105-
106- while (xs_list_iter(&p, &v)) {
107- xs *id = xs_replace(v, ".json", "");
108-
109- out = xs_list_append(out, id);
110- }
111-
112- return out;
113-}
114-
115-
116 void notify_clear(snac *snac)
117 /* clears all notifications */
118 {
119@@ -2118,6 +2153,14 @@ void notify_clear(snac *snac)
120
121 while (xs_list_iter(&p, &v))
122 unlink(v);
123+
124+ xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
125+
126+ if (mtime(idx) != 0.0) {
127+ pthread_mutex_lock(&data_mutex);
128+ truncate(idx, 0);
129+ pthread_mutex_unlock(&data_mutex);
130+ }
131 }
132
133