diff options
Diffstat (limited to 'mastoapi.c')
| -rw-r--r-- | mastoapi.c | 196 |
1 files changed, 157 insertions, 39 deletions
@@ -355,6 +355,7 @@ int oauth_post_handler(const xs_dict *req, const char *q_path, else if (strcmp(cmd, "/token") == 0) { /** **/ xs *wrk = NULL; + xs *codewrk = NULL; const char *gtype = xs_dict_get(args, "grant_type"); const char *code = xs_dict_get(args, "code"); const char *cid = xs_dict_get(args, "client_id"); @@ -387,7 +388,7 @@ int oauth_post_handler(const xs_dict *req, const char *q_path, I'm not sure of the impacts of this right now, but Subway Tooter does not provide a code so one must be generated */ if (xs_is_null(code)){ - code = random_str(); + code = codewrk = random_str(); } if (gtype && code && cid && csec && ruri) { xs *app = app_get(cid); @@ -1185,13 +1186,13 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) int count = 1; if (contentl) { - count = atoi(xs_list_get(contentl, 0)) + 1; + count = xs_number_get(xs_list_get(contentl, 0)) + 1; if (strncmp(xs_list_get(contentl, 1), xs_stock(XSTYPE_TRUE), 1) == 0) me = xs_stock(XSTYPE_TRUE); } xs *fl = xs_list_new(); - xs *c1 = xs_fmt("%d", count); + xs *c1 = xs_number_new(count); fl = xs_list_append(fl, c1, me); sfrl = xs_dict_append(sfrl, content, fl); } @@ -1780,7 +1781,7 @@ xs_list *mastoapi_account_lists(snac *user, const char *uid) { xs_list *out = xs_list_new(); xs *actor_md5 = NULL; - xs *lol = list_maint(user, NULL, 0); + xs *lol = list_maint(user, NULL, OP_LIST); if (uid) { if (!xs_is_hex(uid)) @@ -1794,7 +1795,7 @@ xs_list *mastoapi_account_lists(snac *user, const char *uid) const char *list_id = xs_list_get(li, 0); const char *list_title = xs_list_get(li, 1); if (uid) { - xs *users = list_members(user, list_id, NULL, 0); + xs *users = list_members(user, list_id, NULL, OP_LIST); if (xs_list_in(users, actor_md5) == -1) continue; } @@ -2306,10 +2307,13 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, } else if (strcmp(cmd, "/v1/conversations") == 0) { /** **/ - /* TBD */ - *body = xs_dup("[]"); - *ctype = "application/json"; - status = HTTP_STATUS_OK; + if (logged_in) { + *body = xs_dup("[]"); + *ctype = "application/json"; + status = HTTP_STATUS_OK; + } + else + status = HTTP_STATUS_UNAUTHORIZED; } else if (strcmp(cmd, "/v1/notifications") == 0) { /** **/ @@ -2470,25 +2474,37 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, else if (strcmp(cmd, "/v1/filters") == 0) { /** **/ /* snac will never have filters */ - *body = xs_dup("[]"); - *ctype = "application/json"; - status = HTTP_STATUS_OK; + if (logged_in) { + *body = xs_dup("[]"); + *ctype = "application/json"; + status = HTTP_STATUS_OK; + } + else + status = HTTP_STATUS_UNAUTHORIZED; } else if (strcmp(cmd, "/v2/filters") == 0) { /** **/ /* snac will never have filters * but still, without a v2 endpoint a short delay is introduced * in some apps */ - *body = xs_dup("[]"); - *ctype = "application/json"; - status = HTTP_STATUS_OK; + if (logged_in) { + *body = xs_dup("[]"); + *ctype = "application/json"; + status = HTTP_STATUS_OK; + } + else + status = HTTP_STATUS_UNAUTHORIZED; } else if (strcmp(cmd, "/v1/favourites") == 0) { /** **/ /* snac will never support a list of favourites */ - *body = xs_dup("[]"); - *ctype = "application/json"; - status = HTTP_STATUS_OK; + if (logged_in) { + *body = xs_dup("[]"); + *ctype = "application/json"; + status = HTTP_STATUS_OK; + } + else + status = HTTP_STATUS_UNAUTHORIZED; } else if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/ @@ -2526,7 +2542,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, p = xs_list_get(l, -2); if (p && xs_is_hex(p)) { - xs *actors = list_members(&snac1, p, NULL, 0); + xs *actors = list_members(&snac1, p, NULL, OP_LIST); xs *out = xs_list_new(); int c = 0; const char *v; @@ -2548,7 +2564,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, else if (xs_is_hex(p)) { xs *out = xs_list_new(); - xs *lol = list_maint(&snac1, NULL, 0); + xs *lol = list_maint(&snac1, NULL, OP_LIST); int c = 0; const xs_list *v; @@ -2579,10 +2595,13 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, } else if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/ - /* TBD */ - *body = xs_dup("[]"); - *ctype = "application/json"; - status = HTTP_STATUS_OK; + if (logged_in) { + *body = xs_dup("[]"); + *ctype = "application/json"; + status = HTTP_STATUS_OK; + } + else + status = HTTP_STATUS_UNAUTHORIZED; } else if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/ @@ -2950,9 +2969,13 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, else if (strcmp(cmd, "/v1/preferences") == 0) { /** **/ /* TBD */ - *body = xs_dup("{}"); - *ctype = "application/json"; - status = HTTP_STATUS_OK; + if (logged_in) { + *body = xs_dup("{}"); + *ctype = "application/json"; + status = HTTP_STATUS_OK; + } + else + status = HTTP_STATUS_UNAUTHORIZED; } else if (strcmp(cmd, "/v1/markers") == 0) { /** **/ @@ -3003,15 +3026,23 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, } else if (strcmp(cmd, "/v1/blocks") == 0) { /** **/ - *body = xs_dup("[]"); - *ctype = "application/json"; - status = HTTP_STATUS_OK; + if (logged_in) { + *body = xs_dup("[]"); + *ctype = "application/json"; + status = HTTP_STATUS_OK; + } + else + status = HTTP_STATUS_UNAUTHORIZED; } else if (strcmp(cmd, "/v1/mutes") == 0) { /** **/ - *body = xs_dup("[]"); - *ctype = "application/json"; - status = HTTP_STATUS_OK; + if (logged_in) { + *body = xs_dup("[]"); + *ctype = "application/json"; + status = HTTP_STATUS_OK; + } + else + status = HTTP_STATUS_UNAUTHORIZED; } else if (strcmp(cmd, "/v1/trends/tags") == 0) { /** **/ @@ -3139,6 +3170,43 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, else status = HTTP_STATUS_UNAUTHORIZED; } + else + if (xs_startswith(cmd, "/v1/media/") || xs_startswith(cmd, "/v2/media/")) { /** **/ + if (logged_in) { + xs *p = xs_split(cmd, "/"); + const char *id = xs_list_get(p, -1); + + if (xs_is_string(id)) { + xs *filename = xs_fmt("%s/static/%s", snac1.basedir, id); + + if (mtime(filename)) { + /* file exists */ + xs *alt = static_get_meta(&snac1, id); + xs *url = xs_fmt("%s/s/%s", snac1.actor, id); + xs *mime = xs_split(xs_mime_by_ext(filename), "/"); + const char *type = xs_list_get(mime, 0); + + if (!xs_match(type, "image|video|audio")) + type = "unknown"; + + xs *d = xs_dict_new(); + + d = xs_dict_set(d, "id", id); + d = xs_dict_set(d, "url", url); + d = xs_dict_set(d, "preview_url", url); + d = xs_dict_set(d, "remote_url", url); + d = xs_dict_set(d, "type", type); + d = xs_dict_set(d, "description", xs_or(alt, "")); + + *body = xs_json_dumps(d, 4); + *ctype = "application/json"; + status = HTTP_STATUS_OK; + } + } + } + else + status = HTTP_STATUS_UNAUTHORIZED; + } /* user cleanup */ if (logged_in) @@ -3435,6 +3503,7 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, xs *n_msg = msg_repulsion(&snac, id, "Like"); if (n_msg != NULL) { + object_user_cache_del(&snac, id, "admire"); enqueue_message(&snac, n_msg); out = mastoapi_status(&snac, msg); @@ -3456,6 +3525,7 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, xs *n_msg = msg_emoji_unreact(&snac, id, content); if (n_msg != NULL) { + object_user_cache_del(&snac, id, "admire"); enqueue_message(&snac, n_msg); out = mastoapi_status(&snac, msg); @@ -3800,7 +3870,7 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, if (xs_type(title) == XSTYPE_STRING) { /* add the list */ xs *out = xs_dict_new(); - xs *lid = list_maint(&snac, title, 1); + xs *lid = list_maint(&snac, title, OP_ADD); if (!xs_is_null(lid)) { out = xs_dict_append(out, "id", lid); @@ -3840,7 +3910,7 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, const char *v; while (xs_list_next(accts, &v, &c)) { - list_members(&snac, id, v, 1); + list_members(&snac, id, v, OP_ADD); } xs *out = xs_dict_new(); @@ -4049,13 +4119,13 @@ int mastoapi_delete_handler(const xs_dict *req, const char *q_path, const char *v; while (xs_list_next(accts, &v, &c)) { - list_members(&snac, p, v, 2); + list_members(&snac, p, v, OP_DEL); } } else { /* delete list */ if (xs_is_hex(p)) { - list_maint(&snac, p, 2); + list_maint(&snac, p, OP_DEL); } } } @@ -4101,6 +4171,24 @@ int mastoapi_delete_handler(const xs_dict *req, const char *q_path, else status = HTTP_STATUS_UNAUTHORIZED; } + else + if (xs_startswith(cmd, "/v1/profile/")) { + if (logged_in) { + xs *l = xs_split(cmd, "/"); + const char *p = xs_list_get(l, -1); + + if (strcmp(p, "avatar") == 0 || strcmp(p, "header") == 0) { + snac.config = xs_dict_set(snac.config, p, ""); + + if (user_persist(&snac, 1) == 0) + credentials_get(body, ctype, &status, snac); + else + status = HTTP_STATUS_INTERNAL_SERVER_ERROR; + } + } + else + status = HTTP_STATUS_UNAUTHORIZED; + } /* user cleanup */ if (logged_in) @@ -4205,17 +4293,47 @@ int mastoapi_put_handler(const xs_dict *req, const char *q_path, if (valid_status(timeline_get_by_md5(&snac, md5, &msg))) { const char *content = xs_dict_get(args, "status"); xs *atls = xs_list_new(); - xs *f_content = not_really_markdown(content, &atls, NULL); + xs *tag = xs_list_new(); + xs *f_content0 = not_really_markdown(content, &atls, &tag); + xs *f_content = process_tags(&snac, f_content0, &tag); /* replace fields with new content */ msg = xs_dict_set(msg, "sourceContent", content); msg = xs_dict_set(msg, "content", f_content); + xs *content_map = xs_dup(xs_dict_get(msg, "contentMap")); + if (xs_is_dict(content_map)) { + /* get the first pair */ + const char *k, *v; + int c = 0; + + if (xs_dict_next(content_map, &k, &v, &c)) { + content_map = xs_dict_set(content_map, k, f_content); + msg = xs_dict_set(msg, "contentMap", content_map); + } + } + xs *updated = xs_str_utctime(0, ISO_DATE_SPEC); msg = xs_dict_set(msg, "updated", updated); + msg = xs_dict_set(msg, "tag", tag); + + if (xs_is_true(xs_dict_get(args, "sensitive"))) { + const char *spoiler_text = xs_dict_get(args, "spoiler_text"); + msg = xs_dict_set(msg, "summary", spoiler_text); + msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE)); + } + else { + msg = xs_dict_set(msg, "summary", ""); + msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_FALSE)); + } + /* overwrite object, not updating the indexes */ - object_add_ow(xs_dict_get(msg, "id"), msg); + const char *id = xs_dict_get(msg, "id"); + + object_add_ow(id, msg); + + tag_index(id, msg); /* update message */ xs *c_msg = msg_update(&snac, msg); |