]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/search/search-video-channels.ts
Add ability to search by uuids/actor names
[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'
3import { doJSONRequest } from '@server/helpers/requests'
4import { CONFIG } from '@server/initializers/config'
5import { WEBSERVER } from '@server/initializers/constants'
6import { Hooks } from '@server/lib/plugins/hooks'
7import { buildMutedForSearchIndex, isSearchIndexSearch, isURISearch } from '@server/lib/search'
8import { getServerActor } from '@server/models/application/application'
4c7e60bc 9import { HttpStatusCode, ResultList, VideoChannel } from '@shared/models'
37a44fc9
C
10import { VideoChannelsSearchQuery } from '../../../../shared/models/search'
11import { isUserAbleToSearchRemoteURI } from '../../../helpers/express-utils'
12import { logger } from '../../../helpers/logger'
13import { getFormattedObjects } from '../../../helpers/utils'
14import { getOrCreateAPActor, loadActorUrlOrGetFromWebfinger } from '../../../lib/activitypub/actors'
15import {
16 asyncMiddleware,
17 openapiOperationDoc,
18 optionalAuthenticate,
19 paginationValidator,
20 setDefaultPagination,
21 setDefaultSearchSort,
22 videoChannelsListSearchValidator,
23 videoChannelsSearchSortValidator
24} from '../../../middlewares'
25import { VideoChannelModel } from '../../../models/video/video-channel'
26import { MChannelAccountDefault } from '../../../types/models'
27
28const searchChannelsRouter = express.Router()
29
30searchChannelsRouter.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
43export { searchChannelsRouter }
44
45// ---------------------------------------------------------------------------
46
47function searchVideoChannels (req: express.Request, res: express.Response) {
48 const query: VideoChannelsSearchQuery = req.query
fbd67e7f 49 let search = query.search || ''
37a44fc9
C
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
fbd67e7f 60 if (search.startsWith('@')) search = search.replace(/^@/, '')
37a44fc9
C
61
62 if (isSearchIndexSearch(query)) {
63 return searchVideoChannelsIndex(query, res)
64 }
65
66 return searchVideoChannelsDB(query, res)
67}
68
69async 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
93async 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 101 sort: query.sort,
fbd67e7f
C
102 host: query.host,
103 names: query.names
37a44fc9
C
104 }, 'filter:api.search.video-channels.local.list.params')
105
106 const resultList = await Hooks.wrapPromiseFun(
107 VideoChannelModel.searchForApi,
108 apiOptions,
109 'filter:api.search.video-channels.local.list.result'
110 )
111
112 return res.json(getFormattedObjects(resultList.data, resultList.total))
113}
114
115async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) {
116 let videoChannel: MChannelAccountDefault
117 let uri = search
118
119 if (isWebfingerSearch) {
120 try {
121 uri = await loadActorUrlOrGetFromWebfinger(search)
122 } catch (err) {
123 logger.warn('Cannot load actor URL or get from webfinger.', { search, err })
124
125 return res.json({ total: 0, data: [] })
126 }
127 }
128
129 if (isUserAbleToSearchRemoteURI(res)) {
130 try {
131 const actor = await getOrCreateAPActor(uri, '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 VideoChannelModel.loadByUrlAndPopulateAccount(sanitizeLocalUrl(uri))
138 }
139
140 return res.json({
141 total: videoChannel ? 1 : 0,
142 data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
143 })
144}
145
146function 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}