gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit833dcfc9833dcfc9Merge hm52-search: report only, tracker#52 blocked by framework gapmre833dcfc9/lib/watches.hl

6.5 KB

  1. // lib/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'|'movie'), target, user, at, oldId }. 'movie' (mission 058,
  5. // tracker#24): target = the movie's shows.db id. A season's CHECK on the page is derived from its episodes (components/show.hl).
  6. import { MPackDB } from 'hl:mpackdb'
  7. import { now } from 'hl:time'
  8. import { randomBytes } from 'hl:crypto'
  9. import { storageDir } from './util.hl'
  10. static watchesTable = new MPackDB(file = storageDir + '/watches.db', primaryKey = '@id', indexes = ['!oldId'])
  11. // every watch of this user — a linear scan (11692 rows total across every user, migrated data; cheap
  12. // once per page load) — no declared index on `user`
  13. // mission 062: KEPT per user until that user's next write (setWatched) — one watch click asked for it 3 times (the face, then
  14. // the page's re-mount for the session sync: rows + watchedEpisodes), each a scan + decode of every watch (~25 ms on the real data).
  15. // One process per storage (hybriel#21): every write goes through setWatched below. Flat statics, written key by key (plain data
  16. // deep-copies on assignment)
  17. static watchCache = {}
  18. static watchCacheOk = {}
  19. static watchesOfUser = (&userId) => {
  20. if (userId == null) { return [] }
  21. if (watchCacheOk[userId] == true) { return watchCache[userId] }
  22. out = []
  23. for (w of watchesTable.iterate(null, null)) { if (w.user == userId) { out.push(w) } }
  24. watchCache[userId] = out
  25. watchCacheOk[userId] = true
  26. return out
  27. }
  28. // '<targetType>:<target>' → true for every watch in `rows`: built ONCE per page load so a list asks
  29. // `watched['episode:' + e.id] == true` instead of `isWatched` scanning all the user's watches per episode
  30. // (/my/unwatched, tracker.worldapi.org#8: 831 rows × 11,692 watches took ~15 s)
  31. static watchedSet = (&rows) => {
  32. m = {}
  33. for (w of rows) { if (w.target != null) { m[w.targetType + ':' + w.target] = true } }
  34. return m
  35. }
  36. // tracker#30 (mission 025): `watchedSet` of a user's watches, KEPT like watchesOfUser until that user's next setWatched — the
  37. // /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
  38. // cardOf).
  39. static watchedSetCache = {}
  40. static watchedSetOk = {}
  41. static watchedSetOf = (&userId) => {
  42. if (userId == null) { return {} }
  43. if (watchedSetOk[userId] == true && watchCacheOk[userId] == true) { return watchedSetCache[userId] }
  44. watchedSetCache[userId] = watchedSet(watchesOfUser(&userId))
  45. watchedSetOk[userId] = true
  46. return watchedSetCache[userId]
  47. }
  48. // GC1 measurement: users whose watch rows / set are kept
  49. static watchCacheSizes = () => { return { rows = watchCache.keys().length sets = watchedSetCache.keys().length } }
  50. // merge.hl (tracker#31) moved watches of this user to another target: the kept rows/set are read again
  51. static forgetUserWatches = (&userId) => {
  52. if (userId == null) { return null }
  53. watchCacheOk[userId] = false
  54. watchedSetOk[userId] = false
  55. return null
  56. }
  57. // every episode id anyone has watched (project.hl warms shows.hl episodeKeyOf with them at boot, tracker#30)
  58. static allWatchedEpisodeIds = () => {
  59. out = []
  60. for (w of watchesTable.iterate(null, null)) { if (w.targetType == 'episode' && w.target != null) { out.push(w.target) } }
  61. return out
  62. }
  63. static findWatch = (&rows, &targetType, &targetId) => {
  64. for (w of rows) { if (w.targetType == targetType && w.target == targetId) { return w } }
  65. return null
  66. }
  67. static isWatched = (&rows, &targetType, &targetId) => { return findWatch(&rows, &targetType, &targetId) != null }
  68. // the check's class — "a disc with a check in it", solid once watched, regular while not (the
  69. // SAME icon and class the show page uses, tracker.worldapi.org#4/#5 — /unwatched and /schedule
  70. // reuse this instead of each declaring their own)
  71. static watchClassOf = (&watched) => { return watched ? 'checkicon solid' : 'checkicon regular' }
  72. // a NEW watch's oldId can't be null: the table's `!oldId` UNIQUE index would refuse a second one
  73. // (the migrated rows all carry a real Mongo hex id; a fresh watch gets its own unique marker instead)
  74. static setWatched = (&userId, targetType, &targetId, &rows, &watched) => {
  75. existing = findWatch(&rows, &targetType, &targetId)
  76. watchCacheOk[userId] = false
  77. watchedSetOk[userId] = false
  78. if (watched) {
  79. if (existing != null) { return true }
  80. id = watchesTable.put({ id = randomBytes(8, 'hex') targetType = targetType target = targetId user = userId at = now() oldId = 'app:' + randomBytes(8, 'hex') })
  81. return id != null
  82. }
  83. if (existing == null) { return true }
  84. return watchesTable.delete(existing.id) > 0
  85. }
  86. // a season check toggles the season's OWN watch record AND every one of its episodes (creator: "if
  87. // the season is checked or unchecked all its episodes get checked or unchecked") — `rows` is the
  88. // snapshot from BEFORE this batch; every target below is distinct, so the staleness across the loop
  89. // never matters (no two calls touch the same target)
  90. static setSeasonWatched = (&userId, &seasonId, &episodeIds, &rows, &watched) => {
  91. let ok = setWatched(&userId, 'season', &seasonId, &rows, &watched)
  92. for (eid of episodeIds) { ok = setWatched(&userId, 'episode', &eid, &rows, &watched) && ok }
  93. return ok
  94. }
  95. // ---- the checks clicked (the show page's faces, components/show.hl — mission 028: out of the faces) ------------------------
  96. // 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
  97. static toggleWatched = (&userId, targetType, &targetId) => {
  98. rows = watchesOfUser(&userId)
  99. watched = isWatched(&rows, &targetType, &targetId) == false
  100. return setWatched(&userId, targetType, &targetId, &rows, &watched) ? watched : null
  101. }
  102. // #23: a season is watched when ALL its episodes are (by the ids it lists); one without episodes keeps its own season watch
  103. static seasonAllWatched = (&s, &watched) => {
  104. if (s.episodes.length == 0) { return watched['season:' + s.id] == true }
  105. for (eid of s.episodes) { if (watched['episode:' + eid] != true) { return false } }
  106. return true
  107. }
  108. // #23: a season's check: not all its episodes watched → watch them all; all watched → unwatch them all. The new state or null
  109. static toggleSeasonWatched = (&userId, &season) => {
  110. rows = watchesOfUser(&userId)
  111. current = watchedSetOf(&userId)
  112. watched = !seasonAllWatched(&season, &current)
  113. return setSeasonWatched(&userId, &season.id, &season.episodes, &rows, &watched) ? watched : null
  114. }

Branches

Latest commits

  • 833dcfc9Merge hm52-search: report only, tracker#52 blocked by framework gapmre
  • 73010cc0HM52: report replace-navigation search draft regressionmre
  • 1742e92bstatus: DP64 and HM52 runningmre
  • 29ad5b58log: #64 merged, DP64 and HM52 startedmre
  • fafddbf9Merge hm64-catalog: tracker#64 catalog from packed entriesmre
  • b2e2206dlog: #63 livemre
  • 5831e0e0log: HM64 donemre
  • 4042b923Flatten catalog membership rows into paired slotsmre
  • ea8a759elog: #61 #62 live, DP63 and HM64 startedmre
  • 40439456Merge hm63-searchtext: tracker#63 packed search textmre
  • b7e6b48ePack normalized search text behind numeric offsetsmre
  • b35f53e1log: DP62 stale runtime decisionmre
  • f9593602log: Apollo deploy rule correctionmre
  • 3c95f7f3log: DP62 and HM63 startedmre
  • 9c4397c9mission HM63mre
  • 8dfe42c8mission DP62mre
  • e347fef4Merge rv44: tracker#61 #62 on Hybriel day44mre
  • 8274bcf1log: RV44 done, deploy held for the usage windowmre
  • 1ef7a3b1RV44: report day44 gates and migration/restart proofsmre
  • a5f62f6cRV44: re-vendor Hybriel set day44 (source 222edef7, bin 4f0f263e)mre