PeerTube/server/controllers/api/search.ts

93 lines
2.7 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-07-19 16:17:54 +02:00
import { getFormattedObjects } from '../../helpers/utils'
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,
searchValidator,
setDefaultPagination,
setDefaultSearchSort,
videosSearchSortValidator
} from '../../middlewares'
2018-07-20 14:35:18 +02:00
import { VideosSearchQuery } from '../../../shared/models/search'
2018-08-22 16:15:35 +02:00
import { 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-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-07-19 16:17:54 +02:00
searchValidator,
asyncMiddleware(searchVideos)
)
// ---------------------------------------------------------------------------
export { searchRouter }
// ---------------------------------------------------------------------------
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://'))) {
return searchVideoUrl(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))
}
async function searchVideoUrl (url: string, res: express.Response) {
let video: VideoModel
2018-08-22 16:15:35 +02:00
const user: User = res.locals.oauth ? res.locals.oauth.token.User : undefined
2018-08-22 16:15:35 +02:00
// 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)
) {
try {
const syncParam = {
likes: false,
dislikes: false,
shares: false,
comments: false,
thumbnail: true,
refreshVideo: false
}
2018-08-22 16:15:35 +02:00
const res = await getOrCreateVideoAndAccountAndChannel(url, syncParam)
video = res ? res.video : undefined
} 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() ] : []
})
}