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