diff options
| author | 2024-02-05 14:53:45 +0100 | |
|---|---|---|
| committer | 2024-02-05 14:53:45 +0100 | |
| commit | 85bf3472b79e73428b0394e75fc41b35109a6cbb (patch) | |
| tree | 6abd1408abf468891ed69511dd961768978aa40b | |
| parent | Updated RELEASE_NOTES. (diff) | |
| download | snac2-85bf3472b79e73428b0394e75fc41b35109a6cbb.tar.gz snac2-85bf3472b79e73428b0394e75fc41b35109a6cbb.tar.xz snac2-85bf3472b79e73428b0394e75fc41b35109a6cbb.zip | |
Added a notification index.
| -rw-r--r-- | data.c | 89 |
1 files changed, 66 insertions, 23 deletions
| @@ -2045,13 +2045,33 @@ void notify_add(snac *snac, const char *type, const char *utype, | |||
| 2045 | xs_json_dump(noti, 4, f); | 2045 | xs_json_dump(noti, 4, f); |
| 2046 | fclose(f); | 2046 | fclose(f); |
| 2047 | } | 2047 | } |
| 2048 | |||
| 2049 | /* add it to the index if it already exists */ | ||
| 2050 | xs *idx = xs_fmt("%s/notify.idx", snac->basedir); | ||
| 2051 | |||
| 2052 | if (mtime(idx) != 0.0) { | ||
| 2053 | pthread_mutex_lock(&data_mutex); | ||
| 2054 | |||
| 2055 | if ((f = fopen(idx, "a")) != NULL) { | ||
| 2056 | fprintf(f, "%-32s\n", ntid); | ||
| 2057 | fclose(f); | ||
| 2058 | } | ||
| 2059 | |||
| 2060 | pthread_mutex_unlock(&data_mutex); | ||
| 2061 | } | ||
| 2048 | } | 2062 | } |
| 2049 | 2063 | ||
| 2050 | 2064 | ||
| 2051 | xs_dict *notify_get(snac *snac, const char *id) | 2065 | xs_dict *notify_get(snac *snac, const char *id) |
| 2052 | /* gets a notification */ | 2066 | /* gets a notification */ |
| 2053 | { | 2067 | { |
| 2054 | xs *fn = xs_fmt("%s/notify/%s.json", snac->basedir, id); | 2068 | /* base file */ |
| 2069 | xs *fn = xs_fmt("%s/notify/%s", snac->basedir, id); | ||
| 2070 | |||
| 2071 | /* strip spaces and add extension */ | ||
| 2072 | fn = xs_strip_i(fn); | ||
| 2073 | fn = xs_str_cat(fn, ".json"); | ||
| 2074 | |||
| 2055 | FILE *f; | 2075 | FILE *f; |
| 2056 | xs_dict *out = NULL; | 2076 | xs_dict *out = NULL; |
| 2057 | 2077 | ||
| @@ -2064,19 +2084,53 @@ xs_dict *notify_get(snac *snac, const char *id) | |||
| 2064 | } | 2084 | } |
| 2065 | 2085 | ||
| 2066 | 2086 | ||
| 2087 | xs_list *notify_list(snac *snac) | ||
| 2088 | /* returns a list of notification ids */ | ||
| 2089 | { | ||
| 2090 | xs *idx = xs_fmt("%s/notify.idx", snac->basedir); | ||
| 2091 | |||
| 2092 | if (mtime(idx) == 0.0) { | ||
| 2093 | /* create the index from scratch */ | ||
| 2094 | FILE *f; | ||
| 2095 | |||
| 2096 | pthread_mutex_lock(&data_mutex); | ||
| 2097 | |||
| 2098 | if ((f = fopen(idx, "w")) != NULL) { | ||
| 2099 | xs *spec = xs_fmt("%s/notify/" "*.json", snac->basedir); | ||
| 2100 | xs *lst = xs_glob(spec, 1, 0); | ||
| 2101 | xs_list *p = lst; | ||
| 2102 | char *v; | ||
| 2103 | |||
| 2104 | while (xs_list_iter(&p, &v)) { | ||
| 2105 | char *p = strrchr(v, '.'); | ||
| 2106 | if (p) { | ||
| 2107 | *p = '\0'; | ||
| 2108 | fprintf(f, "%-32s\n", v); | ||
| 2109 | } | ||
| 2110 | } | ||
| 2111 | |||
| 2112 | fclose(f); | ||
| 2113 | } | ||
| 2114 | |||
| 2115 | pthread_mutex_unlock(&data_mutex); | ||
| 2116 | } | ||
| 2117 | |||
| 2118 | return index_list_desc(idx, 0, 64); | ||
| 2119 | } | ||
| 2120 | |||
| 2121 | |||
| 2067 | int notify_new_num(snac *snac) | 2122 | int notify_new_num(snac *snac) |
| 2068 | /* counts the number of new notifications */ | 2123 | /* counts the number of new notifications */ |
| 2069 | { | 2124 | { |
| 2070 | xs *t = notify_check_time(snac, 0); | 2125 | xs *t = notify_check_time(snac, 0); |
| 2071 | xs *spec = xs_fmt("%s/notify/" "*.json", snac->basedir); | 2126 | xs *lst = notify_list(snac); |
| 2072 | xs *lst = xs_glob(spec, 1, 1); | ||
| 2073 | int cnt = 0; | 2127 | int cnt = 0; |
| 2074 | 2128 | ||
| 2075 | xs_list *p = lst; | 2129 | xs_list *p = lst; |
| 2076 | xs_str *v; | 2130 | xs_str *v; |
| 2077 | 2131 | ||
| 2078 | while (xs_list_iter(&p, &v)) { | 2132 | while (xs_list_iter(&p, &v)) { |
| 2079 | xs *id = xs_replace(v, ".json", ""); | 2133 | xs *id = xs_strip_i(xs_dup(v)); |
| 2080 | 2134 | ||
| 2081 | /* old? count no more */ | 2135 | /* old? count no more */ |
| 2082 | if (strcmp(id, t) < 0) | 2136 | if (strcmp(id, t) < 0) |
| @@ -2089,25 +2143,6 @@ int notify_new_num(snac *snac) | |||
| 2089 | } | 2143 | } |
| 2090 | 2144 | ||
| 2091 | 2145 | ||
| 2092 | xs_list *notify_list(snac *snac) | ||
| 2093 | /* returns a list of notification ids, optionally only the new ones */ | ||
| 2094 | { | ||
| 2095 | xs *spec = xs_fmt("%s/notify/" "*.json", snac->basedir); | ||
| 2096 | xs *lst = xs_glob(spec, 1, 1); | ||
| 2097 | xs_list *out = xs_list_new(); | ||
| 2098 | xs_list *p = lst; | ||
| 2099 | xs_str *v; | ||
| 2100 | |||
| 2101 | while (xs_list_iter(&p, &v)) { | ||
| 2102 | xs *id = xs_replace(v, ".json", ""); | ||
| 2103 | |||
| 2104 | out = xs_list_append(out, id); | ||
| 2105 | } | ||
| 2106 | |||
| 2107 | return out; | ||
| 2108 | } | ||
| 2109 | |||
| 2110 | |||
| 2111 | void notify_clear(snac *snac) | 2146 | void notify_clear(snac *snac) |
| 2112 | /* clears all notifications */ | 2147 | /* clears all notifications */ |
| 2113 | { | 2148 | { |
| @@ -2118,6 +2153,14 @@ void notify_clear(snac *snac) | |||
| 2118 | 2153 | ||
| 2119 | while (xs_list_iter(&p, &v)) | 2154 | while (xs_list_iter(&p, &v)) |
| 2120 | unlink(v); | 2155 | unlink(v); |
| 2156 | |||
| 2157 | xs *idx = xs_fmt("%s/notify.idx", snac->basedir); | ||
| 2158 | |||
| 2159 | if (mtime(idx) != 0.0) { | ||
| 2160 | pthread_mutex_lock(&data_mutex); | ||
| 2161 | truncate(idx, 0); | ||
| 2162 | pthread_mutex_unlock(&data_mutex); | ||
| 2163 | } | ||
| 2121 | } | 2164 | } |
| 2122 | 2165 | ||
| 2123 | 2166 | ||