Support handles and uuids filters

This commit is contained in:
Chocobozzz 2021-07-29 10:57:33 +02:00
parent cf445ace4e
commit ac0eb5f3ac
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
9 changed files with 78 additions and 6 deletions

@ -1 +1 @@
Subproject commit 28be9d1d3ee92db7c9fd84c82e4ea20900eab0f5
Subproject commit b033851fb54241bb703f86add025229e68cc6f59

View File

@ -32,6 +32,14 @@ export { searchChannelsRouter }
async function searchChannels (req: express.Request, res: express.Response) {
const query = Object.assign(req.query || {}, req.body || {}) as ChannelsSearchQuery
if (query.handles && query.fromHost) {
query.handles = query.handles.map(h => {
if (h.includes('@')) return h
return h + '@' + query.fromHost
})
}
const searcher = new Searcher(queryChannels, formatChannelForAPI)
const result = await searcher.getResult(query)

View File

@ -32,7 +32,6 @@ export { searchVideosRouter }
async function searchVideos (req: express.Request, res: express.Response) {
const query = Object.assign(req.query || {}, req.body || {}) as VideosSearchQuery
const searcher = new Searcher(queryVideos, formatVideoForAPI)
const result = await searcher.getResult(query)

View File

@ -51,6 +51,14 @@ async function queryChannels (search: ChannelsSearchQuery) {
})
}
if (search.handles) {
filter.push({
terms: {
handle: search.handles
}
})
}
if (filter.length !== 0) {
Object.assign(bool, { filter })
}

View File

@ -5,6 +5,7 @@ import { CONFIG, ELASTIC_SEARCH_QUERY } from '../../initializers/constants'
import { DBPlaylist, EnhancedPlaylist, IndexablePlaylist } from '../../types/playlist.model'
import { PlaylistsSearchQuery } from '../../types/search-query/playlist-search.model'
import { buildSort, extractQueryResult } from './elastic-search-queries'
import { addUUIDFilters } from './shared'
import { buildChannelOrAccountSummaryMapping, formatActorForDB, formatActorSummaryForAPI } from './shared/elastic-search-actor'
async function queryPlaylists (search: PlaylistsSearchQuery) {
@ -50,6 +51,10 @@ async function queryPlaylists (search: PlaylistsSearchQuery) {
})
}
if (search.uuids) {
addUUIDFilters(filter, search.uuids)
}
if (filter.length !== 0) {
Object.assign(bool, { filter })
}

View File

@ -7,6 +7,7 @@ import { CONFIG, ELASTIC_SEARCH_QUERY } from '../../initializers/constants'
import { VideosSearchQuery } from '../../types/search-query/video-search.model'
import { DBVideo, DBVideoDetails, EnhancedVideo, IndexableVideo, IndexableVideoDetails } from '../../types/video.model'
import { buildSort, extractQueryResult } from './elastic-search-queries'
import { addUUIDFilters } from './shared'
import { buildChannelOrAccountSummaryMapping, formatActorForDB, formatActorSummaryForAPI } from './shared/elastic-search-actor'
async function queryVideos (search: VideosSearchQuery) {
@ -162,7 +163,7 @@ async function queryVideos (search: VideosSearchQuery) {
})
}
if (exists(search.host)) {
if (search.host) {
filter.push({
term: {
'account.host': search.host
@ -170,6 +171,10 @@ async function queryVideos (search: VideosSearchQuery) {
})
}
if (search.uuids) {
addUUIDFilters(filter, search.uuids)
}
Object.assign(bool, { filter })
if (mustNot.length !== 0) {

View File

@ -1,2 +1,3 @@
export * from './elastic-search-actor'
export * from './elastic-search-avatar'
export * from './query-helpers'

View File

@ -0,0 +1,36 @@
import validator from 'validator'
function addUUIDFilters (filters: any[], uuids: string[]) {
if (!filters) return
const result = {
shortUUIDs: [] as string[],
uuids: [] as string[]
}
for (const uuid of uuids) {
if (validator.isUUID(uuid)) result.uuids.push(uuid)
else result.shortUUIDs.push(uuid)
}
filters.push({
bool: {
should: [
{
terms: {
uuid: result.uuids
}
},
{
terms: {
shortUUID: result.shortUUIDs
}
}
]
}
})
}
export {
addUUIDFilters
}

View File

@ -1,5 +1,5 @@
import { check } from 'express-validator'
import * as express from 'express'
import { check } from 'express-validator'
import { isDateValid, toArray } from '../../helpers/custom-validators/misc'
import { isNSFWQueryValid, isNumberArray, isStringArray } from '../../helpers/custom-validators/search-videos'
import { logger } from '../../helpers/logger'
@ -78,6 +78,10 @@ const videosSearchValidator = [
.customSanitizer(toArray)
.custom(isStringArray).withMessage('Should have a valid boostLanguages array'),
check('uuids')
.optional()
.toArray(),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug({ query: req.query, body: req.body }, 'Checking videos search query')
@ -88,8 +92,9 @@ const videosSearchValidator = [
]
const videoChannelsSearchValidator = [
check('search').not().isEmpty().withMessage('Should have a valid search'),
check('search').optional().not().isEmpty().withMessage('Should have a valid search'),
check('host').optional().not().isEmpty().withMessage('Should have a valid host'),
check('handles').optional().toArray(),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug({ query: req.query, body: req.body }, 'Checking video channels search query')
@ -101,9 +106,14 @@ const videoChannelsSearchValidator = [
]
const videoPlaylistsSearchValidator = [
check('search').not().isEmpty().withMessage('Should have a valid search'),
check('search').optional().not().isEmpty().withMessage('Should have a valid search'),
check('host').optional().not().isEmpty().withMessage('Should have a valid host'),
check('uuids')
.optional()
.toArray(),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug({ query: req.query, body: req.body }, 'Checking video playlists search query')