]>
Commit | Line | Data |
---|---|---|
2a491182 F |
1 | import express from 'express' |
2 | import { auditLoggerFactory, getAuditIdFromRes, VideoChannelSyncAuditView } from '@server/helpers/audit-logger' | |
3 | import { logger } from '@server/helpers/logger' | |
4 | import { | |
e915cde3 | 5 | apiRateLimiter, |
2a491182 F |
6 | asyncMiddleware, |
7 | asyncRetryTransactionMiddleware, | |
8 | authenticate, | |
d4d9bbc6 | 9 | ensureCanManageChannelOrAccount, |
2a491182 F |
10 | ensureSyncExists, |
11 | ensureSyncIsEnabled, | |
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' | |
17 | ||
18 | const videoChannelSyncRouter = express.Router() | |
19 | const auditLogger = auditLoggerFactory('channel-syncs') | |
20 | ||
e915cde3 C |
21 | videoChannelSyncRouter.use(apiRateLimiter) |
22 | ||
2a491182 F |
23 | videoChannelSyncRouter.post('/', |
24 | authenticate, | |
25 | ensureSyncIsEnabled, | |
26 | asyncMiddleware(videoChannelSyncValidator), | |
d4d9bbc6 | 27 | ensureCanManageChannelOrAccount, |
2a491182 F |
28 | asyncRetryTransactionMiddleware(createVideoChannelSync) |
29 | ) | |
30 | ||
31 | videoChannelSyncRouter.delete('/:id', | |
32 | authenticate, | |
33 | asyncMiddleware(ensureSyncExists), | |
d4d9bbc6 | 34 | ensureCanManageChannelOrAccount, |
2a491182 F |
35 | asyncRetryTransactionMiddleware(removeVideoChannelSync) |
36 | ) | |
37 | ||
38 | export { videoChannelSyncRouter } | |
39 | ||
40 | // --------------------------------------------------------------------------- | |
41 | ||
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 | |
47 | }) | |
48 | ||
49 | await syncCreated.save() | |
50 | syncCreated.VideoChannel = res.locals.videoChannel | |
51 | ||
52 | auditLogger.create(getAuditIdFromRes(res), new VideoChannelSyncAuditView(syncCreated.toFormattedJSON())) | |
53 | ||
54 | logger.info( | |
55 | 'Video synchronization for channel "%s" with external channel "%s" created.', | |
56 | syncCreated.VideoChannel.name, | |
57 | syncCreated.externalChannelUrl | |
58 | ) | |
59 | ||
60 | return res.json({ | |
61 | videoChannelSync: syncCreated.toFormattedJSON() | |
62 | }) | |
63 | } | |
64 | ||
65 | async function removeVideoChannelSync (req: express.Request, res: express.Response) { | |
66 | const syncInstance = res.locals.videoChannelSync | |
67 | ||
68 | await syncInstance.destroy() | |
69 | ||
70 | auditLogger.delete(getAuditIdFromRes(res), new VideoChannelSyncAuditView(syncInstance.toFormattedJSON())) | |
71 | ||
72 | logger.info( | |
73 | 'Video synchronization for channel "%s" with external channel "%s" deleted.', | |
74 | syncInstance.VideoChannel.name, | |
75 | syncInstance.externalChannelUrl | |
76 | ) | |
77 | ||
78 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() | |
79 | } |