]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/video-channel.ts
Add videos list filters
[github/Chocobozzz/PeerTube.git] / server / controllers / api / video-channel.ts
1 import * as express from 'express'
2 import { getFormattedObjects, resetSequelizeInstance } from '../../helpers/utils'
3 import {
4 asyncMiddleware,
5 asyncRetryTransactionMiddleware,
6 authenticate, commonVideosFiltersValidator,
7 optionalAuthenticate,
8 paginationValidator,
9 setDefaultPagination,
10 setDefaultSort,
11 videoChannelsAddValidator,
12 videoChannelsGetValidator,
13 videoChannelsRemoveValidator,
14 videoChannelsSortValidator,
15 videoChannelsUpdateValidator
16 } from '../../middlewares'
17 import { VideoChannelModel } from '../../models/video/video-channel'
18 import { videosSortValidator } from '../../middlewares/validators'
19 import { sendUpdateActor } from '../../lib/activitypub/send'
20 import { VideoChannelCreate, VideoChannelUpdate } from '../../../shared'
21 import { createVideoChannel } from '../../lib/video-channel'
22 import { createReqFiles, buildNSFWFilter } from '../../helpers/express-utils'
23 import { setAsyncActorKeys } from '../../lib/activitypub'
24 import { AccountModel } from '../../models/account/account'
25 import { CONFIG, IMAGE_MIMETYPE_EXT, sequelizeTypescript } from '../../initializers'
26 import { logger } from '../../helpers/logger'
27 import { VideoModel } from '../../models/video/video'
28 import { updateAvatarValidator } from '../../middlewares/validators/avatar'
29 import { updateActorAvatarFile } from '../../lib/avatar'
30
31 const reqAvatarFile = createReqFiles([ 'avatarfile' ], IMAGE_MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.AVATARS_DIR })
32
33 const videoChannelRouter = express.Router()
34
35 videoChannelRouter.get('/',
36 paginationValidator,
37 videoChannelsSortValidator,
38 setDefaultSort,
39 setDefaultPagination,
40 asyncMiddleware(listVideoChannels)
41 )
42
43 videoChannelRouter.post('/',
44 authenticate,
45 videoChannelsAddValidator,
46 asyncRetryTransactionMiddleware(addVideoChannel)
47 )
48
49 videoChannelRouter.post('/:id/avatar/pick',
50 authenticate,
51 reqAvatarFile,
52 // Check the rights
53 asyncMiddleware(videoChannelsUpdateValidator),
54 updateAvatarValidator,
55 asyncMiddleware(updateVideoChannelAvatar)
56 )
57
58 videoChannelRouter.put('/:id',
59 authenticate,
60 asyncMiddleware(videoChannelsUpdateValidator),
61 asyncRetryTransactionMiddleware(updateVideoChannel)
62 )
63
64 videoChannelRouter.delete('/:id',
65 authenticate,
66 asyncMiddleware(videoChannelsRemoveValidator),
67 asyncRetryTransactionMiddleware(removeVideoChannel)
68 )
69
70 videoChannelRouter.get('/:id',
71 asyncMiddleware(videoChannelsGetValidator),
72 asyncMiddleware(getVideoChannel)
73 )
74
75 videoChannelRouter.get('/:id/videos',
76 asyncMiddleware(videoChannelsGetValidator),
77 paginationValidator,
78 videosSortValidator,
79 setDefaultSort,
80 setDefaultPagination,
81 optionalAuthenticate,
82 commonVideosFiltersValidator,
83 asyncMiddleware(listVideoChannelVideos)
84 )
85
86 // ---------------------------------------------------------------------------
87
88 export {
89 videoChannelRouter
90 }
91
92 // ---------------------------------------------------------------------------
93
94 async function listVideoChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
95 const resultList = await VideoChannelModel.listForApi(req.query.start, req.query.count, req.query.sort)
96
97 return res.json(getFormattedObjects(resultList.data, resultList.total))
98 }
99
100 async function updateVideoChannelAvatar (req: express.Request, res: express.Response, next: express.NextFunction) {
101 const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ]
102 const videoChannel = res.locals.videoChannel
103
104 const avatar = await updateActorAvatarFile(avatarPhysicalFile, videoChannel.Actor, videoChannel)
105
106 return res
107 .json({
108 avatar: avatar.toFormattedJSON()
109 })
110 .end()
111 }
112
113 async function addVideoChannel (req: express.Request, res: express.Response) {
114 const videoChannelInfo: VideoChannelCreate = req.body
115 const account: AccountModel = res.locals.oauth.token.User.Account
116
117 const videoChannelCreated: VideoChannelModel = await sequelizeTypescript.transaction(async t => {
118 return createVideoChannel(videoChannelInfo, account, t)
119 })
120
121 setAsyncActorKeys(videoChannelCreated.Actor)
122 .catch(err => logger.error('Cannot set async actor keys for account %s.', videoChannelCreated.Actor.uuid, { err }))
123
124 logger.info('Video channel with uuid %s created.', videoChannelCreated.Actor.uuid)
125
126 return res.json({
127 videoChannel: {
128 id: videoChannelCreated.id,
129 uuid: videoChannelCreated.Actor.uuid
130 }
131 }).end()
132 }
133
134 async function updateVideoChannel (req: express.Request, res: express.Response) {
135 const videoChannelInstance = res.locals.videoChannel as VideoChannelModel
136 const videoChannelFieldsSave = videoChannelInstance.toJSON()
137 const videoChannelInfoToUpdate = req.body as VideoChannelUpdate
138
139 try {
140 await sequelizeTypescript.transaction(async t => {
141 const sequelizeOptions = {
142 transaction: t
143 }
144
145 if (videoChannelInfoToUpdate.displayName !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.displayName)
146 if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description)
147 if (videoChannelInfoToUpdate.support !== undefined) videoChannelInstance.set('support', videoChannelInfoToUpdate.support)
148
149 const videoChannelInstanceUpdated = await videoChannelInstance.save(sequelizeOptions)
150 await sendUpdateActor(videoChannelInstanceUpdated, t)
151 })
152
153 logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
154 } catch (err) {
155 logger.debug('Cannot update the video channel.', { err })
156
157 // Force fields we want to update
158 // If the transaction is retried, sequelize will think the object has not changed
159 // So it will skip the SQL request, even if the last one was ROLLBACKed!
160 resetSequelizeInstance(videoChannelInstance, videoChannelFieldsSave)
161
162 throw err
163 }
164
165 return res.type('json').status(204).end()
166 }
167
168 async function removeVideoChannel (req: express.Request, res: express.Response) {
169 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
170
171 await sequelizeTypescript.transaction(async t => {
172 await videoChannelInstance.destroy({ transaction: t })
173
174 logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
175 })
176
177 return res.type('json').status(204).end()
178 }
179
180 async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) {
181 const videoChannelWithVideos = await VideoChannelModel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id)
182
183 return res.json(videoChannelWithVideos.toFormattedJSON())
184 }
185
186 async function listVideoChannelVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
187 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
188
189 const resultList = await VideoModel.listForApi({
190 start: req.query.start,
191 count: req.query.count,
192 sort: req.query.sort,
193 categoryOneOf: req.query.categoryOneOf,
194 licenceOneOf: req.query.licenceOneOf,
195 languageOneOf: req.query.languageOneOf,
196 tagsOneOf: req.query.tagsOneOf,
197 tagsAllOf: req.query.tagsAllOf,
198 nsfw: buildNSFWFilter(res, req.query.nsfw),
199 withFiles: false,
200 videoChannelId: videoChannelInstance.id
201 })
202
203 return res.json(getFormattedObjects(resultList.data, resultList.total))
204 }