+function searchVideoChannels (req: express.Request, res: express.Response) {
+ const query: VideoChannelsSearchQuery = req.query
+ const search = query.search
+
+ const isURISearch = search.startsWith('http://') || search.startsWith('https://')
+
+ const parts = search.split('@')
+ const isHandleSearch = parts.length === 2 && parts.every(p => p.indexOf(' ') === -1)
+
+ if (isURISearch || isHandleSearch) return searchVideoChannelURI(search, isHandleSearch, res)
+
+ return searchVideoChannelsDB(query, res)
+}
+
+async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) {
+ const serverActor = await getServerActor()
+
+ const options = {
+ actorId: serverActor.id,
+ search: query.search,
+ start: query.start,
+ count: query.count,
+ sort: query.sort
+ }
+ const resultList = await VideoChannelModel.searchForApi(options)
+
+ return res.json(getFormattedObjects(resultList.data, resultList.total))
+}
+
+async function searchVideoChannelURI (search: string, isHandleSearch: boolean, res: express.Response) {
+ let videoChannel: VideoChannelModel
+
+ if (isUserAbleToSearchRemoteURI(res)) {
+ let uri = search
+ if (isHandleSearch) uri = await loadActorUrlOrGetFromWebfinger(search)
+
+ const actor = await getOrCreateActorAndServerAndModel(uri)
+ videoChannel = actor.VideoChannel
+ } else {
+ videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(search)
+ }
+
+ return res.json({
+ total: videoChannel ? 1 : 0,
+ data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
+ })
+}
+