]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - 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
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'
9 import { HttpStatusCode, ResultList, VideoChannel } from '@shared/models'
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 let 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 (search.startsWith('@')) search = 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,
101 sort: query.sort,
102 host: query.host,
103 names: query.names
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
115 async 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
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 }