X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fsearch.ts;h=bb7174891e2b02c39246c5f2f01a9a73ef9f9777;hb=d9eaee3939bf2e93e5d775d32bce77842201faba;hp=87aa5d76ff387836a08109c47a951a313846f06d;hpb=f37dc0dd14d9ce0b59c454c2c1b935fcbe9727e9;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/search.ts b/server/controllers/api/search.ts index 87aa5d76f..bb7174891 100644 --- a/server/controllers/api/search.ts +++ b/server/controllers/api/search.ts @@ -1,5 +1,5 @@ import * as express from 'express' -import { buildNSFWFilter } from '../../helpers/express-utils' +import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils' import { getFormattedObjects, getServerActor } from '../../helpers/utils' import { VideoModel } from '../../models/video/video' import { @@ -41,7 +41,6 @@ searchRouter.get('/video-channels', videoChannelsSearchSortValidator, setDefaultSearchSort, optionalAuthenticate, - commonVideosFiltersValidator, videoChannelsSearchValidator, asyncMiddleware(searchVideoChannels) ) @@ -59,9 +58,9 @@ function searchVideoChannels (req: express.Request, res: express.Response) { const isURISearch = search.startsWith('http://') || search.startsWith('https://') const parts = search.split('@') - const isHandleSearch = parts.length === 2 && parts.every(p => p.indexOf(' ') === -1) + const isWebfingerSearch = parts.length === 2 && parts.every(p => p.indexOf(' ') === -1) - if (isURISearch || isHandleSearch) return searchVideoChannelURI(search, isHandleSearch, res) + if (isURISearch || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res) return searchVideoChannelsDB(query, res) } @@ -81,17 +80,21 @@ async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: expr return res.json(getFormattedObjects(resultList.data, resultList.total)) } -async function searchVideoChannelURI (search: string, isHandleSearch: boolean, res: express.Response) { +async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) { let videoChannel: VideoChannelModel + let uri = search - if (isUserAbleToSearchRemoteURI(res)) { - let uri = search - if (isHandleSearch) uri = await loadActorUrlOrGetFromWebfinger(search) + if (isWebfingerSearch) uri = await loadActorUrlOrGetFromWebfinger(search) - const actor = await getOrCreateActorAndServerAndModel(uri) - videoChannel = actor.VideoChannel + if (isUserAbleToSearchRemoteURI(res)) { + try { + const actor = await getOrCreateActorAndServerAndModel(uri, true, true) + videoChannel = actor.VideoChannel + } catch (err) { + logger.info('Cannot search remote video channel %s.', uri, { err }) + } } else { - videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(search) + videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri) } return res.json({ @@ -138,7 +141,7 @@ async function searchVideoURI (url: string, res: express.Response) { const result = await getOrCreateVideoAndAccountAndChannel(url, syncParam) video = result ? result.video : undefined } catch (err) { - logger.info('Cannot search remote video %s.', url) + logger.info('Cannot search remote video %s.', url, { err }) } } else { video = await VideoModel.loadByUrlAndPopulateAccount(url) @@ -149,10 +152,3 @@ async function searchVideoURI (url: string, res: express.Response) { data: video ? [ video.toFormattedJSON() ] : [] }) } - -function isUserAbleToSearchRemoteURI (res: express.Response) { - const user: User = res.locals.oauth ? res.locals.oauth.token.User : undefined - - return CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true || - (CONFIG.SEARCH.REMOTE_URI.USERS === true && user !== undefined) -}