gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit796bface796bfaceT36A: report validated fixes, failed live SSH check and verified rollbackmre796bface/components/search.hl

10.7 KB

  1. // components/search.hl — `/search` and `/search/<text>` (tracker.worldapi.org#14). Creator's words: "in the actionbar a
  2. // search symbol that first checks our database and then has a fetch from web button that actually searches tmdb then for
  3. // things we dont have yet". The header's search icon (components/main.hl) opens this page.
  4. //
  5. // * OUR DATABASE as you type: every keystroke asks the face `searchDb` (search.hl's in-memory index — no table scan) and the
  6. // lists are replaced whole; the address follows the text (`history.replaceState` → `/search/<text>`), so a reload or a
  7. // shared link opens the same results, rendered on the server. Titles (series + movies, with the year) and people (tracker#37:
  8. // with their photo, each → their page).
  9. // * "Fetch from web": the face `searchWeb` asks TMDB (search/multi) and lists the titles we DON'T have yet; "Add" imports
  10. // one (`searchImport`: a new show, then the daily sync's own `syncShow` — seasons, episodes, poster, ids) and opens its
  11. // page. Signed out, "Add" opens the sign-in modal and sends nothing; searching itself needs no sign-in.
  12. //
  13. // THE ADDRESS: `/search/<text>`, not `/search?q=` (the architect's reading) — hl:web gives a page its route's path params
  14. // but no query string, so the text travels as the route param `q`. A full page load gets it already decoded by the server, a
  15. // client-side navigation still encoded — search-helpers.hl `queryOfParam` decodes what is left. '/', '?', '#' go into the address
  16. // as spaces (see searchTyped).
  17. //
  18. // HYBRIEL TRAPS (mission 052): hybriel#121 (rows appended after a session face appear twice) — no list here is ever
  19. // appended to, every list is replaced whole; hybriel#122 (after a session face, members using a route param + session are
  20. // re-derived without the route param) — no member reads both: the results come from `q` alone, the sign-in state from
  21. // `session` alone, and the only session face (`searchImport`) answers no list. The gate checks the lists after a failed
  22. // import (a session face that stays on the page).
  23. parent './main.hl'
  24. import Modal from './modal.hl'
  25. import { navigate } from 'hl:web'
  26. import { appTitle } from '../project.hl'
  27. import { userIdOfSession, loginHref } from '../lib/users.hl'
  28. import { showById, personById, posterUrlOf, titleWithYear, titlePath, typeWordOf, typeClassOf } from '../lib/shows.hl'
  29. import { dbSearch, webSearch } from '../lib/search.hl'
  30. import { personPhotoOf } from '../lib/people.hl'
  31. import { queryOfParam } from '../lib/search-helpers.hl'
  32. import { importWithCredits } from '../lib/details.hl'
  33. import { warmReady, warmText } from '../lib/warm.hl'
  34. import { textArg } from '../lib/util.hl'
  35. import { allowHit, importPerHour } from '../lib/security.hl'
  36. // ---- pure helpers (static: an mpackdb-backed helper must be `static`, hybriel#115/#116; and LOCAL, mission 047) ----
  37. // tracker.worldapi.org#20/#21: our titles say Movie / Series / Show in their type's colour and link to their own page
  38. // (/movies/, /series/, /shows/<slug>); a TMDB result says Movie or TV (TMDB's search tells no TV type — its kind is known
  39. // once it is added)
  40. // everything the page shows for one text — replaced whole on every keystroke
  41. static resultsOf = (text) => {
  42. t = text != null ? '' + text : ''
  43. r = dbSearch(t)
  44. let titleRows = []
  45. for (x of r.titles) {
  46. let s = showById(&x.id)
  47. if (s != null) { titleRows.push({ id = s.id title = titleWithYear(&s) href = titlePath(&s) posterUrl = posterUrlOf(&s) typeLabel = typeWordOf(&s) typeClass = typeClassOf(&s) }) }
  48. }
  49. let peopleRows = []
  50. for (x of r.people) {
  51. let p = personById(&x.id)
  52. // tracker#37: with their photo (people.hl personPhotoOf: ours, else TMDB's, else the placeholder)
  53. if (p != null) { peopleRows.push({ id = p.id name = p.name href = '/person/' + p.urlSegment photoUrl = personPhotoOf(&p) }) }
  54. }
  55. let note = ''
  56. if (t.trim() == '') { note = 'Search our shows, movies and people.' }
  57. else if (r.tooShort) { note = 'Type at least 2 letters.' }
  58. else if (titleRows.length == 0 && peopleRows.length == 0) { note = 'Nothing in our database yet.' }
  59. let more = ''
  60. if (r.titleCount > titleRows.length) { more = 'The first ' + titleRows.length + ' of ' + r.titleCount + ' titles — type more to narrow it down.' }
  61. return { text = t titleRows = titleRows peopleRows = peopleRows note = note more = more canFetch = t.trim() != '' && !r.tooShort }
  62. }
  63. static webRowsOf = (text) => {
  64. w = webSearch(&text)
  65. rows = []
  66. for (x of w.rows) {
  67. rows.push({ key = x.kind + ':' + x.tmdbId kind = x.kind tmdbId = x.tmdbId title = x.year != '' ? x.title + ' (' + x.year + ')' : x.title
  68. overview = x.overview posterUrl = x.posterUrl typeLabel = x.kind == 'movie' ? 'Movie' : 'TV' typeClass = x.kind == 'movie' ? 'pill type-1' : 'pill type-2' })
  69. }
  70. let note = ''
  71. if (w.failed != null) { note = 'TMDB did not answer — try again in a moment.' }
  72. else if (rows.length == 0) { note = w.left > 0 ? 'TMDB found nothing we don\'t have already.' : 'TMDB found nothing either.' }
  73. return { text = text rows = rows note = note }
  74. }
  75. // ---- reactive members -----------------------------------------------------------------------
  76. q = null
  77. session = null
  78. userId = userIdOfSession(&session)
  79. signedIn = userId != null
  80. signInHref = loginHref
  81. startText = queryOfParam(&q)
  82. query = startText
  83. found = resultsOf(startText)
  84. // the text the lists on the page belong to (also an attribute of <search-page>: the gate waits for it)
  85. foundText = found.text
  86. titleRows = found.titleRows
  87. peopleRows = found.peopleRows
  88. hasTitles = titleRows.length > 0
  89. hasPeople = peopleRows.length > 0
  90. note = found.note
  91. hasNote = note != ''
  92. more = found.more
  93. hasMore = more != ''
  94. canFetch = found.canFetch
  95. webRows = []
  96. hasWeb = webRows.length > 0
  97. webNote = ''
  98. hasWebNote = webNote != ''
  99. webBusy = false
  100. busyText = ''
  101. isBusy = busyText != ''
  102. importError = ''
  103. hasImportError = importError != ''
  104. __title = startText != '' ? startText + ' — search — ' + appTitle : 'Search — ' + appTitle
  105. // mission T17 (boot time): the server listens while bootscan.hl still builds the lists / slug maps / search index (lib/warm.hl); until the 'all'
  106. // stage is there this page says so instead of showing half a map
  107. warming = !warmReady('all')
  108. warmNote = warming ? warmText() : ''
  109. View {
  110. searchPage { "data-for" = foundText
  111. if (warming) { p { id = "warming" class = "message banner" warmNote } }
  112. searchBar {
  113. input { id = "searchinput" type = "search" name = "q" value = startText placeholder = "Shows, movies, people" autocomplete = "off" autofocus = "autofocus" enterkeyhint = "search" "aria-label" = "Search" on input(e) { emit searchTyped(e) } }
  114. }
  115. if (hasNote) { p { id = "searchnote" class = "muted" note } }
  116. if (hasTitles) {
  117. searchSection { id = "titles"
  118. h2 { "Titles" }
  119. for (row of titleRows) {
  120. searchRow {
  121. img { class = "search-poster" src = row.posterUrl alt = "" }
  122. a { class = "search-title" href = row.href row.title }
  123. searchType { class = row.typeClass row.typeLabel }
  124. }
  125. }
  126. if (hasMore) { p { class = "muted search-more" more } }
  127. }
  128. }
  129. if (hasPeople) {
  130. searchSection { id = "people"
  131. h2 { "People" }
  132. for (row of peopleRows) {
  133. searchRow {
  134. img { class = "search-poster" src = row.photoUrl alt = "" }
  135. a { class = "search-person" href = row.href row.name }
  136. }
  137. }
  138. }
  139. }
  140. if (canFetch) {
  141. webBar {
  142. button { id = "fetchweb" type = "button" class = "primary" "Fetch from web" on click(e) { emit searchFetch(e) } }
  143. if (webBusy) { span { class = "muted" "Asking TMDB…" } }
  144. }
  145. }
  146. if (hasWebNote) { p { id = "webnote" class = "muted" webNote } }
  147. if (hasImportError) { p { id = "importerror" class = "message" importError } }
  148. if (isBusy) { p { id = "importbusy" class = "muted" busyText } }
  149. if (hasWeb) {
  150. searchSection { id = "web"
  151. h2 { "From the web (TMDB)" }
  152. for (row of webRows) {
  153. webRow {
  154. img { class = "search-poster" src = row.posterUrl alt = "" }
  155. webText {
  156. webTitle { row.title }
  157. searchType { class = row.typeClass row.typeLabel }
  158. }
  159. button { type = "button" class = "inverted add" value = row.key "Add" on click(e) { emit searchPick(row.kind, row.tmdbId, row.title) } }
  160. }
  161. }
  162. }
  163. }
  164. Modal { id = "signin" title = "Sign in"
  165. p { "You need to sign in to add titles." }
  166. footer {
  167. button { type = "button" value = "close" "Close" }
  168. a { id = "signin-link" class = "button primary" href = signInHref "Sign in with ident" }
  169. }
  170. }
  171. }
  172. }
  173. // every keystroke: the address follows the text, then our database answers (an answer for an older text is dropped)
  174. on searchTyped(e) {
  175. v = e.target.value
  176. query = v
  177. // the server decodes the path BEFORE routing: an encoded '/' would split the segment (404) and '?' / '#' would cut it —
  178. // in the address they become spaces (the search treats them as word breaks anyway)
  179. t = v.replaceAll('/', ' ').replaceAll('?', ' ').replaceAll('#', ' ').trim()
  180. window.history.replaceState(window.history.state, '', t == '' ? '/search' : '/search/' + encodeURIComponent(t))
  181. r = emit server searchDb(v)
  182. if (r == null || query != v) { return null }
  183. found = r
  184. webRows = []
  185. webNote = ''
  186. importError = ''
  187. }
  188. on searchFetch(e) {
  189. v = query
  190. webBusy = true
  191. importError = ''
  192. r = emit server searchWeb(v)
  193. webBusy = false
  194. if (r == null || query != v) { return null }
  195. webRows = r.rows
  196. webNote = r.note
  197. }
  198. // signed out: the modal, nothing is sent
  199. on searchPick(kind, tmdbId, title) {
  200. if (!signedIn) { document.getElementById('signin').showModal() return null }
  201. if (busyText != '') { return null }
  202. importError = ''
  203. busyText = 'Adding ' + title + ' …'
  204. r = emit server searchImport(kind, '' + tmdbId)
  205. busyText = ''
  206. if (r == null || r.error != null) {
  207. importError = r != null ? 'Could not add it: ' + r.error : 'Could not add it.'
  208. return null
  209. }
  210. navigate(r.path)
  211. }
  212. // ---- the faces (unique names, hl:web dispatches by name across all files; the trailing `session` is the server's) ----
  213. on server searchDb(text) {
  214. return resultsOf(textArg(&text))
  215. }
  216. on server searchWeb(text) {
  217. return webRowsOf(textArg(&text))
  218. }
  219. on server searchImport(kind, tmdbId, session) {
  220. uid = userIdOfSession(&session)
  221. if (uid == null) { return { error = 'you are not signed in' } }
  222. // mission SK: an import is a TMDB request and a new record — at most `importPerHour` an hour per user; the id is a number or text
  223. if (hlTypeName(tmdbId) != 'Number' && hlTypeName(tmdbId) != 'String') { return { error = 'bad TMDB id' } }
  224. if (!allowHit('import', &uid, &importPerHour, 3600000)) { return { error = 'too many imports this hour — try again later' } }
  225. // tracker#28: with the imported title's cast + crew, from the import's own details answer (details.hl)
  226. r = importWithCredits(&kind, tmdbId)
  227. if (r.error != null) { return { error = r.error } }
  228. return { ok = true slug = r.slug path = r.path created = r.created }
  229. }

Branches

Latest commits

  • 796bfaceT36A: report validated fixes, failed live SSH check and verified rollbackmre
  • 7aea52batmdb fix: protect owners without slugs and record passing gatesmre
  • 9b3c2559tmdb fix: revalidate planned identities and reserve target idsmre
  • d56ee0daT36A: report unsafe TMDB apply revalidation and stop before livemre
  • 08e55255log: HM56 done, T36A startedmre
  • 5fd2ac75status: #36 GOmre
  • 0d253b45mission: T36Amre
  • 2aa22d23log: #36 GOmre
  • 833aee2flog: #50 closed, HM56 startedmre
  • ac58c1e5mission: HM56mre
  • 58979c0clog: HM54 held for decision, DP65 startedmre
  • bb2ebc67log: #64 live, #53 mergedmre
  • 27cde42fMerge hm53-dom: tracker#53 inputs bound to membersmre
  • 2156cc7flog: HM54 reviewmre
  • 9fb82efclog: HM54 gap, fill continuesmre
  • b632eb8dlog: HM53 done, HM54 startedmre
  • ccb1a263tracker#53: bind editor drafts and use key event focus targetmre
  • 3356fcfclog: #52 blocked, HM53 startedmre
  • 833dcfc9Merge hm52-search: report only, tracker#52 blocked by framework gapmre
  • 73010cc0HM52: report replace-navigation search draft regressionmre