gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit09d66ed509d66ed5status: #60 livemre09d66ed5/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

  • 09d66ed5status: #60 livemre
  • 4171672flog: #60 livemre
  • f52862a2log: HM62b startedmre
  • 88fde2c4mission HM62bmre
  • a722e526log: #62 blocked by hybriel#317mre
  • 074a7418log: #62 assigned to Tracker; pending rulemre
  • 06f6ae8fHM62 report: mpackdb index gapmre
  • 86fe5430log: HM62 startedmre
  • 755307bbmission HM62mre
  • 4b2f2995HM61 report: tracker#61 packed people ordermre
  • 48faa84flog: #60 deploy startedmre
  • 90734c6cMerge branch 'hm60-shortids'mre
  • e0db0d1bHM60 report: tracker#60 packed short-ID ownersmre
  • 3c79b576Pack short-ID ownership into byte buffersmre
  • e822524bstatus wordingmre
  • 1c7f0480log: HM60/HM61 startedmre
  • 2841d215missions HM60 HM61mre
  • 289c6ce4HM1 report: heap object breakdownmre
  • 2e7d6257log: HM1 startedmre
  • 2f790465mission HM1: heap object breakdownmre