blacklist.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import * as express from 'express'
  2. import { VideoBlacklist, UserRight, VideoBlacklistCreate, VideoBlacklistType } from '../../../../shared'
  3. import { logger } from '../../../helpers/logger'
  4. import { getFormattedObjects } from '../../../helpers/utils'
  5. import {
  6. asyncMiddleware,
  7. authenticate,
  8. blacklistSortValidator,
  9. ensureUserHasRight,
  10. paginationValidator,
  11. setBlacklistSort,
  12. setDefaultPagination,
  13. videosBlacklistAddValidator,
  14. videosBlacklistRemoveValidator,
  15. videosBlacklistUpdateValidator,
  16. videosBlacklistFiltersValidator
  17. } from '../../../middlewares'
  18. import { VideoBlacklistModel } from '../../../models/video/video-blacklist'
  19. import { sequelizeTypescript } from '../../../initializers'
  20. import { Notifier } from '../../../lib/notifier'
  21. import { sendDeleteVideo } from '../../../lib/activitypub/send'
  22. import { federateVideoIfNeeded } from '../../../lib/activitypub'
  23. const blacklistRouter = express.Router()
  24. blacklistRouter.post('/:videoId/blacklist',
  25. authenticate,
  26. ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
  27. asyncMiddleware(videosBlacklistAddValidator),
  28. asyncMiddleware(addVideoToBlacklist)
  29. )
  30. blacklistRouter.get('/blacklist',
  31. authenticate,
  32. ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
  33. paginationValidator,
  34. blacklistSortValidator,
  35. setBlacklistSort,
  36. setDefaultPagination,
  37. videosBlacklistFiltersValidator,
  38. asyncMiddleware(listBlacklist)
  39. )
  40. blacklistRouter.put('/:videoId/blacklist',
  41. authenticate,
  42. ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
  43. asyncMiddleware(videosBlacklistUpdateValidator),
  44. asyncMiddleware(updateVideoBlacklistController)
  45. )
  46. blacklistRouter.delete('/:videoId/blacklist',
  47. authenticate,
  48. ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
  49. asyncMiddleware(videosBlacklistRemoveValidator),
  50. asyncMiddleware(removeVideoFromBlacklistController)
  51. )
  52. // ---------------------------------------------------------------------------
  53. export {
  54. blacklistRouter
  55. }
  56. // ---------------------------------------------------------------------------
  57. async function addVideoToBlacklist (req: express.Request, res: express.Response) {
  58. const videoInstance = res.locals.video
  59. const body: VideoBlacklistCreate = req.body
  60. const toCreate = {
  61. videoId: videoInstance.id,
  62. unfederated: body.unfederate === true,
  63. reason: body.reason,
  64. type: VideoBlacklistType.MANUAL
  65. }
  66. const blacklist = await VideoBlacklistModel.create(toCreate)
  67. blacklist.Video = videoInstance
  68. if (body.unfederate === true) {
  69. await sendDeleteVideo(videoInstance, undefined)
  70. }
  71. Notifier.Instance.notifyOnVideoBlacklist(blacklist)
  72. logger.info('Video %s blacklisted.', res.locals.video.uuid)
  73. return res.type('json').status(204).end()
  74. }
  75. async function updateVideoBlacklistController (req: express.Request, res: express.Response) {
  76. const videoBlacklist = res.locals.videoBlacklist
  77. if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason
  78. await sequelizeTypescript.transaction(t => {
  79. return videoBlacklist.save({ transaction: t })
  80. })
  81. return res.type('json').status(204).end()
  82. }
  83. async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
  84. const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort, req.query.type)
  85. return res.json(getFormattedObjects<VideoBlacklist, VideoBlacklistModel>(resultList.data, resultList.total))
  86. }
  87. async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) {
  88. const videoBlacklist = res.locals.videoBlacklist
  89. const video = res.locals.video
  90. const videoBlacklistType = await sequelizeTypescript.transaction(async t => {
  91. const unfederated = videoBlacklist.unfederated
  92. const videoBlacklistType = videoBlacklist.type
  93. await videoBlacklist.destroy({ transaction: t })
  94. // Re federate the video
  95. if (unfederated === true) {
  96. await federateVideoIfNeeded(video, true, t)
  97. }
  98. return videoBlacklistType
  99. })
  100. Notifier.Instance.notifyOnVideoUnblacklist(video)
  101. if (videoBlacklistType === VideoBlacklistType.AUTO_BEFORE_PUBLISHED) {
  102. Notifier.Instance.notifyOnVideoPublishedAfterRemovedFromAutoBlacklist(video)
  103. // Delete on object so new video notifications will send
  104. delete video.VideoBlacklist
  105. Notifier.Instance.notifyOnNewVideo(video)
  106. }
  107. logger.info('Video %s removed from blacklist.', res.locals.video.uuid)
  108. return res.type('json').status(204).end()
  109. }