]>
Commit | Line | Data |
---|---|---|
37a44fc9 C |
1 | import * as express from 'express' |
2 | import { sanitizeUrl } from '@server/helpers/core-utils' | |
3 | import { doJSONRequest } from '@server/helpers/requests' | |
4 | import { CONFIG } from '@server/initializers/config' | |
5 | import { WEBSERVER } from '@server/initializers/constants' | |
6 | import { Hooks } from '@server/lib/plugins/hooks' | |
7 | import { buildMutedForSearchIndex, isSearchIndexSearch, isURISearch } from '@server/lib/search' | |
8 | import { getServerActor } from '@server/models/application/application' | |
4c7e60bc | 9 | import { HttpStatusCode, ResultList, VideoChannel } from '@shared/models' |
37a44fc9 C |
10 | import { VideoChannelsSearchQuery } from '../../../../shared/models/search' |
11 | import { isUserAbleToSearchRemoteURI } from '../../../helpers/express-utils' | |
12 | import { logger } from '../../../helpers/logger' | |
13 | import { getFormattedObjects } from '../../../helpers/utils' | |
14 | import { getOrCreateAPActor, loadActorUrlOrGetFromWebfinger } from '../../../lib/activitypub/actors' | |
15 | import { | |
16 | asyncMiddleware, | |
17 | openapiOperationDoc, | |
18 | optionalAuthenticate, | |
19 | paginationValidator, | |
20 | setDefaultPagination, | |
21 | setDefaultSearchSort, | |
22 | videoChannelsListSearchValidator, | |
23 | videoChannelsSearchSortValidator | |
24 | } from '../../../middlewares' | |
25 | import { VideoChannelModel } from '../../../models/video/video-channel' | |
26 | import { MChannelAccountDefault } from '../../../types/models' | |
27 | ||
28 | const searchChannelsRouter = express.Router() | |
29 | ||
30 | searchChannelsRouter.get('/video-channels', | |
31 | openapiOperationDoc({ operationId: 'searchChannels' }), | |
32 | paginationValidator, | |
33 | setDefaultPagination, | |
34 | videoChannelsSearchSortValidator, | |
35 | setDefaultSearchSort, | |
36 | optionalAuthenticate, | |
37 | videoChannelsListSearchValidator, | |
38 | asyncMiddleware(searchVideoChannels) | |
39 | ) | |
40 | ||
41 | // --------------------------------------------------------------------------- | |
42 | ||
43 | export { searchChannelsRouter } | |
44 | ||
45 | // --------------------------------------------------------------------------- | |
46 | ||
47 | function searchVideoChannels (req: express.Request, res: express.Response) { | |
48 | const query: VideoChannelsSearchQuery = req.query | |
49 | const search = query.search | |
50 | ||
51 | const parts = search.split('@') | |
52 | ||
53 | // Handle strings like @toto@example.com | |
54 | if (parts.length === 3 && parts[0].length === 0) parts.shift() | |
55 | const isWebfingerSearch = parts.length === 2 && parts.every(p => p && !p.includes(' ')) | |
56 | ||
57 | if (isURISearch(search) || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res) | |
58 | ||
59 | // @username -> username to search in DB | |
60 | if (query.search.startsWith('@')) query.search = query.search.replace(/^@/, '') | |
61 | ||
62 | if (isSearchIndexSearch(query)) { | |
63 | return searchVideoChannelsIndex(query, res) | |
64 | } | |
65 | ||
66 | return searchVideoChannelsDB(query, res) | |
67 | } | |
68 | ||
69 | async function searchVideoChannelsIndex (query: VideoChannelsSearchQuery, res: express.Response) { | |
70 | const result = await buildMutedForSearchIndex(res) | |
71 | ||
72 | const body = await Hooks.wrapObject(Object.assign(query, result), 'filter:api.search.video-channels.index.list.params') | |
73 | ||
74 | const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/video-channels' | |
75 | ||
76 | try { | |
77 | logger.debug('Doing video channels search index request on %s.', url, { body }) | |
78 | ||
79 | const { body: searchIndexResult } = await doJSONRequest<ResultList<VideoChannel>>(url, { method: 'POST', json: body }) | |
80 | const jsonResult = await Hooks.wrapObject(searchIndexResult, 'filter:api.search.video-channels.index.list.result') | |
81 | ||
82 | return res.json(jsonResult) | |
83 | } catch (err) { | |
84 | logger.warn('Cannot use search index to make video channels search.', { err }) | |
85 | ||
86 | return res.fail({ | |
87 | status: HttpStatusCode.INTERNAL_SERVER_ERROR_500, | |
88 | message: 'Cannot use search index to make video channels search' | |
89 | }) | |
90 | } | |
91 | } | |
92 | ||
93 | async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) { | |
94 | const serverActor = await getServerActor() | |
95 | ||
96 | const apiOptions = await Hooks.wrapObject({ | |
97 | actorId: serverActor.id, | |
98 | search: query.search, | |
99 | start: query.start, | |
100 | count: query.count, | |
fa47956e C |
101 | sort: query.sort, |
102 | host: query.host | |
37a44fc9 C |
103 | }, 'filter:api.search.video-channels.local.list.params') |
104 | ||
105 | const resultList = await Hooks.wrapPromiseFun( | |
106 | VideoChannelModel.searchForApi, | |
107 | apiOptions, | |
108 | 'filter:api.search.video-channels.local.list.result' | |
109 | ) | |
110 | ||
111 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
112 | } | |
113 | ||
114 | async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) { | |
115 | let videoChannel: MChannelAccountDefault | |
116 | let uri = search | |
117 | ||
118 | if (isWebfingerSearch) { | |
119 | try { | |
120 | uri = await loadActorUrlOrGetFromWebfinger(search) | |
121 | } catch (err) { | |
122 | logger.warn('Cannot load actor URL or get from webfinger.', { search, err }) | |
123 | ||
124 | return res.json({ total: 0, data: [] }) | |
125 | } | |
126 | } | |
127 | ||
128 | if (isUserAbleToSearchRemoteURI(res)) { | |
129 | try { | |
130 | const actor = await getOrCreateAPActor(uri, 'all', true, true) | |
131 | videoChannel = actor.VideoChannel | |
132 | } catch (err) { | |
133 | logger.info('Cannot search remote video channel %s.', uri, { err }) | |
134 | } | |
135 | } else { | |
136 | videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(sanitizeLocalUrl(uri)) | |
137 | } | |
138 | ||
139 | return res.json({ | |
140 | total: videoChannel ? 1 : 0, | |
141 | data: videoChannel ? [ videoChannel.toFormattedJSON() ] : [] | |
142 | }) | |
143 | } | |
144 | ||
145 | function sanitizeLocalUrl (url: string) { | |
146 | if (!url) return '' | |
147 | ||
148 | // Handle alternative channel URLs | |
149 | return url.replace(new RegExp('^' + WEBSERVER.URL + '/c/'), WEBSERVER.URL + '/video-channels/') | |
150 | } |