]>
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' | |
c0e8b12e | 9 | import { HttpStatusCode } from '@shared/models' |
37a44fc9 C |
10 | import { ResultList, VideoChannel } from '@shared/models' |
11 | import { VideoChannelsSearchQuery } from '../../../../shared/models/search' | |
12 | import { isUserAbleToSearchRemoteURI } from '../../../helpers/express-utils' | |
13 | import { logger } from '../../../helpers/logger' | |
14 | import { getFormattedObjects } from '../../../helpers/utils' | |
15 | import { getOrCreateAPActor, loadActorUrlOrGetFromWebfinger } from '../../../lib/activitypub/actors' | |
16 | import { | |
17 | asyncMiddleware, | |
18 | openapiOperationDoc, | |
19 | optionalAuthenticate, | |
20 | paginationValidator, | |
21 | setDefaultPagination, | |
22 | setDefaultSearchSort, | |
23 | videoChannelsListSearchValidator, | |
24 | videoChannelsSearchSortValidator | |
25 | } from '../../../middlewares' | |
26 | import { VideoChannelModel } from '../../../models/video/video-channel' | |
27 | import { MChannelAccountDefault } from '../../../types/models' | |
28 | ||
29 | const searchChannelsRouter = express.Router() | |
30 | ||
31 | searchChannelsRouter.get('/video-channels', | |
32 | openapiOperationDoc({ operationId: 'searchChannels' }), | |
33 | paginationValidator, | |
34 | setDefaultPagination, | |
35 | videoChannelsSearchSortValidator, | |
36 | setDefaultSearchSort, | |
37 | optionalAuthenticate, | |
38 | videoChannelsListSearchValidator, | |
39 | asyncMiddleware(searchVideoChannels) | |
40 | ) | |
41 | ||
42 | // --------------------------------------------------------------------------- | |
43 | ||
44 | export { searchChannelsRouter } | |
45 | ||
46 | // --------------------------------------------------------------------------- | |
47 | ||
48 | function searchVideoChannels (req: express.Request, res: express.Response) { | |
49 | const query: VideoChannelsSearchQuery = req.query | |
50 | const search = query.search | |
51 | ||
52 | const parts = search.split('@') | |
53 | ||
54 | // Handle strings like @toto@example.com | |
55 | if (parts.length === 3 && parts[0].length === 0) parts.shift() | |
56 | const isWebfingerSearch = parts.length === 2 && parts.every(p => p && !p.includes(' ')) | |
57 | ||
58 | if (isURISearch(search) || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res) | |
59 | ||
60 | // @username -> username to search in DB | |
61 | if (query.search.startsWith('@')) query.search = query.search.replace(/^@/, '') | |
62 | ||
63 | if (isSearchIndexSearch(query)) { | |
64 | return searchVideoChannelsIndex(query, res) | |
65 | } | |
66 | ||
67 | return searchVideoChannelsDB(query, res) | |
68 | } | |
69 | ||
70 | async function searchVideoChannelsIndex (query: VideoChannelsSearchQuery, res: express.Response) { | |
71 | const result = await buildMutedForSearchIndex(res) | |
72 | ||
73 | const body = await Hooks.wrapObject(Object.assign(query, result), 'filter:api.search.video-channels.index.list.params') | |
74 | ||
75 | const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/video-channels' | |
76 | ||
77 | try { | |
78 | logger.debug('Doing video channels search index request on %s.', url, { body }) | |
79 | ||
80 | const { body: searchIndexResult } = await doJSONRequest<ResultList<VideoChannel>>(url, { method: 'POST', json: body }) | |
81 | const jsonResult = await Hooks.wrapObject(searchIndexResult, 'filter:api.search.video-channels.index.list.result') | |
82 | ||
83 | return res.json(jsonResult) | |
84 | } catch (err) { | |
85 | logger.warn('Cannot use search index to make video channels search.', { err }) | |
86 | ||
87 | return res.fail({ | |
88 | status: HttpStatusCode.INTERNAL_SERVER_ERROR_500, | |
89 | message: 'Cannot use search index to make video channels search' | |
90 | }) | |
91 | } | |
92 | } | |
93 | ||
94 | async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) { | |
95 | const serverActor = await getServerActor() | |
96 | ||
97 | const apiOptions = await Hooks.wrapObject({ | |
98 | actorId: serverActor.id, | |
99 | search: query.search, | |
100 | start: query.start, | |
101 | count: query.count, | |
102 | sort: query.sort | |
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 | } |