]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/search/search-video-channels.ts
Merge branch 'release/3.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / controllers / api / search / search-video-channels.ts
CommitLineData
37a44fc9
C
1import * as express from 'express'
2import { sanitizeUrl } from '@server/helpers/core-utils'
d6886027 3import { pickSearchChannelQuery } from '@server/helpers/query'
37a44fc9
C
4import { doJSONRequest } from '@server/helpers/requests'
5import { CONFIG } from '@server/initializers/config'
6import { WEBSERVER } from '@server/initializers/constants'
7import { Hooks } from '@server/lib/plugins/hooks'
8import { buildMutedForSearchIndex, isSearchIndexSearch, isURISearch } from '@server/lib/search'
9import { getServerActor } from '@server/models/application/application'
4c7e60bc 10import { HttpStatusCode, ResultList, VideoChannel } from '@shared/models'
d6886027 11import { VideoChannelsSearchQueryAfterSanitize } from '../../../../shared/models/search'
37a44fc9
C
12import { isUserAbleToSearchRemoteURI } from '../../../helpers/express-utils'
13import { logger } from '../../../helpers/logger'
14import { getFormattedObjects } from '../../../helpers/utils'
15import { getOrCreateAPActor, loadActorUrlOrGetFromWebfinger } from '../../../lib/activitypub/actors'
16import {
17 asyncMiddleware,
18 openapiOperationDoc,
19 optionalAuthenticate,
20 paginationValidator,
21 setDefaultPagination,
22 setDefaultSearchSort,
23 videoChannelsListSearchValidator,
24 videoChannelsSearchSortValidator
25} from '../../../middlewares'
26import { VideoChannelModel } from '../../../models/video/video-channel'
27import { MChannelAccountDefault } from '../../../types/models'
28
29const searchChannelsRouter = express.Router()
30
31searchChannelsRouter.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
44export { searchChannelsRouter }
45
46// ---------------------------------------------------------------------------
47
48function searchVideoChannels (req: express.Request, res: express.Response) {
d6886027 49 const query = pickSearchChannelQuery(req.query)
fbd67e7f 50 let search = query.search || ''
37a44fc9
C
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
fbd67e7f 61 if (search.startsWith('@')) search = search.replace(/^@/, '')
37a44fc9
C
62
63 if (isSearchIndexSearch(query)) {
64 return searchVideoChannelsIndex(query, res)
65 }
66
67 return searchVideoChannelsDB(query, res)
68}
69
d6886027 70async function searchVideoChannelsIndex (query: VideoChannelsSearchQueryAfterSanitize, res: express.Response) {
37a44fc9
C
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
d6886027 94async function searchVideoChannelsDB (query: VideoChannelsSearchQueryAfterSanitize, res: express.Response) {
37a44fc9
C
95 const serverActor = await getServerActor()
96
97 const apiOptions = await Hooks.wrapObject({
d6886027
C
98 ...query,
99
100 actorId: serverActor.id
37a44fc9
C
101 }, 'filter:api.search.video-channels.local.list.params')
102
103 const resultList = await Hooks.wrapPromiseFun(
104 VideoChannelModel.searchForApi,
105 apiOptions,
106 'filter:api.search.video-channels.local.list.result'
107 )
108
109 return res.json(getFormattedObjects(resultList.data, resultList.total))
110}
111
112async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) {
113 let videoChannel: MChannelAccountDefault
114 let uri = search
115
116 if (isWebfingerSearch) {
117 try {
118 uri = await loadActorUrlOrGetFromWebfinger(search)
119 } catch (err) {
120 logger.warn('Cannot load actor URL or get from webfinger.', { search, err })
121
122 return res.json({ total: 0, data: [] })
123 }
124 }
125
126 if (isUserAbleToSearchRemoteURI(res)) {
127 try {
128 const actor = await getOrCreateAPActor(uri, 'all', true, true)
129 videoChannel = actor.VideoChannel
130 } catch (err) {
131 logger.info('Cannot search remote video channel %s.', uri, { err })
132 }
133 } else {
134 videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(sanitizeLocalUrl(uri))
135 }
136
137 return res.json({
138 total: videoChannel ? 1 : 0,
139 data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
140 })
141}
142
143function sanitizeLocalUrl (url: string) {
144 if (!url) return ''
145
146 // Handle alternative channel URLs
147 return url.replace(new RegExp('^' + WEBSERVER.URL + '/c/'), WEBSERVER.URL + '/video-channels/')
148}