]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/search.ts
Add ability to list all local videos
[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 120 nsfw: buildNSFWFilter(res, query.nsfw),
1cd3facc 121 filter: query.filter,
6e46de09 122 userId: res.locals.oauth ? res.locals.oauth.token.User.id : undefined
06a05d5f 123 })
d525fc39 124 const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
57c36b27
C
125
126 return res.json(getFormattedObjects(resultList.data, resultList.total))
127}
f6eebcb3 128
f37dc0dd 129async function searchVideoURI (url: string, res: express.Response) {
f6eebcb3
C
130 let video: VideoModel
131
1297eb5d 132 // Check if we can fetch a remote video with the URL
f37dc0dd 133 if (isUserAbleToSearchRemoteURI(res)) {
1297eb5d
C
134 try {
135 const syncParam = {
136 likes: false,
137 dislikes: false,
138 shares: false,
139 comments: false,
140 thumbnail: true,
141 refreshVideo: false
142 }
f6eebcb3 143
4157cdb1 144 const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: url, syncParam })
f37dc0dd 145 video = result ? result.video : undefined
1297eb5d 146 } catch (err) {
f5b0af50 147 logger.info('Cannot search remote video %s.', url, { err })
1297eb5d
C
148 }
149 } else {
150 video = await VideoModel.loadByUrlAndPopulateAccount(url)
f6eebcb3
C
151 }
152
153 return res.json({
154 total: video ? 1 : 0,
155 data: video ? [ video.toFormattedJSON() ] : []
156 })
157}