gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitb24cd2d5b24cd2d5log: Anton on hl:fetch, mission 569mreb24cd2d5/lib/follows.hl

4.0 KB

  1. // lib/follows.hl — per-user follow data (tracker.worldapi.org#5): which shows a user follows; since mission 047 also the
  2. // show page's Follow toggle (`setFollowed`).
  3. // storage/mpackdb/follows.db, migrated by tools/migrate.hl (tracker.worldapi.org#2): { show, user, at, oldId }.
  4. // Kept apart from shows.hl (show data) and watches.hl (watch state) so /unwatched, /schedule and /my/shows (tickets #5/#6/#7,
  5. // "reuse, don't copy") can all import the same functions.
  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 followsTable = new MPackDB(file = storageDir + '/follows.db', primaryKey = '@id', indexes = ['!oldId'])
  11. // every follow record of this user ({ show, user, at, … }, `at` = epoch ms) — a linear scan (128 rows
  12. // total across every user, migrated data; cheap once per page load) — no declared index on `user`.
  13. // /my/shows (tracker.worldapi.org#7) needs the whole record for its follow-date order.
  14. static followsOfUser = (&userId) => {
  15. if (userId == null) { return [] }
  16. out = []
  17. for (f of followsTable.iterate(null, null)) { if (f.user == userId && f.show != null) { out.push(f) } }
  18. return out
  19. }
  20. // insertion sort, hand-written (no list sort(), hybriel#1) — newest follow first; `at` is epoch ms (migrated
  21. // `_createdAt`, or now() for a new follow), a missing one counts as the oldest. Rows carry `followedAt`.
  22. // /my/shows and /my/movies (tracker.worldapi.org#7/#13)
  23. static insertByFollowDesc = (&sorted, &row) => {
  24. out = []
  25. let inserted = false
  26. for (x of sorted) {
  27. if (!inserted && row.followedAt >= x.followedAt) { out.push(row) inserted = true }
  28. out.push(x)
  29. }
  30. if (!inserted) { out.push(row) }
  31. return out
  32. }
  33. // the ids of every show this user follows
  34. static followedShowIds = (&userId) => {
  35. out = []
  36. for (f of followsOfUser(&userId)) { out.push(f.show) }
  37. return out
  38. }
  39. // every show ANYONE follows, each once, in follow-table order — what the TMDB sync (sync.hl,
  40. // tracker.worldapi.org#9) keeps up to date
  41. static allFollowedShowIds = () => {
  42. seen = {}
  43. out = []
  44. for (f of followsTable.iterate(null, null)) {
  45. if (f.show != null && seen[f.show] != true) { seen[f.show] = true out.push(f.show) }
  46. }
  47. return out
  48. }
  49. // mission 047 (the show page's Follow / Following toggle): does this user follow this show?
  50. static isFollowing = (&userId, &showId) => {
  51. if (userId == null || showId == null) { return false }
  52. for (f of followsOfUser(&userId)) { if (f.show == showId) { return true } }
  53. return false
  54. }
  55. // follow (a new record, follow date = now → the top of /my/shows) or unfollow (every record of this user+show is
  56. // deleted). Own 16-hex id + a unique `oldId` marker (the `!oldId` UNIQUE index refuses a second null), never update()
  57. // (hybriel#113) — the same shape as watches.hl `setWatched`.
  58. static setFollowed = (&userId, &showId, &follow) => {
  59. if (userId == null || showId == null) { return false }
  60. mine = []
  61. for (f of followsOfUser(&userId)) { if (f.show == showId) { mine.push(f) } }
  62. if (follow) {
  63. if (mine.length > 0) { return true }
  64. id = followsTable.put({ id = randomBytes(8, 'hex') show = showId user = userId at = now() oldId = 'app:' + randomBytes(8, 'hex') })
  65. return id != null
  66. }
  67. let ok = true
  68. for (f of mine) { ok = followsTable.delete(f.id) > 0 && ok }
  69. return ok
  70. }
  71. // the show page's Follow button (components/show.hl face showToggleFollow): following → not, not → following; the new state, or
  72. // null when it could not be saved
  73. static toggleFollowed = (&userId, &showId) => {
  74. follow = isFollowing(&userId, &showId) == false
  75. return setFollowed(&userId, &showId, &follow) ? follow : null
  76. }
  77. // 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
  78. // "not found" (pages) or "no such show" (faces). Unknown (`null`) is not hidden here, only in the lists.
  79. static adultHiddenForUser = (&show, &uid) => { return show.adult == true && !(uid != null && isFollowing(&uid, &show.id)) }

Branches

Latest commits

  • b24cd2d5log: Anton on hl:fetch, mission 569mre
  • 7d42cac1log+status: SL1 measured, fetch stalls remainmre
  • 8e5f2ceflog: SL1 deployedmre
  • 29f34212log: SL1 deploy startedmre
  • 529dd741Merge sl1-slices: slice details repair and person fill (SL1)mre
  • 8a83316blog: SL1 result, session drain slowermre
  • 35e93e8dSL1: slice the details repair and person fill: catalog lists changed in place, 40 ms fill steps, people created in steps, writes one table per tickmre
  • 72c9119flog+status: SL1 startedmre
  • d74620fcmission SL1: slice repair and person fillmre
  • 5d542d73log+status: ST1 resultmre
  • 3945eb81ST1: stall diagnosis reportmre
  • 404733aclog: ST1 startedmre
  • 3e3d5a55mission ST1: stall diagnosismre
  • 9c3180eblog+status: RV28d measured after deploymre
  • fdf10b1alog: RV28d deployed on day38bmre
  • 320005e9log: RV28d merged, deploy runningmre
  • a8fef27dRV28d: reportmre
  • 2bae8373RV28d: re-vendor Hybriel set day38b (source 1e554d75, bin 5ff2a46d)mre
  • 16b3354aMerge branch 'main' into rv28cmre
  • 6f4f27d8log: RV28d started (day38b)mre