commit e9588a7

default  ·  2023-05-15 09:15:28 +0000 UTC
parent 59b049f
Add /v1/account/search support.
3 files changed,  +38, -5
M data.c
M snac.h
M data.c
+4, -3
 1@@ -1222,13 +1222,14 @@ int following_get(snac *snac, const char *actor, d_char **data)
 2 }
 3 
 4 
 5-d_char *following_list(snac *snac)
 6+xs_list *following_list(snac *snac)
 7 /* returns the list of people being followed */
 8 {
 9     xs *spec = xs_fmt("%s/following/" "*.json", snac->basedir);
10     xs *glist = xs_glob(spec, 0, 0);
11-    char *p, *v;
12-    d_char *list = xs_list_new();
13+    xs_list *p;
14+    xs_str *v;
15+    xs_list *list = xs_list_new();
16 
17     /* iterate the list of files */
18     p = glist;
+33, -1
 1@@ -957,6 +957,38 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
 2             xs *out   = NULL;
 3             xs *actor = NULL;
 4 
 5+            if (logged_in && strcmp(uid, "search") == 0) { /** **/
 6+                /* search for accounts starting with q */
 7+                const char *q = xs_dict_get(args, "q");
 8+
 9+                if (!xs_is_null(q)) {
10+                    out      = xs_list_new();
11+                    xs *wing = following_list(&snac1);
12+                    xs *wers = follower_list(&snac1);
13+                    xs_list *p;
14+
15+                    xs_list *lsts[] = { wing, wers, NULL };
16+                    int n;
17+                    for (n = 0; (p = lsts[n]) != NULL; n++) {
18+                        xs_str *v;
19+
20+                        while (xs_list_iter(&p, &v)) {
21+                            xs *actor = NULL;
22+
23+                            if (valid_status(object_get(v, &actor))) {
24+                                const char *uname = xs_dict_get(actor, "preferredUsername");
25+
26+                                if (!xs_is_null(uname) && xs_startswith(uname, q)) {
27+                                    xs *acct = mastoapi_account(actor);
28+
29+                                    out = xs_list_append(out, acct);
30+                                }
31+                            }
32+                        }
33+                    }
34+                }
35+            }
36+            else
37             /* is it a local user? */
38             if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
39                 if (opt == NULL) {
40@@ -965,7 +997,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
41                     out   = mastoapi_account(actor);
42                 }
43                 else
44-                if (strcmp(opt, "statuses") == 0) {
45+                if (strcmp(opt, "statuses") == 0) { /** **/
46                     /* the public list of posts of a user */
47                     xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
48                     xs_list *p   = timeline;
M snac.h
+1, -1
1@@ -119,7 +119,7 @@ int following_add(snac *snac, const char *actor, const xs_dict *msg);
2 int following_del(snac *snac, const char *actor);
3 int following_check(snac *snac, const char *actor);
4 int following_get(snac *snac, const char *actor, d_char **data);
5-d_char *following_list(snac *snac);
6+xs_list *following_list(snac *snac);
7 
8 void mute(snac *snac, const char *actor);
9 void unmute(snac *snac, const char *actor);