1
0
Fork 0
peertube/server/controllers/api/search/search-videos.ts

169 lines
5.4 KiB
TypeScript
Raw Normal View History

2021-08-27 08:32:44 -04:00
import express from 'express'
2021-06-17 10:02:38 -04:00
import { sanitizeUrl } from '@server/helpers/core-utils'
2021-07-29 05:54:38 -04:00
import { pickSearchVideoQuery } from '@server/helpers/query'
import { doJSONRequest, findLatestRedirection } from '@server/helpers/requests'
2021-06-17 10:02:38 -04:00
import { CONFIG } from '@server/initializers/config'
import { WEBSERVER } from '@server/initializers/constants'
import { getOrCreateAPVideo } from '@server/lib/activitypub/videos'
import { Hooks } from '@server/lib/plugins/hooks'
import { buildMutedForSearchIndex, isSearchIndexSearch, isURISearch } from '@server/lib/search'
import { getServerActor } from '@server/models/application/application'
import { guessAdditionalAttributesFromQuery } from '@server/models/video/formatter/video-format-utils'
2021-07-16 08:27:30 -04:00
import { HttpStatusCode, ResultList, Video } from '@shared/models'
2021-07-29 05:54:38 -04:00
import { VideosSearchQueryAfterSanitize } from '../../../../shared/models/search'
2021-06-17 10:02:38 -04:00
import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../../helpers/express-utils'
import { logger } from '../../../helpers/logger'
import { getFormattedObjects } from '../../../helpers/utils'
import {
asyncMiddleware,
commonVideosFiltersValidator,
openapiOperationDoc,
optionalAuthenticate,
paginationValidator,
setDefaultPagination,
setDefaultSearchSort,
videosSearchSortValidator,
videosSearchValidator
} from '../../../middlewares'
import { VideoModel } from '../../../models/video/video'
import { MVideoAccountLightBlacklistAllFiles } from '../../../types/models'
import { searchLocalUrl } from './shared'
2021-06-17 10:02:38 -04:00
const searchVideosRouter = express.Router()
searchVideosRouter.get('/videos',
openapiOperationDoc({ operationId: 'searchVideos' }),
paginationValidator,
setDefaultPagination,
videosSearchSortValidator,
setDefaultSearchSort,
optionalAuthenticate,
commonVideosFiltersValidator,
videosSearchValidator,
asyncMiddleware(searchVideos)
)
// ---------------------------------------------------------------------------
export { searchVideosRouter }
// ---------------------------------------------------------------------------
function searchVideos (req: express.Request, res: express.Response) {
2021-07-29 05:54:38 -04:00
const query = pickSearchVideoQuery(req.query)
2021-06-17 10:02:38 -04:00
const search = query.search
if (isURISearch(search)) {
return searchVideoURI(search, res)
}
if (isSearchIndexSearch(query)) {
return searchVideosIndex(query, res)
}
return searchVideosDB(query, res)
}
2021-07-29 05:54:38 -04:00
async function searchVideosIndex (query: VideosSearchQueryAfterSanitize, res: express.Response) {
2021-06-17 10:02:38 -04:00
const result = await buildMutedForSearchIndex(res)
2021-07-29 05:54:38 -04:00
let body = { ...query, ...result }
2021-06-17 10:02:38 -04:00
// Use the default instance NSFW policy if not specified
if (!body.nsfw) {
const nsfwPolicy = res.locals.oauth
? res.locals.oauth.token.User.nsfwPolicy
: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
body.nsfw = nsfwPolicy === 'do_not_list'
? 'false'
: 'both'
}
body = await Hooks.wrapObject(body, 'filter:api.search.videos.index.list.params')
const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/videos'
try {
logger.debug('Doing videos search index request on %s.', url, { body })
const { body: searchIndexResult } = await doJSONRequest<ResultList<Video>>(url, { method: 'POST', json: body })
const jsonResult = await Hooks.wrapObject(searchIndexResult, 'filter:api.search.videos.index.list.result')
return res.json(jsonResult)
} catch (err) {
logger.warn('Cannot use search index to make video search.', { err })
return res.fail({
status: HttpStatusCode.INTERNAL_SERVER_ERROR_500,
message: 'Cannot use search index to make video search'
})
}
}
2021-07-29 05:54:38 -04:00
async function searchVideosDB (query: VideosSearchQueryAfterSanitize, res: express.Response) {
const serverActor = await getServerActor()
2021-07-29 05:54:38 -04:00
const apiOptions = await Hooks.wrapObject({
...query,
displayOnlyForFollower: {
actorId: serverActor.id,
orLocalVideos: true
},
2021-07-29 05:54:38 -04:00
nsfw: buildNSFWFilter(res, query.nsfw),
user: res.locals.oauth
? res.locals.oauth.token.User
: undefined
}, 'filter:api.search.videos.local.list.params')
2021-06-17 10:02:38 -04:00
const resultList = await Hooks.wrapPromiseFun(
VideoModel.searchAndPopulateAccountAndServer,
apiOptions,
'filter:api.search.videos.local.list.result'
)
return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query)))
2021-06-17 10:02:38 -04:00
}
async function searchVideoURI (url: string, res: express.Response) {
let video: MVideoAccountLightBlacklistAllFiles
// Check if we can fetch a remote video with the URL
if (isUserAbleToSearchRemoteURI(res)) {
try {
const syncParam = {
likes: false,
dislikes: false,
shares: false,
comments: false,
thumbnail: true,
refreshVideo: false
}
const result = await getOrCreateAPVideo({
videoObject: await findLatestRedirection(url, { activityPub: true }),
syncParam
})
2021-06-17 10:02:38 -04:00
video = result ? result.video : undefined
} catch (err) {
logger.info('Cannot search remote video %s.', url, { err })
}
} else {
video = await searchLocalUrl(sanitizeLocalUrl(url), url => VideoModel.loadByUrlAndPopulateAccount(url))
2021-06-17 10:02:38 -04:00
}
return res.json({
total: video ? 1 : 0,
data: video ? [ video.toFormattedJSON() ] : []
})
}
function sanitizeLocalUrl (url: string) {
if (!url) return ''
// Handle alternative video URLs
return url.replace(new RegExp('^' + WEBSERVER.URL + '/w/'), WEBSERVER.URL + '/videos/watch/')
}