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