]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { pickCommonVideoQuery } from '@server/helpers/query' | |
3 | import { Hooks } from '@server/lib/plugins/hooks' | |
4 | import { getServerActor } from '@server/models/application/application' | |
5 | import { MChannelBannerAccountDefault } from '@server/types/models' | |
6 | import { ActorImageType, VideoChannelCreate, VideoChannelUpdate } from '../../../shared' | |
7 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' | |
8 | import { auditLoggerFactory, getAuditIdFromRes, VideoChannelAuditView } from '../../helpers/audit-logger' | |
9 | import { resetSequelizeInstance } from '../../helpers/database-utils' | |
10 | import { buildNSFWFilter, createReqFiles, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils' | |
11 | import { logger } from '../../helpers/logger' | |
12 | import { getFormattedObjects } from '../../helpers/utils' | |
13 | import { CONFIG } from '../../initializers/config' | |
14 | import { MIMETYPES } from '../../initializers/constants' | |
15 | import { sequelizeTypescript } from '../../initializers/database' | |
16 | import { sendUpdateActor } from '../../lib/activitypub/send' | |
17 | import { JobQueue } from '../../lib/job-queue' | |
18 | import { deleteLocalActorImageFile, updateLocalActorImageFile } from '../../lib/local-actor' | |
19 | import { createLocalVideoChannel, federateAllVideosOfChannel } from '../../lib/video-channel' | |
20 | import { | |
21 | asyncMiddleware, | |
22 | asyncRetryTransactionMiddleware, | |
23 | authenticate, | |
24 | commonVideosFiltersValidator, | |
25 | optionalAuthenticate, | |
26 | paginationValidator, | |
27 | setDefaultPagination, | |
28 | setDefaultSort, | |
29 | setDefaultVideosSort, | |
30 | videoChannelsAddValidator, | |
31 | videoChannelsRemoveValidator, | |
32 | videoChannelsSortValidator, | |
33 | videoChannelsUpdateValidator, | |
34 | videoPlaylistsSortValidator | |
35 | } from '../../middlewares' | |
36 | import { videoChannelsListValidator, videoChannelsNameWithHostValidator, videosSortValidator } from '../../middlewares/validators' | |
37 | import { updateAvatarValidator, updateBannerValidator } from '../../middlewares/validators/actor-image' | |
38 | import { commonVideoPlaylistFiltersValidator } from '../../middlewares/validators/videos/video-playlists' | |
39 | import { AccountModel } from '../../models/account/account' | |
40 | import { VideoModel } from '../../models/video/video' | |
41 | import { VideoChannelModel } from '../../models/video/video-channel' | |
42 | import { VideoPlaylistModel } from '../../models/video/video-playlist' | |
43 | ||
44 | const auditLogger = auditLoggerFactory('channels') | |
45 | const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR }) | |
46 | const reqBannerFile = createReqFiles([ 'bannerfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { bannerfile: CONFIG.STORAGE.TMP_DIR }) | |
47 | ||
48 | const videoChannelRouter = express.Router() | |
49 | ||
50 | videoChannelRouter.get('/', | |
51 | paginationValidator, | |
52 | videoChannelsSortValidator, | |
53 | setDefaultSort, | |
54 | setDefaultPagination, | |
55 | videoChannelsListValidator, | |
56 | asyncMiddleware(listVideoChannels) | |
57 | ) | |
58 | ||
59 | videoChannelRouter.post('/', | |
60 | authenticate, | |
61 | asyncMiddleware(videoChannelsAddValidator), | |
62 | asyncRetryTransactionMiddleware(addVideoChannel) | |
63 | ) | |
64 | ||
65 | videoChannelRouter.post('/:nameWithHost/avatar/pick', | |
66 | authenticate, | |
67 | reqAvatarFile, | |
68 | // Check the rights | |
69 | asyncMiddleware(videoChannelsUpdateValidator), | |
70 | updateAvatarValidator, | |
71 | asyncMiddleware(updateVideoChannelAvatar) | |
72 | ) | |
73 | ||
74 | videoChannelRouter.post('/:nameWithHost/banner/pick', | |
75 | authenticate, | |
76 | reqBannerFile, | |
77 | // Check the rights | |
78 | asyncMiddleware(videoChannelsUpdateValidator), | |
79 | updateBannerValidator, | |
80 | asyncMiddleware(updateVideoChannelBanner) | |
81 | ) | |
82 | ||
83 | videoChannelRouter.delete('/:nameWithHost/avatar', | |
84 | authenticate, | |
85 | // Check the rights | |
86 | asyncMiddleware(videoChannelsUpdateValidator), | |
87 | asyncMiddleware(deleteVideoChannelAvatar) | |
88 | ) | |
89 | ||
90 | videoChannelRouter.delete('/:nameWithHost/banner', | |
91 | authenticate, | |
92 | // Check the rights | |
93 | asyncMiddleware(videoChannelsUpdateValidator), | |
94 | asyncMiddleware(deleteVideoChannelBanner) | |
95 | ) | |
96 | ||
97 | videoChannelRouter.put('/:nameWithHost', | |
98 | authenticate, | |
99 | asyncMiddleware(videoChannelsUpdateValidator), | |
100 | asyncRetryTransactionMiddleware(updateVideoChannel) | |
101 | ) | |
102 | ||
103 | videoChannelRouter.delete('/:nameWithHost', | |
104 | authenticate, | |
105 | asyncMiddleware(videoChannelsRemoveValidator), | |
106 | asyncRetryTransactionMiddleware(removeVideoChannel) | |
107 | ) | |
108 | ||
109 | videoChannelRouter.get('/:nameWithHost', | |
110 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
111 | asyncMiddleware(getVideoChannel) | |
112 | ) | |
113 | ||
114 | videoChannelRouter.get('/:nameWithHost/video-playlists', | |
115 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
116 | paginationValidator, | |
117 | videoPlaylistsSortValidator, | |
118 | setDefaultSort, | |
119 | setDefaultPagination, | |
120 | commonVideoPlaylistFiltersValidator, | |
121 | asyncMiddleware(listVideoChannelPlaylists) | |
122 | ) | |
123 | ||
124 | videoChannelRouter.get('/:nameWithHost/videos', | |
125 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
126 | paginationValidator, | |
127 | videosSortValidator, | |
128 | setDefaultVideosSort, | |
129 | setDefaultPagination, | |
130 | optionalAuthenticate, | |
131 | commonVideosFiltersValidator, | |
132 | asyncMiddleware(listVideoChannelVideos) | |
133 | ) | |
134 | ||
135 | // --------------------------------------------------------------------------- | |
136 | ||
137 | export { | |
138 | videoChannelRouter | |
139 | } | |
140 | ||
141 | // --------------------------------------------------------------------------- | |
142 | ||
143 | async function listVideoChannels (req: express.Request, res: express.Response) { | |
144 | const serverActor = await getServerActor() | |
145 | const resultList = await VideoChannelModel.listForApi({ | |
146 | actorId: serverActor.id, | |
147 | start: req.query.start, | |
148 | count: req.query.count, | |
149 | sort: req.query.sort | |
150 | }) | |
151 | ||
152 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
153 | } | |
154 | ||
155 | async function updateVideoChannelBanner (req: express.Request, res: express.Response) { | |
156 | const bannerPhysicalFile = req.files['bannerfile'][0] | |
157 | const videoChannel = res.locals.videoChannel | |
158 | const oldVideoChannelAuditKeys = new VideoChannelAuditView(videoChannel.toFormattedJSON()) | |
159 | ||
160 | const banner = await updateLocalActorImageFile(videoChannel, bannerPhysicalFile, ActorImageType.BANNER) | |
161 | ||
162 | auditLogger.update(getAuditIdFromRes(res), new VideoChannelAuditView(videoChannel.toFormattedJSON()), oldVideoChannelAuditKeys) | |
163 | ||
164 | return res.json({ banner: banner.toFormattedJSON() }) | |
165 | } | |
166 | ||
167 | async function updateVideoChannelAvatar (req: express.Request, res: express.Response) { | |
168 | const avatarPhysicalFile = req.files['avatarfile'][0] | |
169 | const videoChannel = res.locals.videoChannel | |
170 | const oldVideoChannelAuditKeys = new VideoChannelAuditView(videoChannel.toFormattedJSON()) | |
171 | ||
172 | const avatar = await updateLocalActorImageFile(videoChannel, avatarPhysicalFile, ActorImageType.AVATAR) | |
173 | ||
174 | auditLogger.update(getAuditIdFromRes(res), new VideoChannelAuditView(videoChannel.toFormattedJSON()), oldVideoChannelAuditKeys) | |
175 | ||
176 | return res.json({ avatar: avatar.toFormattedJSON() }) | |
177 | } | |
178 | ||
179 | async function deleteVideoChannelAvatar (req: express.Request, res: express.Response) { | |
180 | const videoChannel = res.locals.videoChannel | |
181 | ||
182 | await deleteLocalActorImageFile(videoChannel, ActorImageType.AVATAR) | |
183 | ||
184 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
185 | } | |
186 | ||
187 | async function deleteVideoChannelBanner (req: express.Request, res: express.Response) { | |
188 | const videoChannel = res.locals.videoChannel | |
189 | ||
190 | await deleteLocalActorImageFile(videoChannel, ActorImageType.BANNER) | |
191 | ||
192 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
193 | } | |
194 | ||
195 | async function addVideoChannel (req: express.Request, res: express.Response) { | |
196 | const videoChannelInfo: VideoChannelCreate = req.body | |
197 | ||
198 | const videoChannelCreated = await sequelizeTypescript.transaction(async t => { | |
199 | const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t) | |
200 | ||
201 | return createLocalVideoChannel(videoChannelInfo, account, t) | |
202 | }) | |
203 | ||
204 | const payload = { actorId: videoChannelCreated.actorId } | |
205 | await JobQueue.Instance.createJobWithPromise({ type: 'actor-keys', payload }) | |
206 | ||
207 | auditLogger.create(getAuditIdFromRes(res), new VideoChannelAuditView(videoChannelCreated.toFormattedJSON())) | |
208 | logger.info('Video channel %s created.', videoChannelCreated.Actor.url) | |
209 | ||
210 | return res.json({ | |
211 | videoChannel: { | |
212 | id: videoChannelCreated.id | |
213 | } | |
214 | }) | |
215 | } | |
216 | ||
217 | async function updateVideoChannel (req: express.Request, res: express.Response) { | |
218 | const videoChannelInstance = res.locals.videoChannel | |
219 | const videoChannelFieldsSave = videoChannelInstance.toJSON() | |
220 | const oldVideoChannelAuditKeys = new VideoChannelAuditView(videoChannelInstance.toFormattedJSON()) | |
221 | const videoChannelInfoToUpdate = req.body as VideoChannelUpdate | |
222 | let doBulkVideoUpdate = false | |
223 | ||
224 | try { | |
225 | await sequelizeTypescript.transaction(async t => { | |
226 | if (videoChannelInfoToUpdate.displayName !== undefined) videoChannelInstance.name = videoChannelInfoToUpdate.displayName | |
227 | if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.description = videoChannelInfoToUpdate.description | |
228 | ||
229 | if (videoChannelInfoToUpdate.support !== undefined) { | |
230 | const oldSupportField = videoChannelInstance.support | |
231 | videoChannelInstance.support = videoChannelInfoToUpdate.support | |
232 | ||
233 | if (videoChannelInfoToUpdate.bulkVideosSupportUpdate === true && oldSupportField !== videoChannelInfoToUpdate.support) { | |
234 | doBulkVideoUpdate = true | |
235 | await VideoModel.bulkUpdateSupportField(videoChannelInstance, t) | |
236 | } | |
237 | } | |
238 | ||
239 | const videoChannelInstanceUpdated = await videoChannelInstance.save({ transaction: t }) as MChannelBannerAccountDefault | |
240 | await sendUpdateActor(videoChannelInstanceUpdated, t) | |
241 | ||
242 | auditLogger.update( | |
243 | getAuditIdFromRes(res), | |
244 | new VideoChannelAuditView(videoChannelInstanceUpdated.toFormattedJSON()), | |
245 | oldVideoChannelAuditKeys | |
246 | ) | |
247 | ||
248 | logger.info('Video channel %s updated.', videoChannelInstance.Actor.url) | |
249 | }) | |
250 | } catch (err) { | |
251 | logger.debug('Cannot update the video channel.', { err }) | |
252 | ||
253 | // Force fields we want to update | |
254 | // If the transaction is retried, sequelize will think the object has not changed | |
255 | // So it will skip the SQL request, even if the last one was ROLLBACKed! | |
256 | resetSequelizeInstance(videoChannelInstance, videoChannelFieldsSave) | |
257 | ||
258 | throw err | |
259 | } | |
260 | ||
261 | res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() | |
262 | ||
263 | // Don't process in a transaction, and after the response because it could be long | |
264 | if (doBulkVideoUpdate) { | |
265 | await federateAllVideosOfChannel(videoChannelInstance) | |
266 | } | |
267 | } | |
268 | ||
269 | async function removeVideoChannel (req: express.Request, res: express.Response) { | |
270 | const videoChannelInstance = res.locals.videoChannel | |
271 | ||
272 | await sequelizeTypescript.transaction(async t => { | |
273 | await VideoPlaylistModel.resetPlaylistsOfChannel(videoChannelInstance.id, t) | |
274 | ||
275 | await videoChannelInstance.destroy({ transaction: t }) | |
276 | ||
277 | auditLogger.delete(getAuditIdFromRes(res), new VideoChannelAuditView(videoChannelInstance.toFormattedJSON())) | |
278 | logger.info('Video channel %s deleted.', videoChannelInstance.Actor.url) | |
279 | }) | |
280 | ||
281 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() | |
282 | } | |
283 | ||
284 | async function getVideoChannel (req: express.Request, res: express.Response) { | |
285 | const videoChannel = res.locals.videoChannel | |
286 | ||
287 | if (videoChannel.isOutdated()) { | |
288 | JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: videoChannel.Actor.url } }) | |
289 | } | |
290 | ||
291 | return res.json(videoChannel.toFormattedJSON()) | |
292 | } | |
293 | ||
294 | async function listVideoChannelPlaylists (req: express.Request, res: express.Response) { | |
295 | const serverActor = await getServerActor() | |
296 | ||
297 | const resultList = await VideoPlaylistModel.listForApi({ | |
298 | followerActorId: serverActor.id, | |
299 | start: req.query.start, | |
300 | count: req.query.count, | |
301 | sort: req.query.sort, | |
302 | videoChannelId: res.locals.videoChannel.id, | |
303 | type: req.query.playlistType | |
304 | }) | |
305 | ||
306 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
307 | } | |
308 | ||
309 | async function listVideoChannelVideos (req: express.Request, res: express.Response) { | |
310 | const videoChannelInstance = res.locals.videoChannel | |
311 | const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined | |
312 | const countVideos = getCountVideos(req) | |
313 | const query = pickCommonVideoQuery(req.query) | |
314 | ||
315 | const apiOptions = await Hooks.wrapObject({ | |
316 | ...query, | |
317 | ||
318 | followerActorId, | |
319 | includeLocalVideos: true, | |
320 | nsfw: buildNSFWFilter(res, query.nsfw), | |
321 | withFiles: false, | |
322 | videoChannelId: videoChannelInstance.id, | |
323 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined, | |
324 | countVideos | |
325 | }, 'filter:api.video-channels.videos.list.params') | |
326 | ||
327 | const resultList = await Hooks.wrapPromiseFun( | |
328 | VideoModel.listForApi, | |
329 | apiOptions, | |
330 | 'filter:api.video-channels.videos.list.result' | |
331 | ) | |
332 | ||
333 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
334 | } |