diff options
Diffstat (limited to 'server/controllers/api/search.ts')
-rw-r--r-- | server/controllers/api/search.ts | 35 |
1 files changed, 34 insertions, 1 deletions
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 { | |||
13 | videosSearchSortValidator | 13 | videosSearchSortValidator |
14 | } from '../../middlewares' | 14 | } from '../../middlewares' |
15 | import { VideosSearchQuery } from '../../../shared/models/search' | 15 | import { VideosSearchQuery } from '../../../shared/models/search' |
16 | import { getOrCreateAccountAndVideoAndChannel } from '../../lib/activitypub' | ||
17 | import { logger } from '../../helpers/logger' | ||
16 | 18 | ||
17 | const searchRouter = express.Router() | 19 | const searchRouter = express.Router() |
18 | 20 | ||
@@ -33,9 +35,16 @@ export { searchRouter } | |||
33 | 35 | ||
34 | // --------------------------------------------------------------------------- | 36 | // --------------------------------------------------------------------------- |
35 | 37 | ||
36 | async function searchVideos (req: express.Request, res: express.Response) { | 38 | function searchVideos (req: express.Request, res: express.Response) { |
37 | const query: VideosSearchQuery = req.query | 39 | const query: VideosSearchQuery = req.query |
40 | if (query.search.startsWith('http://') || query.search.startsWith('https://')) { | ||
41 | return searchVideoUrl(query.search, res) | ||
42 | } | ||
38 | 43 | ||
44 | return searchVideosDB(query, res) | ||
45 | } | ||
46 | |||
47 | async function searchVideosDB (query: VideosSearchQuery, res: express.Response) { | ||
39 | const options = Object.assign(query, { | 48 | const options = Object.assign(query, { |
40 | includeLocalVideos: true, | 49 | includeLocalVideos: true, |
41 | nsfw: buildNSFWFilter(res, query.nsfw) | 50 | nsfw: buildNSFWFilter(res, query.nsfw) |
@@ -44,3 +53,27 @@ async function searchVideos (req: express.Request, res: express.Response) { | |||
44 | 53 | ||
45 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | 54 | return res.json(getFormattedObjects(resultList.data, resultList.total)) |
46 | } | 55 | } |
56 | |||
57 | async function searchVideoUrl (url: string, res: express.Response) { | ||
58 | let video: VideoModel | ||
59 | |||
60 | try { | ||
61 | const syncParam = { | ||
62 | likes: false, | ||
63 | dislikes: false, | ||
64 | shares: false, | ||
65 | comments: false, | ||
66 | thumbnail: true | ||
67 | } | ||
68 | |||
69 | const res = await getOrCreateAccountAndVideoAndChannel(url, syncParam) | ||
70 | video = res ? res.video : undefined | ||
71 | } catch (err) { | ||
72 | logger.info('Cannot search remote video %s.', url) | ||
73 | } | ||
74 | |||
75 | return res.json({ | ||
76 | total: video ? 1 : 0, | ||
77 | data: video ? [ video.toFormattedJSON() ] : [] | ||
78 | }) | ||
79 | } | ||