tracker
All repositories: gitoria
10.7 KB
// components/search.hl — `/search` and `/search/<text>` (tracker.worldapi.org#14). Creator's words: "in the actionbar a// search symbol that first checks our database and then has a fetch from web button that actually searches tmdb then for// things we dont have yet". The header's search icon (components/main.hl) opens this page.//// * OUR DATABASE as you type: every keystroke asks the face `searchDb` (search.hl's in-memory index — no table scan) and the// lists are replaced whole; the address follows the text (`history.replaceState` → `/search/<text>`), so a reload or a// shared link opens the same results, rendered on the server. Titles (series + movies, with the year) and people (tracker#37:// with their photo, each → their page).// * "Fetch from web": the face `searchWeb` asks TMDB (search/multi) and lists the titles we DON'T have yet; "Add" imports// one (`searchImport`: a new show, then the daily sync's own `syncShow` — seasons, episodes, poster, ids) and opens its// page. Signed out, "Add" opens the sign-in modal and sends nothing; searching itself needs no sign-in.//// THE ADDRESS: `/search/<text>`, not `/search?q=` (the architect's reading) — hl:web gives a page its route's path params// 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// client-side navigation still encoded — search-helpers.hl `queryOfParam` decodes what is left. '/', '?', '#' go into the address// as spaces (see searchTyped).//// HYBRIEL TRAPS (mission 052): hybriel#121 (rows appended after a session face appear twice) — no list here is ever// appended to, every list is replaced whole; hybriel#122 (after a session face, members using a route param + session are// re-derived without the route param) — no member reads both: the results come from `q` alone, the sign-in state from// `session` alone, and the only session face (`searchImport`) answers no list. The gate checks the lists after a failed// import (a session face that stays on the page).parent './main.hl'import Modal from './modal.hl'import { navigate } from 'hl:web'import { appTitle } from '../project.hl'import { userIdOfSession, loginHref } from '../lib/users.hl'import { showById, personById, posterUrlOf, titleWithYear, titlePath, typeWordOf, typeClassOf } from '../lib/shows.hl'import { dbSearch, webSearch } from '../lib/search.hl'import { personPhotoOf } from '../lib/people.hl'import { queryOfParam } from '../lib/search-helpers.hl'import { importWithCredits } from '../lib/details.hl'import { warmReady, warmText } from '../lib/warm.hl'import { textArg } from '../lib/util.hl'import { allowHit, importPerHour } from '../lib/security.hl'// ---- pure helpers (static: an mpackdb-backed helper must be `static`, hybriel#115/#116; and LOCAL, mission 047) ----// tracker.worldapi.org#20/#21: our titles say Movie / Series / Show in their type's colour and link to their own page// (/movies/, /series/, /shows/<slug>); a TMDB result says Movie or TV (TMDB's search tells no TV type — its kind is known// once it is added)// everything the page shows for one text — replaced whole on every keystrokestatic resultsOf = (text) => {t = text != null ? '' + text : ''r = dbSearch(t)let titleRows = []for (x of r.titles) {let s = showById(&x.id)if (s != null) { titleRows.push({ id = s.id title = titleWithYear(&s) href = titlePath(&s) posterUrl = posterUrlOf(&s) typeLabel = typeWordOf(&s) typeClass = typeClassOf(&s) }) }}let peopleRows = []for (x of r.people) {let p = personById(&x.id)// tracker#37: with their photo (people.hl personPhotoOf: ours, else TMDB's, else the placeholder)if (p != null) { peopleRows.push({ id = p.id name = p.name href = '/person/' + p.urlSegment photoUrl = personPhotoOf(&p) }) }}let note = ''if (t.trim() == '') { note = 'Search our shows, movies and people.' }else if (r.tooShort) { note = 'Type at least 2 letters.' }else if (titleRows.length == 0 && peopleRows.length == 0) { note = 'Nothing in our database yet.' }let more = ''if (r.titleCount > titleRows.length) { more = 'The first ' + titleRows.length + ' of ' + r.titleCount + ' titles — type more to narrow it down.' }return { text = t titleRows = titleRows peopleRows = peopleRows note = note more = more canFetch = t.trim() != '' && !r.tooShort }}static webRowsOf = (text) => {w = webSearch(&text)rows = []for (x of w.rows) {rows.push({ key = x.kind + ':' + x.tmdbId kind = x.kind tmdbId = x.tmdbId title = x.year != '' ? x.title + ' (' + x.year + ')' : x.titleoverview = x.overview posterUrl = x.posterUrl typeLabel = x.kind == 'movie' ? 'Movie' : 'TV' typeClass = x.kind == 'movie' ? 'pill type-1' : 'pill type-2' })}let note = ''if (w.failed != null) { note = 'TMDB did not answer — try again in a moment.' }else if (rows.length == 0) { note = w.left > 0 ? 'TMDB found nothing we don\'t have already.' : 'TMDB found nothing either.' }return { text = text rows = rows note = note }}// ---- reactive members -----------------------------------------------------------------------q = nullsession = nulluserId = userIdOfSession(&session)signedIn = userId != nullsignInHref = loginHrefstartText = queryOfParam(&q)query = startTextfound = resultsOf(startText)// the text the lists on the page belong to (also an attribute of <search-page>: the gate waits for it)foundText = found.texttitleRows = found.titleRowspeopleRows = found.peopleRowshasTitles = titleRows.length > 0hasPeople = peopleRows.length > 0note = found.notehasNote = note != ''more = found.morehasMore = more != ''canFetch = found.canFetchwebRows = []hasWeb = webRows.length > 0webNote = ''hasWebNote = webNote != ''webBusy = falsebusyText = ''isBusy = busyText != ''importError = ''hasImportError = importError != ''__title = startText != '' ? startText + ' — search — ' + appTitle : 'Search — ' + appTitle// mission T17 (boot time): the server listens while bootscan.hl still builds the lists / slug maps / search index (lib/warm.hl); until the 'all'// stage is there this page says so instead of showing half a mapwarming = !warmReady('all')warmNote = warming ? warmText() : ''View {searchPage { "data-for" = foundTextif (warming) { p { id = "warming" class = "message banner" warmNote } }searchBar {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) } }}if (hasNote) { p { id = "searchnote" class = "muted" note } }if (hasTitles) {searchSection { id = "titles"h2 { "Titles" }for (row of titleRows) {searchRow {img { class = "search-poster" src = row.posterUrl alt = "" }a { class = "search-title" href = row.href row.title }searchType { class = row.typeClass row.typeLabel }}}if (hasMore) { p { class = "muted search-more" more } }}}if (hasPeople) {searchSection { id = "people"h2 { "People" }for (row of peopleRows) {searchRow {img { class = "search-poster" src = row.photoUrl alt = "" }a { class = "search-person" href = row.href row.name }}}}}if (canFetch) {webBar {button { id = "fetchweb" type = "button" class = "primary" "Fetch from web" on click(e) { emit searchFetch(e) } }if (webBusy) { span { class = "muted" "Asking TMDB…" } }}}if (hasWebNote) { p { id = "webnote" class = "muted" webNote } }if (hasImportError) { p { id = "importerror" class = "message" importError } }if (isBusy) { p { id = "importbusy" class = "muted" busyText } }if (hasWeb) {searchSection { id = "web"h2 { "From the web (TMDB)" }for (row of webRows) {webRow {img { class = "search-poster" src = row.posterUrl alt = "" }webText {webTitle { row.title }searchType { class = row.typeClass row.typeLabel }}button { type = "button" class = "inverted add" value = row.key "Add" on click(e) { emit searchPick(row.kind, row.tmdbId, row.title) } }}}}}Modal { id = "signin" title = "Sign in"p { "You need to sign in to add titles." }footer {button { type = "button" value = "close" "Close" }a { id = "signin-link" class = "button primary" href = signInHref "Sign in with ident" }}}}}// every keystroke: the address follows the text, then our database answers (an answer for an older text is dropped)on searchTyped(e) {v = e.target.valuequery = v// the server decodes the path BEFORE routing: an encoded '/' would split the segment (404) and '?' / '#' would cut it —// in the address they become spaces (the search treats them as word breaks anyway)t = v.replaceAll('/', ' ').replaceAll('?', ' ').replaceAll('#', ' ').trim()window.history.replaceState(window.history.state, '', t == '' ? '/search' : '/search/' + encodeURIComponent(t))r = emit server searchDb(v)if (r == null || query != v) { return null }found = rwebRows = []webNote = ''importError = ''}on searchFetch(e) {v = querywebBusy = trueimportError = ''r = emit server searchWeb(v)webBusy = falseif (r == null || query != v) { return null }webRows = r.rowswebNote = r.note}// signed out: the modal, nothing is senton searchPick(kind, tmdbId, title) {if (!signedIn) { document.getElementById('signin').showModal() return null }if (busyText != '') { return null }importError = ''busyText = 'Adding ' + title + ' …'r = emit server searchImport(kind, '' + tmdbId)busyText = ''if (r == null || r.error != null) {importError = r != null ? 'Could not add it: ' + r.error : 'Could not add it.'return null}navigate(r.path)}// ---- the faces (unique names, hl:web dispatches by name across all files; the trailing `session` is the server's) ----on server searchDb(text) {return resultsOf(textArg(&text))}on server searchWeb(text) {return webRowsOf(textArg(&text))}on server searchImport(kind, tmdbId, session) {uid = userIdOfSession(&session)if (uid == null) { return { error = 'you are not signed in' } }// 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 textif (hlTypeName(tmdbId) != 'Number' && hlTypeName(tmdbId) != 'String') { return { error = 'bad TMDB id' } }if (!allowHit('import', &uid, &importPerHour, 3600000)) { return { error = 'too many imports this hour — try again later' } }// tracker#28: with the imported title's cast + crew, from the import's own details answer (details.hl)r = importWithCredits(&kind, tmdbId)if (r.error != null) { return { error = r.error } }return { ok = true slug = r.slug path = r.path created = r.created }}
Branches
Latest commits
- a31c8f2fdocs: tracker#36 remaining listmre
- 975d06d1log: #36 live, DP66 startedmre
- d6d03fa3mission: T36Bmre
- e7512d00status: #36 rolled back, retry askedmre
- c98e2043log: T36A rollbackmre
- ae905f15Merge t36a: tracker#36 apply rechecks, reportmre
- 796bfaceT36A: report validated fixes, failed live SSH check and verified rollbackmre
- 55257852log: #53 live, #56 mergedmre
- 78b39f1bMerge hm56-rows: tracker#56 list builders insert oncemre
- 7aea52batmdb fix: protect owners without slugs and record passing gatesmre
- 9b3c2559tmdb fix: revalidate planned identities and reserve target idsmre
- 6da612fclog: T36A tool defectmre
- d56ee0daT36A: report unsafe TMDB apply revalidation and stop before livemre
- 08e55255log: HM56 done, T36A startedmre
- 91bcdf0eAvoid per-row list copies and reproduce keyed-row blockersmre
- 5fd2ac75status: #36 GOmre
- 0d253b45mission: T36Amre
- 2aa22d23log: #36 GOmre
- 833aee2flog: #50 closed, HM56 startedmre
- ac58c1e5mission: HM56mre