]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/video-channel-sync.ts
Fix runner api rate limit bypass
[github/Chocobozzz/PeerTube.git] / server / controllers / api / video-channel-sync.ts
CommitLineData
2a491182
F
1import express from 'express'
2import { auditLoggerFactory, getAuditIdFromRes, VideoChannelSyncAuditView } from '@server/helpers/audit-logger'
3import { logger } from '@server/helpers/logger'
4import {
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'
14import { VideoChannelSyncModel } from '@server/models/video/video-channel-sync'
15import { MChannelSyncFormattable } from '@server/types/models'
16import { HttpStatusCode, VideoChannelSyncState } from '@shared/models'
17
18const videoChannelSyncRouter = express.Router()
19const auditLogger = auditLoggerFactory('channel-syncs')
20
e915cde3
C
21videoChannelSyncRouter.use(apiRateLimiter)
22
2a491182
F
23videoChannelSyncRouter.post('/',
24 authenticate,
25 ensureSyncIsEnabled,
26 asyncMiddleware(videoChannelSyncValidator),
d4d9bbc6 27 ensureCanManageChannelOrAccount,
2a491182
F
28 asyncRetryTransactionMiddleware(createVideoChannelSync)
29)
30
31videoChannelSyncRouter.delete('/:id',
32 authenticate,
33 asyncMiddleware(ensureSyncExists),
d4d9bbc6 34 ensureCanManageChannelOrAccount,
2a491182
F
35 asyncRetryTransactionMiddleware(removeVideoChannelSync)
36)
37
38export { videoChannelSyncRouter }
39
40// ---------------------------------------------------------------------------
41
42async 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
65async 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}