gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commite5c9b659e5c9b659Pack retained search IDs into UTF-8 bytesmree5c9b659/lib/follows.hl

7.0 KB

  1. import { watchesOfUser, watchedSetOf } from './watches.hl'
  2. import { cardOf, episodeKeyOf, posterUrlOfName, episodeCode, episodeRowsOf } from './shows.hl'
  3. // lib/follows.hl — per-user follow data (tracker.worldapi.org#5): which shows a user follows; since mission 047 also the
  4. // show page's Follow toggle (`setFollowed`).
  5. // storage/mpackdb/follows.db, migrated by tools/migrate.hl (tracker.worldapi.org#2): { show, user, at, oldId }.
  6. // Kept apart from shows.hl (show data) and watches.hl (watch state) so /unwatched, /schedule and /my/shows (tickets #5/#6/#7,
  7. // "reuse, don't copy") can all import the same functions.
  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 followsTable = new MPackDB(file = storageDir + '/follows.db', primaryKey = '@id', indexes = ['!oldId'])
  13. // every follow record of this user ({ show, user, at, … }, `at` = epoch ms) — a linear scan (128 rows
  14. // total across every user, migrated data; cheap once per page load) — no declared index on `user`.
  15. // /my/shows (tracker.worldapi.org#7) needs the whole record for its follow-date order.
  16. static followsOfUser = (&userId) => {
  17. if (userId == null) { return [] }
  18. out = []
  19. for (f of followsTable.iterate(null, null)) { if (f.user == userId && f.show != null) { out.push(f) } }
  20. return out
  21. }
  22. // Insert into the caller's list once. Equal follow times keep the historical newest-insertion-first order.
  23. // Both ends use &: no copy of the growing list per row (tracker#56).
  24. static insertByFollowDesc = (&sorted, &row) => {
  25. let low = 0
  26. let high = sorted.length
  27. while (low < high) {
  28. middle = (low + high - (low + high) % 2) / 2
  29. if (row.followedAt >= sorted[middle].followedAt) { high = middle }
  30. else { low = middle + 1 }
  31. }
  32. sorted.splice(low, 0, row)
  33. return null
  34. }
  35. // the ids of every show this user follows
  36. static followedShowIds = (&userId) => {
  37. out = []
  38. for (f of followsOfUser(&userId)) { out.push(f.show) }
  39. return out
  40. }
  41. // every show ANYONE follows, each once, in follow-table order — what the TMDB sync (sync.hl,
  42. // tracker.worldapi.org#9) keeps up to date
  43. static allFollowedShowIds = () => {
  44. seen = {}
  45. out = []
  46. for (f of followsTable.iterate(null, null)) {
  47. if (f.show != null && seen[f.show] != true) { seen[f.show] = true out.push(f.show) }
  48. }
  49. return out
  50. }
  51. // mission 047 (the show page's Follow / Following toggle): does this user follow this show?
  52. static isFollowing = (&userId, &showId) => {
  53. if (userId == null || showId == null) { return false }
  54. for (f of followsOfUser(&userId)) { if (f.show == showId) { return true } }
  55. return false
  56. }
  57. // follow (a new record, follow date = now → the top of /my/shows) or unfollow (every record of this user+show is
  58. // deleted). Own 16-hex id + a unique `oldId` marker (the `!oldId` UNIQUE index refuses a second null), never update()
  59. // (hybriel#113) — the same shape as watches.hl `setWatched`.
  60. static setFollowed = (&userId, &showId, &follow) => {
  61. if (userId == null || showId == null) { return false }
  62. mine = []
  63. for (f of followsOfUser(&userId)) { if (f.show == showId) { mine.push(f) } }
  64. if (follow) {
  65. if (mine.length > 0) { return true }
  66. id = followsTable.put({ id = randomBytes(8, 'hex') show = showId user = userId at = now() oldId = 'app:' + randomBytes(8, 'hex') })
  67. return id != null
  68. }
  69. let ok = true
  70. for (f of mine) { ok = followsTable.delete(f.id) > 0 && ok }
  71. return ok
  72. }
  73. // the show page's Follow button (components/show.hl face showToggleFollow): following → not, not → following; the new state, or
  74. // null when it could not be saved
  75. static toggleFollowed = (&userId, &showId) => {
  76. follow = isFollowing(&userId, &showId) == false
  77. return setFollowed(&userId, &showId, &follow) ? follow : null
  78. }
  79. // mission SK (components/show.hl adultHiddenFor is the page's own local copy, see there): an ADULT title (TMDB's flag `adult == true`, mission 054) is for the users who follow it — everyone else gets
  80. // "not found" (pages) or "no such show" (faces). Unknown (`null`) is not hidden here, only in the lists.
  81. static adultHiddenForUser = (&show, &uid) => { return show.adult == true && !(uid != null && isFollowing(&uid, &show.id)) }
  82. // tracker#59: myshows page rows.
  83. static lastWatchedOf = (&userId, &follows) => {
  84. followed = {}
  85. for (f of follows) { followed[f.show] = true }
  86. best = {}
  87. for (w of watchesOfUser(&userId)) {
  88. if (w.targetType == 'episode' && w.target != null) {
  89. k = episodeKeyOf(&w.target)
  90. if (k.show != null && followed[k.show] == true && k.s != null && k.e != null) {
  91. b = best[k.show]
  92. if (b == null || k.s > b.seasonNumber || (k.s == b.seasonNumber && k.e > b.episodeNumber)) {
  93. best[k.show] = { seasonNumber = k.s episodeNumber = k.e }
  94. }
  95. }
  96. }
  97. }
  98. return best
  99. }
  100. static listKindOf = (&tv) => { return tv == 'series' ? 'series' : (tv == 'shows' ? 'show' : null) }
  101. static myShowsRowsOf = (&userId, &kind) => {
  102. if (userId == null || kind == null) { return [] }
  103. // tracker.worldapi.org#13: movies have their own list, /my/movies; #21: series and shows each theirs
  104. follows = []
  105. for (f of followsOfUser(&userId)) {
  106. c = cardOf(&f.show)
  107. if (c != null && c.type != 'movie' && c.kind == kind) { follows.push(f) }
  108. }
  109. last = lastWatchedOf(&userId, &follows)
  110. out = []
  111. for (f of follows) {
  112. c = cardOf(&f.show)
  113. if (c != null) {
  114. lw = last[c.id]
  115. hasEpisode = lw != null
  116. insertByFollowDesc(&out, {
  117. id = c.id title = c.titleYear href = c.href
  118. posterUrl = posterUrlOfName(&c.posterName)
  119. hasEpisode = hasEpisode
  120. episodeLabel = hasEpisode ? episodeCode(&lw.seasonNumber, &lw.episodeNumber) : ''
  121. followedAt = f.at != null ? f.at : 0
  122. })
  123. }
  124. }
  125. return out
  126. }
  127. // tracker#59: mymovies page rows.
  128. static myMoviesRowsOf = (&userId) => {
  129. if (userId == null) { return [] }
  130. watched = watchedSetOf(&userId)
  131. out = []
  132. for (f of followsOfUser(&userId)) {
  133. c = cardOf(&f.show)
  134. if (c != null && c.type == 'movie') {
  135. insertByFollowDesc(&out, {
  136. id = c.id title = c.titleYear href = c.href
  137. posterUrl = posterUrlOfName(&c.posterName) followedAt = f.at != null ? f.at : 0
  138. watched = watched['movie:' + c.id] == true
  139. })
  140. }
  141. }
  142. return out
  143. }
  144. // tracker#59: schedule page rows.
  145. static insertByReleaseAsc = (&sorted, &row) => {
  146. let low = 0
  147. let high = sorted.length
  148. while (low < high) {
  149. middle = (low + high - (low + high) % 2) / 2
  150. if (row.release <= sorted[middle].release) { high = middle }
  151. else { low = middle + 1 }
  152. }
  153. sorted.splice(low, 0, row)
  154. return null
  155. }
  156. static scheduleRowsOf = (&userId) => {
  157. if (userId == null) { return [] }
  158. today = todayStr()
  159. out = []
  160. // tracker#30 (mission 025): the slim cached card + episode rows per followed title (shows.hl), no record fetched per load
  161. for (sid of followedShowIds(&userId)) {
  162. c = cardOf(&sid)
  163. if (c != null) {
  164. for (e of episodeRowsOf(&sid)) {
  165. upcoming = e.release != null && e.release > today
  166. if (upcoming) {
  167. insertByReleaseAsc(&out, {
  168. id = e.id showTitle = c.titleYear showHref = c.href
  169. episodeLabel = episodeCode(&e.s, &e.e) + ' · ' + e.title
  170. release = e.release
  171. })
  172. }
  173. }
  174. }
  175. }
  176. return out
  177. }

Branches

Latest commits

  • e5c9b659Pack retained search IDs into UTF-8 bytesmre
  • 62a5f6c7HM67 brief, HM66 mergedmre
  • b32b8440Merge hm66-catalogindex: packed catalog indexmre
  • 2db0f7adReport HM66 heap measurements and validationmre
  • 6ce15623Pack catalog lists and membership maps in UTF-8 bytesmre
  • 71134e21HM66 briefmre
  • ed4cbab2status timemre
  • e982c26dlog: HM65 mergedmre
  • 6e72f9d1Merge hm65-fields: episode boot scan reads three fieldsmre
  • 07a5c883HM65 report measured field projection and complete validationmre
  • 0eab858fRead only catalog date fields during the episode boot scanmre
  • d3179273log: HM65 hl:heap vendoringmre
  • cb78a1a0HM65 report missing heap diagnostic package with repromre
  • 89606a5cHM65 briefmre
  • 096ce0d4log: DP69 live on day48mre
  • b3d8a80bDP69 brief, RV48 mergedmre
  • ce6d7d32Merge rv48: Hybriel day48mre
  • 3da2ea86RV48: report day48 cold starts, gates and cleanupmre
  • 8d9ecdfeRV48: re-vendor Hybriel set day48 (source b6a8f429, bin 660a9524)mre
  • 4a802ae7status: RV48 runningmre