More work in lists.

This commit is contained in:
default 2024-04-29 08:29:18 +02:00
parent 8275a5f4d8
commit 9a13e330f1
3 changed files with 92 additions and 27 deletions

66
data.c
View File

@ -1731,9 +1731,9 @@ xs_list *tag_search(char *tag, int skip, int show)
/** lists **/
xs_list *list_maint(snac *user, const char *list, int op)
xs_val *list_maint(snac *user, const char *list, int op)
{
xs_list *l = NULL;
xs_val *l = NULL;
switch (op) {
case 0: /** list of lists **/
@ -1752,10 +1752,12 @@ xs_list *list_maint(snac *user, const char *list, int op)
fclose(f);
title = xs_strip_i(title);
xs *md5 = xs_md5_hex(title, strlen(title));
xs *v2 = xs_replace(v, ".id", "");
xs *l2 = xs_split(v2, "/");
/* return [ list_id, list_title ] */
l = xs_list_append(l, xs_list_append(xs_list_new(), md5, title));
l = xs_list_append(l, xs_list_append(xs_list_new(), xs_list_get(l2, -1), title));
}
}
}
@ -1764,26 +1766,58 @@ xs_list *list_maint(snac *user, const char *list, int op)
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));
xs *lol = list_maint(user, NULL, 0);
int c = 0;
xs_list *v;
int add = 1;
mkdirx(dir);
/* check if this list name already exists */
while (xs_list_next(lol, &v, &c)) {
if (strcmp(xs_list_get(v, 1), list) == 0) {
add = 0;
break;
}
}
xs *fn = xs_fmt("%s%s.id", dir, md5);
if (add) {
FILE *f;
xs *dir = xs_fmt("%s/list/", user->basedir);
xs *id = xs_fmt("%010x", time(NULL));
if ((f = fopen(fn, "w")) != NULL) {
fprintf(f, "%s\n", list);
fclose(f);
mkdirx(dir);
xs *fn = xs_fmt("%s%s.id", dir, id);
if ((f = fopen(fn, "w")) != NULL) {
fprintf(f, "%s\n", list);
fclose(f);
}
l = xs_stock(XSTYPE_TRUE);
}
else
l = xs_stock(XSTYPE_FALSE);
}
break;
case 2: /** delete list (list is the id) **/
{
if (xs_is_hex(list)) {
xs *fn = xs_fmt("%s/list/%s.id", user->basedir, list);
unlink(fn);
fn = xs_replace_i(fn, ".id", ".lst");
unlink(fn);
fn = xs_replace_i(fn, ".list", ".idx");
unlink(fn);
}
}
break;
case 2: /** delete list (list is md5 id) **/
break;
case 3: /** list content (list is md5 id) **/
case 3: /** list content (list is the id) **/
break;
}

View File

@ -1766,7 +1766,6 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
}
else
if (strcmp(cmd, "/v1/lists") == 0) { /** **/
/* snac does not support lists */
if (logged_in) {
xs *lol = list_maint(&snac1, NULL, 0);
xs *l = xs_list_new();
@ -2656,17 +2655,22 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
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));
if (xs_type(list_maint(&snac, title, 1)) == XSTYPE_TRUE) {
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));
status = 200;
}
else {
out = xs_dict_append(out, "error", "cannot create list");
status = 422;
}
*body = xs_json_dumps(out, 4);
*ctype = "application/json";
status = 200;
}
else
status = 422;
@ -2691,16 +2695,43 @@ int mastoapi_delete_handler(const xs_dict *req, const char *q_path,
(void)b_size;
(void)ctype;
int status = 404;
if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
return 0;
srv_debug(1, xs_fmt("mastoapi_delete_handler %s", q_path));
snac snac = {0};
int logged_in = process_auth_token(&snac, req);
xs *cmd = xs_replace_n(q_path, "/api", "", 1);
if (xs_startswith(cmd, "/v1/push/subscription") || xs_startswith(cmd, "/v2/push/subscription")) { /** **/
// pretend we deleted it, since it doesn't exist anyway
return 200;
status = 200;
}
return 0;
else
if (xs_startswith(cmd, "/v1/lists/")) {
if (logged_in) {
xs *l = xs_split(cmd, "/");
char *id = xs_list_get(l, -1);
if (xs_is_hex(id)) {
list_maint(&snac, id, 2);
}
status = 200;
}
else
status = 401;
}
/* user cleanup */
if (logged_in)
user_free(&snac);
srv_debug(1, xs_fmt("mastoapi_delete_handler %s %d", q_path, status));
return status;
}

2
snac.h
View File

@ -174,7 +174,7 @@ 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);
xs_val *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);