From f6eebcb336c067e160a62020a5140d8d992ba384 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 22 Aug 2018 11:51:39 +0200 Subject: Add ability to search a video with an URL --- server/controllers/api/search.ts | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'server/controllers/api') diff --git a/server/controllers/api/search.ts b/server/controllers/api/search.ts index 7a7504b7d..9c2c7d6c1 100644 --- a/server/controllers/api/search.ts +++ b/server/controllers/api/search.ts @@ -13,6 +13,8 @@ import { videosSearchSortValidator } from '../../middlewares' import { VideosSearchQuery } from '../../../shared/models/search' +import { getOrCreateAccountAndVideoAndChannel } from '../../lib/activitypub' +import { logger } from '../../helpers/logger' const searchRouter = express.Router() @@ -33,9 +35,16 @@ export { searchRouter } // --------------------------------------------------------------------------- -async function searchVideos (req: express.Request, res: express.Response) { +function searchVideos (req: express.Request, res: express.Response) { const query: VideosSearchQuery = req.query + if (query.search.startsWith('http://') || query.search.startsWith('https://')) { + return searchVideoUrl(query.search, res) + } + return searchVideosDB(query, res) +} + +async function searchVideosDB (query: VideosSearchQuery, res: express.Response) { const options = Object.assign(query, { includeLocalVideos: true, nsfw: buildNSFWFilter(res, query.nsfw) @@ -44,3 +53,27 @@ async function searchVideos (req: express.Request, res: express.Response) { return res.json(getFormattedObjects(resultList.data, resultList.total)) } + +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() ] : [] + }) +} -- cgit v1.2.3