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