]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/search.ts
Avoir some circular dependencies
[github/Chocobozzz/PeerTube.git] / server / controllers / api / search.ts
1 import * as express from 'express'
2 import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
3 import { getFormattedObjects } from '../../helpers/utils'
4 import { VideoModel } from '../../models/video/video'
5 import {
6 asyncMiddleware,
7 commonVideosFiltersValidator,
8 optionalAuthenticate,
9 paginationValidator,
10 setDefaultPagination,
11 setDefaultSearchSort,
12 videoChannelsSearchSortValidator,
13 videoChannelsSearchValidator,
14 videosSearchSortValidator,
15 videosSearchValidator
16 } from '../../middlewares'
17 import { VideoChannelsSearchQuery, VideosSearchQuery } from '../../../shared/models/search'
18 import { getOrCreateActorAndServerAndModel } from '../../lib/activitypub/actor'
19 import { logger } from '../../helpers/logger'
20 import { VideoChannelModel } from '../../models/video/video-channel'
21 import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
22 import { MChannelAccountDefault, MVideoAccountLightBlacklistAllFiles } from '../../typings/models'
23 import { getServerActor } from '@server/models/application/application'
24 import { getOrCreateVideoAndAccountAndChannel } from '@server/lib/activitypub/videos'
25
26 const searchRouter = express.Router()
27
28 searchRouter.get('/videos',
29 paginationValidator,
30 setDefaultPagination,
31 videosSearchSortValidator,
32 setDefaultSearchSort,
33 optionalAuthenticate,
34 commonVideosFiltersValidator,
35 videosSearchValidator,
36 asyncMiddleware(searchVideos)
37 )
38
39 searchRouter.get('/video-channels',
40 paginationValidator,
41 setDefaultPagination,
42 videoChannelsSearchSortValidator,
43 setDefaultSearchSort,
44 optionalAuthenticate,
45 videoChannelsSearchValidator,
46 asyncMiddleware(searchVideoChannels)
47 )
48
49 // ---------------------------------------------------------------------------
50
51 export { searchRouter }
52
53 // ---------------------------------------------------------------------------
54
55 function 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('@')
62
63 // Handle strings like @toto@example.com
64 if (parts.length === 3 && parts[0].length === 0) parts.shift()
65 const isWebfingerSearch = parts.length === 2 && parts.every(p => p && !p.includes(' '))
66
67 if (isURISearch || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res)
68
69 // @username -> username to search in DB
70 if (query.search.startsWith('@')) query.search = query.search.replace(/^@/, '')
71 return searchVideoChannelsDB(query, res)
72 }
73
74 async 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
89 async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) {
90 let videoChannel: MChannelAccountDefault
91 let uri = search
92
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 }
102
103 if (isUserAbleToSearchRemoteURI(res)) {
104 try {
105 const actor = await getOrCreateActorAndServerAndModel(uri, 'all', true, true)
106 videoChannel = actor.VideoChannel
107 } catch (err) {
108 logger.info('Cannot search remote video channel %s.', uri, { err })
109 }
110 } else {
111 videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri)
112 }
113
114 return res.json({
115 total: videoChannel ? 1 : 0,
116 data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
117 })
118 }
119
120 function searchVideos (req: express.Request, res: express.Response) {
121 const query: VideosSearchQuery = req.query
122 const search = query.search
123 if (search && (search.startsWith('http://') || search.startsWith('https://'))) {
124 return searchVideoURI(search, res)
125 }
126
127 return searchVideosDB(query, res)
128 }
129
130 async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
131 const options = Object.assign(query, {
132 includeLocalVideos: true,
133 nsfw: buildNSFWFilter(res, query.nsfw),
134 filter: query.filter,
135 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
136 })
137 const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
138
139 return res.json(getFormattedObjects(resultList.data, resultList.total))
140 }
141
142 async function searchVideoURI (url: string, res: express.Response) {
143 let video: MVideoAccountLightBlacklistAllFiles
144
145 // Check if we can fetch a remote video with the URL
146 if (isUserAbleToSearchRemoteURI(res)) {
147 try {
148 const syncParam = {
149 likes: false,
150 dislikes: false,
151 shares: false,
152 comments: false,
153 thumbnail: true,
154 refreshVideo: false
155 }
156
157 const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: url, syncParam })
158 video = result ? result.video : undefined
159 } catch (err) {
160 logger.info('Cannot search remote video %s.', url, { err })
161 }
162 } else {
163 video = await VideoModel.loadByUrlAndPopulateAccount(url)
164 }
165
166 return res.json({
167 total: video ? 1 : 0,
168 data: video ? [ video.toFormattedJSON() ] : []
169 })
170 }