commit 9a13e33

default  ·  2024-04-29 06:29:18 +0000 UTC
parent 8275a5f
More work in lists.
3 files changed,  +90, -25
M data.c
M snac.h
M data.c
+48, -14
 1@@ -1731,9 +1731,9 @@ xs_list *tag_search(char *tag, int skip, int show)
 2 
 3 /** lists **/
 4 
 5-xs_list *list_maint(snac *user, const char *list, int op)
 6+xs_val *list_maint(snac *user, const char *list, int op)
 7 {
 8-    xs_list *l = NULL;
 9+    xs_val *l = NULL;
10 
11     switch (op) {
12     case 0: /** list of lists **/
13@@ -1752,10 +1752,12 @@ xs_list *list_maint(snac *user, const char *list, int op)
14                     fclose(f);
15 
16                     title = xs_strip_i(title);
17-                    xs *md5 = xs_md5_hex(title, strlen(title));
18+
19+                    xs *v2 = xs_replace(v, ".id", "");
20+                    xs *l2 = xs_split(v2, "/");
21 
22                     /* return [ list_id, list_title ] */
23-                    l = xs_list_append(l, xs_list_append(xs_list_new(), md5, title));
24+                    l = xs_list_append(l, xs_list_append(xs_list_new(), xs_list_get(l2, -1), title));
25                 }
26             }
27         }
28@@ -1764,26 +1766,58 @@ xs_list *list_maint(snac *user, const char *list, int op)
29 
30     case 1: /** create new list (list is the name) **/
31         {
32-            FILE *f;
33-            xs *dir = xs_fmt("%s/list/", user->basedir);
34-            xs *md5 = xs_md5_hex(list, strlen(list));
35+            xs *lol = list_maint(user, NULL, 0);
36+            int c = 0;
37+            xs_list *v;
38+            int add = 1;
39 
40-            mkdirx(dir);
41+            /* check if this list name already exists */
42+            while (xs_list_next(lol, &v, &c)) {
43+                if (strcmp(xs_list_get(v, 1), list) == 0) {
44+                    add = 0;
45+                    break;
46+                }
47+            }
48 
49-            xs *fn = xs_fmt("%s%s.id", dir, md5);
50+            if (add) {
51+                FILE *f;
52+                xs *dir = xs_fmt("%s/list/", user->basedir);
53+                xs *id  = xs_fmt("%010x", time(NULL));
54 
55-            if ((f = fopen(fn, "w")) != NULL) {
56-                fprintf(f, "%s\n", list);
57-                fclose(f);
58+                mkdirx(dir);
59+
60+                xs *fn = xs_fmt("%s%s.id", dir, id);
61+
62+                if ((f = fopen(fn, "w")) != NULL) {
63+                    fprintf(f, "%s\n", list);
64+                    fclose(f);
65+                }
66+
67+                l = xs_stock(XSTYPE_TRUE);
68             }
69+            else
70+                l = xs_stock(XSTYPE_FALSE);
71         }
72 
73         break;
74 
75-    case 2: /** delete list (list is md5 id) **/
76+    case 2: /** delete list (list is the id) **/
77+        {
78+            if (xs_is_hex(list)) {
79+                xs *fn = xs_fmt("%s/list/%s.id", user->basedir, list);
80+                unlink(fn);
81+
82+                fn = xs_replace_i(fn, ".id", ".lst");
83+                unlink(fn);
84+
85+                fn = xs_replace_i(fn, ".list", ".idx");
86+                unlink(fn);
87+            }
88+        }
89+
90         break;
91 
92-    case 3: /** list content (list is md5 id) **/
93+    case 3: /** list content (list is the id) **/
94         break;
95     }
96 
+41, -10
 1@@ -1766,7 +1766,6 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
 2     }
 3     else
 4     if (strcmp(cmd, "/v1/lists") == 0) { /** **/
 5-        /* snac does not support lists */
 6         if (logged_in) {
 7             xs *lol = list_maint(&snac1, NULL, 0);
 8             xs *l   = xs_list_new();
 9@@ -2656,17 +2655,22 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
10 
11             if (xs_type(title) == XSTYPE_STRING) {
12                 /* add the list */
13-                list_maint(&snac, title, 1);
14-
15                 xs *out = xs_dict_new();
16 
17-                out = xs_dict_append(out, "title", title);
18-                out = xs_dict_append(out, "replies_policy", xs_dict_get_def(args, "replies_policy", "list"));
19-                out = xs_dict_append(out, "exclusive", xs_stock(XSTYPE_FALSE));
20+                if (xs_type(list_maint(&snac, title, 1)) == XSTYPE_TRUE) {
21+                    out = xs_dict_append(out, "title", title);
22+                    out = xs_dict_append(out, "replies_policy", xs_dict_get_def(args, "replies_policy", "list"));
23+                    out = xs_dict_append(out, "exclusive", xs_stock(XSTYPE_FALSE));
24+
25+                    status = 200;
26+                }
27+                else {
28+                    out = xs_dict_append(out, "error", "cannot create list");
29+                    status = 422;
30+                }
31 
32                 *body  = xs_json_dumps(out, 4);
33                 *ctype = "application/json";
34-                status = 200;
35             }
36             else
37                 status = 422;
38@@ -2691,16 +2695,43 @@ int mastoapi_delete_handler(const xs_dict *req, const char *q_path,
39     (void)b_size;
40     (void)ctype;
41 
42+    int status = 404;
43+
44     if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
45         return 0;
46 
47-    srv_debug(1, xs_fmt("mastoapi_delete_handler %s", q_path));
48+    snac snac = {0};
49+    int logged_in = process_auth_token(&snac, req);
50+
51     xs *cmd = xs_replace_n(q_path, "/api", "", 1);
52+
53     if (xs_startswith(cmd, "/v1/push/subscription") || xs_startswith(cmd, "/v2/push/subscription")) { /** **/
54         // pretend we deleted it, since it doesn't exist anyway
55-        return 200;
56+        status = 200;
57     }
58-    return 0;
59+    else
60+    if (xs_startswith(cmd, "/v1/lists/")) {
61+        if (logged_in) {
62+            xs *l = xs_split(cmd, "/");
63+            char *id = xs_list_get(l, -1);
64+
65+            if (xs_is_hex(id)) {
66+                list_maint(&snac, id, 2);
67+            }
68+
69+            status = 200;
70+        }
71+        else
72+            status = 401;
73+    }
74+
75+    /* user cleanup */
76+    if (logged_in)
77+        user_free(&snac);
78+
79+    srv_debug(1, xs_fmt("mastoapi_delete_handler %s %d", q_path, status));
80+
81+    return status;
82 }
83 
84 
M snac.h
+1, -1
1@@ -174,7 +174,7 @@ int is_hidden(snac *snac, const char *id);
2 void tag_index(const char *id, const xs_dict *obj);
3 xs_list *tag_search(char *tag, int skip, int show);
4 
5-xs_list *list_maint(snac *user, const char *list, int op);
6+xs_val *list_maint(snac *user, const char *list, int op);
7 
8 int actor_add(const char *actor, xs_dict *msg);
9 int actor_get(const char *actor, xs_dict **data);