]>
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 | ||
60 | // Handle strings like @toto@example.com | |
61 | if (parts.length === 3 && parts[0].length === 0) parts.shift() | |
62 | const isWebfingerSearch = parts.length === 2 && parts.every(p => p && p.indexOf(' ') === -1) | |
63 | ||
64 | if (isURISearch || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res) | |
65 | ||
66 | // @username -> username to search in DB | |
67 | if (query.search.startsWith('@')) query.search = query.search.replace(/^@/, '') | |
68 | return searchVideoChannelsDB(query, res) | |
69 | } | |
70 | ||
71 | async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) { | |
72 | const serverActor = await getServerActor() | |
73 | ||
74 | const options = { | |
75 | actorId: serverActor.id, | |
76 | search: query.search, | |
77 | start: query.start, | |
78 | count: query.count, | |
79 | sort: query.sort | |
80 | } | |
81 | const resultList = await VideoChannelModel.searchForApi(options) | |
82 | ||
83 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
84 | } | |
85 | ||
86 | async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) { | |
87 | let videoChannel: VideoChannelModel | |
88 | let uri = search | |
89 | ||
90 | if (isWebfingerSearch) { | |
91 | try { | |
92 | uri = await loadActorUrlOrGetFromWebfinger(search) | |
93 | } catch (err) { | |
94 | logger.warn('Cannot load actor URL or get from webfinger.', { search, err }) | |
95 | ||
96 | return res.json({ total: 0, data: [] }) | |
97 | } | |
98 | } | |
99 | ||
100 | if (isUserAbleToSearchRemoteURI(res)) { | |
101 | try { | |
102 | const actor = await getOrCreateActorAndServerAndModel(uri, 'all', true, true) | |
103 | videoChannel = actor.VideoChannel | |
104 | } catch (err) { | |
105 | logger.info('Cannot search remote video channel %s.', uri, { err }) | |
106 | } | |
107 | } else { | |
108 | videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri) | |
109 | } | |
110 | ||
111 | return res.json({ | |
112 | total: videoChannel ? 1 : 0, | |
113 | data: videoChannel ? [ videoChannel.toFormattedJSON() ] : [] | |
114 | }) | |
115 | } | |
116 | ||
117 | function searchVideos (req: express.Request, res: express.Response) { | |
118 | const query: VideosSearchQuery = req.query | |
119 | const search = query.search | |
120 | if (search && (search.startsWith('http://') || search.startsWith('https://'))) { | |
121 | return searchVideoURI(search, res) | |
122 | } | |
123 | ||
124 | return searchVideosDB(query, res) | |
125 | } | |
126 | ||
127 | async function searchVideosDB (query: VideosSearchQuery, res: express.Response) { | |
128 | const options = Object.assign(query, { | |
129 | includeLocalVideos: true, | |
130 | nsfw: buildNSFWFilter(res, query.nsfw), | |
131 | filter: query.filter, | |
132 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined | |
133 | }) | |
134 | const resultList = await VideoModel.searchAndPopulateAccountAndServer(options) | |
135 | ||
136 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
137 | } | |
138 | ||
139 | async function searchVideoURI (url: string, res: express.Response) { | |
140 | let video: VideoModel | |
141 | ||
142 | // Check if we can fetch a remote video with the URL | |
143 | if (isUserAbleToSearchRemoteURI(res)) { | |
144 | try { | |
145 | const syncParam = { | |
146 | likes: false, | |
147 | dislikes: false, | |
148 | shares: false, | |
149 | comments: false, | |
150 | thumbnail: true, | |
151 | refreshVideo: false | |
152 | } | |
153 | ||
154 | const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: url, syncParam }) | |
155 | video = result ? result.video : undefined | |
156 | } catch (err) { | |
157 | logger.info('Cannot search remote video %s.', url, { err }) | |
158 | } | |
159 | } else { | |
160 | video = await VideoModel.loadByUrlAndPopulateAccount(url) | |
161 | } | |
162 | ||
163 | return res.json({ | |
164 | total: video ? 1 : 0, | |
165 | data: video ? [ video.toFormattedJSON() ] : [] | |
166 | }) | |
167 | } |