]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils' | |
3 | import { getFormattedObjects, getServerActor } from '../../helpers/utils' | |
4 | import { VideoModel } from '../../models/video/video' | |
5 | import { | |
6 | asyncMiddleware, | |
7 | commonVideosFiltersValidator, | |
8 | optionalAuthenticate, | |
9 | paginationValidator, | |
10 | setDefaultPagination, | |
11 | setDefaultSearchSort, | |
12 | videoChannelsSearchSortValidator, | |
13 | videoChannelsSearchValidator, | |
14 | videosSearchSortValidator, | |
15 | videosSearchValidator | |
16 | } from '../../middlewares' | |
17 | import { VideoChannelsSearchQuery, VideosSearchQuery } from '../../../shared/models/search' | |
18 | import { getOrCreateActorAndServerAndModel, getOrCreateVideoAndAccountAndChannel } from '../../lib/activitypub' | |
19 | import { logger } from '../../helpers/logger' | |
20 | import { VideoChannelModel } from '../../models/video/video-channel' | |
21 | import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger' | |
22 | ||
23 | const searchRouter = express.Router() | |
24 | ||
25 | searchRouter.get('/videos', | |
26 | paginationValidator, | |
27 | setDefaultPagination, | |
28 | videosSearchSortValidator, | |
29 | setDefaultSearchSort, | |
30 | optionalAuthenticate, | |
31 | commonVideosFiltersValidator, | |
32 | videosSearchValidator, | |
33 | asyncMiddleware(searchVideos) | |
34 | ) | |
35 | ||
36 | searchRouter.get('/video-channels', | |
37 | paginationValidator, | |
38 | setDefaultPagination, | |
39 | videoChannelsSearchSortValidator, | |
40 | setDefaultSearchSort, | |
41 | optionalAuthenticate, | |
42 | videoChannelsSearchValidator, | |
43 | asyncMiddleware(searchVideoChannels) | |
44 | ) | |
45 | ||
46 | // --------------------------------------------------------------------------- | |
47 | ||
48 | export { searchRouter } | |
49 | ||
50 | // --------------------------------------------------------------------------- | |
51 | ||
52 | function searchVideoChannels (req: express.Request, res: express.Response) { | |
53 | const query: VideoChannelsSearchQuery = req.query | |
54 | const search = query.search | |
55 | ||
56 | const isURISearch = search.startsWith('http://') || search.startsWith('https://') | |
57 | ||
58 | const parts = search.split('@') | |
59 | const isWebfingerSearch = parts.length === 2 && parts.every(p => p.indexOf(' ') === -1) | |
60 | ||
61 | if (isURISearch || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res) | |
62 | ||
63 | return searchVideoChannelsDB(query, res) | |
64 | } | |
65 | ||
66 | async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) { | |
67 | const serverActor = await getServerActor() | |
68 | ||
69 | const options = { | |
70 | actorId: serverActor.id, | |
71 | search: query.search, | |
72 | start: query.start, | |
73 | count: query.count, | |
74 | sort: query.sort | |
75 | } | |
76 | const resultList = await VideoChannelModel.searchForApi(options) | |
77 | ||
78 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
79 | } | |
80 | ||
81 | async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) { | |
82 | let videoChannel: VideoChannelModel | |
83 | let uri = search | |
84 | ||
85 | if (isWebfingerSearch) uri = await loadActorUrlOrGetFromWebfinger(search) | |
86 | ||
87 | if (isUserAbleToSearchRemoteURI(res)) { | |
88 | try { | |
89 | const actor = await getOrCreateActorAndServerAndModel(uri, true, true) | |
90 | videoChannel = actor.VideoChannel | |
91 | } catch (err) { | |
92 | logger.info('Cannot search remote video channel %s.', uri, { err }) | |
93 | } | |
94 | } else { | |
95 | videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri) | |
96 | } | |
97 | ||
98 | return res.json({ | |
99 | total: videoChannel ? 1 : 0, | |
100 | data: videoChannel ? [ videoChannel.toFormattedJSON() ] : [] | |
101 | }) | |
102 | } | |
103 | ||
104 | function searchVideos (req: express.Request, res: express.Response) { | |
105 | const query: VideosSearchQuery = req.query | |
106 | const search = query.search | |
107 | if (search && (search.startsWith('http://') || search.startsWith('https://'))) { | |
108 | return searchVideoURI(search, res) | |
109 | } | |
110 | ||
111 | return searchVideosDB(query, res) | |
112 | } | |
113 | ||
114 | async function searchVideosDB (query: VideosSearchQuery, res: express.Response) { | |
115 | const options = Object.assign(query, { | |
116 | includeLocalVideos: true, | |
117 | nsfw: buildNSFWFilter(res, query.nsfw) | |
118 | }) | |
119 | const resultList = await VideoModel.searchAndPopulateAccountAndServer(options) | |
120 | ||
121 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
122 | } | |
123 | ||
124 | async function searchVideoURI (url: string, res: express.Response) { | |
125 | let video: VideoModel | |
126 | ||
127 | // Check if we can fetch a remote video with the URL | |
128 | if (isUserAbleToSearchRemoteURI(res)) { | |
129 | try { | |
130 | const syncParam = { | |
131 | likes: false, | |
132 | dislikes: false, | |
133 | shares: false, | |
134 | comments: false, | |
135 | thumbnail: true, | |
136 | refreshVideo: false | |
137 | } | |
138 | ||
139 | const result = await getOrCreateVideoAndAccountAndChannel(url, syncParam) | |
140 | video = result ? result.video : undefined | |
141 | } catch (err) { | |
142 | logger.info('Cannot search remote video %s.', url, { err }) | |
143 | } | |
144 | } else { | |
145 | video = await VideoModel.loadByUrlAndPopulateAccount(url) | |
146 | } | |
147 | ||
148 | return res.json({ | |
149 | total: video ? 1 : 0, | |
150 | data: video ? [ video.toFormattedJSON() ] : [] | |
151 | }) | |
152 | } |