commit af18068

default  ·  2023-06-28 18:52:09 +0000 UTC
parent 394240b
Added some error control to pinning.
3 files changed,  +35, -11
M data.c
M main.c
M snac.h
M data.c
+12, -10
 1@@ -1352,7 +1352,7 @@ int is_muted(snac *snac, const char *actor)
 2 xs_str *_pinned_fn(snac *user, const char *id)
 3 {
 4     xs *md5 = xs_md5_hex(id, strlen(id));
 5-    return xs_fmt("%s/pinned/%s", user->basedir, md5);
 6+    return xs_fmt("%s/pinned/%s.json", user->basedir, md5);
 7 }
 8 
 9 
10@@ -1367,26 +1367,28 @@ int is_pinned(snac *user, const char *id)
11 int pin(snac *user, const char *id)
12 /* pins a message */
13 {
14-    int ret = 0;
15+    int ret = -2;
16 
17     if (xs_startswith(id, user->actor)) {
18-        /* create the subfolder, if it does not exist */
19-        xs *fn = xs_fmt("%s/pinned/", user->basedir);
20-        mkdirx(fn);
21-
22-        object_user_cache_add(user, id, "pinned");
23+        if (is_pinned(user, id))
24+            ret = -3;
25+        else {
26+            /* create the subfolder, if it does not exist */
27+            xs *fn = xs_fmt("%s/pinned/", user->basedir);
28+            mkdirx(fn);
29 
30-        ret = 1;
31+            ret = object_user_cache_add(user, id, "pinned");
32+        }
33     }
34 
35     return ret;
36 }
37 
38 
39-void unpin(snac *user, const char *id)
40+int unpin(snac *user, const char *id)
41 /* unpin a message */
42 {
43-    object_user_cache_del(user, id, "pinned");
44+    return object_user_cache_del(user, id, "pinned");
45 }
46 
47 
M main.c
+22, -0
 1@@ -31,6 +31,8 @@ int usage(void)
 2     printf("resetpwd {basedir} {uid}            Resets the password of a user\n");
 3     printf("ping {basedir} {uid} {actor}        Pings an actor\n");
 4     printf("webfinger_s {basedir} {uid} {actor} Queries about an actor (@user@host or actor url)\n");
 5+    printf("pin {basedir} {uid} {msg_url}       Pins a message\n");
 6+    printf("unpin {basedir} {uid} {msg_url}     Unpins a message\n");
 7 /*    printf("question {basedir} {uid} 'opts'  Generates a poll (;-separated opts)\n");*/
 8 
 9     return 1;
10@@ -269,6 +271,26 @@ int main(int argc, char *argv[])
11         return 0;
12     }
13 
14+    if (strcmp(cmd, "pin") == 0) { /** **/
15+        int ret = pin(&snac, url);
16+        if (ret < 0) {
17+            fprintf(stderr, "error pinning %s %d\n", url, ret);
18+            return 1;
19+        }
20+
21+        return 0;
22+    }
23+
24+    if (strcmp(cmd, "unpin") == 0) { /** **/
25+        int ret = unpin(&snac, url);
26+        if (ret < 0) {
27+            fprintf(stderr, "error unpinning %s %d\n", url, ret);
28+            return 1;
29+        }
30+
31+        return 0;
32+    }
33+
34     if (strcmp(cmd, "question") == 0) { /** **/
35         int end_secs = 5 * 60;
36         xs *opts = xs_split(url, ";");
M snac.h
+1, -1
1@@ -127,7 +127,7 @@ void unmute(snac *snac, const char *actor);
2 int is_muted(snac *snac, const char *actor);
3 
4 int pin(snac *user, const char *id);
5-void unpin(snac *user, const char *id);
6+int unpin(snac *user, const char *id);
7 int is_pinned(snac *user, const char *id);
8 xs_list *pinned_list(snac *user);
9