gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchrecords-hm55d3edaa05|||PR records: HM55 start, PROCEDURES.mdTrackerrecords-hm55/lib/watches.hl

8.9 KB

  1. import { cardOf, episodeRowsOf, episodeCode, episodeById, showById } from './shows.hl'
  2. import { followedShowIds, adultHiddenForUser } from './follows.hl'
  3. // lib/watches.hl — PER-USER WATCH STATE (episode/season checks, tracker.worldapi.org#4), reused as-is
  4. // (not copied) by /unwatched and /schedule (tickets #5/#6: "same check icon and watch logic as the
  5. // show page — reuse, don't copy"). storage/mpackdb/watches.db, migrated by tools/migrate.hl
  6. // (tracker.worldapi.org#2): { targetType ('episode'|'season'|'movie'), target, user, at, oldId }. 'movie' (mission 058,
  7. // tracker#24): target = the movie's shows.db id. A season's CHECK on the page is derived from its episodes (components/show.hl).
  8. import { MPackDB } from 'hl:mpackdb'
  9. import { now } from 'hl:time'
  10. import { randomBytes } from 'hl:crypto'
  11. import { storageDir, todayStr } from './util.hl'
  12. static watchesTable = new MPackDB(file = storageDir + '/watches.db', primaryKey = '@id', indexes = ['!oldId'])
  13. // every watch of this user — a linear scan (11692 rows total across every user, migrated data; cheap
  14. // once per page load) — no declared index on `user`
  15. // mission 062: KEPT per user until that user's next write (setWatched) — one watch click asked for it 3 times (the face, then
  16. // the page's re-mount for the session sync: rows + watchedEpisodes), each a scan + decode of every watch (~25 ms on the real data).
  17. // One process per storage (hybriel#21): every write goes through setWatched below. Flat statics, written key by key (plain data
  18. // deep-copies on assignment)
  19. static watchCache = {}
  20. static watchCacheOk = {}
  21. static watchesOfUser = (&userId) => {
  22. if (userId == null) { return [] }
  23. if (watchCacheOk[userId] == true) { return watchCache[userId] }
  24. out = []
  25. for (w of watchesTable.iterate(null, null)) { if (w.user == userId) { out.push(w) } }
  26. watchCache[userId] = out
  27. watchCacheOk[userId] = true
  28. return out
  29. }
  30. // '<targetType>:<target>' → true for every watch in `rows`: built ONCE per page load so a list asks
  31. // `watched['episode:' + e.id] == true` instead of `isWatched` scanning all the user's watches per episode
  32. // (/my/unwatched, tracker.worldapi.org#8: 831 rows × 11,692 watches took ~15 s)
  33. static watchedSet = (&rows) => {
  34. m = {}
  35. for (w of rows) { if (w.target != null) { m[w.targetType + ':' + w.target] = true } }
  36. return m
  37. }
  38. // tracker#30 (mission 025): `watchedSet` of a user's watches, KEPT like watchesOfUser until that user's next setWatched — the
  39. // /my/ lists ask for it on every load (11.7k rows → a fresh 11.7k-key map each time). Returned, never `let`-copied (shows.hl
  40. // cardOf).
  41. static watchedSetCache = {}
  42. static watchedSetOk = {}
  43. static watchedSetOf = (&userId) => {
  44. if (userId == null) { return {} }
  45. if (watchedSetOk[userId] == true && watchCacheOk[userId] == true) { return watchedSetCache[userId] }
  46. watchedSetCache[userId] = watchedSet(watchesOfUser(&userId))
  47. watchedSetOk[userId] = true
  48. return watchedSetCache[userId]
  49. }
  50. // GC1 measurement: users whose watch rows / set are kept
  51. static watchCacheSizes = () => { return { rows = watchCache.keys().length sets = watchedSetCache.keys().length } }
  52. // merge.hl (tracker#31) moved watches of this user to another target: the kept rows/set are read again
  53. static forgetUserWatches = (&userId) => {
  54. if (userId == null) { return null }
  55. watchCacheOk[userId] = false
  56. watchedSetOk[userId] = false
  57. return null
  58. }
  59. // every episode id anyone has watched (project.hl warms shows.hl episodeKeyOf with them at boot, tracker#30)
  60. static allWatchedEpisodeIds = () => {
  61. out = []
  62. for (w of watchesTable.iterate(null, null)) { if (w.targetType == 'episode' && w.target != null) { out.push(w.target) } }
  63. return out
  64. }
  65. static findWatch = (&rows, &targetType, &targetId) => {
  66. for (w of rows) { if (w.targetType == targetType && w.target == targetId) { return w } }
  67. return null
  68. }
  69. static isWatched = (&rows, &targetType, &targetId) => { return findWatch(&rows, &targetType, &targetId) != null }
  70. // the check's class — "a disc with a check in it", solid once watched, regular while not (the
  71. // SAME icon and class the show page uses, tracker.worldapi.org#4/#5 — /unwatched and /schedule
  72. // reuse this instead of each declaring their own)
  73. static watchClassOf = (&watched) => { return watched ? 'checkicon solid' : 'checkicon regular' }
  74. // a NEW watch's oldId can't be null: the table's `!oldId` UNIQUE index would refuse a second one
  75. // (the migrated rows all carry a real Mongo hex id; a fresh watch gets its own unique marker instead)
  76. static setWatched = (&userId, targetType, &targetId, &rows, &watched) => {
  77. existing = findWatch(&rows, &targetType, &targetId)
  78. watchCacheOk[userId] = false
  79. watchedSetOk[userId] = false
  80. if (watched) {
  81. if (existing != null) { return true }
  82. id = watchesTable.put({ id = randomBytes(8, 'hex') targetType = targetType target = targetId user = userId at = now() oldId = 'app:' + randomBytes(8, 'hex') })
  83. return id != null
  84. }
  85. if (existing == null) { return true }
  86. return watchesTable.delete(existing.id) > 0
  87. }
  88. // a season check toggles the season's OWN watch record AND every one of its episodes (creator: "if
  89. // the season is checked or unchecked all its episodes get checked or unchecked") — `rows` is the
  90. // snapshot from BEFORE this batch; every target below is distinct, so the staleness across the loop
  91. // never matters (no two calls touch the same target)
  92. static setSeasonWatched = (&userId, &seasonId, &episodeIds, &rows, &watched) => {
  93. let ok = setWatched(&userId, 'season', &seasonId, &rows, &watched)
  94. for (eid of episodeIds) { ok = setWatched(&userId, 'episode', &eid, &rows, &watched) && ok }
  95. return ok
  96. }
  97. // ---- the checks clicked (the show page's faces, components/show.hl — mission 028: out of the faces) ------------------------
  98. // an episode's or a movie's check (#4, #24): watched → not, not → watched; the new state, or null when it could not be saved
  99. static toggleWatched = (&userId, targetType, &targetId) => {
  100. rows = watchesOfUser(&userId)
  101. watched = isWatched(&rows, &targetType, &targetId) == false
  102. return setWatched(&userId, targetType, &targetId, &rows, &watched) ? watched : null
  103. }
  104. // #23: a season is watched when ALL its episodes are (by the ids it lists); one without episodes keeps its own season watch
  105. static seasonAllWatched = (&s, &watched) => {
  106. if (s.episodes.length == 0) { return watched['season:' + s.id] == true }
  107. for (eid of s.episodes) { if (watched['episode:' + eid] != true) { return false } }
  108. return true
  109. }
  110. // #23: a season's check: not all its episodes watched → watch them all; all watched → unwatch them all. The new state or null
  111. static toggleSeasonWatched = (&userId, &season) => {
  112. rows = watchesOfUser(&userId)
  113. current = watchedSetOf(&userId)
  114. watched = !seasonAllWatched(&season, &current)
  115. return setSeasonWatched(&userId, &season.id, &season.episodes, &rows, &watched) ? watched : null
  116. }
  117. // tracker#59: mymovies page rows.
  118. static watchedCountOf = (&rows) => {
  119. let n = 0
  120. for (r of rows) { if (r.watched) { n = n + 1 } }
  121. return n
  122. }
  123. // tracker#59: unwatched page rows.
  124. static mergeByReleaseDesc = (&a, &b) => {
  125. out = []
  126. let i = 0
  127. let j = 0
  128. while (i < a.length && j < b.length) {
  129. if (a[i].release >= b[j].release) { out.push(a[i]) i = i + 1 } else { out.push(b[j]) j = j + 1 }
  130. }
  131. while (i < a.length) { out.push(a[i]) i = i + 1 }
  132. while (j < b.length) { out.push(b[j]) j = j + 1 }
  133. return out
  134. }
  135. static sortByReleaseDesc = (&rows) => {
  136. // runs of one, the found order reversed (ties: later first), merged pairwise until one run is left
  137. let runs = []
  138. let k = rows.length - 1
  139. while (k >= 0) { runs.push([rows[k]]) k = k - 1 }
  140. if (runs.length == 0) { return [] }
  141. while (runs.length > 1) {
  142. next = []
  143. let r = 0
  144. while (r + 1 < runs.length) { next.push(mergeByReleaseDesc(&runs[r], runs[r + 1])) r = r + 2 }
  145. if (r < runs.length) { next.push(runs[r]) }
  146. runs = next
  147. }
  148. return runs[0]
  149. }
  150. static unwatchedRowsOf = (&userId) => {
  151. if (userId == null) { return [] }
  152. today = todayStr()
  153. watched = watchedSetOf(&userId)
  154. out = []
  155. for (sid of followedShowIds(&userId)) {
  156. c = cardOf(&sid)
  157. if (c != null) {
  158. for (e of episodeRowsOf(&sid)) {
  159. released = e.release != null && e.release <= today
  160. if (released && watched['episode:' + e.id] != true) {
  161. out.push({
  162. id = e.id showTitle = c.titleYear showHref = c.href
  163. episodeLabel = episodeCode(&e.s, &e.e) + ' · ' + e.title
  164. release = e.release watchClass = watchClassOf(false)
  165. })
  166. }
  167. }
  168. }
  169. }
  170. return sortByReleaseDesc(&out)
  171. }
  172. static unwatchedToggleOf = (episodeId, uid) => {
  173. // mission SK: only an episode that exists, of a title this user may see (any text became a watch row before)
  174. ep = episodeId != null && hlTypeName(episodeId) == 'String' ? episodeById(&episodeId) : null
  175. show = ep != null ? showById(&ep.show) : null
  176. if (show == null || adultHiddenForUser(&show, &uid)) { return { error = 'no such episode' } }
  177. watchRows = watchesOfUser(&uid)
  178. ok = setWatched(&uid, 'episode', &episodeId, &watchRows, true)
  179. if (!ok) { return { error = 'could not save the watch' } }
  180. return { ok = true rows = unwatchedRowsOf(&uid) }
  181. }

Branches

Latest commits

  • d3edaa05|||PR records: HM55 start, PROCEDURES.mdTracker
  • 3059f86cMerge branch 'records-design-rule' into mainTracker
  • 421a4809|||PR records: his-design rule (Apollo 2026-11-13)Tracker
  • d1390692Merge branch 'records-dp75b' into mainTracker
  • a4d1a7d2|||PR records: later steps checked, branch auditTracker
  • e070e16fMerge branch 'records-dp75' into mainTracker
  • 642b4fa8|||PR records: DP75 day62 live, AP62 briefTracker
  • bfc4ea4dMerge branch 'ap62' into mainTracker
  • 2c95ac46|||PR AP62: re-vendor Hybriel set day62 (source 70341ad2, bin a54f5f08)Tracker
  • b9c7c3c8Merge branch 'records-dp74' into mainTracker
  • 85cfb4ae|||PR records: AP61 day61, DP74 liveTracker
  • b1496147Merge branch 'ap61' into mainTracker
  • 2047955bMerge branch 'u12-gateports' into mainTracker
  • c8da40f3|||PR AP61: re-vendor Hybriel set day61 (source a05af47c, bin a11042e5)Tracker
  • 4e018678|||PR deploy.sh: gates use Tracker's ports 8716-8719 and LC_ALL=C; README: 2 backups keptTracker
  • 0b86324aMerge branch 'records-dp73' into mainTracker
  • 9ce5f59b|||PR records: DP73 live 613ae0e; deploy.sh keeps 2 backupsTracker
  • 613ae0ecMerge branch 'bs3' into mainTracker
  • 1383264b|||PR BS3: keep shared synchronous sorting fastTracker
  • 63e5ba1a|||PR BS3: guard boot imports and share resumable sort buffersTracker