X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fsearch.ts;h=7e1b7b230b9ec9381a9917e967de982350a53301;hb=afd4ee86ddb97f8d49585fddd318bedcd3ad55b8;hp=d95e7cac9dd8ef6a0d44a25b6dc99eb6074548f2;hpb=1297eb5db651a230474670c5da1517862fb9cc3e;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/search.ts b/server/controllers/api/search.ts index d95e7cac9..7e1b7b230 100644 --- a/server/controllers/api/search.ts +++ b/server/controllers/api/search.ts @@ -1,22 +1,35 @@ import * as express from 'express' -import { buildNSFWFilter } from '../../helpers/express-utils' +import { sanitizeUrl } from '@server/helpers/core-utils' +import { doRequest } from '@server/helpers/requests' +import { CONFIG } from '@server/initializers/config' +import { getOrCreateVideoAndAccountAndChannel } from '@server/lib/activitypub/videos' +import { AccountBlocklistModel } from '@server/models/account/account-blocklist' +import { getServerActor } from '@server/models/application/application' +import { ServerBlocklistModel } from '@server/models/server/server-blocklist' +import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' +import { ResultList, Video, VideoChannel } from '@shared/models' +import { SearchTargetQuery } from '@shared/models/search/search-target-query.model' +import { VideoChannelsSearchQuery, VideosSearchQuery } from '../../../shared/models/search' +import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils' +import { logger } from '../../helpers/logger' import { getFormattedObjects } from '../../helpers/utils' -import { VideoModel } from '../../models/video/video' +import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger' +import { getOrCreateActorAndServerAndModel } from '../../lib/activitypub/actor' import { asyncMiddleware, commonVideosFiltersValidator, optionalAuthenticate, paginationValidator, - searchValidator, setDefaultPagination, setDefaultSearchSort, - videosSearchSortValidator + videoChannelsSearchSortValidator, + videoChannelsListSearchValidator, + videosSearchSortValidator, + videosSearchValidator } from '../../middlewares' -import { VideosSearchQuery } from '../../../shared/models/search' -import { getOrCreateVideoAndAccountAndChannel } from '../../lib/activitypub' -import { logger } from '../../helpers/logger' -import { User } from '../../../shared/models/users' -import { CONFIG } from '../../initializers/constants' +import { VideoModel } from '../../models/video/video' +import { VideoChannelModel } from '../../models/video/video-channel' +import { MChannelAccountDefault, MVideoAccountLightBlacklistAllFiles } from '../../types/models' const searchRouter = express.Router() @@ -27,44 +40,179 @@ searchRouter.get('/videos', setDefaultSearchSort, optionalAuthenticate, commonVideosFiltersValidator, - searchValidator, + videosSearchValidator, asyncMiddleware(searchVideos) ) +searchRouter.get('/video-channels', + paginationValidator, + setDefaultPagination, + videoChannelsSearchSortValidator, + setDefaultSearchSort, + optionalAuthenticate, + videoChannelsListSearchValidator, + asyncMiddleware(searchVideoChannels) +) + // --------------------------------------------------------------------------- export { searchRouter } // --------------------------------------------------------------------------- +function searchVideoChannels (req: express.Request, res: express.Response) { + const query: VideoChannelsSearchQuery = req.query + const search = query.search + + const isURISearch = search.startsWith('http://') || search.startsWith('https://') + + const parts = search.split('@') + + // Handle strings like @toto@example.com + if (parts.length === 3 && parts[0].length === 0) parts.shift() + const isWebfingerSearch = parts.length === 2 && parts.every(p => p && !p.includes(' ')) + + if (isURISearch || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res) + + // @username -> username to search in DB + if (query.search.startsWith('@')) query.search = query.search.replace(/^@/, '') + + if (isSearchIndexSearch(query)) { + return searchVideoChannelsIndex(query, res) + } + + return searchVideoChannelsDB(query, res) +} + +async function searchVideoChannelsIndex (query: VideoChannelsSearchQuery, res: express.Response) { + const result = await buildMutedForSearchIndex(res) + + const body = Object.assign(query, result) + + const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/video-channels' + + try { + logger.debug('Doing video channels search index request on %s.', url, { body }) + + const searchIndexResult = await doRequest>({ uri: url, body, json: true }) + + return res.json(searchIndexResult.body) + } catch (err) { + logger.warn('Cannot use search index to make video channels search.', { err }) + + return res.sendStatus(HttpStatusCode.INTERNAL_SERVER_ERROR_500) + } +} + +async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) { + const serverActor = await getServerActor() + + const options = { + actorId: serverActor.id, + search: query.search, + start: query.start, + count: query.count, + sort: query.sort + } + const resultList = await VideoChannelModel.searchForApi(options) + + return res.json(getFormattedObjects(resultList.data, resultList.total)) +} + +async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) { + let videoChannel: MChannelAccountDefault + let uri = search + + if (isWebfingerSearch) { + try { + uri = await loadActorUrlOrGetFromWebfinger(search) + } catch (err) { + logger.warn('Cannot load actor URL or get from webfinger.', { search, err }) + + return res.json({ total: 0, data: [] }) + } + } + + if (isUserAbleToSearchRemoteURI(res)) { + try { + const actor = await getOrCreateActorAndServerAndModel(uri, 'all', true, true) + videoChannel = actor.VideoChannel + } catch (err) { + logger.info('Cannot search remote video channel %s.', uri, { err }) + } + } else { + videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri) + } + + return res.json({ + total: videoChannel ? 1 : 0, + data: videoChannel ? [ videoChannel.toFormattedJSON() ] : [] + }) +} + function searchVideos (req: express.Request, res: express.Response) { const query: VideosSearchQuery = req.query - if (query.search.startsWith('http://') || query.search.startsWith('https://')) { - return searchVideoUrl(query.search, res) + const search = query.search + + if (search && (search.startsWith('http://') || search.startsWith('https://'))) { + return searchVideoURI(search, res) + } + + if (isSearchIndexSearch(query)) { + return searchVideosIndex(query, res) } return searchVideosDB(query, res) } +async function searchVideosIndex (query: VideosSearchQuery, res: express.Response) { + const result = await buildMutedForSearchIndex(res) + + const body: VideosSearchQuery = Object.assign(query, result) + + // 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' + } + + 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 searchIndexResult = await doRequest>({ uri: url, body, json: true }) + + return res.json(searchIndexResult.body) + } catch (err) { + logger.warn('Cannot use search index to make video search.', { err }) + + return res.sendStatus(HttpStatusCode.INTERNAL_SERVER_ERROR_500) + } +} + async function searchVideosDB (query: VideosSearchQuery, res: express.Response) { const options = Object.assign(query, { includeLocalVideos: true, - nsfw: buildNSFWFilter(res, query.nsfw) + nsfw: buildNSFWFilter(res, query.nsfw), + filter: query.filter, + user: res.locals.oauth ? res.locals.oauth.token.User : undefined }) const resultList = await VideoModel.searchAndPopulateAccountAndServer(options) return res.json(getFormattedObjects(resultList.data, resultList.total)) } -async function searchVideoUrl (url: string, res: express.Response) { - let video: VideoModel - const user: User = res.locals.oauth ? res.locals.oauth.token.User : undefined +async function searchVideoURI (url: string, res: express.Response) { + let video: MVideoAccountLightBlacklistAllFiles // Check if we can fetch a remote video with the URL - if ( - CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true || - (CONFIG.SEARCH.REMOTE_URI.USERS === true && user !== undefined) - ) { + if (isUserAbleToSearchRemoteURI(res)) { try { const syncParam = { likes: false, @@ -75,10 +223,10 @@ async function searchVideoUrl (url: string, res: express.Response) { refreshVideo: false } - const res = await getOrCreateVideoAndAccountAndChannel(url, syncParam) - video = res ? res.video : undefined + const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: 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) @@ -89,3 +237,35 @@ async function searchVideoUrl (url: string, res: express.Response) { data: video ? [ video.toFormattedJSON() ] : [] }) } + +function isSearchIndexSearch (query: SearchTargetQuery) { + if (query.searchTarget === 'search-index') return true + + const searchIndexConfig = CONFIG.SEARCH.SEARCH_INDEX + + if (searchIndexConfig.ENABLED !== true) return false + + if (searchIndexConfig.DISABLE_LOCAL_SEARCH) return true + if (searchIndexConfig.IS_DEFAULT_SEARCH && !query.searchTarget) return true + + return false +} + +async function buildMutedForSearchIndex (res: express.Response) { + const serverActor = await getServerActor() + const accountIds = [ serverActor.Account.id ] + + if (res.locals.oauth) { + accountIds.push(res.locals.oauth.token.User.Account.id) + } + + const [ blockedHosts, blockedAccounts ] = await Promise.all([ + ServerBlocklistModel.listHostsBlockedBy(accountIds), + AccountBlocklistModel.listHandlesBlockedBy(accountIds) + ]) + + return { + blockedHosts, + blockedAccounts + } +}