commit 2609bb0

grunfink  ·  2026-10-02 07:16:55 +0000 UTC
parent f072461
Moved rfctime() and marker functions to mastoapi.c, where they really belong.
4 files changed,  +94, -95
M data.c
M snac.c
M snac.h
M data.c
+0, -76
 1@@ -3474,82 +3474,6 @@ xs_str *notify_check_time(snac *snac, int reset)
 2     return t;
 3 }
 4 
 5-xs_dict *markers_get(snac *snac, const xs_list *markers)
 6-{
 7-    xs *data = NULL;
 8-    xs_dict *returns = xs_dict_new();
 9-    xs *fn = xs_fmt("%s/markers.json", snac->basedir);
10-    const xs_str *v = NULL;
11-    FILE *f;
12-
13-    if ((f = fopen(fn, "r")) != NULL) {
14-        data = xs_json_load(f);
15-        fclose(f);
16-    }
17-
18-    if (xs_is_null(data))
19-        data = xs_dict_new();
20-
21-    xs_list_foreach(markers, v) {
22-        const xs_dict *mark = xs_dict_get(data, v);
23-        if (!xs_is_null(mark)) {
24-            xs *copy = xs_dup(mark);
25-            const xs_val *datetime = xs_dict_get(mark, "updated_at");
26-            if (!xs_is_null(datetime) && datetime[0] != '2') {
27-                /* fix wrong timestamp format */
28-                struct timeval tv;
29-                if (sscanf(datetime, "%lu.%lu", (unsigned long *)&(tv.tv_sec), (unsigned long *)&(tv.tv_usec))==2)
30-                    copy = xs_dict_set(copy, "updated_at", rfctime(0, &tv));
31-            }
32-            returns = xs_dict_append(returns, v, copy);
33-        }
34-    }
35-    return returns;
36-}
37-
38-xs_dict *markers_set(snac *snac, const char *home_marker, const char *notify_marker)
39-/* gets or sets notification marker */
40-{
41-    xs *data = NULL;
42-    xs_dict *written = xs_dict_new();
43-    xs *fn = xs_fmt("%s/markers.json", snac->basedir);
44-    FILE *f;
45-
46-    if ((f = fopen(fn, "r")) != NULL) {
47-        data = xs_json_load(f);
48-        fclose(f);
49-    }
50-
51-    if (xs_is_null(data))
52-        data = xs_dict_new();
53-
54-    if (!xs_is_null(home_marker)) {
55-        xs *home = xs_dict_new();
56-        xs *s_datetime = rfctime(0, NULL);
57-        home = xs_dict_append(home, "last_read_id", home_marker);
58-        home = xs_dict_append(home, "version", xs_stock(0));
59-        home = xs_dict_append(home, "updated_at", s_datetime);
60-        data = xs_dict_set(data, "home", home);
61-        written = xs_dict_append(written, "home", home);
62-    }
63-
64-    if (!xs_is_null(notify_marker)) {
65-        xs *notify = xs_dict_new();
66-        xs *s_datetime = rfctime(0, NULL);
67-        notify = xs_dict_append(notify, "last_read_id", notify_marker);
68-        notify = xs_dict_append(notify, "version", xs_stock(0));
69-        notify = xs_dict_append(notify, "updated_at", s_datetime);
70-        data = xs_dict_set(data, "notifications", notify);
71-        written = xs_dict_append(written, "notifications", notify);
72-    }
73-
74-    if ((f = fopen(fn, "w")) != NULL) {
75-        xs_json_dump(data, 4, f);
76-        fclose(f);
77-    }
78-
79-    return written;
80-}
81 
82 void notify_add(snac *snac, const char *type, const char *utype,
83                 const char *actor, const char *objid, const xs_dict *msg)
+94, -0
  1@@ -875,6 +875,100 @@ xs_str *mastoapi_date(const char *date)
  2 }
  3 
  4 
  5+xs_str *rfctime(int offset, struct timeval *tv)
  6+/* returns an RFC3339 format (with fractional seconds) compliant with Mastodon datetime */
  7+{
  8+    struct timeval tv2;
  9+
 10+    if (tv == NULL) {
 11+        gettimeofday(&tv2, NULL);
 12+        tv = &tv2;
 13+    }
 14+    xs_str *iso_date = xs_str_utctime((long)(*tv).tv_sec + (long)offset, "%Y-%m-%dT%H:%M:%S");
 15+    xs_str *date_with_ms =xs_fmt("%s.%03ldZ", iso_date, (long)(*tv).tv_usec/1000L);
 16+    xs_free(iso_date);
 17+
 18+    return date_with_ms;
 19+}
 20+
 21+
 22+xs_dict *markers_get(snac *snac, const xs_list *markers)
 23+{
 24+    xs *data = NULL;
 25+    xs_dict *returns = xs_dict_new();
 26+    xs *fn = xs_fmt("%s/markers.json", snac->basedir);
 27+    const xs_str *v = NULL;
 28+    FILE *f;
 29+
 30+    if ((f = fopen(fn, "r")) != NULL) {
 31+        data = xs_json_load(f);
 32+        fclose(f);
 33+    }
 34+
 35+    if (xs_is_null(data))
 36+        data = xs_dict_new();
 37+
 38+    xs_list_foreach(markers, v) {
 39+        const xs_dict *mark = xs_dict_get(data, v);
 40+        if (!xs_is_null(mark)) {
 41+            xs *copy = xs_dup(mark);
 42+            const xs_val *datetime = xs_dict_get(mark, "updated_at");
 43+            if (!xs_is_null(datetime) && datetime[0] != '2') {
 44+                /* fix wrong timestamp format */
 45+                struct timeval tv;
 46+                if (sscanf(datetime, "%lu.%lu", (unsigned long *)&(tv.tv_sec), (unsigned long *)&(tv.tv_usec))==2)
 47+                    copy = xs_dict_set(copy, "updated_at", rfctime(0, &tv));
 48+            }
 49+            returns = xs_dict_append(returns, v, copy);
 50+        }
 51+    }
 52+    return returns;
 53+}
 54+
 55+xs_dict *markers_set(snac *snac, const char *home_marker, const char *notify_marker)
 56+/* gets or sets notification marker */
 57+{
 58+    xs *data = NULL;
 59+    xs_dict *written = xs_dict_new();
 60+    xs *fn = xs_fmt("%s/markers.json", snac->basedir);
 61+    FILE *f;
 62+
 63+    if ((f = fopen(fn, "r")) != NULL) {
 64+        data = xs_json_load(f);
 65+        fclose(f);
 66+    }
 67+
 68+    if (xs_is_null(data))
 69+        data = xs_dict_new();
 70+
 71+    if (!xs_is_null(home_marker)) {
 72+        xs *home = xs_dict_new();
 73+        xs *s_datetime = rfctime(0, NULL);
 74+        home = xs_dict_append(home, "last_read_id", home_marker);
 75+        home = xs_dict_append(home, "version", xs_stock(0));
 76+        home = xs_dict_append(home, "updated_at", s_datetime);
 77+        data = xs_dict_set(data, "home", home);
 78+        written = xs_dict_append(written, "home", home);
 79+    }
 80+
 81+    if (!xs_is_null(notify_marker)) {
 82+        xs *notify = xs_dict_new();
 83+        xs *s_datetime = rfctime(0, NULL);
 84+        notify = xs_dict_append(notify, "last_read_id", notify_marker);
 85+        notify = xs_dict_append(notify, "version", xs_stock(0));
 86+        notify = xs_dict_append(notify, "updated_at", s_datetime);
 87+        data = xs_dict_set(data, "notifications", notify);
 88+        written = xs_dict_append(written, "notifications", notify);
 89+    }
 90+
 91+    if ((f = fopen(fn, "w")) != NULL) {
 92+        xs_json_dump(data, 4, f);
 93+        fclose(f);
 94+    }
 95+
 96+    return written;
 97+}
 98+
 99 xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg)
100 /* creates a mastoapi Poll object */
101 {
M snac.c
+0, -16
 1@@ -85,22 +85,6 @@ double ftime(void)
 2     return atof(ntid);
 3 }
 4 
 5-xs_str *rfctime(int offset, struct timeval *tv)
 6-/* returns an RFC3339 format (with fractional seconds) compliant with Mastodon datetime */
 7-{
 8-    struct timeval tv2;
 9-
10-    if (tv == NULL) {
11-        gettimeofday(&tv2, NULL);
12-        tv = &tv2;
13-    }
14-    xs_str *iso_date = xs_str_utctime((long)(*tv).tv_sec + (long)offset, "%Y-%m-%dT%H:%M:%S");
15-    xs_str *date_with_ms =xs_fmt("%s.%03ldZ", iso_date, (long)(*tv).tv_usec/1000L);
16-    xs_free(iso_date);
17-
18-    return date_with_ms;
19-}
20-
21 
22 int validate_uid(const char *uid)
23 /* returns if uid is a valid identifier */
M snac.h
+0, -3
 1@@ -1,9 +1,6 @@
 2 /* snac - A simple, minimalistic ActivityPub instance */
 3 /* copyright (c) 2022 - 2026 grunfink et al. / MIT license */
 4 
 5-#include <sys/time.h> /* for struct timeval */
 6-#include "xs.h"       /* for xs_* */
 7-
 8 #define VERSION "2.95"
 9 
10 #define USER_AGENT "snac/" VERSION