Browse Source

Start of list support.

default 11 months ago
parent
commit
8275a5f4d8
3 changed files with 108 additions and 3 deletions
  1. 62 0
      data.c
  2. 44 3
      mastoapi.c
  3. 2 0
      snac.h

+ 62 - 0
data.c

@@ -1729,6 +1729,68 @@ xs_list *tag_search(char *tag, int skip, int show)
 }
 
 
+/** lists **/
+
+xs_list *list_maint(snac *user, const char *list, int op)
+{
+    xs_list *l = NULL;
+
+    switch (op) {
+    case 0: /** list of lists **/
+        {
+            FILE *f;
+            xs *spec = xs_fmt("%s/list/" "*.id", user->basedir);
+            xs *ls   = xs_glob(spec, 0, 0);
+            int c = 0;
+            char *v;
+
+            l = xs_list_new();
+
+            while (xs_list_next(ls, &v, &c)) {
+                if ((f = fopen(v, "r")) != NULL) {
+                    xs *title = xs_readline(f);
+                    fclose(f);
+
+                    title = xs_strip_i(title);
+                    xs *md5 = xs_md5_hex(title, strlen(title));
+
+                    /* return [ list_id, list_title ] */
+                    l = xs_list_append(l, xs_list_append(xs_list_new(), md5, title));
+                }
+            }
+        }
+
+        break;
+
+    case 1: /** create new list (list is the name) **/
+        {
+            FILE *f;
+            xs *dir = xs_fmt("%s/list/", user->basedir);
+            xs *md5 = xs_md5_hex(list, strlen(list));
+
+            mkdirx(dir);
+
+            xs *fn = xs_fmt("%s%s.id", dir, md5);
+
+            if ((f = fopen(fn, "w")) != NULL) {
+                fprintf(f, "%s\n", list);
+                fclose(f);
+            }
+        }
+
+        break;
+
+    case 2: /** delete list (list is md5 id) **/
+        break;
+
+    case 3: /** list content (list is md5 id) **/
+        break;
+    }
+
+    return l;
+}
+
+
 /** static data **/
 
 static int _load_raw_file(const char *fn, xs_val **data, int *size,

+ 44 - 3
mastoapi.c

@@ -1767,9 +1767,27 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
     else
     if (strcmp(cmd, "/v1/lists") == 0) { /** **/
         /* snac does not support lists */
-        *body  = xs_dup("[]");
-        *ctype = "application/json";
-        status = 200;
+        if (logged_in) {
+            xs *lol = list_maint(&snac1, NULL, 0);
+            xs *l   = xs_list_new();
+            int c = 0;
+            xs_list *li;
+
+            while (xs_list_next(lol, &li, &c)) {
+                xs *d = xs_dict_new();
+
+                d = xs_dict_append(d, "id", xs_list_get(li, 0));
+                d = xs_dict_append(d, "title", xs_list_get(li, 1));
+                d = xs_dict_append(d, "replies_policy", "list");
+                d = xs_dict_append(d, "exclusive", xs_stock(XSTYPE_FALSE));
+
+                l = xs_list_append(l, d);
+            }
+
+            *body  = xs_json_dumps(l, 4);
+            *ctype = "application/json";
+            status = 200;
+        }
     }
     else
     if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
@@ -2631,6 +2649,29 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
         else
             status = 401;
     }
+    else
+    if (strcmp(cmd, "/v1/lists") == 0) {
+        if (logged_in) {
+            const char *title = xs_dict_get(args, "title");
+
+            if (xs_type(title) == XSTYPE_STRING) {
+                /* add the list */
+                list_maint(&snac, title, 1);
+
+                xs *out = xs_dict_new();
+
+                out = xs_dict_append(out, "title", title);
+                out = xs_dict_append(out, "replies_policy", xs_dict_get_def(args, "replies_policy", "list"));
+                out = xs_dict_append(out, "exclusive", xs_stock(XSTYPE_FALSE));
+
+                *body  = xs_json_dumps(out, 4);
+                *ctype = "application/json";
+                status = 200;
+            }
+            else
+                status = 422;
+        }
+    }
 
     /* user cleanup */
     if (logged_in)

+ 2 - 0
snac.h

@@ -174,6 +174,8 @@ int is_hidden(snac *snac, const char *id);
 void tag_index(const char *id, const xs_dict *obj);
 xs_list *tag_search(char *tag, int skip, int show);
 
+xs_list *list_maint(snac *user, const char *list, int op);
+
 int actor_add(const char *actor, xs_dict *msg);
 int actor_get(const char *actor, xs_dict **data);
 int actor_get_refresh(snac *user, const char *actor, xs_dict **data);