1 import express from 'express'
2 import { auditLoggerFactory, getAuditIdFromRes, VideoChannelSyncAuditView } from '@server/helpers/audit-logger'
3 import { logger } from '@server/helpers/logger'
6 asyncRetryTransactionMiddleware,
8 ensureCanManageChannelOrAccount,
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'
17 const videoChannelSyncRouter = express.Router()
18 const auditLogger = auditLoggerFactory('channel-syncs')
20 videoChannelSyncRouter.post('/',
23 asyncMiddleware(videoChannelSyncValidator),
24 ensureCanManageChannelOrAccount,
25 asyncRetryTransactionMiddleware(createVideoChannelSync)
28 videoChannelSyncRouter.delete('/:id',
30 asyncMiddleware(ensureSyncExists),
31 ensureCanManageChannelOrAccount,
32 asyncRetryTransactionMiddleware(removeVideoChannelSync)
35 export { videoChannelSyncRouter }
37 // ---------------------------------------------------------------------------
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
46 await syncCreated.save()
47 syncCreated.VideoChannel = res.locals.videoChannel
49 auditLogger.create(getAuditIdFromRes(res), new VideoChannelSyncAuditView(syncCreated.toFormattedJSON()))
52 'Video synchronization for channel "%s" with external channel "%s" created.',
53 syncCreated.VideoChannel.name,
54 syncCreated.externalChannelUrl
58 videoChannelSync: syncCreated.toFormattedJSON()
62 async function removeVideoChannelSync (req: express.Request, res: express.Response) {
63 const syncInstance = res.locals.videoChannelSync
65 await syncInstance.destroy()
67 auditLogger.delete(getAuditIdFromRes(res), new VideoChannelSyncAuditView(syncInstance.toFormattedJSON()))
70 'Video synchronization for channel "%s" with external channel "%s" deleted.',
71 syncInstance.VideoChannel.name,
72 syncInstance.externalChannelUrl
75 return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()