]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/video-channel-sync.ts
Channel sync (#5135)
[github/Chocobozzz/PeerTube.git] / server / controllers / api / video-channel-sync.ts
1 import express from 'express'
2 import { auditLoggerFactory, getAuditIdFromRes, VideoChannelSyncAuditView } from '@server/helpers/audit-logger'
3 import { logger } from '@server/helpers/logger'
4 import {
5 asyncMiddleware,
6 asyncRetryTransactionMiddleware,
7 authenticate,
8 ensureCanManageChannel as ensureCanManageSyncedChannel,
9 ensureSyncExists,
10 ensureSyncIsEnabled,
11 videoChannelSyncValidator
12 } from '@server/middlewares'
13 import { VideoChannelSyncModel } from '@server/models/video/video-channel-sync'
14 import { MChannelSyncFormattable } from '@server/types/models'
15 import { HttpStatusCode, VideoChannelSyncState } from '@shared/models'
16
17 const videoChannelSyncRouter = express.Router()
18 const auditLogger = auditLoggerFactory('channel-syncs')
19
20 videoChannelSyncRouter.post('/',
21 authenticate,
22 ensureSyncIsEnabled,
23 asyncMiddleware(videoChannelSyncValidator),
24 ensureCanManageSyncedChannel,
25 asyncRetryTransactionMiddleware(createVideoChannelSync)
26 )
27
28 videoChannelSyncRouter.delete('/:id',
29 authenticate,
30 asyncMiddleware(ensureSyncExists),
31 ensureCanManageSyncedChannel,
32 asyncRetryTransactionMiddleware(removeVideoChannelSync)
33 )
34
35 export { videoChannelSyncRouter }
36
37 // ---------------------------------------------------------------------------
38
39 async function createVideoChannelSync (req: express.Request, res: express.Response) {
40 const syncCreated: MChannelSyncFormattable = new VideoChannelSyncModel({
41 externalChannelUrl: req.body.externalChannelUrl,
42 videoChannelId: req.body.videoChannelId,
43 state: VideoChannelSyncState.WAITING_FIRST_RUN
44 })
45
46 await syncCreated.save()
47 syncCreated.VideoChannel = res.locals.videoChannel
48
49 auditLogger.create(getAuditIdFromRes(res), new VideoChannelSyncAuditView(syncCreated.toFormattedJSON()))
50
51 logger.info(
52 'Video synchronization for channel "%s" with external channel "%s" created.',
53 syncCreated.VideoChannel.name,
54 syncCreated.externalChannelUrl
55 )
56
57 return res.json({
58 videoChannelSync: syncCreated.toFormattedJSON()
59 })
60 }
61
62 async function removeVideoChannelSync (req: express.Request, res: express.Response) {
63 const syncInstance = res.locals.videoChannelSync
64
65 await syncInstance.destroy()
66
67 auditLogger.delete(getAuditIdFromRes(res), new VideoChannelSyncAuditView(syncInstance.toFormattedJSON()))
68
69 logger.info(
70 'Video synchronization for channel "%s" with external channel "%s" deleted.',
71 syncInstance.VideoChannel.name,
72 syncInstance.externalChannelUrl
73 )
74
75 return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
76 }