mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 02:39:33 +02:00
server/server -> server/core
This commit is contained in:
parent
114327d4ce
commit
5a3d0650c9
838 changed files with 111 additions and 111 deletions
19
server/core/controllers/api/search/index.ts
Normal file
19
server/core/controllers/api/search/index.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
import express from 'express'
|
||||
import { apiRateLimiter } from '@server/middlewares/index.js'
|
||||
import { searchChannelsRouter } from './search-video-channels.js'
|
||||
import { searchPlaylistsRouter } from './search-video-playlists.js'
|
||||
import { searchVideosRouter } from './search-videos.js'
|
||||
|
||||
const searchRouter = express.Router()
|
||||
|
||||
searchRouter.use(apiRateLimiter)
|
||||
|
||||
searchRouter.use('/', searchVideosRouter)
|
||||
searchRouter.use('/', searchChannelsRouter)
|
||||
searchRouter.use('/', searchPlaylistsRouter)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
searchRouter
|
||||
}
|
151
server/core/controllers/api/search/search-video-channels.ts
Normal file
151
server/core/controllers/api/search/search-video-channels.ts
Normal file
|
@ -0,0 +1,151 @@
|
|||
import express from 'express'
|
||||
import { sanitizeUrl } from '@server/helpers/core-utils.js'
|
||||
import { pickSearchChannelQuery } 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 { 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, VideoChannel, VideoChannelsSearchQueryAfterSanitize } from '@peertube/peertube-models'
|
||||
import { isUserAbleToSearchRemoteURI } from '../../../helpers/express-utils.js'
|
||||
import { logger } from '../../../helpers/logger.js'
|
||||
import { getFormattedObjects } from '../../../helpers/utils.js'
|
||||
import { getOrCreateAPActor, loadActorUrlOrGetFromWebfinger } from '../../../lib/activitypub/actors/index.js'
|
||||
import {
|
||||
asyncMiddleware,
|
||||
openapiOperationDoc,
|
||||
optionalAuthenticate,
|
||||
paginationValidator,
|
||||
setDefaultPagination,
|
||||
setDefaultSearchSort,
|
||||
videoChannelsListSearchValidator,
|
||||
videoChannelsSearchSortValidator
|
||||
} from '../../../middlewares/index.js'
|
||||
import { VideoChannelModel } from '../../../models/video/video-channel.js'
|
||||
import { MChannelAccountDefault } from '../../../types/models/index.js'
|
||||
import { searchLocalUrl } from './shared/index.js'
|
||||
|
||||
const searchChannelsRouter = express.Router()
|
||||
|
||||
searchChannelsRouter.get('/video-channels',
|
||||
openapiOperationDoc({ operationId: 'searchChannels' }),
|
||||
paginationValidator,
|
||||
setDefaultPagination,
|
||||
videoChannelsSearchSortValidator,
|
||||
setDefaultSearchSort,
|
||||
optionalAuthenticate,
|
||||
videoChannelsListSearchValidator,
|
||||
asyncMiddleware(searchVideoChannels)
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export { searchChannelsRouter }
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function searchVideoChannels (req: express.Request, res: express.Response) {
|
||||
const query = pickSearchChannelQuery(req.query)
|
||||
const search = query.search || ''
|
||||
|
||||
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(search) || isWebfingerSearch) return searchVideoChannelURI(search, res)
|
||||
|
||||
// @username -> username to search in DB
|
||||
if (search.startsWith('@')) query.search = search.replace(/^@/, '')
|
||||
|
||||
if (isSearchIndexSearch(query)) {
|
||||
return searchVideoChannelsIndex(query, res)
|
||||
}
|
||||
|
||||
return searchVideoChannelsDB(query, res)
|
||||
}
|
||||
|
||||
async function searchVideoChannelsIndex (query: VideoChannelsSearchQueryAfterSanitize, res: express.Response) {
|
||||
const result = await buildMutedForSearchIndex(res)
|
||||
|
||||
const body = await Hooks.wrapObject(Object.assign(query, result), 'filter:api.search.video-channels.index.list.params')
|
||||
|
||||
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 { body: searchIndexResult } = await doJSONRequest<ResultList<VideoChannel>>(url, { method: 'POST', json: body })
|
||||
const jsonResult = await Hooks.wrapObject(searchIndexResult, 'filter:api.search.video-channels.index.list.result')
|
||||
|
||||
return res.json(jsonResult)
|
||||
} catch (err) {
|
||||
logger.warn('Cannot use search index to make video channels search.', { err })
|
||||
|
||||
return res.fail({
|
||||
status: HttpStatusCode.INTERNAL_SERVER_ERROR_500,
|
||||
message: 'Cannot use search index to make video channels search'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async function searchVideoChannelsDB (query: VideoChannelsSearchQueryAfterSanitize, res: express.Response) {
|
||||
const serverActor = await getServerActor()
|
||||
|
||||
const apiOptions = await Hooks.wrapObject({
|
||||
...query,
|
||||
|
||||
actorId: serverActor.id
|
||||
}, 'filter:api.search.video-channels.local.list.params')
|
||||
|
||||
const resultList = await Hooks.wrapPromiseFun(
|
||||
VideoChannelModel.searchForApi,
|
||||
apiOptions,
|
||||
'filter:api.search.video-channels.local.list.result'
|
||||
)
|
||||
|
||||
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
||||
}
|
||||
|
||||
async function searchVideoChannelURI (search: string, res: express.Response) {
|
||||
let videoChannel: MChannelAccountDefault
|
||||
let uri = search
|
||||
|
||||
if (!isURISearch(search)) {
|
||||
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 latestUri = await findLatestAPRedirection(uri)
|
||||
|
||||
const actor = await getOrCreateAPActor(latestUri, 'all', true, true)
|
||||
videoChannel = actor.VideoChannel
|
||||
} catch (err) {
|
||||
logger.info('Cannot search remote video channel %s.', uri, { err })
|
||||
}
|
||||
} else {
|
||||
videoChannel = await searchLocalUrl(sanitizeLocalUrl(uri), url => VideoChannelModel.loadByUrlAndPopulateAccount(url))
|
||||
}
|
||||
|
||||
return res.json({
|
||||
total: videoChannel ? 1 : 0,
|
||||
data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
|
||||
})
|
||||
}
|
||||
|
||||
function sanitizeLocalUrl (url: string) {
|
||||
if (!url) return ''
|
||||
|
||||
// Handle alternative channel URLs
|
||||
return url.replace(new RegExp('^' + WEBSERVER.URL + '/c/'), WEBSERVER.URL + '/video-channels/')
|
||||
}
|
131
server/core/controllers/api/search/search-video-playlists.ts
Normal file
131
server/core/controllers/api/search/search-video-playlists.ts
Normal file
|
@ -0,0 +1,131 @@
|
|||
import express from 'express'
|
||||
import { sanitizeUrl } from '@server/helpers/core-utils.js'
|
||||
import { isUserAbleToSearchRemoteURI } from '@server/helpers/express-utils.js'
|
||||
import { logger } from '@server/helpers/logger.js'
|
||||
import { pickSearchPlaylistQuery } from '@server/helpers/query.js'
|
||||
import { doJSONRequest } from '@server/helpers/requests.js'
|
||||
import { getFormattedObjects } from '@server/helpers/utils.js'
|
||||
import { CONFIG } from '@server/initializers/config.js'
|
||||
import { WEBSERVER } from '@server/initializers/constants.js'
|
||||
import { findLatestAPRedirection } from '@server/lib/activitypub/activity.js'
|
||||
import { getOrCreateAPVideoPlaylist } from '@server/lib/activitypub/playlists/get.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 { VideoPlaylistModel } from '@server/models/video/video-playlist.js'
|
||||
import { MVideoPlaylistFullSummary } from '@server/types/models/index.js'
|
||||
import { HttpStatusCode, ResultList, VideoPlaylist, VideoPlaylistsSearchQueryAfterSanitize } from '@peertube/peertube-models'
|
||||
import {
|
||||
asyncMiddleware,
|
||||
openapiOperationDoc,
|
||||
optionalAuthenticate,
|
||||
paginationValidator,
|
||||
setDefaultPagination,
|
||||
setDefaultSearchSort,
|
||||
videoPlaylistsListSearchValidator,
|
||||
videoPlaylistsSearchSortValidator
|
||||
} from '../../../middlewares/index.js'
|
||||
import { searchLocalUrl } from './shared/index.js'
|
||||
|
||||
const searchPlaylistsRouter = express.Router()
|
||||
|
||||
searchPlaylistsRouter.get('/video-playlists',
|
||||
openapiOperationDoc({ operationId: 'searchPlaylists' }),
|
||||
paginationValidator,
|
||||
setDefaultPagination,
|
||||
videoPlaylistsSearchSortValidator,
|
||||
setDefaultSearchSort,
|
||||
optionalAuthenticate,
|
||||
videoPlaylistsListSearchValidator,
|
||||
asyncMiddleware(searchVideoPlaylists)
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export { searchPlaylistsRouter }
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function searchVideoPlaylists (req: express.Request, res: express.Response) {
|
||||
const query = pickSearchPlaylistQuery(req.query)
|
||||
const search = query.search
|
||||
|
||||
if (isURISearch(search)) return searchVideoPlaylistsURI(search, res)
|
||||
|
||||
if (isSearchIndexSearch(query)) {
|
||||
return searchVideoPlaylistsIndex(query, res)
|
||||
}
|
||||
|
||||
return searchVideoPlaylistsDB(query, res)
|
||||
}
|
||||
|
||||
async function searchVideoPlaylistsIndex (query: VideoPlaylistsSearchQueryAfterSanitize, res: express.Response) {
|
||||
const result = await buildMutedForSearchIndex(res)
|
||||
|
||||
const body = await Hooks.wrapObject(Object.assign(query, result), 'filter:api.search.video-playlists.index.list.params')
|
||||
|
||||
const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/video-playlists'
|
||||
|
||||
try {
|
||||
logger.debug('Doing video playlists search index request on %s.', url, { body })
|
||||
|
||||
const { body: searchIndexResult } = await doJSONRequest<ResultList<VideoPlaylist>>(url, { method: 'POST', json: body })
|
||||
const jsonResult = await Hooks.wrapObject(searchIndexResult, 'filter:api.search.video-playlists.index.list.result')
|
||||
|
||||
return res.json(jsonResult)
|
||||
} catch (err) {
|
||||
logger.warn('Cannot use search index to make video playlists search.', { err })
|
||||
|
||||
return res.fail({
|
||||
status: HttpStatusCode.INTERNAL_SERVER_ERROR_500,
|
||||
message: 'Cannot use search index to make video playlists search'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async function searchVideoPlaylistsDB (query: VideoPlaylistsSearchQueryAfterSanitize, res: express.Response) {
|
||||
const serverActor = await getServerActor()
|
||||
|
||||
const apiOptions = await Hooks.wrapObject({
|
||||
...query,
|
||||
|
||||
followerActorId: serverActor.id
|
||||
}, 'filter:api.search.video-playlists.local.list.params')
|
||||
|
||||
const resultList = await Hooks.wrapPromiseFun(
|
||||
VideoPlaylistModel.searchForApi,
|
||||
apiOptions,
|
||||
'filter:api.search.video-playlists.local.list.result'
|
||||
)
|
||||
|
||||
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
||||
}
|
||||
|
||||
async function searchVideoPlaylistsURI (search: string, res: express.Response) {
|
||||
let videoPlaylist: MVideoPlaylistFullSummary
|
||||
|
||||
if (isUserAbleToSearchRemoteURI(res)) {
|
||||
try {
|
||||
const url = await findLatestAPRedirection(search)
|
||||
|
||||
videoPlaylist = await getOrCreateAPVideoPlaylist(url)
|
||||
} catch (err) {
|
||||
logger.info('Cannot search remote video playlist %s.', search, { err })
|
||||
}
|
||||
} else {
|
||||
videoPlaylist = await searchLocalUrl(sanitizeLocalUrl(search), url => VideoPlaylistModel.loadByUrlWithAccountAndChannelSummary(url))
|
||||
}
|
||||
|
||||
return res.json({
|
||||
total: videoPlaylist ? 1 : 0,
|
||||
data: videoPlaylist ? [ videoPlaylist.toFormattedJSON() ] : []
|
||||
})
|
||||
}
|
||||
|
||||
function sanitizeLocalUrl (url: string) {
|
||||
if (!url) return ''
|
||||
|
||||
// Handle alternative channel URLs
|
||||
return url.replace(new RegExp('^' + WEBSERVER.URL + '/videos/watch/playlist/'), WEBSERVER.URL + '/video-playlists/')
|
||||
.replace(new RegExp('^' + WEBSERVER.URL + '/w/p/'), WEBSERVER.URL + '/video-playlists/')
|
||||
}
|
166
server/core/controllers/api/search/search-videos.ts
Normal file
166
server/core/controllers/api/search/search-videos.ts
Normal file
|
@ -0,0 +1,166 @@
|
|||
import express from 'express'
|
||||
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'
|
||||
import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../../helpers/express-utils.js'
|
||||
import { logger } from '../../../helpers/logger.js'
|
||||
import { getFormattedObjects } from '../../../helpers/utils.js'
|
||||
import {
|
||||
asyncMiddleware,
|
||||
commonVideosFiltersValidator,
|
||||
openapiOperationDoc,
|
||||
optionalAuthenticate,
|
||||
paginationValidator,
|
||||
setDefaultPagination,
|
||||
setDefaultSearchSort,
|
||||
videosSearchSortValidator,
|
||||
videosSearchValidator
|
||||
} 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'
|
||||
|
||||
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) {
|
||||
const query = pickSearchVideoQuery(req.query)
|
||||
const search = query.search
|
||||
|
||||
if (isURISearch(search)) {
|
||||
return searchVideoURI(search, res)
|
||||
}
|
||||
|
||||
if (isSearchIndexSearch(query)) {
|
||||
return searchVideosIndex(query, res)
|
||||
}
|
||||
|
||||
return searchVideosDB(query, res)
|
||||
}
|
||||
|
||||
async function searchVideosIndex (query: VideosSearchQueryAfterSanitize, res: express.Response) {
|
||||
const result = await buildMutedForSearchIndex(res)
|
||||
|
||||
let body = { ...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'
|
||||
}
|
||||
|
||||
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'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async function searchVideosDB (query: VideosSearchQueryAfterSanitize, res: express.Response) {
|
||||
const serverActor = await getServerActor()
|
||||
|
||||
const apiOptions = await Hooks.wrapObject({
|
||||
...query,
|
||||
|
||||
displayOnlyForFollower: {
|
||||
actorId: serverActor.id,
|
||||
orLocalVideos: true
|
||||
},
|
||||
|
||||
nsfw: buildNSFWFilter(res, query.nsfw),
|
||||
user: res.locals.oauth
|
||||
? res.locals.oauth.token.User
|
||||
: undefined
|
||||
}, 'filter:api.search.videos.local.list.params')
|
||||
|
||||
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)))
|
||||
}
|
||||
|
||||
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 = {
|
||||
rates: false,
|
||||
shares: false,
|
||||
comments: false,
|
||||
refreshVideo: false
|
||||
}
|
||||
|
||||
const result = await getOrCreateAPVideo({
|
||||
videoObject: await findLatestAPRedirection(url),
|
||||
syncParam
|
||||
})
|
||||
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))
|
||||
}
|
||||
|
||||
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/')
|
||||
}
|
1
server/core/controllers/api/search/shared/index.ts
Normal file
1
server/core/controllers/api/search/shared/index.ts
Normal file
|
@ -0,0 +1 @@
|
|||
export * from './utils.js'
|
16
server/core/controllers/api/search/shared/utils.ts
Normal file
16
server/core/controllers/api/search/shared/utils.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
async function searchLocalUrl <T> (url: string, finder: (url: string) => Promise<T>) {
|
||||
const data = await finder(url)
|
||||
if (data) return data
|
||||
|
||||
return finder(removeQueryParams(url))
|
||||
}
|
||||
|
||||
export {
|
||||
searchLocalUrl
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function removeQueryParams (url: string) {
|
||||
return url.split('?').shift()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue