commit 9a67b19
default
·
2024-08-29 04:59:44 +0000 UTC
parent 3ff7e62
Simplified pinning functions.
1 files changed,
+19,
-27
M
data.c
M
data.c
+19,
-27
1@@ -1013,10 +1013,16 @@ int object_unadmire(const char *id, const char *actor, int like)
2 }
3
4
5+xs_str *object_user_cache_fn_by_md5(snac *user, const char *md5, const char *cachedir)
6+{
7+ return xs_fmt("%s/%s/%s.json", user->basedir, cachedir, md5);
8+}
9+
10+
11 xs_str *object_user_cache_fn(snac *user, const char *id, const char *cachedir)
12 {
13 xs *md5 = xs_md5_hex(id, strlen(id));
14- return xs_fmt("%s/%s/%s.json", user->basedir, cachedir, md5);
15+ return object_user_cache_fn_by_md5(user, md5, cachedir);
16 }
17
18
19@@ -1067,6 +1073,14 @@ int object_user_cache_in(snac *user, const char *id, const char *cachedir)
20 }
21
22
23+int object_user_cache_in_by_md5(snac *user, const char *md5, const char *cachedir)
24+/* checks if an object is stored in a cache */
25+{
26+ xs *cfn = object_user_cache_fn_by_md5(user, md5, cachedir);
27+ return !!(mtime(cfn) != 0.0);
28+}
29+
30+
31 xs_list *object_user_cache_list(snac *user, const char *cachedir, int max, int inv)
32 /* returns the objects in a cache as a list */
33 {
34@@ -1571,25 +1585,16 @@ int unbookmark(snac *user, const char *id)
35
36 /** pinning **/
37
38-xs_str *_pinned_fn(snac *user, const char *id)
39-{
40- xs *md5 = xs_md5_hex(id, strlen(id));
41- return xs_fmt("%s/pinned/%s.json", user->basedir, md5);
42-}
43-
44-
45 int is_pinned(snac *user, const char *id)
46 /* returns true if this note is pinned */
47 {
48- xs *fn = _pinned_fn(user, id);
49- return !!(mtime(fn) != 0.0);
50+ return object_user_cache_in(user, id, "pinned");
51 }
52
53
54 int is_pinned_by_md5(snac *user, const char *md5)
55 {
56- xs *fn = xs_fmt("%s/pinned/%s.json", user->basedir, md5);
57- return !!(mtime(fn) != 0.0);
58+ return object_user_cache_in_by_md5(user, md5, "pinned");
59 }
60
61
62@@ -1601,13 +1606,8 @@ int pin(snac *user, const char *id)
63 if (xs_startswith(id, user->actor)) {
64 if (is_pinned(user, id))
65 ret = -3;
66- else {
67- /* create the subfolder, if it does not exist */
68- xs *fn = xs_fmt("%s/pinned/", user->basedir);
69- mkdirx(fn);
70-
71+ else
72 ret = object_user_cache_add(user, id, "pinned");
73- }
74 }
75
76 return ret;
77@@ -1617,15 +1617,7 @@ int pin(snac *user, const char *id)
78 int unpin(snac *user, const char *id)
79 /* unpin a message */
80 {
81- int ret = object_user_cache_del(user, id, "pinned");
82-
83- if (ret != -1) {
84- /* delete from the index */
85- xs *idx = xs_fmt("%s/pinned.idx", user->basedir);
86- index_del(idx, id);
87- }
88-
89- return ret;
90+ return object_user_cache_del(user, id, "pinned");
91 }
92
93