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
- 44b934e5log: respawn cleanupmre
- 03a80097Log: Anton GC A/Bmre
- c7cdef62Log: Apollo STOP, workers pausedmre
- d0c083fbLog: usage stopmre
- c6d464e5Log: Apollo GO backlog, AP56 and BS1 startedmre
- 1eb6f1a1Missions AP56, BS1mre
- 4871f8adLog: AP52 live on day52, browser gate greenmre
- d9bf2988Merge ap52: day52, tracker#58 server argument, #51 modal close rule, #56 keyed season rowsmre
- c4a60ef2Log: RT52 retest passed, AP52 startedmre
- 83760a55Browser gate: search results belong to the address form of the typed text (tracker#52 navigate replace)mre
- 6e951d91AP52: show-page rows are keyed records; closing a season is one upsert and removes (tracker#56, hybriel#331)mre
- 3bb4ccddAP52: modal close button refers to its nested rule with Style.buttonModalX (tracker#51, hybriel#333)mre
- ba16933fAP52: login callback and title route are static handlers taking the server as third argument (tracker#58, hybriel#332)mre
- 85153900AP52: re-vendor Hybriel set day52 (source 77cfca31, bin 0a8ed496)mre
- 7c051eddLog: hybriel#338 answermre
- d049b8b8Log: HM50 live, #52 donemre
- 6d05fa37Log: HM50 Part A mergedmre
- 1c7dab74HM50: the shell reads the framework's online state, netProbe removed (tracker#55)mre
- e8b91697HM50: search address follows the text via navigate(path, replace = true) (tracker#52)mre
- 6fe1896cHM50: re-vendor Hybriel set day50 (source 0ae38232, bin d3ef9d16)mre