]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/search.ts
Add ability to search playlists
[github/Chocobozzz/PeerTube.git] / server / lib / search.ts
1 import * as express from 'express'
2 import { CONFIG } from '@server/initializers/config'
3 import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
4 import { getServerActor } from '@server/models/application/application'
5 import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
6 import { SearchTargetQuery } from '@shared/models'
7
8 function isSearchIndexSearch (query: SearchTargetQuery) {
9 if (query.searchTarget === 'search-index') return true
10
11 const searchIndexConfig = CONFIG.SEARCH.SEARCH_INDEX
12
13 if (searchIndexConfig.ENABLED !== true) return false
14
15 if (searchIndexConfig.DISABLE_LOCAL_SEARCH) return true
16 if (searchIndexConfig.IS_DEFAULT_SEARCH && !query.searchTarget) return true
17
18 return false
19 }
20
21 async function buildMutedForSearchIndex (res: express.Response) {
22 const serverActor = await getServerActor()
23 const accountIds = [ serverActor.Account.id ]
24
25 if (res.locals.oauth) {
26 accountIds.push(res.locals.oauth.token.User.Account.id)
27 }
28
29 const [ blockedHosts, blockedAccounts ] = await Promise.all([
30 ServerBlocklistModel.listHostsBlockedBy(accountIds),
31 AccountBlocklistModel.listHandlesBlockedBy(accountIds)
32 ])
33
34 return {
35 blockedHosts,
36 blockedAccounts
37 }
38 }
39
40 function isURISearch (search: string) {
41 if (!search) return false
42
43 return search.startsWith('http://') || search.startsWith('https://')
44 }
45
46 export {
47 isSearchIndexSearch,
48 buildMutedForSearchIndex,
49 isURISearch
50 }