tracker
All repositories: gitoria
3.3 KB
// watches.hl — PER-USER WATCH STATE (episode/season checks, tracker.worldapi.org#4), reused as-is// (not copied) by /unwatched and /schedule (tickets #5/#6: "same check icon and watch logic as the// show page — reuse, don't copy"). storage/mpackdb/watches.db, migrated by tools/migrate.hl// (tracker.worldapi.org#2): { targetType ('episode'|'season'), target, user, at, oldId }.import { MPackDB } from 'hl:mpackdb'import { env } from 'hl:proc'import { now } from 'hl:time'import { randomBytes } from 'hl:crypto'static envOr = (name, fallback) => {let v = env(name)return v != null && v.trim() != '' ? v.trim() : fallback}static storageDir = envOr('TRACKER_STORAGE', './storage/mpackdb')static watchesTable = new MPackDB(file = storageDir + '/watches.db', primaryKey = '@id', indexes = ['!oldId'])// every watch of this user — a linear scan (11692 rows total across every user, migrated data; cheap// once per page load) — no declared index on `user`static watchesOfUser = (userId) => {if (userId == null) { return [] }let out = []for (w of watchesTable.find(null, null)) { if (w.user == userId) { out.push(w) } }return out}// '<targetType>:<target>' → true for every watch in `rows`: built ONCE per page load so a list asks// `watched['episode:' + e.id] == true` instead of `isWatched` scanning all the user's watches per episode// (/my/unwatched, tracker.worldapi.org#8: 831 rows × 11,692 watches took ~15 s)static watchedSet = (rows) => {let m = {}for (w of rows) { if (w.target != null) { m[w.targetType + ':' + w.target] = true } }return m}static findWatch = (rows, targetType, targetId) => {for (w of rows) { if (w.targetType == targetType && w.target == targetId) { return w } }return null}static isWatched = (rows, targetType, targetId) => { return findWatch(rows, targetType, targetId) != null }// the check's class — "a disc with a check in it", solid once watched, regular while not (the// SAME icon and class the show page uses, tracker.worldapi.org#4/#5 — /unwatched and /schedule// reuse this instead of each declaring their own)static watchClassOf = (watched) => { return watched ? 'checkicon solid' : 'checkicon regular' }// a NEW watch's oldId can't be null: the table's `!oldId` UNIQUE index would refuse a second one// (the migrated rows all carry a real Mongo hex id; a fresh watch gets its own unique marker instead)static setWatched = (userId, targetType, targetId, rows, watched) => {let existing = findWatch(rows, targetType, targetId)if (watched) {if (existing != null) { return true }let id = watchesTable.put({ id = randomBytes(8, 'hex') targetType = targetType target = targetId user = userId at = now() oldId = 'app:' + randomBytes(8, 'hex') })return id != null}if (existing == null) { return true }return watchesTable.delete(existing.id) > 0}// a season check toggles the season's OWN watch record AND every one of its episodes (creator: "if// the season is checked or unchecked all its episodes get checked or unchecked") — `rows` is the// snapshot from BEFORE this batch; every target below is distinct, so the staleness across the loop// never matters (no two calls touch the same target)static setSeasonWatched = (userId, seasonId, episodeIds, rows, watched) => {let ok = setWatched(userId, 'season', seasonId, rows, watched)for (eid of episodeIds) { ok = setWatched(userId, 'episode', eid, rows, watched) && ok }return ok}
Branches
- mainmain branch
Latest commits
- 49e1f61edeploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre
- 54070a4etracker#8: /my/unwatched + /my/schedule (301 from old), S01E01, title (year), 1 episode, watched-set lookup (unwatched 15s -> 1s)mre
- 3251488atracker#7: /my/shows (followed shows, newest follow first, poster, title, last watched SxxEyy); gate can take screenshots (TRACKER_GATE_SHOTS)mre
- 44b7d9f9tracker#6: /schedule — upcoming episodes of followed shows, soonest firstmre
- 91c9fc8ctracker#5: /unwatched — unwatched released episodes of followed shows, newest firstmre
- a97c0295tracker#4: show page /shows/:slug (header, seasons, episodes, watch checks) + tools/relink-episode-seasons.hlmre
- 05f407c5tracker: no border on any button except inverted ones (Log out, ident status and identities too); header brand weight 100mre
- 17375427tracker#2: tools/migrate.hl + tools/verify.hl — old MongoDB data into mpackdb with new idsmre
- 31aac936tracker#3: no border on the header and on filled buttons; inverted buttons keep theirsmre
- 2ad9d29ctracker#1: login exactly like calendar (identity selector in the header, empty homepage)mre
- 3691e176tracker#1: empty tracker with the ident login (state of 2026-09-27)mre