allow limiting video-comments rss feeds to an account or video channel
[oweals/peertube.git] / server / helpers / custom-validators / search.ts
1 import validator from 'validator'
2 import { SearchTargetType } from '@shared/models/search/search-target-query.model'
3 import { isArray, exists } from './misc'
4 import { CONFIG } from '@server/initializers/config'
5
6 function isNumberArray (value: any) {
7   return isArray(value) && value.every(v => validator.isInt('' + v))
8 }
9
10 function isStringArray (value: any) {
11   return isArray(value) && value.every(v => typeof v === 'string')
12 }
13
14 function isNSFWQueryValid (value: any) {
15   return value === 'true' || value === 'false' || value === 'both'
16 }
17
18 function isSearchTargetValid (value: SearchTargetType) {
19   if (!exists(value)) return true
20
21   const searchIndexConfig = CONFIG.SEARCH.SEARCH_INDEX
22
23   if (value === 'local' && (!searchIndexConfig.ENABLED || !searchIndexConfig.DISABLE_LOCAL_SEARCH)) return true
24
25   if (value === 'search-index' && searchIndexConfig.ENABLED) return true
26
27   return false
28 }
29
30 // ---------------------------------------------------------------------------
31
32 export {
33   isNumberArray,
34   isStringArray,
35   isNSFWQueryValid,
36   isSearchTargetValid
37 }