gitoriaLog in with ident

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit44b7d9f944b7d9f9tracker#6: /schedule — upcoming episodes of followed shows, soonest firstmre44b7d9f9/watches.hl

2.9 KB

  1. // watches.hl — PER-USER WATCH STATE (episode/season checks, tracker.worldapi.org#4), reused as-is
  2. // (not copied) by /unwatched and /schedule (tickets #5/#6: "same check icon and watch logic as the
  3. // show page — reuse, don't copy"). storage/mpackdb/watches.db, migrated by tools/migrate.hl
  4. // (tracker.worldapi.org#2): { targetType ('episode'|'season'), target, user, at, oldId }.
  5. import { MPackDB } from 'hl:mpackdb'
  6. import { env } from 'hl:proc'
  7. import { now } from 'hl:time'
  8. import { randomBytes } from 'hl:crypto'
  9. static envOr = (name, fallback) => {
  10. let v = env(name)
  11. return v != null && v.trim() != '' ? v.trim() : fallback
  12. }
  13. static storageDir = envOr('TRACKER_STORAGE', './storage/mpackdb')
  14. static watchesTable = new MPackDB(file = storageDir + '/watches.db', primaryKey = '@id', indexes = ['!oldId'])
  15. // every watch of this user — a linear scan (11692 rows total across every user, migrated data; cheap
  16. // once per page load) — no declared index on `user`
  17. static watchesOfUser = (userId) => {
  18. if (userId == null) { return [] }
  19. let out = []
  20. for (w of watchesTable.find(null, null)) { if (w.user == userId) { out.push(w) } }
  21. return out
  22. }
  23. static findWatch = (rows, targetType, targetId) => {
  24. for (w of rows) { if (w.targetType == targetType && w.target == targetId) { return w } }
  25. return null
  26. }
  27. static isWatched = (rows, targetType, targetId) => { return findWatch(rows, targetType, targetId) != null }
  28. // the check's class — "a disc with a check in it", solid once watched, regular while not (the
  29. // SAME icon and class the show page uses, tracker.worldapi.org#4/#5 — /unwatched and /schedule
  30. // reuse this instead of each declaring their own)
  31. static watchClassOf = (watched) => { return watched ? 'checkicon solid' : 'checkicon regular' }
  32. // a NEW watch's oldId can't be null: the table's `!oldId` UNIQUE index would refuse a second one
  33. // (the migrated rows all carry a real Mongo hex id; a fresh watch gets its own unique marker instead)
  34. static setWatched = (userId, targetType, targetId, rows, watched) => {
  35. let existing = findWatch(rows, targetType, targetId)
  36. if (watched) {
  37. if (existing != null) { return true }
  38. let id = watchesTable.put({ id = randomBytes(8, 'hex') targetType = targetType target = targetId user = userId at = now() oldId = 'app:' + randomBytes(8, 'hex') })
  39. return id != null
  40. }
  41. if (existing == null) { return true }
  42. return watchesTable.delete(existing.id) > 0
  43. }
  44. // a season check toggles the season's OWN watch record AND every one of its episodes (creator: "if
  45. // the season is checked or unchecked all its episodes get checked or unchecked") — `rows` is the
  46. // snapshot from BEFORE this batch; every target below is distinct, so the staleness across the loop
  47. // never matters (no two calls touch the same target)
  48. static setSeasonWatched = (userId, seasonId, episodeIds, rows, watched) => {
  49. let ok = setWatched(userId, 'season', seasonId, rows, watched)
  50. for (eid of episodeIds) { ok = setWatched(userId, 'episode', eid, rows, watched) && ok }
  51. return ok
  52. }

Branches

Latest commits

  • 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