]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/search.ts
Add explicit error message that changing video ownership only works with local accou...
[github/Chocobozzz/PeerTube.git] / server / controllers / api / search.ts
CommitLineData
57c36b27 1import * as express from 'express'
687d638c 2import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
f37dc0dd 3import { getFormattedObjects, getServerActor } from '../../helpers/utils'
57c36b27
C
4import { VideoModel } from '../../models/video/video'
5import {
6 asyncMiddleware,
d525fc39 7 commonVideosFiltersValidator,
57c36b27
C
8 optionalAuthenticate,
9 paginationValidator,
57c36b27
C
10 setDefaultPagination,
11 setDefaultSearchSort,
f37dc0dd
C
12 videoChannelsSearchSortValidator,
13 videoChannelsSearchValidator,
14 videosSearchSortValidator,
15 videosSearchValidator
57c36b27 16} from '../../middlewares'
f37dc0dd
C
17import { VideoChannelsSearchQuery, VideosSearchQuery } from '../../../shared/models/search'
18import { getOrCreateActorAndServerAndModel, getOrCreateVideoAndAccountAndChannel } from '../../lib/activitypub'
f6eebcb3 19import { logger } from '../../helpers/logger'
f37dc0dd
C
20import { VideoChannelModel } from '../../models/video/video-channel'
21import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
57c36b27
C
22
23const searchRouter = express.Router()
24
25searchRouter.get('/videos',
26 paginationValidator,
27 setDefaultPagination,
28 videosSearchSortValidator,
29 setDefaultSearchSort,
30 optionalAuthenticate,
d525fc39 31 commonVideosFiltersValidator,
f37dc0dd 32 videosSearchValidator,
57c36b27
C
33 asyncMiddleware(searchVideos)
34)
35
f37dc0dd
C
36searchRouter.get('/video-channels',
37 paginationValidator,
38 setDefaultPagination,
39 videoChannelsSearchSortValidator,
40 setDefaultSearchSort,
41 optionalAuthenticate,
f37dc0dd
C
42 videoChannelsSearchValidator,
43 asyncMiddleware(searchVideoChannels)
44)
45
57c36b27
C
46// ---------------------------------------------------------------------------
47
48export { searchRouter }
49
50// ---------------------------------------------------------------------------
51
f37dc0dd
C
52function searchVideoChannels (req: express.Request, res: express.Response) {
53 const query: VideoChannelsSearchQuery = req.query
54 const search = query.search
55
56 const isURISearch = search.startsWith('http://') || search.startsWith('https://')
57
58 const parts = search.split('@')
2ff83ae2
C
59
60 // Handle strings like @toto@example.com
61 if (parts.length === 3 && parts[0].length === 0) parts.shift()
f5b0af50 62 const isWebfingerSearch = parts.length === 2 && parts.every(p => p.indexOf(' ') === -1)
f37dc0dd 63
f5b0af50 64 if (isURISearch || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res)
f37dc0dd
C
65
66 return searchVideoChannelsDB(query, res)
67}
68
69async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) {
70 const serverActor = await getServerActor()
71
72 const options = {
73 actorId: serverActor.id,
74 search: query.search,
75 start: query.start,
76 count: query.count,
77 sort: query.sort
78 }
79 const resultList = await VideoChannelModel.searchForApi(options)
80
81 return res.json(getFormattedObjects(resultList.data, resultList.total))
82}
83
f5b0af50 84async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) {
f37dc0dd 85 let videoChannel: VideoChannelModel
f5b0af50 86 let uri = search
f37dc0dd 87
f5b0af50 88 if (isWebfingerSearch) uri = await loadActorUrlOrGetFromWebfinger(search)
f37dc0dd 89
f5b0af50
C
90 if (isUserAbleToSearchRemoteURI(res)) {
91 try {
e587e0ec 92 const actor = await getOrCreateActorAndServerAndModel(uri, 'all', true, true)
f5b0af50
C
93 videoChannel = actor.VideoChannel
94 } catch (err) {
95 logger.info('Cannot search remote video channel %s.', uri, { err })
96 }
f37dc0dd 97 } else {
f5b0af50 98 videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri)
f37dc0dd
C
99 }
100
101 return res.json({
102 total: videoChannel ? 1 : 0,
103 data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
104 })
105}
106
f6eebcb3 107function searchVideos (req: express.Request, res: express.Response) {
d525fc39 108 const query: VideosSearchQuery = req.query
240085d0
C
109 const search = query.search
110 if (search && (search.startsWith('http://') || search.startsWith('https://'))) {
f37dc0dd 111 return searchVideoURI(search, res)
f6eebcb3 112 }
d525fc39 113
f6eebcb3
C
114 return searchVideosDB(query, res)
115}
116
117async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
06a05d5f
C
118 const options = Object.assign(query, {
119 includeLocalVideos: true,
6e46de09
C
120 nsfw: buildNSFWFilter(res, query.nsfw),
121 userId: res.locals.oauth ? res.locals.oauth.token.User.id : undefined
06a05d5f 122 })
d525fc39 123 const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
57c36b27
C
124
125 return res.json(getFormattedObjects(resultList.data, resultList.total))
126}
f6eebcb3 127
f37dc0dd 128async function searchVideoURI (url: string, res: express.Response) {
f6eebcb3
C
129 let video: VideoModel
130
1297eb5d 131 // Check if we can fetch a remote video with the URL
f37dc0dd 132 if (isUserAbleToSearchRemoteURI(res)) {
1297eb5d
C
133 try {
134 const syncParam = {
135 likes: false,
136 dislikes: false,
137 shares: false,
138 comments: false,
139 thumbnail: true,
140 refreshVideo: false
141 }
f6eebcb3 142
4157cdb1 143 const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: url, syncParam })
f37dc0dd 144 video = result ? result.video : undefined
1297eb5d 145 } catch (err) {
f5b0af50 146 logger.info('Cannot search remote video %s.', url, { err })
1297eb5d
C
147 }
148 } else {
149 video = await VideoModel.loadByUrlAndPopulateAccount(url)
f6eebcb3
C
150 }
151
152 return res.json({
153 total: video ? 1 : 0,
154 data: video ? [ video.toFormattedJSON() ] : []
155 })
156}