// Modified 2026-11-29 from Lucille (AGPL-3.0): trail status line (pace / distance left / ETA); // street address (reverse geocoding) removed. // See NOTICE.md. class Application { username: string; secretKey: Uint8Array; receivingBoxId: Uint8Array; userId: Uint8Array; senderPublicKey: Uint8Array; updateTimer: number | null; private lastLocation: LocationInfo | null; map: L.Map | null; marker: L.Marker | null; constructor() { this.updateTimer = null; this.lastLocation = null; this.map = null; this.marker = null; } private getBatteryClassName(level: number): string { let clazzName = "battery "; if (level >= 95) { clazzName += "battery-100"; } else if (level >= 85) { clazzName += "battery-90"; } else if (level >= 75) { clazzName += "battery-80"; } else if (level >= 65) { clazzName += "battery-70"; } else if (level >= 55) { clazzName += "battery-60"; } else if (level >= 45) { clazzName += "battery-50"; } else if (level >= 35) { clazzName += "battery-40"; } else if (level >= 25) { clazzName += "battery-30"; } else if (level >= 15) { clazzName += "battery-20"; } else if (level >= 5) { clazzName += "battery-10"; } else { clazzName += "battery-0"; } return clazzName; } getLocation(): LocationInfo | null { return this.lastLocation; } async setLocation(locInfo: LocationInfo) { app.lastLocation = locInfo; let batteryPowerDiv = document.getElementById("battery-power") as HTMLDivElement; if (locInfo.battery_level != null) { batteryPowerDiv.innerText = `${locInfo.battery_level}%`; } else { batteryPowerDiv.innerText = ""; } // trail fork: pace / distance left / ETA, only when the share carries a route let trailStatus = document.getElementById("trail-status") as HTMLParagraphElement | null; if (trailStatus != null) { let s = trail.status(locInfo.time, locInfo.rem_m, locInfo.speed); trailStatus.innerText = s ?? ""; trailStatus.hidden = s == null; } this.updateTimeAgo(); // clear out the old timer, and schedule a new one if (this.updateTimer != null) { clearInterval(this.updateTimer); } this.updateTimer = setInterval(this.updateTimeAgo, 60 * 1000); let compass = document.getElementById("compass") as HTMLSpanElement; if (locInfo.bearing != null) { compass.style.display = "inline-block"; compass.style.transform = `rotate(${locInfo.bearing}deg)`; } else { compass.style.display = "none"; } let transportationMode = document.getElementById("transportation-mode") as HTMLImageElement; switch (locInfo.movement) { case zood.MovementType.Bicycle: transportationMode.src = "../images/activities/bike.svg"; break; case zood.MovementType.OnFoot: case zood.MovementType.Running: case zood.MovementType.Walking: transportationMode.src = "../images/activities/walk.svg"; break; case zood.MovementType.Vehicle: transportationMode.src = "../images/activities/car.svg"; break; case null: default: transportationMode.src = "" break; } transportationMode.hidden = transportationMode.src == "" // Make sure the map has loaded first if (app.map == null) { return; } let pos = L.latLng(locInfo.latitude, locInfo.longitude) // if we already have a marker for the user, update it. otherwise, build one. if (app.marker == null) { app.marker = L.marker(pos).addTo(app.map as L.Map).bindPopup(app.username); app.map.flyTo(pos, 15); } else { app.marker.setLatLng(pos); } // set the battery level let batteryIcon = document.getElementById("battery-icon") as HTMLSpanElement; batteryIcon.className = this.getBatteryClassName(locInfo.battery_level); // trail fork: no street address, so positions aren't sent to a reverse-geocoding service } private updateTimeAgo(): void { let updateTimeDiv = document.getElementById("update-time") as HTMLDivElement; let location = app.lastLocation; if (location == null) { updateTimeDiv.innerHTML = `   •   …`; return; } let ago = zdtime.relativeTime(new Date().getTime(), location.time, "en-US") updateTimeDiv.innerHTML = `   •   ${ago}`; } } const app = new Application(); const lightMapStyle = "https://www.zood.xyz/static/map-style.json"; const darkMapStyle = "https://www.zood.xyz/static/map-style-dark.json"; // Matches while the viewer's system is in dark mode const darkScheme = window.matchMedia("(prefers-color-scheme: dark)"); function mapStyle(): string { return darkScheme.matches ? darkMapStyle : lightMapStyle; } interface EncryptedData { cipher_text: Uint8Array; nonce: Uint8Array; } interface PublicKeyResponse { public_key: string; } interface LocationInfo { accuracy: number; battery_charging: boolean; battery_level: number; bearing: number | null; latitude: number; longitude: number; movement: zood.MovementType | null; speed: number | null; /** Trail fork: metres along the route to the finish; absent from upstream apps. */ rem_m?: number | null; time: number; type: string | null | undefined; } function extractDataFromFragment(): boolean { if (!window.document.location.hash) { return false; } let fragment = window.document.location.hash.substring(1); let keyVals = fragment.split("&"); for (var kv of keyVals) { let parts = kv.split("="); if (parts.length != 2) { continue; } switch (parts[0]) { case "u": // trail fork: links URL-encode the name try { app.username = decodeURIComponent(parts[1]); } catch { app.username = parts[1]; } break; case "k": { // trail fork: base64url (short links) or hex (old links) let k = trail.decodeKey(parts[1], 32); if (k == null) { console.log("ERROR: secret key in fragment is invalid"); return false; } app.secretKey = k; break; } case "b": app.receivingBoxId = sodium.from_hex(parts[1]); if (app.receivingBoxId.length != zood.DROP_BOX_ID_LENGTH) { console.log( "ERROR: box id in fragment is the incorrect length", app.receivingBoxId.length ); return false; } break; case "i": { // trail fork: base64url (short links) or hex (old links) let id = trail.decodeKey(parts[1], zood.USER_ID_LENGTH); if (id == null) { console.log("ERROR: user id in fragment is invalid"); return false; } app.userId = id; break; } } } return true; } async function onPackageReceived(pkg: zood.Package) { // console.log("onPackageReceived:", pkg); // make sure this is the box we're interested in if (!sodium.memcmp(pkg.boxId, app.receivingBoxId)) { console.log( "Received a package from a drop box for which we're not interested", sodium.to_hex(pkg.boxId) ); return; } let msgStr = String.fromCharCode.apply(this, pkg.bytes); let msgObj = JSON.parse(msgStr); let encData = { cipher_text: sodium.from_base64( msgObj.cipher_text, sodium.base64_variants.ORIGINAL ), nonce: sodium.from_base64(msgObj.nonce, sodium.base64_variants.ORIGINAL) } as EncryptedData; let unencData = sodium.crypto_box_open_easy( encData.cipher_text, encData.nonce, app.senderPublicKey, app.secretKey ); let locInfoStr = String.fromCharCode.apply(this, unencData); let locInfo = JSON.parse(locInfoStr) as LocationInfo; if (!locInfo.type) { console.log( "received package without a 'type'. Expecting LocationInfo, found: ", locInfoStr ); return; } if (locInfo.type != "location_info") { console.log("unexpected message type:", locInfo.type); return; } console.log(locInfoStr); app.setLocation(locInfo); } async function run() { console.log("run"); if (!extractDataFromFragment()) { return; } if (app.secretKey == null || app.userId == null) { console.log("ERROR: link is missing the key or user id"); return; } // trail fork: short links don't carry the box id; it's derived from the secret key if (app.receivingBoxId == null) { app.receivingBoxId = await trail.deriveBoxId(app.secretKey); } // initialize the Leaflet map const map = L.map('map').setView([0, 0], 2) const glLayer = L.maplibreGL({ // style: 'https://tiles.openfreemap.org/styles/liberty', style: mapStyle(), // The plugin shows this instead of the attributions in the style, which has none attributionControl: { customAttribution: 'OpenFreeMap © OpenMapTiles Data from OpenStreetMap', }, }).addTo(map); app.map = map; // switch the map along with the system's light or dark mode darkScheme.addEventListener("change", () => { glLayer.getMaplibreMap().setStyle(mapStyle()); }); let usernameSpans = document.getElementsByClassName("username") as HTMLCollectionOf; for (let i = 0; i < usernameSpans.length; i++) { let span = usernameSpans.item(i); if (span == null) { break; } span.innerText = app.username; } try { let client = new zood.Client(null); let pkr = await client.getUserPublicKey(app.userId); app.senderPublicKey = pkr.public_key; // console.log("got spk:", app.senderPublicKey); } catch (err) { console.log("error retrieving public key: ", err); return; } watchDropBox(); } const RECONNECT_MIN_DELAY_MS = 1000; const RECONNECT_MAX_DELAY_MS = 60 * 1000; // A connection that stays up at least this long resets the reconnect backoff const STABLE_CONNECTION_MS = 60 * 1000; // Watches the receiving drop box, reconnecting whenever the socket to oscar fails or drops. oscar // sends the box's current package as soon as it's watched, so a reconnect also catches up on any // location update that arrived while we were disconnected. function watchDropBox() { let delay = RECONNECT_MIN_DELAY_MS; let retryTimer: number | null = null; const scheduleReconnect = () => { if (retryTimer != null) { return; } // jitter, so viewers don't all reconnect at the same instant after an oscar restart let wait = delay / 2 + Math.random() * delay / 2; console.log(`reconnecting drop box watcher in ${Math.round(wait)}ms`); retryTimer = window.setTimeout(connect, wait); delay = Math.min(delay * 2, RECONNECT_MAX_DELAY_MS); }; const connect = async () => { retryTimer = null; let watcher = new zood.DropBoxWatcher(); watcher.onPackageReceived = onPackageReceived; let connectedAt = 0; watcher.onDisconnect = () => { console.log("drop box watcher disconnected"); if (Date.now() - connectedAt >= STABLE_CONNECTION_MS) { delay = RECONNECT_MIN_DELAY_MS; } scheduleReconnect(); }; try { await watcher.connect(zood.DropBoxServer.production); } catch (err) { console.log("error connecting drop box watcher", err); scheduleReconnect(); return; } connectedAt = Date.now(); console.log("drop box watcher is connected"); watcher.watch(app.receivingBoxId); }; // when the network comes back, don't sit out the rest of the backoff window.addEventListener("online", () => { if (retryTimer != null) { clearTimeout(retryTimer); delay = RECONNECT_MIN_DELAY_MS; connect(); } }); connect(); }