snac2

Fork of https://codeberg.org/grunfink/snac2
git clone https://git.inz.fi/snac2
Log | Files | Refs | README | LICENSE

commit e9588a71ae67427d3ed0c50246f46724aa7611c3
parent 59b049fe3b689eac6e028a9b5537bc593b9e3696
Author: default <nobody@localhost>
Date:   Mon, 15 May 2023 11:15:28 +0200

Add /v1/account/search support.

Diffstat:
Mdata.c | 7++++---
Mmastoapi.c | 34+++++++++++++++++++++++++++++++++-
Msnac.h | 2+-
3 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/data.c b/data.c @@ -1222,13 +1222,14 @@ int following_get(snac *snac, const char *actor, d_char **data) } -d_char *following_list(snac *snac) +xs_list *following_list(snac *snac) /* returns the list of people being followed */ { xs *spec = xs_fmt("%s/following/" "*.json", snac->basedir); xs *glist = xs_glob(spec, 0, 0); - char *p, *v; - d_char *list = xs_list_new(); + xs_list *p; + xs_str *v; + xs_list *list = xs_list_new(); /* iterate the list of files */ p = glist; diff --git a/mastoapi.c b/mastoapi.c @@ -957,6 +957,38 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, xs *out = NULL; xs *actor = NULL; + if (logged_in && strcmp(uid, "search") == 0) { /** **/ + /* search for accounts starting with q */ + const char *q = xs_dict_get(args, "q"); + + if (!xs_is_null(q)) { + out = xs_list_new(); + xs *wing = following_list(&snac1); + xs *wers = follower_list(&snac1); + xs_list *p; + + xs_list *lsts[] = { wing, wers, NULL }; + int n; + for (n = 0; (p = lsts[n]) != NULL; n++) { + xs_str *v; + + while (xs_list_iter(&p, &v)) { + xs *actor = NULL; + + if (valid_status(object_get(v, &actor))) { + const char *uname = xs_dict_get(actor, "preferredUsername"); + + if (!xs_is_null(uname) && xs_startswith(uname, q)) { + xs *acct = mastoapi_account(actor); + + out = xs_list_append(out, acct); + } + } + } + } + } + } + else /* is it a local user? */ if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) { if (opt == NULL) { @@ -965,7 +997,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, out = mastoapi_account(actor); } else - if (strcmp(opt, "statuses") == 0) { + if (strcmp(opt, "statuses") == 0) { /** **/ /* the public list of posts of a user */ xs *timeline = timeline_simple_list(&snac2, "public", 0, 256); xs_list *p = timeline; diff --git a/snac.h b/snac.h @@ -119,7 +119,7 @@ int following_add(snac *snac, const char *actor, const xs_dict *msg); int following_del(snac *snac, const char *actor); int following_check(snac *snac, const char *actor); int following_get(snac *snac, const char *actor, d_char **data); -d_char *following_list(snac *snac); +xs_list *following_list(snac *snac); void mute(snac *snac, const char *actor); void unmute(snac *snac, const char *actor);