2018-07-19 09:17:54 -05:00
|
|
|
import * as express from 'express'
|
2018-07-20 07:35:18 -05:00
|
|
|
import { buildNSFWFilter } from '../../helpers/express-utils'
|
2018-07-19 09:17:54 -05:00
|
|
|
import { getFormattedObjects } from '../../helpers/utils'
|
|
|
|
import { VideoModel } from '../../models/video/video'
|
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
2018-07-20 07:35:18 -05:00
|
|
|
commonVideosFiltersValidator,
|
2018-07-19 09:17:54 -05:00
|
|
|
optionalAuthenticate,
|
|
|
|
paginationValidator,
|
|
|
|
searchValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
setDefaultSearchSort,
|
|
|
|
videosSearchSortValidator
|
|
|
|
} from '../../middlewares'
|
2018-07-20 07:35:18 -05:00
|
|
|
import { VideosSearchQuery } from '../../../shared/models/search'
|
2018-08-22 04:51:39 -05:00
|
|
|
import { getOrCreateAccountAndVideoAndChannel } from '../../lib/activitypub'
|
|
|
|
import { logger } from '../../helpers/logger'
|
2018-07-19 09:17:54 -05:00
|
|
|
|
|
|
|
const searchRouter = express.Router()
|
|
|
|
|
|
|
|
searchRouter.get('/videos',
|
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
videosSearchSortValidator,
|
|
|
|
setDefaultSearchSort,
|
|
|
|
optionalAuthenticate,
|
2018-07-20 07:35:18 -05:00
|
|
|
commonVideosFiltersValidator,
|
2018-07-19 09:17:54 -05:00
|
|
|
searchValidator,
|
|
|
|
asyncMiddleware(searchVideos)
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export { searchRouter }
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2018-08-22 04:51:39 -05:00
|
|
|
function searchVideos (req: express.Request, res: express.Response) {
|
2018-07-20 07:35:18 -05:00
|
|
|
const query: VideosSearchQuery = req.query
|
2018-08-22 04:51:39 -05:00
|
|
|
if (query.search.startsWith('http://') || query.search.startsWith('https://')) {
|
|
|
|
return searchVideoUrl(query.search, res)
|
|
|
|
}
|
2018-07-20 07:35:18 -05:00
|
|
|
|
2018-08-22 04:51:39 -05:00
|
|
|
return searchVideosDB(query, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
|
2018-08-16 08:25:20 -05:00
|
|
|
const options = Object.assign(query, {
|
|
|
|
includeLocalVideos: true,
|
|
|
|
nsfw: buildNSFWFilter(res, query.nsfw)
|
|
|
|
})
|
2018-07-20 07:35:18 -05:00
|
|
|
const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
|
2018-07-19 09:17:54 -05:00
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
|
|
|
}
|
2018-08-22 04:51:39 -05:00
|
|
|
|
|
|
|
async function searchVideoUrl (url: string, res: express.Response) {
|
|
|
|
let video: VideoModel
|
|
|
|
|
|
|
|
try {
|
|
|
|
const syncParam = {
|
|
|
|
likes: false,
|
|
|
|
dislikes: false,
|
|
|
|
shares: false,
|
|
|
|
comments: false,
|
|
|
|
thumbnail: true
|
|
|
|
}
|
|
|
|
|
|
|
|
const res = await getOrCreateAccountAndVideoAndChannel(url, syncParam)
|
|
|
|
video = res ? res.video : undefined
|
|
|
|
} catch (err) {
|
|
|
|
logger.info('Cannot search remote video %s.', url)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
total: video ? 1 : 0,
|
|
|
|
data: video ? [ video.toFormattedJSON() ] : []
|
|
|
|
})
|
|
|
|
}
|