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