| 1 | // The reader pages' script. It takes the read link out of the address |
| 2 | // bar first, and keeps it in this closure alone: never in the page, in |
| 3 | // storage or in a log. Nothing is sent until the reader clicks. |
| 4 | (function () { |
| 5 | "use strict"; |
| 6 | let link = location.hash.slice(1); |
| 7 | history.replaceState(null, "", location.pathname + location.search); |
| 8 | |
| 9 | const box = document.getElementById("reader"); |
| 10 | if (!box) return; |
| 11 | const el = function (id) { return document.getElementById(id); }; |
| 12 | const notices = ["r-missing", "r-refused", "r-other", "r-unknown"]; |
| 13 | function show(id) { |
| 14 | notices.forEach(function (n) { if (el(n)) el(n).hidden = n !== id; }); |
| 15 | } |
| 16 | const sent = { credentials: "same-origin", cache: "no-store", redirect: "error" }; |
| 17 | function post(path, body) { |
| 18 | return fetch(path, Object.assign({ |
| 19 | method: "POST", |
| 20 | headers: { "Content-Type": "application/json", "X-Valley-Reader": "1" }, |
| 21 | body: JSON.stringify(body), |
| 22 | }, sent)).then(function (r) { return r.status; }); |
| 23 | } |
| 24 | // signedIn asks the server which link this browser's session was |
| 25 | // redeemed from: its credential ID, or "" for no session. |
| 26 | function signedIn() { |
| 27 | return fetch("/-/session", sent).then(function (r) { |
| 28 | if (r.status !== 200) throw new Error("the session state did not arrive"); |
| 29 | return r.json(); |
| 30 | }).then(function (s) { return s.link; }); |
| 31 | } |
| 32 | function done() { |
| 33 | link = ""; |
| 34 | location.replace("/"); |
| 35 | } |
| 36 | |
| 37 | if (box.dataset.page === "logout") { |
| 38 | const out = el("r-logout"); |
| 39 | if (!out) return; |
| 40 | out.hidden = false; |
| 41 | out.addEventListener("click", function () { |
| 42 | out.disabled = true; |
| 43 | post("/-/logout", {}).then(function (status) { |
| 44 | if (status === 204) return done(); |
| 45 | show("r-unknown"); |
| 46 | out.disabled = false; |
| 47 | }, function () { |
| 48 | show("r-unknown"); |
| 49 | out.disabled = false; |
| 50 | }); |
| 51 | }); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | const redeem = el("r-redeem"); |
| 56 | const swap = el("r-switch"); |
| 57 | if (!/^lk-[0-9a-f]{16}\.[A-Za-z0-9_-]{43}$/.test(link)) { |
| 58 | link = ""; |
| 59 | show("r-missing"); |
| 60 | return; |
| 61 | } |
| 62 | const id = link.split(".")[0]; |
| 63 | // unsure is set once a request's outcome is unknown: no answer came, or |
| 64 | // one that is neither success nor a refusal. The link may be used, and |
| 65 | // this browser may hold the session it started. |
| 66 | let unsure = false; |
| 67 | // mode is which button is shown: redeem alone, or sign out, then redeem. |
| 68 | function mode(switching) { |
| 69 | redeem.hidden = switching; |
| 70 | swap.hidden = !switching; |
| 71 | el("r-switching").hidden = !switching; |
| 72 | } |
| 73 | function unknown(button) { |
| 74 | unsure = true; |
| 75 | show("r-unknown"); |
| 76 | button.disabled = false; |
| 77 | } |
| 78 | function answered(status, button) { |
| 79 | if (status === 204) return done(); |
| 80 | if (status === 403) { |
| 81 | link = ""; |
| 82 | show("r-refused"); |
| 83 | redeem.hidden = swap.hidden = true; |
| 84 | return; |
| 85 | } |
| 86 | if (status !== 409) return unknown(button); |
| 87 | show("r-other"); |
| 88 | mode(true); |
| 89 | button.disabled = false; |
| 90 | } |
| 91 | // attempt sends the link, after a logout when signOut is set. After an |
| 92 | // unknown outcome it first asks which session this browser holds. One |
| 93 | // from this link means the earlier attempt succeeded, so nothing is |
| 94 | // sent. With none, there is no one to sign out, and the logout is left |
| 95 | // out, so a retry never ends a session the earlier attempt started. |
| 96 | function attempt(button, signOut) { |
| 97 | button.disabled = true; |
| 98 | (unsure ? signedIn() : Promise.resolve(null)).then(function (current) { |
| 99 | if (current === id) return done(); |
| 100 | if (current === "") signOut = false; |
| 101 | return (signOut ? post("/-/logout", {}) : Promise.resolve(204)).then(function (status) { |
| 102 | if (status !== 204) return unknown(button); |
| 103 | return post("/-/redeem", { credential: link }).then(function (status) { |
| 104 | answered(status, button); |
| 105 | }); |
| 106 | }); |
| 107 | }).catch(function () { unknown(button); }); |
| 108 | } |
| 109 | redeem.addEventListener("click", function () { attempt(redeem, false); }); |
| 110 | swap.addEventListener("click", function () { attempt(swap, true); }); |
| 111 | mode(box.dataset.session === "1"); |
| 112 | })(); |