tracker
All repositories: gitoria
7.0 KB
import { watchesOfUser, watchedSetOf } from './watches.hl'import { cardOf, episodeKeyOf, posterUrlOfName, episodeCode, episodeRowsOf } from './shows.hl'// lib/follows.hl — per-user follow data (tracker.worldapi.org#5): which shows a user follows; since mission 047 also the// show page's Follow toggle (`setFollowed`).// storage/mpackdb/follows.db, migrated by tools/migrate.hl (tracker.worldapi.org#2): { show, user, at, oldId }.// Kept apart from shows.hl (show data) and watches.hl (watch state) so /unwatched, /schedule and /my/shows (tickets #5/#6/#7,// "reuse, don't copy") can all import the same functions.import { MPackDB } from 'hl:mpackdb'import { now } from 'hl:time'import { randomBytes } from 'hl:crypto'import { storageDir, todayStr } from './util.hl'static followsTable = new MPackDB(file = storageDir + '/follows.db', primaryKey = '@id', indexes = ['!oldId'])// every follow record of this user ({ show, user, at, … }, `at` = epoch ms) — a linear scan (128 rows// total across every user, migrated data; cheap once per page load) — no declared index on `user`.// /my/shows (tracker.worldapi.org#7) needs the whole record for its follow-date order.static followsOfUser = (&userId) => {if (userId == null) { return [] }out = []for (f of followsTable.iterate(null, null)) { if (f.user == userId && f.show != null) { out.push(f) } }return out}// Insert into the caller's list once. Equal follow times keep the historical newest-insertion-first order.// Both ends use &: no copy of the growing list per row (tracker#56).static insertByFollowDesc = (&sorted, &row) => {let low = 0let high = sorted.lengthwhile (low < high) {middle = (low + high - (low + high) % 2) / 2if (row.followedAt >= sorted[middle].followedAt) { high = middle }else { low = middle + 1 }}sorted.splice(low, 0, row)return null}// the ids of every show this user followsstatic followedShowIds = (&userId) => {out = []for (f of followsOfUser(&userId)) { out.push(f.show) }return out}// every show ANYONE follows, each once, in follow-table order — what the TMDB sync (sync.hl,// tracker.worldapi.org#9) keeps up to datestatic allFollowedShowIds = () => {seen = {}out = []for (f of followsTable.iterate(null, null)) {if (f.show != null && seen[f.show] != true) { seen[f.show] = true out.push(f.show) }}return out}// mission 047 (the show page's Follow / Following toggle): does this user follow this show?static isFollowing = (&userId, &showId) => {if (userId == null || showId == null) { return false }for (f of followsOfUser(&userId)) { if (f.show == showId) { return true } }return false}// follow (a new record, follow date = now → the top of /my/shows) or unfollow (every record of this user+show is// deleted). Own 16-hex id + a unique `oldId` marker (the `!oldId` UNIQUE index refuses a second null), never update()// (hybriel#113) — the same shape as watches.hl `setWatched`.static setFollowed = (&userId, &showId, &follow) => {if (userId == null || showId == null) { return false }mine = []for (f of followsOfUser(&userId)) { if (f.show == showId) { mine.push(f) } }if (follow) {if (mine.length > 0) { return true }id = followsTable.put({ id = randomBytes(8, 'hex') show = showId user = userId at = now() oldId = 'app:' + randomBytes(8, 'hex') })return id != null}let ok = truefor (f of mine) { ok = followsTable.delete(f.id) > 0 && ok }return ok}// the show page's Follow button (components/show.hl face showToggleFollow): following → not, not → following; the new state, or// null when it could not be savedstatic toggleFollowed = (&userId, &showId) => {follow = isFollowing(&userId, &showId) == falsereturn setFollowed(&userId, &showId, &follow) ? follow : null}// 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// "not found" (pages) or "no such show" (faces). Unknown (`null`) is not hidden here, only in the lists.static adultHiddenForUser = (&show, &uid) => { return show.adult == true && !(uid != null && isFollowing(&uid, &show.id)) }// tracker#59: myshows page rows.static lastWatchedOf = (&userId, &follows) => {followed = {}for (f of follows) { followed[f.show] = true }best = {}for (w of watchesOfUser(&userId)) {if (w.targetType == 'episode' && w.target != null) {k = episodeKeyOf(&w.target)if (k.show != null && followed[k.show] == true && k.s != null && k.e != null) {b = best[k.show]if (b == null || k.s > b.seasonNumber || (k.s == b.seasonNumber && k.e > b.episodeNumber)) {best[k.show] = { seasonNumber = k.s episodeNumber = k.e }}}}}return best}static listKindOf = (&tv) => { return tv == 'series' ? 'series' : (tv == 'shows' ? 'show' : null) }static myShowsRowsOf = (&userId, &kind) => {if (userId == null || kind == null) { return [] }// tracker.worldapi.org#13: movies have their own list, /my/movies; #21: series and shows each theirsfollows = []for (f of followsOfUser(&userId)) {c = cardOf(&f.show)if (c != null && c.type != 'movie' && c.kind == kind) { follows.push(f) }}last = lastWatchedOf(&userId, &follows)out = []for (f of follows) {c = cardOf(&f.show)if (c != null) {lw = last[c.id]hasEpisode = lw != nullinsertByFollowDesc(&out, {id = c.id title = c.titleYear href = c.hrefposterUrl = posterUrlOfName(&c.posterName)hasEpisode = hasEpisodeepisodeLabel = hasEpisode ? episodeCode(&lw.seasonNumber, &lw.episodeNumber) : ''followedAt = f.at != null ? f.at : 0})}}return out}// tracker#59: mymovies page rows.static myMoviesRowsOf = (&userId) => {if (userId == null) { return [] }watched = watchedSetOf(&userId)out = []for (f of followsOfUser(&userId)) {c = cardOf(&f.show)if (c != null && c.type == 'movie') {insertByFollowDesc(&out, {id = c.id title = c.titleYear href = c.hrefposterUrl = posterUrlOfName(&c.posterName) followedAt = f.at != null ? f.at : 0watched = watched['movie:' + c.id] == true})}}return out}// tracker#59: schedule page rows.static insertByReleaseAsc = (&sorted, &row) => {let low = 0let high = sorted.lengthwhile (low < high) {middle = (low + high - (low + high) % 2) / 2if (row.release <= sorted[middle].release) { high = middle }else { low = middle + 1 }}sorted.splice(low, 0, row)return null}static scheduleRowsOf = (&userId) => {if (userId == null) { return [] }today = todayStr()out = []// tracker#30 (mission 025): the slim cached card + episode rows per followed title (shows.hl), no record fetched per loadfor (sid of followedShowIds(&userId)) {c = cardOf(&sid)if (c != null) {for (e of episodeRowsOf(&sid)) {upcoming = e.release != null && e.release > todayif (upcoming) {insertByReleaseAsc(&out, {id = e.id showTitle = c.titleYear showHref = c.hrefepisodeLabel = episodeCode(&e.s, &e.e) + ' · ' + e.titlerelease = e.release})}}}}return out}
Branches
Latest commits
- 2c95ac46|||PR AP62: re-vendor Hybriel set day62 (source 70341ad2, bin a54f5f08)Tracker
- b9c7c3c8Merge branch 'records-dp74' into mainTracker
- 85cfb4ae|||PR records: AP61 day61, DP74 liveTracker
- b1496147Merge branch 'ap61' into mainTracker
- 2047955bMerge branch 'u12-gateports' into mainTracker
- c8da40f3|||PR AP61: re-vendor Hybriel set day61 (source a05af47c, bin a11042e5)Tracker
- 4e018678|||PR deploy.sh: gates use Tracker's ports 8716-8719 and LC_ALL=C; README: 2 backups keptTracker
- 0b86324aMerge branch 'records-dp73' into mainTracker
- 9ce5f59b|||PR records: DP73 live 613ae0e; deploy.sh keeps 2 backupsTracker
- 613ae0ecMerge branch 'bs3' into mainTracker
- 1383264b|||PR BS3: keep shared synchronous sorting fastTracker
- 63e5ba1a|||PR BS3: guard boot imports and share resumable sort buffersTracker
- 310613f1Merge branch 'records-dp72' into mainTracker
- 6f7b61ba|||PR records: deploy-swap fix live, Kunibert findingsTracker
- c41587d2Merge branch 'bs2' into mainTracker
- 0dd538a6|||PR BS2: finish the boot index in slices so a booting instance keeps answeringTracker
- 019d5cedMerge branch 'records-1110' into mainTracker
- f88ae8b9|||PR records: day56 toggle live, reviewTracker
- b6b3bb4dMerge branch 'ap56e' into mainTracker
- 29a26da9|||PR AP56e: guard hidden show helpers during session syncTracker