package main // Exhibit 16. Two halves, both push's: // 1. Allowing: the card sends the person to push's own page (`/?enable=1&back=…`), which knows the // zoo from the Referer. Only push can subscribe a browser, so no app can plant an endpoint for // anyone; and no push code runs on the zoo's origin. // 2. Sending: the zoo asks push to notify the person, with an assertion that names the zoo: // POST rc.app_url("push") + "/?api=send" {user, title, body, url} rc.app_headers("push", true) // push delivers only to browsers where that person allowed the zoo (`sent: 0` otherwise). // "In ten seconds": the zoo takes the assertion now (it lives two minutes), answers at once // (rc.done), and goes on: waits ten seconds, then sends. // Human-only: a notification arriving on a phone cannot be seen by the zoo. The check proves the send // path (the robot has no browser, so `sent` may be 0); a person presses "this worked for me". import "core:encoding/json" import "core:fmt" import "ratcloud:rc" ping_send :: proc(uid: i64, headers: map[string]string) -> (json.Value, string) { hs := make(map[string]string) for k, v in headers do hs[k] = v hs["Content-Type"] = "application/json" body := fmt.tprintf(`{{"user":%d,"title":"The zoo says hello","body":"Exhibit 16 worked. Tap to go back to the zoo.","url":%s}}`, uid, jq(cat(rc.app_url("zoo"), "/#e16"))) res := rc.fetch({method = "POST", url = cat(rc.app_url("push"), "/?api=send"), headers = hs, body = transmute([]u8)body, timeout_ms = 20_000}) r := parse(string(res.body)) if res.status != 200 || !jhas(r, "sent") do return nil, fmt.tprintf("push answered %d: %s%s", res.status, clip(string(res.body), 160), res.err) return r, "" } ping_state :: proc(uid: i64) -> json.Value { v, _ := meta(fmt.tprintf("ping16.%d", uid)) return parse(v) } ping_set :: proc(uid: i64, state_json: string) { meta_set(fmt.tprintf("ping16.%d", uid), state_json) } e16 :: proc() -> Exhibit { return { n = 16, try_ = "get a notification", wing = "Notifications", kind = "human", title = "Ping me", promise = "An app can send a notification to your phone or browser.", block = `The push app keeps each person's devices (Web Push); an app asks it to notify someone with rc.app_headers("push", true).`, src = {"e16_ping_me.odin", #load("e16_ping_me.odin", string)}, script = {"web/e16.js", #load("web/e16.js", string)}, show = proc(me: ^rc.User) -> string { if me == nil do return signin_line("to be notified.") return cat(`
The first time, push asks you to allow the zoo on this browser.
`, seen_button(16, me, "The notification arrived and opened the zoo: this worked for me")) }, api = proc(a: ^Api) -> bool { if a.me == nil {reply_err(a, 401, "sign in first"); return true} uid := a.me.id switch a.do_ { case "status": reply_raw(a, fmt.tprintf(`{{"state":%s}}`, to_json(ping_state(uid)))) return true case "send": if !a.post do return false hs, err := rc.app_headers("push", true) if err != "" {reply_err(a, 502, err); return true} r, serr := ping_send(uid, hs) if serr != "" {reply_err(a, 502, serr); return true} reply(a, r) return true case "notify": if !a.post do return false hs, err := rc.app_headers("push", true) // taken now: it names this person and lives two minutes if err != "" {reply_err(a, 502, err); return true} ping_set(uid, fmt.tprintf(`{{"state":"waiting","at":%d}}`, now())) reply_raw(a, `{"scheduled":true,"in":10}`) rc.done(a.w) // the browser has its answer; the rest runs inside this request's own time rc.sleep(10_000) r, serr := ping_send(uid, hs) if serr != "" { ping_set(uid, fmt.tprintf(`{{"state":"error","error":%s,"at":%d}}`, jq(serr), now())) } else { ping_set(uid, fmt.tprintf(`{{"state":%s,"sent":%d,"signed_out":%d,"at":%d}}`, `"sent"` if jint(r, "sent") > 0 else `"nobody"`, jint(r, "sent"), jint(r, "signed_out"), now())) } return true } return false }, // The send path, as the robot: push must accept the assertion and answer with its counts. It // has no browser, so sent is expected to be 0. check = proc() -> Check { b, err := robot_at() if err != "" do return fail(err) r := post(b, self_url("/api/16/send")) if r.status != 200 || r.j == nil do return fail(fmt.tprintf("push's send failed (%d): %s", r.status, jstr(r.j, "error") if r.j != nil else clip(r.body, 120))) return pass(fmt.tprintf("push accepted the zoo's send for the robot: sent %d, gone %d, signed out %d, failed %d", jint(r.j, "sent"), jint(r.j, "gone"), jint(r.j, "signed_out"), jint(r.j, "failed")), r.j) }, } }