Add search target check params
This commit is contained in:
parent
7c87746e4b
commit
3b0bd70aa0
|
@ -112,10 +112,10 @@ export class SearchTypeaheadComponent implements OnInit, AfterViewInit, AfterVie
|
||||||
const searchIndexConfig = this.serverConfig.search.searchIndex
|
const searchIndexConfig = this.serverConfig.search.searchIndex
|
||||||
|
|
||||||
if (!this.activeSearch) {
|
if (!this.activeSearch) {
|
||||||
if (searchIndexConfig.enabled && searchIndexConfig.isDefaultSearch) {
|
if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
|
||||||
this.activeSearch = 'search-instance'
|
|
||||||
} else {
|
|
||||||
this.activeSearch = 'search-index'
|
this.activeSearch = 'search-index'
|
||||||
|
} else {
|
||||||
|
this.activeSearch = 'search-instance'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -110,7 +110,7 @@ search:
|
||||||
# Use a third party index instead of your local index, only for search results
|
# Use a third party index instead of your local index, only for search results
|
||||||
# Useful to discover content outside of your instance
|
# Useful to discover content outside of your instance
|
||||||
search_index:
|
search_index:
|
||||||
enabled: true
|
enabled: false
|
||||||
# URL of the search index, that should use the same search API and routes
|
# URL of the search index, that should use the same search API and routes
|
||||||
# than PeerTube: https://docs.joinpeertube.org/api-rest-reference.html
|
# than PeerTube: https://docs.joinpeertube.org/api-rest-reference.html
|
||||||
# You should deploy your own with https://framagit.org/framasoft/peertube/search-index,
|
# You should deploy your own with https://framagit.org/framasoft/peertube/search-index,
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import validator from 'validator'
|
import validator from 'validator'
|
||||||
import { isArray } from './misc'
|
import { SearchTargetType } from '@shared/models/search/search-target-query.model'
|
||||||
|
import { isArray, exists } from './misc'
|
||||||
|
import { CONFIG } from '@server/initializers/config'
|
||||||
|
|
||||||
function isNumberArray (value: any) {
|
function isNumberArray (value: any) {
|
||||||
return isArray(value) && value.every(v => validator.isInt('' + v))
|
return isArray(value) && value.every(v => validator.isInt('' + v))
|
||||||
|
@ -13,10 +15,23 @@ function isNSFWQueryValid (value: any) {
|
||||||
return value === 'true' || value === 'false' || value === 'both'
|
return value === 'true' || value === 'false' || value === 'both'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isSearchTargetValid (value: SearchTargetType) {
|
||||||
|
if (!exists(value)) return true
|
||||||
|
|
||||||
|
const searchIndexConfig = CONFIG.SEARCH.SEARCH_INDEX
|
||||||
|
|
||||||
|
if (value === 'local' && (!searchIndexConfig.ENABLED || !searchIndexConfig.DISABLE_LOCAL_SEARCH)) return true
|
||||||
|
|
||||||
|
if (value === 'search-index' && searchIndexConfig.ENABLED) return true
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
export {
|
export {
|
||||||
isNumberArray,
|
isNumberArray,
|
||||||
isStringArray,
|
isStringArray,
|
||||||
isNSFWQueryValid
|
isNSFWQueryValid,
|
||||||
|
isSearchTargetValid
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { areValidationErrors } from './utils'
|
||||||
import { logger } from '../../helpers/logger'
|
import { logger } from '../../helpers/logger'
|
||||||
import { query } from 'express-validator'
|
import { query } from 'express-validator'
|
||||||
import { isDateValid } from '../../helpers/custom-validators/misc'
|
import { isDateValid } from '../../helpers/custom-validators/misc'
|
||||||
|
import { isSearchTargetValid } from '@server/helpers/custom-validators/search'
|
||||||
|
|
||||||
const videosSearchValidator = [
|
const videosSearchValidator = [
|
||||||
query('search').optional().not().isEmpty().withMessage('Should have a valid search'),
|
query('search').optional().not().isEmpty().withMessage('Should have a valid search'),
|
||||||
|
@ -16,6 +17,8 @@ const videosSearchValidator = [
|
||||||
query('durationMin').optional().isInt().withMessage('Should have a valid min duration'),
|
query('durationMin').optional().isInt().withMessage('Should have a valid min duration'),
|
||||||
query('durationMax').optional().isInt().withMessage('Should have a valid max duration'),
|
query('durationMax').optional().isInt().withMessage('Should have a valid max duration'),
|
||||||
|
|
||||||
|
query('searchTarget').optional().custom(isSearchTargetValid).withMessage('Should have a valid search target'),
|
||||||
|
|
||||||
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||||
logger.debug('Checking videos search query', { parameters: req.query })
|
logger.debug('Checking videos search query', { parameters: req.query })
|
||||||
|
|
||||||
|
@ -27,6 +30,7 @@ const videosSearchValidator = [
|
||||||
|
|
||||||
const videoChannelsSearchValidator = [
|
const videoChannelsSearchValidator = [
|
||||||
query('search').not().isEmpty().withMessage('Should have a valid search'),
|
query('search').not().isEmpty().withMessage('Should have a valid search'),
|
||||||
|
query('searchTarget').optional().custom(isSearchTargetValid).withMessage('Should have a valid search target'),
|
||||||
|
|
||||||
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||||
logger.debug('Checking video channels search query', { parameters: req.query })
|
logger.debug('Checking video channels search query', { parameters: req.query })
|
||||||
|
|
|
@ -1,14 +1,32 @@
|
||||||
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
||||||
|
|
||||||
import 'mocha'
|
import 'mocha'
|
||||||
|
import {
|
||||||
import { cleanupTests, flushAndRunServer, immutableAssign, makeGetRequest, ServerInfo } from '../../../../shared/extra-utils'
|
cleanupTests,
|
||||||
|
flushAndRunServer,
|
||||||
|
immutableAssign,
|
||||||
|
makeGetRequest,
|
||||||
|
ServerInfo,
|
||||||
|
updateCustomSubConfig,
|
||||||
|
setAccessTokensToServers
|
||||||
|
} from '../../../../shared/extra-utils'
|
||||||
import {
|
import {
|
||||||
checkBadCountPagination,
|
checkBadCountPagination,
|
||||||
checkBadSortPagination,
|
checkBadSortPagination,
|
||||||
checkBadStartPagination
|
checkBadStartPagination
|
||||||
} from '../../../../shared/extra-utils/requests/check-api-params'
|
} from '../../../../shared/extra-utils/requests/check-api-params'
|
||||||
|
|
||||||
|
function updateSearchIndex (server: ServerInfo, enabled: boolean, disableLocalSearch = false) {
|
||||||
|
return updateCustomSubConfig(server.url, server.accessToken, {
|
||||||
|
search: {
|
||||||
|
searchIndex: {
|
||||||
|
enabled,
|
||||||
|
disableLocalSearch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
describe('Test videos API validator', function () {
|
describe('Test videos API validator', function () {
|
||||||
let server: ServerInfo
|
let server: ServerInfo
|
||||||
|
|
||||||
|
@ -18,6 +36,7 @@ describe('Test videos API validator', function () {
|
||||||
this.timeout(30000)
|
this.timeout(30000)
|
||||||
|
|
||||||
server = await flushAndRunServer(1)
|
server = await flushAndRunServer(1)
|
||||||
|
await setAccessTokensToServers([ server ])
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('When searching videos', function () {
|
describe('When searching videos', function () {
|
||||||
|
@ -144,6 +163,62 @@ describe('Test videos API validator', function () {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('Search target', function () {
|
||||||
|
|
||||||
|
it('Should fail/succeed depending on the search target', async function () {
|
||||||
|
this.timeout(10000)
|
||||||
|
|
||||||
|
const query = { search: 'coucou' }
|
||||||
|
const paths = [
|
||||||
|
'/api/v1/search/video-channels/',
|
||||||
|
'/api/v1/search/videos/'
|
||||||
|
]
|
||||||
|
|
||||||
|
for (const path of paths) {
|
||||||
|
{
|
||||||
|
const customQuery = immutableAssign(query, { searchTarget: 'hello' })
|
||||||
|
await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: 400 })
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const customQuery = immutableAssign(query, { searchTarget: undefined })
|
||||||
|
await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: 200 })
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const customQuery = immutableAssign(query, { searchTarget: 'local' })
|
||||||
|
await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: 200 })
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const customQuery = immutableAssign(query, { searchTarget: 'search-index' })
|
||||||
|
await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: 400 })
|
||||||
|
}
|
||||||
|
|
||||||
|
await updateSearchIndex(server, true, true)
|
||||||
|
|
||||||
|
{
|
||||||
|
const customQuery = immutableAssign(query, { searchTarget: 'local' })
|
||||||
|
await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: 400 })
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const customQuery = immutableAssign(query, { searchTarget: 'search-index' })
|
||||||
|
await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: 200 })
|
||||||
|
}
|
||||||
|
|
||||||
|
await updateSearchIndex(server, true, false)
|
||||||
|
|
||||||
|
{
|
||||||
|
const customQuery = immutableAssign(query, { searchTarget: 'local' })
|
||||||
|
await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: 200 })
|
||||||
|
}
|
||||||
|
|
||||||
|
await updateSearchIndex(server, false, false)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
after(async function () {
|
after(async function () {
|
||||||
await cleanupTests([ server ])
|
await cleanupTests([ server ])
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
import { NSFWQuery } from './nsfw-query.model'
|
|
||||||
import { VideoFilter } from '../videos'
|
import { VideoFilter } from '../videos'
|
||||||
|
import { NSFWQuery } from './nsfw-query.model'
|
||||||
import { SearchTargetQuery } from './search-target-query.model'
|
import { SearchTargetQuery } from './search-target-query.model'
|
||||||
|
|
||||||
export interface VideosSearchQuery extends SearchTargetQuery {
|
export interface VideosSearchQuery extends SearchTargetQuery {
|
||||||
forceLocalSearch?: boolean
|
|
||||||
|
|
||||||
search?: string
|
search?: string
|
||||||
|
|
||||||
start?: number
|
start?: number
|
||||||
|
|
Loading…
Reference in New Issue