1 import express from 'express'
2 import { auditLoggerFactory, getAuditIdFromRes, VideoChannelSyncAuditView } from '@server/helpers/audit-logger'
3 import { logger } from '@server/helpers/logger'
7 asyncRetryTransactionMiddleware,
9 ensureCanManageChannelOrAccount,
12 videoChannelSyncValidator
13 } from '@server/middlewares'
14 import { VideoChannelSyncModel } from '@server/models/video/video-channel-sync'
15 import { MChannelSyncFormattable } from '@server/types/models'
16 import { HttpStatusCode, VideoChannelSyncState } from '@shared/models'
18 const videoChannelSyncRouter = express.Router()
19 const auditLogger = auditLoggerFactory('channel-syncs')
21 videoChannelSyncRouter.use(apiRateLimiter)
23 videoChannelSyncRouter.post('/',
26 asyncMiddleware(videoChannelSyncValidator),
27 ensureCanManageChannelOrAccount,
28 asyncRetryTransactionMiddleware(createVideoChannelSync)
31 videoChannelSyncRouter.delete('/:id',
33 asyncMiddleware(ensureSyncExists),
34 ensureCanManageChannelOrAccount,
35 asyncRetryTransactionMiddleware(removeVideoChannelSync)
38 export { videoChannelSyncRouter }
40 // ---------------------------------------------------------------------------
42 async function createVideoChannelSync (req: express.Request, res: express.Response) {
43 const syncCreated: MChannelSyncFormattable = new VideoChannelSyncModel({
44 externalChannelUrl: req.body.externalChannelUrl,
45 videoChannelId: req.body.videoChannelId,
46 state: VideoChannelSyncState.WAITING_FIRST_RUN
49 await syncCreated.save()
50 syncCreated.VideoChannel = res.locals.videoChannel
52 auditLogger.create(getAuditIdFromRes(res), new VideoChannelSyncAuditView(syncCreated.toFormattedJSON()))
55 'Video synchronization for channel "%s" with external channel "%s" created.',
56 syncCreated.VideoChannel.name,
57 syncCreated.externalChannelUrl
61 videoChannelSync: syncCreated.toFormattedJSON()
65 async function removeVideoChannelSync (req: express.Request, res: express.Response) {
66 const syncInstance = res.locals.videoChannelSync
68 await syncInstance.destroy()
70 auditLogger.delete(getAuditIdFromRes(res), new VideoChannelSyncAuditView(syncInstance.toFormattedJSON()))
73 'Video synchronization for channel "%s" with external channel "%s" deleted.',
74 syncInstance.VideoChannel.name,
75 syncInstance.externalChannelUrl
78 return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()