commit c21bbd5
default
·
2023-05-01 15:02:44 +0000 UTC
parent 84902d8
New functions index_del_md5(), index_del() and object_unadmire().
1 files changed,
+62,
-0
M
data.c
M
data.c
+62,
-0
1@@ -328,6 +328,51 @@ int index_add(const char *fn, const char *id)
2 }
3
4
5+int index_del_md5(const char *fn, const char *md5)
6+/* deletes an md5 from an index */
7+{
8+ int status = 404;
9+ FILE *f;
10+
11+ pthread_mutex_lock(&data_mutex);
12+
13+ if ((f = fopen(fn, "r+")) != NULL) {
14+ char line[256];
15+
16+ while (fgets(line, sizeof(line), f) != NULL) {
17+ line[32] = '\0';
18+
19+ if (strcmp(line, md5) == 0) {
20+ /* found! just rewind, overwrite it with garbage
21+ and an eventual call to index_gc() will clean it
22+ [yes: this breaks index_len()] */
23+ fseek(f, -33, SEEK_CUR);
24+ fwrite("-", 1, 1, f);
25+ status = 200;
26+
27+ break;
28+ }
29+ }
30+
31+ fclose(f);
32+ }
33+ else
34+ status = 500;
35+
36+ pthread_mutex_unlock(&data_mutex);
37+
38+ return status;
39+}
40+
41+
42+int index_del(const char *fn, const char *id)
43+/* deletes an id from an index */
44+{
45+ xs *md5 = xs_md5_hex(id, strlen(id));
46+ return index_del_md5(fn, md5);
47+}
48+
49+
50 int index_gc(const char *fn)
51 /* garbage-collects an index, deleting objects that are not here */
52 {
53@@ -772,6 +817,23 @@ int object_admire(const char *id, const char *actor, int like)
54 }
55
56
57+int object_unadmire(const char *id, const char *actor, int like)
58+/* actor no longer likes or announces this object */
59+{
60+ int status;
61+ xs *fn = _object_fn(id);
62+
63+ fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx");
64+
65+ status = index_del(fn, actor);
66+
67+ srv_debug(1,
68+ xs_fmt("object_unadmire (%s) %s %s %d", like ? "Like" : "Announce", actor, fn, status));
69+
70+ return status;
71+}
72+
73+
74 int _object_user_cache(snac *snac, const char *id, const char *cachedir, int del)
75 /* adds or deletes from a user cache */
76 {