PeerTube/server/controllers/api/search.ts

159 lines
4.9 KiB
TypeScript
Raw Normal View History

2018-07-19 16:17:54 +02:00
import * as express from 'express'
2018-07-20 14:35:18 +02:00
import { buildNSFWFilter } from '../../helpers/express-utils'
2018-08-23 17:58:39 +02:00
import { getFormattedObjects, getServerActor } from '../../helpers/utils'
2018-07-19 16:17:54 +02:00
import { VideoModel } from '../../models/video/video'
import {
asyncMiddleware,
2018-07-20 14:35:18 +02:00
commonVideosFiltersValidator,
2018-07-19 16:17:54 +02:00
optionalAuthenticate,
paginationValidator,
setDefaultPagination,
setDefaultSearchSort,
2018-08-23 17:58:39 +02:00
videoChannelsSearchSortValidator,
videoChannelsSearchValidator,
videosSearchSortValidator,
videosSearchValidator
2018-07-19 16:17:54 +02:00
} from '../../middlewares'
2018-08-23 17:58:39 +02:00
import { VideoChannelsSearchQuery, VideosSearchQuery } from '../../../shared/models/search'
import { getOrCreateActorAndServerAndModel, getOrCreateVideoAndAccountAndChannel } from '../../lib/activitypub'
import { logger } from '../../helpers/logger'
2018-08-22 16:15:35 +02:00
import { User } from '../../../shared/models/users'
import { CONFIG } from '../../initializers/constants'
2018-08-23 17:58:39 +02:00
import { VideoChannelModel } from '../../models/video/video-channel'
import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
2018-07-19 16:17:54 +02:00
const searchRouter = express.Router()
searchRouter.get('/videos',
paginationValidator,
setDefaultPagination,
videosSearchSortValidator,
setDefaultSearchSort,
optionalAuthenticate,
2018-07-20 14:35:18 +02:00
commonVideosFiltersValidator,
2018-08-23 17:58:39 +02:00
videosSearchValidator,
2018-07-19 16:17:54 +02:00
asyncMiddleware(searchVideos)
)
2018-08-23 17:58:39 +02:00
searchRouter.get('/video-channels',
paginationValidator,
setDefaultPagination,
videoChannelsSearchSortValidator,
setDefaultSearchSort,
optionalAuthenticate,
commonVideosFiltersValidator,
videoChannelsSearchValidator,
asyncMiddleware(searchVideoChannels)
)
2018-07-19 16:17:54 +02:00
// ---------------------------------------------------------------------------
export { searchRouter }
// ---------------------------------------------------------------------------
2018-08-23 17:58:39 +02:00
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('@')
const isHandleSearch = parts.length === 2 && parts.every(p => p.indexOf(' ') === -1)
if (isURISearch || isHandleSearch) return searchVideoChannelURI(search, isHandleSearch, res)
return searchVideoChannelsDB(query, res)
}
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, isHandleSearch: boolean, res: express.Response) {
let videoChannel: VideoChannelModel
if (isUserAbleToSearchRemoteURI(res)) {
let uri = search
if (isHandleSearch) uri = await loadActorUrlOrGetFromWebfinger(search)
const actor = await getOrCreateActorAndServerAndModel(uri)
videoChannel = actor.VideoChannel
} else {
videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(search)
}
return res.json({
total: videoChannel ? 1 : 0,
data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
})
}
function searchVideos (req: express.Request, res: express.Response) {
2018-07-20 14:35:18 +02:00
const query: VideosSearchQuery = req.query
2018-08-23 10:30:53 +02:00
const search = query.search
if (search && (search.startsWith('http://') || search.startsWith('https://'))) {
2018-08-23 17:58:39 +02:00
return searchVideoURI(search, res)
}
2018-07-20 14:35:18 +02:00
return searchVideosDB(query, res)
}
async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
const options = Object.assign(query, {
includeLocalVideos: true,
nsfw: buildNSFWFilter(res, query.nsfw)
})
2018-07-20 14:35:18 +02:00
const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
2018-07-19 16:17:54 +02:00
return res.json(getFormattedObjects(resultList.data, resultList.total))
}
2018-08-23 17:58:39 +02:00
async function searchVideoURI (url: string, res: express.Response) {
let video: VideoModel
2018-08-22 16:15:35 +02:00
// Check if we can fetch a remote video with the URL
2018-08-23 17:58:39 +02:00
if (isUserAbleToSearchRemoteURI(res)) {
2018-08-22 16:15:35 +02:00
try {
const syncParam = {
likes: false,
dislikes: false,
shares: false,
comments: false,
thumbnail: true,
refreshVideo: false
}
2018-08-23 17:58:39 +02:00
const result = await getOrCreateVideoAndAccountAndChannel(url, syncParam)
video = result ? result.video : undefined
2018-08-22 16:15:35 +02:00
} catch (err) {
logger.info('Cannot search remote video %s.', url)
}
} else {
video = await VideoModel.loadByUrlAndPopulateAccount(url)
}
return res.json({
total: video ? 1 : 0,
data: video ? [ video.toFormattedJSON() ] : []
})
}
2018-08-23 17:58:39 +02:00
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)
}