Zoo

← back to the zoo

5. Find a friend

Any app can search for people by name, including people who have never used it.

rc.search_users(q) finds people by the start of their username or of any word of their name, across the whole platform.

This is the code serving the zoo right now: the bytes this very program was built from (each file is embedded when the zoo is compiled), at commit 55c71513c5 (live). The zoo's own self-check fetches this page and compares it byte for byte with the file it runs.

e05_find_a_friend.odin sha256 7d162716daa6 · raw

1package main
2
3// Exhibit 5. The directory is open to every app, so a brand-new app can find people who have never
4// used it. The zoo only searches for signed-in people, so it is not an open directory for the whole
5// internet.
6
7import "core:strings"
8import "ratcloud:rc"
9
10e05 :: proc() -> Exhibit {
11	return {
12		n = 5, try_ = "find a friend", wing = "Identity",
13		title = "Find a friend",
14		promise = "Any app can search for people by name, including people who have never used it.",
15		block = "rc.search_users(q) finds people by the start of their username or of any word of their name, across the whole platform.",
16		src = {"e05_find_a_friend.odin", #load("e05_find_a_friend.odin", string)},
17		script = {"web/e05.js", #load("web/e05.js", string)},
18		show = proc(me: ^rc.User) -> string {
19			if me == nil do return signin_line("to search for people.")
20			return `<form class="row" id="find"><input name="q" placeholder="A name or username" autocomplete="off" required><button>Search</button></form><div class="people" id="found"></div>`
21		},
22		api = proc(a: ^Api) -> bool {
23			if a.do_ != "search" do return false
24			if a.me == nil {reply_err(a, 401, "sign in to search"); return true}
25			reply(a, struct {
26					users: []rc.User `json:"users"`,
27				}{rc.search_users(in_str(a, "q"), 12)})
28			return true
29		},
30		// rc-check is root's robot: it has an account but has never used the zoo.
31		check = proc() -> Check {
32			hits := rc.search_users("rc-check", 5)
33			names := make([dynamic]string)
34			for u in hits {
35				if u.username == "rc-check" do return pass("found @rc-check, who has an account but has never used the zoo")
36				append(&names, u.username)
37			}
38			return fail(cat(`searching "rc-check" did not find root's robot, who has an account`, cat(" (found: ", strings.join(names[:], ", "), ")") if len(names) > 0 else ""))
39		},
40	}
41}

web/e05.js sha256 694ccc832019 · raw

1document.getElementById("find")?.addEventListener("submit", async (ev) => {
2  ev.preventDefault();
3  const out = document.getElementById("found");
4  const j = await zoo.call("/api/5/search?q=" + encodeURIComponent(ev.target.q.value));
5  out.innerHTML = j.error ? zoo.esc(j.error) : (j.users.length ? j.users.map(zoo.person).join("") : '<p class="muted">Nobody by that name.</p>');
6});