From 5e8eb5f17188809a4340aed8f0382bd44820c96c Mon Sep 17 00:00:00 2001 From: default Date: Sat, 28 Oct 2023 07:10:21 +0200 Subject: [PATCH] mastoapi: implemented /api/v1/accounts/lookup. --- mastoapi.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/mastoapi.c b/mastoapi.c index 8e1fb9d..55f685c 100644 --- a/mastoapi.c +++ b/mastoapi.c @@ -1049,8 +1049,6 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/")) return 0; - srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path)); - int status = 404; xs_dict *args = xs_dict_get(req, "q_vars"); xs *cmd = xs_replace_n(q_path, "/api", "", 1); @@ -1143,6 +1141,33 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, status = 422; } else + if (strcmp(cmd, "/v1/accounts/lookup") == 0) { /** **/ + /* lookup an account */ + char *acct = xs_dict_get(args, "acct"); + + if (!xs_is_null(acct)) { + xs *s = xs_strip_chars_i(xs_dup(acct), "@"); + xs *l = xs_split_n(s, "@", 1); + char *uid = xs_list_get(l, 0); + char *host = xs_list_get(l, 1); + + if (uid && (!host || strcmp(host, xs_dict_get(srv_config, "host")) == 0)) { + snac user; + + if (user_open(&user, uid)) { + xs *actor = msg_actor(&user); + xs *macct = mastoapi_account(actor); + + *body = xs_json_dumps(macct, 4); + *ctype = "application/json"; + status = 200; + + user_free(&user); + } + } + } + } + else if (xs_startswith(cmd, "/v1/accounts/")) { /** **/ /* account-related information */ xs *l = xs_split(cmd, "/"); @@ -1883,6 +1908,8 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, if (logged_in) user_free(&snac1); + srv_debug(1, xs_fmt("mastoapi_get_handler %s %d", q_path, status)); + return status; }