package main // Exhibit 4. The guestbook stores only user ids. Names and pictures are asked of account at render // time, in one batch call (rc.users), so a change at account shows on the next load. Storing a copy // of someone's name or picture is how an app ends up showing the old one. import "core:fmt" import "core:strings" import "core:unicode/utf8" import "ratcloud:rc" Guest :: struct { user: rc.User `json:"user"`, note: string `json:"note"`, at: string `json:"at"`, } guestbook :: proc() -> []Guest { rows := rc.query_rows(db(), "SELECT user_id, note, at FROM guestbook ORDER BY at DESC LIMIT 60") ids := make([dynamic]i64) notes := make([dynamic]string) ats := make([dynamic]i64) for rc.next(&rows) { append(&ids, rc.col_int(&rows, 0)) append(¬es, strings.clone(rc.col_text(&rows, 1))) append(&ats, rc.col_int(&rows, 2)) } users := rc.users(ids[:]) // one call for the whole list out := make([dynamic]Guest) // A deleted person's entry is not shown: an unnamed line reads as a broken one. The row stays // (account deletes the person, not what they wrote). If account answered nobody at all, it is more // likely down than everyone gone, so then the entries show without names. down := len(ids) > 0 && len(users) == 0 for id, i in ids { u, ok := users[id] if !ok && !down do continue if !ok do u = {id = 0, username = "", name = "someone", picture = ""} append(&out, Guest{u, notes[i], iso(ats[i])}) } return out[:] } e04 :: proc() -> Exhibit { return { n = 4, try_ = "sign the guestbook", wing = "Identity", title = "Your name and picture follow you", promise = "Change your name and picture at account, and every app shows the new ones.", block = "rc.users(ids) turns stored ids into current names and pictures, one call per page. An app keeps ids, never copies of names or pictures.", src = {"e04_guestbook.odin", #load("e04_guestbook.odin", string)}, files = srcs({"robot.odin", #load("robot.odin", string)}), script = {"web/e04.js", #load("web/e04.js", string)}, show = proc(me: ^rc.User) -> string { list := strings.builder_make() for &g in guestbook() { mine := me != nil && g.user.id == me.id rc.render_to(&list, `{{2}}
{{3}} ยท {{4}}
`, {{"1", rc.raw(` class="mine"` if mine else "")}, {"2", rc.raw(person(&g.user))}, {"3", g.note}, {"4", rc.raw(ago(parse_time(g.at)))}}) } form := `
` if me != nil else signin_line("to sign the guestbook.") listed := strings.to_string(list) if strings.builder_len(list) > 0 else `

Nobody has signed yet.

` return cat(form, `

Change your name or picture at account, then reload: your entry changes too. (The robot renames itself to the time of every check, which is how this exhibit checks itself.)

`, `
`, listed, `
`) }, api = proc(a: ^Api) -> bool { switch a.do_ { case "list": inst, _ := meta("instance") reply(a, struct { env: string `json:"env"`, instance: string `json:"instance"`, guestbook: []Guest `json:"guestbook"`, }{env().env, inst, guestbook()}) return true case "sign": if !a.post do return false if a.me == nil {reply_err(a, 401, "sign in to sign the guestbook"); return true} note := in_str(a, "note") if utf8.rune_count_in_string(note) > 140 { n := 0 for _, i in note {if n == 140 {note = note[:i]; break}; n += 1} } note = strings.trim_space(note) if note == "" {reply_err(a, 400, "say something"); return true} must("INSERT INTO guestbook (user_id, note, at) VALUES (?, ?, ?) ON CONFLICT(user_id) DO UPDATE SET note = excluded.note, at = excluded.at", a.me.id, note, now()) reply_raw(a, `{"ok":true}`) return true } return false }, // The robot renames itself at account, then reads the guestbook as a visitor would. check = proc() -> Check { b, err := robot_at() if err != "" do return fail(err) r := post(b, self_url("/api/4/sign"), fmt.tprintf(`{{"note":%s}}`, jq(fmt.tprintf("The robot was here, on %s.", env().env)))) if r.status != 200 do return fail(fmt.tprintf("the robot could not sign the guestbook (%d)", r.status)) name := cat("Zoo Robot ", hm(now())) if code, _ := account_api(b, "profile", fmt.tprintf(`{{"name":%s}}`, jq(name))); code != 200 do return fail(fmt.tprintf("account would not rename the robot (%d)", code)) l := get(new_browser(), self_url("/api/4/list")) me := robot_user().id for g in jarr(l.j, "guestbook") { if jint(g, "user", "id") != me do continue if got := jstr(g, "user", "name"); got != name do return fail(fmt.tprintf(`renamed to "%s" at account, but the guestbook still says "%s"`, name, got)) return pass(fmt.tprintf(`renamed the robot to "%s" at account; the guestbook showed the new name on the next load`, name)) } return fail("the robot's entry is missing from the guestbook") }, } }