gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit88fde2c488fde2c4mission HM62bmre88fde2c4/lib/franchises.hl

25.6 KB

  1. // lib/franchises.hl — FRANCHISES AND TIMELINES (tracker.worldapi.org#19, mission 066). The creator: "we have franchises, and
  2. // timelines, and some franchises have multiple timelines ... make them sortable by timeline or release date at shows the
  3. // release date of the last episode counts. in a show or movie page we show a franchise widget with < Prequel | TimelineName
  4. // | Sequel > and if the timeline is part of a franchise that franchise is above it and when you click on it you get the
  5. // franchise index."
  6. //
  7. // franchisesTable pk @id index !slug { name, slug, shortId (null: not given out yet), summary, created }
  8. // timelinesTable pk @id index !slug { name, slug, franchise (@id | null), order [show @id …], source ('tmdb' |
  9. // 'editor' | 'seed'), tmdbCollection (TMDB collection id | null), orderKind
  10. // ('in-universe' | 'release' | null: not said, tracker#40), supersededBy (@id | null),
  11. // created }
  12. // storage/mpackdb/{franchises,timelines}.db (new tables; nothing in shows.db changes; the seed's collectionchecks.db is
  13. // collections.hl's, the curated seed's franchiseseed.db franchiseseed.hl's)
  14. //
  15. // A TITLE MAY BE IN SEVERAL TIMELINES (Star Trek Prime and Kelvin, or a TMDB collection and a hand-made one): `byShow` (show
  16. // @id → [timeline @id]) is built once at start (`ensureFranchises`, one scan of the timelines) and kept up to date by every
  17. // edit — the show page's widget reads only its own timelines, never all of them.
  18. //
  19. // THE ORDER of a timeline is its in-world order (`order`, editable). The pages can sort it by RELEASE instead: a movie by its
  20. // `release`, a SERIES by the release date of its LAST episode (the newest one already out; none out yet → the newest
  21. // announced one; no episode dates at all → the series' own `release`). Titles without any date go last.
  22. //
  23. // SUPERSEDED (tracker#40): a TMDB collection timeline whose every title is in ONE curated (seed) timeline is hidden — no widget,
  24. // not on /franchises or a franchise's index — but kept (its own page and the editor's finder still reach it): `supersededBy` =
  25. // that curated timeline. Set by the collection seed when it makes one (collections.hl) and by the curated seed's build
  26. // (franchiseseed.hl), which also lifts it when the cover is gone; `seededCoverOf` decides.
  27. //
  28. // PUBLIC LIKE THE OTHER LISTS (mission 054): the pages and the widget's prequel/sequel links show only titles with
  29. // `adult == false` (shows.hl isPublicTitle); the order keeps them, they are just skipped.
  30. //
  31. // THE SEEDS: TMDB movie collections → timelines (collections.hl); the curated franchises of data/franchises.json, movies AND
  32. // series (franchiseseed.hl, tracker#40) — both write through the functions here.
  33. //
  34. // ONE PROCESS PER STORAGE (hybriel#21): the tables are opened here, in the app's process only.
  35. import { MPackDB } from 'hl:mpackdb'
  36. import { env } from 'hl:proc'
  37. import { now } from 'hl:time'
  38. import { storageDir, newId, todayStr, dateOr, firstOf, textArg } from './util.hl'
  39. import { showById, seasonsOfShow, episodesOfSeason, titleWithYear, posterUrlOf, isPublicTitle, titlePath } from './shows.hl'
  40. import { userOfSession } from './users.hl'
  41. import { dbSearch } from './search.hl'
  42. import { slugBaseOf } from './search-helpers.hl'
  43. import { pageNumberOf } from './catalog.hl'
  44. static franchisesTable = new MPackDB(file = storageDir + '/franchises.db', primaryKey = '@id', indexes = ['!slug'])
  45. static timelinesTable = new MPackDB(file = storageDir + '/timelines.db', primaryKey = '@id', indexes = ['!slug'])
  46. // a name as typed: trimmed, null when there is none (the editor's names and summaries)
  47. static trimmedOr = (&v) => { return v != null && hlTypeName(v) == 'String' && v.trim() != '' ? v.trim() : null }
  48. // ---- who may edit: the creator (ident short id az5b2); TRACKER_EDITORS = a comma list of ident ids (the gate sets its own) ----
  49. static editorIds = () => {
  50. v = env('TRACKER_EDITORS')
  51. raw = v != null && v.trim() != '' ? v : 'az5b2'
  52. out = []
  53. for (p of raw.split(',')) { if (p.trim() != '') { out.push(p.trim()) } }
  54. return out
  55. }
  56. static editors = editorIds()
  57. static isEditorSession = (&session) => {
  58. if (session == null || hlTypeName(session) != 'Instance') { return false }
  59. u = userOfSession(&session)
  60. return u != null && u.identity != null && editors.includes(u.identity)
  61. }
  62. // a list's index of x, -1 when it is not in it (hand-written: list helpers beyond includes/push are not trusted, hybriel#1)
  63. static posOf = (&list, &x) => {
  64. let i = 0
  65. while (i < list.length) {
  66. if (list[i] == x) { return i }
  67. i = i + 1
  68. }
  69. return -1
  70. }
  71. // ---- slugs: "Star Trek" → "Star-Trek" (the shows' urlSegment form, search-helpers.hl slugBaseOf; none → 'timeline') -------
  72. static freeSlug = (&table, &name) => {
  73. base = slugBaseOf(&name, 'timeline')
  74. // 'edit' is the editor's own address (/franchises/edit)
  75. if (firstOf(table.find('slug', base)) == null && base != 'edit') { return base }
  76. let n = 2
  77. while (firstOf(table.find('slug', base + '-' + n)) != null) { n = n + 1 }
  78. return base + '-' + n
  79. }
  80. static franchiseBySlug = (&slug) => {
  81. if (slug == null || hlTypeName(slug) != 'String' || slug == '') { return null }
  82. return firstOf(franchisesTable.find('slug', slug))
  83. }
  84. static timelineBySlug = (&slug) => {
  85. if (slug == null || hlTypeName(slug) != 'String' || slug == '') { return null }
  86. return firstOf(timelinesTable.find('slug', slug))
  87. }
  88. static franchiseById = (&id) => { return id == null || hlTypeName(id) != 'String' || id == '' ? null : franchisesTable.fetch(id) }
  89. static timelineById = (&id) => { return id == null || hlTypeName(id) != 'String' || id == '' ? null : timelinesTable.fetch(id) }
  90. // ---- byShow: show @id → [timeline @id] ------------------------------------------------------------------------------------
  91. static byShow = {}
  92. static state = { built = false timelines = 0 ms = 0 }
  93. static indexAdd = (&showId, &timelineId) => {
  94. let l = byShow[showId]
  95. if (l == null) { l = [] }
  96. if (!l.includes(timelineId)) { l.push(timelineId) }
  97. byShow[showId] = l
  98. return true
  99. }
  100. static indexRemove = (&showId, &timelineId) => {
  101. l = byShow[showId]
  102. if (l == null) { return true }
  103. out = []
  104. for (x of l) { if (x != timelineId) { out.push(x) } }
  105. byShow[showId] = out
  106. return true
  107. }
  108. static ensureFranchises = () => {
  109. if (state.built) { return true }
  110. state.built = true
  111. t0 = now()
  112. let n = 0
  113. for (t of timelinesTable.iterate(null, null)) {
  114. n = n + 1
  115. if (t.order != null) { for (sid of t.order) { indexAdd(&sid, &t.id) } }
  116. }
  117. state.timelines = n
  118. state.ms = now() - t0
  119. console.log('franchises: ' + n + ' timelines, ' + franchisesTable.count() + ' franchises (' + state.ms + ' ms)')
  120. return true
  121. }
  122. static timelineIdsOfShow = (&showId) => {
  123. ensureFranchises()
  124. l = byShow[showId]
  125. return l != null ? l : []
  126. }
  127. // ---- a title as the pages list it ------------------------------------------------------------------------------------------
  128. // the date the release sort uses: a movie's release; a series' LAST episode (newest out ≤ today, else newest announced, else
  129. // its own release)
  130. static releaseKeyOf = (&show) => {
  131. if (show.type != 'series') { return dateOr(&show.release) }
  132. today = todayStr()
  133. let lastOut = ''
  134. let lastAny = ''
  135. for (s of seasonsOfShow(&show)) {
  136. for (e of episodesOfSeason(&s)) {
  137. let d = dateOr(&e.release)
  138. if (d != '') {
  139. if (d > lastAny) { lastAny = d }
  140. if (d <= today && d > lastOut) { lastOut = d }
  141. }
  142. }
  143. }
  144. if (lastOut != '') { return lastOut }
  145. if (lastAny != '') { return lastAny }
  146. return dateOr(&show.release)
  147. }
  148. static itemOf = (&show, pos) => {
  149. series = show.type == 'series'
  150. d = releaseKeyOf(&show)
  151. label = d == '' ? 'no date' : (series ? 'last episode ' + d : d)
  152. return { id = show.id pos = pos title = titleWithYear(&show) href = titlePath(&show) posterUrl = posterUrlOf(&show)
  153. kind = series ? 'Series' : 'Movie' date = d dateLabel = label }
  154. }
  155. // the public titles of a timeline, in its order (`pos` = the place in the in-world order, 1-based)
  156. static itemsOf = (&t) => {
  157. out = []
  158. let pos = 0
  159. if (t.order == null) { return out }
  160. for (sid of t.order) {
  161. let s = showById(&sid)
  162. if (s != null && isPublicTitle(&s)) {
  163. pos = pos + 1
  164. out.push(itemOf(&s, pos))
  165. }
  166. }
  167. return out
  168. }
  169. // release order: oldest first, no date last; equal dates keep the timeline order (a stable insertion)
  170. static byReleaseOf = (&items) => {
  171. let out = []
  172. for (it of items) {
  173. let next = []
  174. let done = false
  175. for (x of out) {
  176. if (!done && it.date != '' && (x.date == '' || it.date < x.date)) { next.push(it) done = true }
  177. next.push(x)
  178. }
  179. if (!done) { next.push(it) }
  180. out = next
  181. }
  182. return out
  183. }
  184. // tracker#40: what the timeline's own order is, said beside its count ("23 titles · in-universe order"); none when not known
  185. static orderNoteOf = (&t) => {
  186. if (t.orderKind == 'in-universe') { return ' · in-universe order' }
  187. if (t.orderKind == 'release') { return ' · release order' }
  188. return ''
  189. }
  190. static blockOf = (&t) => {
  191. items = itemsOf(&t)
  192. return { id = t.id name = t.name slug = t.slug href = '/timelines/' + t.slug items = items byRelease = byReleaseOf(&items)
  193. countText = (items.length == 1 ? '1 title' : items.length + ' titles') + orderNoteOf(&t) first = items.length > 0 ? byReleaseOf(&items)[0].date : '' }
  194. }
  195. // ---- the pages -------------------------------------------------------------------------------------------------------------
  196. // /franchises: every franchise (name, how many timelines), then the timelines WITHOUT one (TMDB's collections, by name)
  197. // by name (sortName), a stable bottom-up MERGE sort: the insertion sort this replaced took 2.3 s for the 1,546 collection timelines of the
  198. // real data (mobile audit MK: /franchises took 20 s), this one 50 ms
  199. static mergeByName = (&a, &b) => {
  200. let out = []
  201. let i = 0
  202. let j = 0
  203. while (i < a.length && j < b.length) {
  204. if (b[j].sortName < a[i].sortName) { out.push(b[j]) j = j + 1 } else { out.push(a[i]) i = i + 1 }
  205. }
  206. while (i < a.length) { out.push(a[i]) i = i + 1 }
  207. while (j < b.length) { out.push(b[j]) j = j + 1 }
  208. return out
  209. }
  210. static byNameOf = (&rows) => {
  211. if (rows.length < 2) { return rows }
  212. let runs = []
  213. for (r of rows) { runs.push([r]) }
  214. while (runs.length > 1) {
  215. let next = []
  216. let k = 0
  217. while (k < runs.length) {
  218. if (k + 1 < runs.length) { next.push(mergeByName(&runs[k], runs[k + 1])) } else { next.push(runs[k]) }
  219. k = k + 2
  220. }
  221. runs = next
  222. }
  223. return runs[0]
  224. }
  225. // how many timelines without a franchise one page of /franchises lists (mobile audit MK: all 1,546 at once were a 54,000 px page, 633 KB, 20 s)
  226. static timelinePageSize = 40
  227. static timelinePagesOf = (&count) => {
  228. let p = 1
  229. while (p * timelinePageSize < count) { p = p + 1 }
  230. return p
  231. }
  232. static franchiseIndex = () => {
  233. counts = {}
  234. loose = []
  235. for (t of timelinesTable.iterate(null, null)) {
  236. let n = t.order != null ? t.order.length : 0
  237. // tracker#40: a superseded collection timeline is not listed (nor counted for its franchise)
  238. if (t.supersededBy != null) { n = 0 }
  239. else if (t.franchise != null && t.franchise != '') { counts[t.franchise] = (counts[t.franchise] != null ? counts[t.franchise] : 0) + 1 }
  240. else if (n > 0) { loose.push({ id = t.id name = t.name href = '/timelines/' + t.slug countText = n == 1 ? '1 title' : n + ' titles' sortName = ('' + t.name).toLowerCase() }) }
  241. }
  242. fr = []
  243. for (f of franchisesTable.iterate(null, null)) {
  244. let c = counts[f.id] != null ? counts[f.id] : 0
  245. fr.push({ id = f.id name = f.name href = '/franchises/' + f.slug summary = f.summary != null ? f.summary : ''
  246. countText = c == 1 ? '1 timeline' : c + ' timelines' sortName = ('' + f.name).toLowerCase() })
  247. }
  248. return { franchises = byNameOf(&fr) timelines = byNameOf(&loose) }
  249. }
  250. // { franchises (page 1 only), timelines (this page's), total, page, pages } — `raw` is the address's page segment (anything but a whole number ≥ 1 is
  251. // page 1, past the last is the last)
  252. static franchiseIndexPage = (&raw) => {
  253. all = franchiseIndex()
  254. pages = timelinePagesOf(&all.timelines.length)
  255. let page = pageNumberOf(&raw, &pages)
  256. return { franchises = page == 1 ? all.franchises : [] timelines = all.timelines.slice((page - 1) * timelinePageSize, page * timelinePageSize) total = all.timelines.length page = page pages = pages }
  257. }
  258. // /franchises/<slug>: the franchise and its timelines (oldest first by their first release) — or null
  259. static franchisePage = (slug) => {
  260. f = franchiseBySlug(&slug)
  261. if (f == null) { return null }
  262. let blocks = []
  263. for (t of timelinesTable.find(null, null)) {
  264. if (t.franchise == f.id && t.supersededBy == null) {
  265. let b = blockOf(&t)
  266. let next = []
  267. let done = false
  268. for (x of blocks) {
  269. if (!done && b.first != '' && (x.first == '' || b.first < x.first)) { next.push(b) done = true }
  270. next.push(x)
  271. }
  272. if (!done) { next.push(b) }
  273. blocks = next
  274. }
  275. }
  276. return { kind = 'franchise' name = f.name slug = f.slug summary = f.summary != null ? f.summary : '' franchiseName = '' franchiseHref = '' blocks = blocks editHref = '' }
  277. }
  278. // /timelines/<slug>: one timeline, its franchise above it — or null
  279. static timelinePage = (slug) => {
  280. t = timelineBySlug(&slug)
  281. if (t == null) { return null }
  282. f = franchiseById(&t.franchise)
  283. return { kind = 'timeline' name = t.name slug = t.slug summary = '' franchiseName = f != null ? f.name : '' franchiseHref = f != null ? '/franchises/' + f.slug : ''
  284. blocks = [blockOf(&t)] editHref = '/timelines/' + t.slug + '/edit' }
  285. }
  286. // ---- the widget on a show / movie page: one per timeline the title is in ----------------------------------------------------
  287. // { id, name, href, hasFranchise, franchiseName, franchiseHref, hasPrev, noPrev, prevHref, prevTitle, hasNext, noNext, … }:
  288. // prequel / sequel = the nearest PUBLIC title before / after this one in the timeline's order
  289. static widgetsOf = (&showId) => {
  290. out = []
  291. if (showId == null) { return out }
  292. for (tid of timelineIdsOfShow(&showId)) {
  293. let t = timelinesTable.fetch(tid)
  294. if (t != null && t.supersededBy == null && t.order != null && t.order.includes(showId)) {
  295. let at = posOf(&t.order, &showId)
  296. let prev = null
  297. let i = at - 1
  298. while (prev == null && i >= 0) {
  299. let s = showById(&t.order[i])
  300. if (s != null && isPublicTitle(&s)) { prev = s }
  301. i = i - 1
  302. }
  303. let next = null
  304. let j = at + 1
  305. while (next == null && j < t.order.length) {
  306. let s = showById(&t.order[j])
  307. if (s != null && isPublicTitle(&s)) { next = s }
  308. j = j + 1
  309. }
  310. let f = franchiseById(&t.franchise)
  311. out.push({ id = t.id name = t.name href = '/timelines/' + t.slug
  312. hasFranchise = f != null franchiseName = f != null ? f.name : '' franchiseHref = f != null ? '/franchises/' + f.slug : ''
  313. hasPrev = prev != null noPrev = prev == null prevHref = prev != null ? titlePath(&prev) : '' prevTitle = prev != null ? titleWithYear(&prev) : ''
  314. hasNext = next != null noNext = next == null nextHref = next != null ? titlePath(&next) : '' nextTitle = next != null ? titleWithYear(&next) : '' })
  315. }
  316. }
  317. return out
  318. }
  319. // ---- the editor (the creator only — every caller checks isEditorSession first) ----------------------------------------------
  320. static createFranchise = (name, summary) => {
  321. n = trimmedOr(&name)
  322. if (n == null || n.length > 120) { return { error = 'a franchise needs a name (at most 120 characters)' } }
  323. s = trimmedOr(&summary)
  324. rec = { id = newId() name = n slug = freeSlug(&franchisesTable, &n) shortId = null summary = s != null ? s : '' created = now() }
  325. if (franchisesTable.put(rec) == null) { return { error = 'not stored: ' + franchisesTable.lastError() } }
  326. franchisesTable.persist()
  327. console.log('franchises: new franchise "' + n + '" /franchises/' + rec.slug)
  328. return { ok = true slug = rec.slug id = rec.id }
  329. }
  330. // a new timeline (the editor's, the collection seed's with its collection id, the curated seed's with its order kind)
  331. static putTimeline = (name, &franchiseId, order, source, &collection, &orderKind) => {
  332. rec = { id = newId() name = name slug = freeSlug(&timelinesTable, &name) franchise = franchiseId order = order source = source
  333. tmdbCollection = collection orderKind = orderKind created = now() }
  334. if (timelinesTable.put(rec) == null) { return null }
  335. for (sid of order) { indexAdd(&sid, &rec.id) }
  336. return rec
  337. }
  338. static createTimeline = (&name) => {
  339. n = trimmedOr(&name)
  340. if (n == null || n.length > 120) { return { error = 'a timeline needs a name (at most 120 characters)' } }
  341. rec = putTimeline(n, null, [], 'editor', null, null)
  342. if (rec == null) { return { error = 'not stored: ' + timelinesTable.lastError() } }
  343. timelinesTable.persist()
  344. console.log('franchises: new timeline "' + n + '" /timelines/' + rec.slug)
  345. return { ok = true slug = rec.slug id = rec.id }
  346. }
  347. // the editor's view of one timeline: every title in its order (adult ones too — the creator sees what is there)
  348. static editRowsOf = (&t) => {
  349. out = []
  350. if (t == null || t.order == null) { return out }
  351. let i = 0
  352. last = t.order.length - 1
  353. for (sid of t.order) {
  354. let s = showById(&sid)
  355. if (s != null) {
  356. out.push({ id = s.id pos = i + 1 title = titleWithYear(&s) posterUrl = posterUrlOf(&s) typeLabel = s.type == 'series' ? 'Series' : 'Movie' typeClass = s.type == 'series' ? 'pill type-2' : 'pill type-1'
  357. upClass = i == 0 ? 'quiet mv off' : 'quiet mv' downClass = i == last ? 'quiet mv off' : 'quiet mv' })
  358. }
  359. i = i + 1
  360. }
  361. return out
  362. }
  363. static chipsOf = (&t) => {
  364. out = [{ id = '' name = 'None' cls = t == null || t.franchise == null || t.franchise == '' ? 'chip on' : 'chip' }]
  365. list = []
  366. for (f of franchisesTable.find(null, null)) { list.push({ id = f.id name = f.name cls = t != null && t.franchise == f.id ? 'chip on' : 'chip' sortName = ('' + f.name).toLowerCase() }) }
  367. for (c of byNameOf(&list)) { out.push(c) }
  368. return out
  369. }
  370. static editPageOf = (slug) => {
  371. t = timelineBySlug(&slug)
  372. if (t == null) { return { found = false id = '' name = '' slug = '' viewHref = '' rows = [] chips = [] } }
  373. return { found = true id = t.id name = t.name slug = t.slug viewHref = '/timelines/' + t.slug rows = editRowsOf(&t) chips = chipsOf(&t) }
  374. }
  375. static storeOrder = (t, order) => {
  376. old = t.order != null ? t.order : []
  377. rec = t
  378. rec.order = order
  379. if (timelinesTable.update(rec.id, rec) == null) { return { error = 'not stored: ' + timelinesTable.lastError() } }
  380. timelinesTable.persist()
  381. for (sid of old) { if (!order.includes(sid)) { indexRemove(&sid, &rec.id) } }
  382. for (sid of order) { indexAdd(&sid, &rec.id) }
  383. return { ok = true rows = editRowsOf(&rec) }
  384. }
  385. // tracker#31 (merge.hl): a duplicate title merged into its keeper — in every timeline that lists it, the keeper takes its place
  386. // (once: a timeline that already lists the keeper just drops it) → the number of timelines changed
  387. static titleMerged = (&fromId, &toId) => {
  388. ensureFranchises()
  389. let n = 0
  390. tids = []
  391. if (byShow[fromId] != null) { for (x of byShow[fromId]) { tids.push(x) } }
  392. for (tid of tids) {
  393. let t = timelinesTable.fetch(tid)
  394. if (t != null && t.order != null) {
  395. let order = []
  396. for (sid of t.order) {
  397. let want = sid == fromId ? toId : sid
  398. if (!order.includes(want)) { order.push(want) }
  399. }
  400. let r = storeOrder(t, order)
  401. if (r.error != null) { console.log('merge: timeline ' + tid + ': ' + r.error) } else { n = n + 1 }
  402. }
  403. }
  404. return n
  405. }
  406. static addToTimeline = (&timelineId, &showId) => {
  407. t = timelineById(&timelineId)
  408. if (t == null) { return { error = 'no such timeline' } }
  409. if (showById(&showId) == null) { return { error = 'no such title' } }
  410. order = []
  411. for (sid of t.order) { order.push(sid) }
  412. if (order.includes(showId)) { return { ok = true rows = editRowsOf(&t) } }
  413. order.push(showId)
  414. return storeOrder(t, order)
  415. }
  416. static removeFromTimeline = (&timelineId, &showId) => {
  417. t = timelineById(&timelineId)
  418. if (t == null) { return { error = 'no such timeline' } }
  419. order = []
  420. for (sid of t.order) { if (sid != showId) { order.push(sid) } }
  421. return storeOrder(t, order)
  422. }
  423. // one place up (delta -1) or down (+1); at an end nothing changes
  424. static moveInTimeline = (&timelineId, &showId, &delta) => {
  425. t = timelineById(&timelineId)
  426. if (t == null) { return { error = 'no such timeline' } }
  427. at = posOf(&t.order, &showId)
  428. to = at + (delta < 0 ? -1 : 1)
  429. if (at < 0 || to < 0 || to >= t.order.length) { return { ok = true rows = editRowsOf(&t) } }
  430. order = []
  431. let i = 0
  432. while (i < t.order.length) {
  433. if (i == at) { order.push(t.order[to]) } else if (i == to) { order.push(t.order[at]) } else { order.push(t.order[i]) }
  434. i = i + 1
  435. }
  436. return storeOrder(t, order)
  437. }
  438. static setTimelineFranchise = (&timelineId, franchiseId) => {
  439. t = timelineById(&timelineId)
  440. if (t == null) { return { error = 'no such timeline' } }
  441. let fid = null
  442. if (franchiseId != null && franchiseId != '') {
  443. if (franchiseById(&franchiseId) == null) { return { error = 'no such franchise' } }
  444. fid = franchiseId
  445. }
  446. rec = t
  447. rec.franchise = fid
  448. if (timelinesTable.update(rec.id, rec) == null) { return { error = 'not stored: ' + timelinesTable.lastError() } }
  449. timelinesTable.persist()
  450. return { ok = true chips = chipsOf(&rec) }
  451. }
  452. // the editor's title search: our database (search.hl's index), the titles not in the timeline yet
  453. static editSearch = (&timelineId, text) => {
  454. t = timelineById(&timelineId)
  455. have = t != null && t.order != null ? t.order : []
  456. out = []
  457. r = dbSearch(textArg(&text))
  458. for (x of r.titles) {
  459. let s = showById(&x.id)
  460. if (s != null && !have.includes(s.id)) { out.push({ id = s.id title = titleWithYear(&s) posterUrl = posterUrlOf(&s) typeLabel = s.type == 'series' ? 'Series' : 'Movie' typeClass = s.type == 'series' ? 'pill type-2' : 'pill type-1' }) }
  461. }
  462. return { text = text rows = out note = r.tooShort ? 'Type at least 2 letters.' : (out.length == 0 ? 'Nothing found.' : '') }
  463. }
  464. // the editor's timeline finder: by name, at most 30
  465. static findTimelines = (&text) => {
  466. q = textArg(&text).trim().toLowerCase()
  467. out = []
  468. if (q.length < 2) { return out }
  469. for (t of timelinesTable.find(null, null)) {
  470. if (out.length < 30 && ('' + t.name).toLowerCase().includes(q)) {
  471. let n = t.order != null ? t.order.length : 0
  472. let hidden = t.supersededBy != null ? ' · hidden: all in ' + nameOfTimeline(&t.supersededBy) : ''
  473. out.push({ id = t.id name = t.name href = '/timelines/' + t.slug + '/edit' countText = (n == 1 ? '1 title' : n + ' titles') + hidden })
  474. }
  475. }
  476. return out
  477. }
  478. // ---- the curated seed's reads and writes (franchiseseed.hl, tracker#40) ----------------------------------------------------
  479. // a franchise / timeline by its name, any case — or null (the creator's own of that name is adopted by the seed, never doubled)
  480. static franchiseByName = (&name) => {
  481. q = ('' + name).toLowerCase()
  482. let hit = null
  483. for (f of franchisesTable.find(null, null)) { if (hit == null && ('' + f.name).toLowerCase() == q) { hit = f } }
  484. return hit
  485. }
  486. static timelineByName = (&name) => {
  487. q = ('' + name).toLowerCase()
  488. let hit = null
  489. for (t of timelinesTable.find(null, null)) { if (hit == null && ('' + t.name).toLowerCase() == q) { hit = t } }
  490. return hit
  491. }
  492. // the timeline the collection seed made from TMDB collection `collection`, or null
  493. static timelineOfCollection = (&collection) => {
  494. let hit = null
  495. for (t of timelinesTable.find(null, null)) { if (hit == null && t.tmdbCollection != null && '' + t.tmdbCollection == '' + collection) { hit = t } }
  496. return hit
  497. }
  498. // a seeded timeline rewritten (the data file changed, or titles were imported since): its order and franchise in one update
  499. static storeSeeded = (t, order, &franchiseId) => {
  500. old = t.order != null ? t.order : []
  501. rec = t
  502. rec.order = order
  503. rec.franchise = franchiseId
  504. if (timelinesTable.update(rec.id, rec) == null) { return { error = 'not stored: ' + timelinesTable.lastError() } }
  505. for (sid of old) { if (!order.includes(sid)) { indexRemove(&sid, &rec.id) } }
  506. for (sid of order) { indexAdd(&sid, &rec.id) }
  507. return { ok = true }
  508. }
  509. static persistFranchises = () => {
  510. franchisesTable.persist()
  511. timelinesTable.persist()
  512. return true
  513. }
  514. // ---- superseded collection timelines (tracker#40) --------------------------------------------------------------------------------
  515. static nameOfTimeline = (&id) => {
  516. t = timelineById(&id)
  517. return t != null ? t.name : '?'
  518. }
  519. // for a collection's titles: `cover` = the curated (seed) timeline holding ALL of them, or null; `franchise` = the cover's
  520. // franchise, else the franchise of a curated timeline holding at least ONE of them (a partly covered collection joins it), or null
  521. static seededTimelines = () => {
  522. out = []
  523. for (t of timelinesTable.find(null, null)) { if (t.source == 'seed' && t.order != null) { out.push(t) } }
  524. return out
  525. }
  526. // mission 046: the cover against a PRE-LOADED list of the curated timelines (seededTimelines): the seed's supersedeCollections asks it for
  527. // every one of the 1,417 live timelines — each ask re-read the whole timelines table (find → 1,417 decodes): 2 M decodes, ONE 10–22 s
  528. // tick at the end of boot (the 22 s TTFB of 045's run A). Same answer, the first match in table order as before.
  529. static seededCoverIn = (&seeds, &order) => {
  530. let whole = null
  531. let part = null
  532. for (t of seeds) {
  533. let all = order.length > 0
  534. let any = false
  535. for (sid of order) { if (t.order.includes(sid)) { any = true } else { all = false } }
  536. if (all && whole == null) { whole = t }
  537. if (any && part == null && t.franchise != null && t.franchise != '') { part = t.franchise }
  538. }
  539. // (locals not named like the answer's entries: inside a record literal an entry shadows that name for the entries after it)
  540. return { cover = whole != null ? whole.id : null franchise = whole != null && whole.franchise != null ? whole.franchise : part }
  541. }
  542. static seededCoverOf = (&order) => {
  543. return seededCoverIn(seededTimelines(), &order)
  544. }
  545. // hidden behind `byId` (null: shown again)
  546. static setSuperseded = (t, &byId) => {
  547. rec = t
  548. rec.supersededBy = byId
  549. if (timelinesTable.update(rec.id, rec) == null) { return { error = 'not stored: ' + timelinesTable.lastError() } }
  550. return { ok = true }
  551. }

Branches

Latest commits

  • 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
  • a18957falog: evening re-measure after CBmre
  • aac9a98dlog: evening re-measure startmre
  • 45163bd4log: person/tmdb error hypothesismre