ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

video-channel-sync.ts 2.6 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import express from 'express'
  2. import { auditLoggerFactory, getAuditIdFromRes, VideoChannelSyncAuditView } from '@server/helpers/audit-logger.js'
  3. import { logger } from '@server/helpers/logger.js'
  4. import {
  5. apiRateLimiter,
  6. asyncMiddleware,
  7. asyncRetryTransactionMiddleware,
  8. authenticate,
  9. ensureCanManageChannelOrAccount,
  10. ensureSyncExists,
  11. ensureSyncIsEnabled,
  12. videoChannelSyncValidator
  13. } from '@server/middlewares/index.js'
  14. import { VideoChannelSyncModel } from '@server/models/video/video-channel-sync.js'
  15. import { MChannelSyncFormattable } from '@server/types/models/index.js'
  16. import { HttpStatusCode, VideoChannelSyncState } from '@peertube/peertube-models'
  17. const videoChannelSyncRouter = express.Router()
  18. const auditLogger = auditLoggerFactory('channel-syncs')
  19. videoChannelSyncRouter.use(apiRateLimiter)
  20. videoChannelSyncRouter.post('/',
  21. authenticate,
  22. ensureSyncIsEnabled,
  23. asyncMiddleware(videoChannelSyncValidator),
  24. ensureCanManageChannelOrAccount,
  25. asyncRetryTransactionMiddleware(createVideoChannelSync)
  26. )
  27. videoChannelSyncRouter.delete('/:id',
  28. authenticate,
  29. asyncMiddleware(ensureSyncExists),
  30. ensureCanManageChannelOrAccount,
  31. asyncRetryTransactionMiddleware(removeVideoChannelSync)
  32. )
  33. export { videoChannelSyncRouter }
  34. // ---------------------------------------------------------------------------
  35. async function createVideoChannelSync (req: express.Request, res: express.Response) {
  36. const syncCreated: MChannelSyncFormattable = new VideoChannelSyncModel({
  37. externalChannelUrl: req.body.externalChannelUrl,
  38. videoChannelId: req.body.videoChannelId,
  39. state: VideoChannelSyncState.WAITING_FIRST_RUN
  40. })
  41. await syncCreated.save()
  42. syncCreated.VideoChannel = res.locals.videoChannel
  43. auditLogger.create(getAuditIdFromRes(res), new VideoChannelSyncAuditView(syncCreated.toFormattedJSON()))
  44. logger.info(
  45. 'Video synchronization for channel "%s" with external channel "%s" created.',
  46. syncCreated.VideoChannel.name,
  47. syncCreated.externalChannelUrl
  48. )
  49. return res.json({
  50. videoChannelSync: syncCreated.toFormattedJSON()
  51. })
  52. }
  53. async function removeVideoChannelSync (req: express.Request, res: express.Response) {
  54. const syncInstance = res.locals.videoChannelSync
  55. await syncInstance.destroy()
  56. auditLogger.delete(getAuditIdFromRes(res), new VideoChannelSyncAuditView(syncInstance.toFormattedJSON()))
  57. logger.info(
  58. 'Video synchronization for channel "%s" with external channel "%s" deleted.',
  59. syncInstance.VideoChannel.name,
  60. syncInstance.externalChannelUrl
  61. )
  62. return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
  63. }