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, `
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.)
`, `