2021-08-27 07:32:44 -05:00
|
|
|
import express from 'express'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { sanitizeUrl } from '@server/helpers/core-utils.js'
|
|
|
|
import { pickSearchVideoQuery } from '@server/helpers/query.js'
|
|
|
|
import { doJSONRequest } from '@server/helpers/requests.js'
|
|
|
|
import { CONFIG } from '@server/initializers/config.js'
|
|
|
|
import { WEBSERVER } from '@server/initializers/constants.js'
|
|
|
|
import { findLatestAPRedirection } from '@server/lib/activitypub/activity.js'
|
|
|
|
import { getOrCreateAPVideo } from '@server/lib/activitypub/videos/index.js'
|
|
|
|
import { Hooks } from '@server/lib/plugins/hooks.js'
|
|
|
|
import { buildMutedForSearchIndex, isSearchIndexSearch, isURISearch } from '@server/lib/search.js'
|
|
|
|
import { getServerActor } from '@server/models/application/application.js'
|
|
|
|
import { HttpStatusCode, ResultList, Video, VideosSearchQueryAfterSanitize } from '@peertube/peertube-models'
|
2023-10-25 07:53:53 -05:00
|
|
|
import { buildNSFWFilter, getCountVideos, isUserAbleToSearchRemoteURI } from '../../../helpers/express-utils.js'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { logger } from '../../../helpers/logger.js'
|
|
|
|
import { getFormattedObjects } from '../../../helpers/utils.js'
|
2021-06-17 09:02:38 -05:00
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
|
|
|
commonVideosFiltersValidator,
|
|
|
|
openapiOperationDoc,
|
|
|
|
optionalAuthenticate,
|
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
setDefaultSearchSort,
|
|
|
|
videosSearchSortValidator,
|
|
|
|
videosSearchValidator
|
2023-07-31 07:34:36 -05:00
|
|
|
} from '../../../middlewares/index.js'
|
|
|
|
import { guessAdditionalAttributesFromQuery } from '../../../models/video/formatter/index.js'
|
|
|
|
import { VideoModel } from '../../../models/video/video.js'
|
|
|
|
import { MVideoAccountLightBlacklistAllFiles } from '../../../types/models/index.js'
|
|
|
|
import { searchLocalUrl } from './shared/index.js'
|
2021-06-17 09:02:38 -05: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 04:54:38 -05:00
|
|
|
const query = pickSearchVideoQuery(req.query)
|
2021-06-17 09:02:38 -05:00
|
|
|
const search = query.search
|
|
|
|
|
|
|
|
if (isURISearch(search)) {
|
|
|
|
return searchVideoURI(search, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSearchIndexSearch(query)) {
|
|
|
|
return searchVideosIndex(query, res)
|
|
|
|
}
|
|
|
|
|
2023-10-25 07:53:53 -05:00
|
|
|
return searchVideosDB(query, req, res)
|
2021-06-17 09:02:38 -05:00
|
|
|
}
|
|
|
|
|
2021-07-29 04:54:38 -05:00
|
|
|
async function searchVideosIndex (query: VideosSearchQueryAfterSanitize, res: express.Response) {
|
2021-06-17 09:02:38 -05:00
|
|
|
const result = await buildMutedForSearchIndex(res)
|
|
|
|
|
2021-07-29 04:54:38 -05:00
|
|
|
let body = { ...query, ...result }
|
2021-06-17 09:02:38 -05: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'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 07:53:53 -05:00
|
|
|
async function searchVideosDB (query: VideosSearchQueryAfterSanitize, req: express.Request, res: express.Response) {
|
2021-10-27 07:37:04 -05:00
|
|
|
const serverActor = await getServerActor()
|
|
|
|
|
2021-07-29 04:54:38 -05:00
|
|
|
const apiOptions = await Hooks.wrapObject({
|
|
|
|
...query,
|
|
|
|
|
2021-10-27 07:37:04 -05:00
|
|
|
displayOnlyForFollower: {
|
|
|
|
actorId: serverActor.id,
|
|
|
|
orLocalVideos: true
|
|
|
|
},
|
2021-07-29 04:54:38 -05:00
|
|
|
|
2023-10-25 07:53:53 -05:00
|
|
|
countVideos: getCountVideos(req),
|
|
|
|
|
2021-07-29 04:54:38 -05: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 09:02:38 -05:00
|
|
|
|
|
|
|
const resultList = await Hooks.wrapPromiseFun(
|
|
|
|
VideoModel.searchAndPopulateAccountAndServer,
|
|
|
|
apiOptions,
|
|
|
|
'filter:api.search.videos.local.list.result'
|
|
|
|
)
|
|
|
|
|
2021-10-27 07:37:04 -05:00
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query)))
|
2021-06-17 09:02:38 -05: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 = {
|
2022-03-18 05:17:35 -05:00
|
|
|
rates: false,
|
2021-06-17 09:02:38 -05:00
|
|
|
shares: false,
|
|
|
|
comments: false,
|
|
|
|
refreshVideo: false
|
|
|
|
}
|
|
|
|
|
2021-11-05 08:11:19 -05:00
|
|
|
const result = await getOrCreateAPVideo({
|
2023-07-27 10:01:15 -05:00
|
|
|
videoObject: await findLatestAPRedirection(url),
|
2021-11-05 08:11:19 -05:00
|
|
|
syncParam
|
|
|
|
})
|
2021-06-17 09:02:38 -05:00
|
|
|
video = result ? result.video : undefined
|
|
|
|
} catch (err) {
|
|
|
|
logger.info('Cannot search remote video %s.', url, { err })
|
|
|
|
}
|
|
|
|
} else {
|
2024-02-12 03:47:52 -06:00
|
|
|
video = await searchLocalUrl(sanitizeLocalUrl(url), url => VideoModel.loadByUrlAndPopulateAccountAndFiles(url))
|
2021-06-17 09:02:38 -05: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/')
|
|
|
|
}
|