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