gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit33ad4bd533ad4bd5Merge commit 'f57f64f'mre33ad4bd5/lib/follows.hl

3.5 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. }

Branches

Latest commits

  • 33ad4bd5Merge commit 'f57f64f'mre
  • 7d38cec4docker-compose: nofile 65536 (default soft 1024 refused boot: errno 24 in fs.listDir, Hybriel 528bc89f)mre
  • f57f64f0mission 046: report, LOGmre
  • 5965963adeploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • efe1b485mission 046: re-vendor Hybriel master 60b2817b (integration 8, #203): plugins mpackdb + web (bin/ git-ignored: hybriel, libhlrt.a, hlc.o, hlrt/ from native/zig-out)mre
  • 53099ffbmission 046: person page reads a slim per-title credit row cache (no whole-record fetch per credit); gate tests/person-lookup.mjsmre
  • 9b3828aemission 046a: report, LOGmre
  • 4b37356emission 046: franchise seed supersedeCollections reads the curated timelines once (10 s one-tick block at the end of boot)mre
  • ac6268e3mission 046a: re-vendor Hybriel 528bc89f (integration 6): plugins mpackdb/time/web (bin/ git-ignored)mre
  • 322b3293mission 046: photoQueue guard for non-row fetch results (damaged persons index)mre
  • e5b99aecmission 046: photoQueue in slices (people.hl photoQueueStart/Step, jobs.hl photoTick), tests/photoqueue.hlmre
  • 31dcf2admission 045: report, LOGmre
  • 9c3971d0tests/persons-skip-count.hlmre
  • 4d72e365mission 045mre
  • b9cae4c9slices: skip and count non-row values from range(); merge persons pass falls back to iterate; guard testmre
  • 3db7dc31merge w44-jobs-slices into w43-trkmemmre
  • 593dab6fmission 043: report, LOGmre
  • 1a5a4818boot scans back to iterate (slices measured slower: titles 114 s, episodes 141 s, persons failed); lambda per rowmre
  • 03d29893boot scans read primary-key slices (lib/slices.hl from mission 044): iterate is eager too; people view log line back; allShows/allEpisodes stay for toolsmre
  • 23375c58mission 044: report, LOG, STATUSmre