video-blacklist.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { Transaction } from 'sequelize'
  2. import { LiveVideoError, UserAdminFlag, UserRight, VideoBlacklistCreate, VideoBlacklistType } from '@peertube/peertube-models'
  3. import { afterCommitIfTransaction } from '@server/helpers/database-utils.js'
  4. import { sequelizeTypescript } from '@server/initializers/database.js'
  5. import {
  6. MUser,
  7. MVideoAccountLight,
  8. MVideoBlacklist,
  9. MVideoBlacklistVideo,
  10. MVideoFullLight,
  11. MVideoWithBlacklistLight
  12. } from '@server/types/models/index.js'
  13. import { logger, loggerTagsFactory } from '../helpers/logger.js'
  14. import { CONFIG } from '../initializers/config.js'
  15. import { VideoBlacklistModel } from '../models/video/video-blacklist.js'
  16. import { sendDeleteVideo } from './activitypub/send/index.js'
  17. import { federateVideoIfNeeded } from './activitypub/videos/index.js'
  18. import { LiveManager } from './live/live-manager.js'
  19. import { Notifier } from './notifier/index.js'
  20. import { Hooks } from './plugins/hooks.js'
  21. const lTags = loggerTagsFactory('blacklist')
  22. async function autoBlacklistVideoIfNeeded (parameters: {
  23. video: MVideoWithBlacklistLight
  24. user?: MUser
  25. isRemote: boolean
  26. isNew: boolean
  27. isNewFile: boolean
  28. notify?: boolean
  29. transaction?: Transaction
  30. }) {
  31. const { video, user, isRemote, isNew, isNewFile, notify = true, transaction } = parameters
  32. const doAutoBlacklist = await Hooks.wrapFun(
  33. autoBlacklistNeeded,
  34. { video, user, isRemote, isNew, isNewFile },
  35. 'filter:video.auto-blacklist.result'
  36. )
  37. if (!doAutoBlacklist) return false
  38. const videoBlacklistToCreate = {
  39. videoId: video.id,
  40. unfederated: true,
  41. reason: 'Auto-blacklisted. Moderator review required.',
  42. type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
  43. }
  44. const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate<MVideoBlacklistVideo>({
  45. where: {
  46. videoId: video.id
  47. },
  48. defaults: videoBlacklistToCreate,
  49. transaction
  50. })
  51. video.VideoBlacklist = videoBlacklist
  52. videoBlacklist.Video = video
  53. if (notify) {
  54. afterCommitIfTransaction(transaction, () => {
  55. Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist)
  56. })
  57. }
  58. logger.info('Video %s auto-blacklisted.', video.uuid, lTags(video.uuid))
  59. return true
  60. }
  61. async function blacklistVideo (videoInstance: MVideoAccountLight, options: VideoBlacklistCreate) {
  62. const blacklist: MVideoBlacklistVideo = await VideoBlacklistModel.create({
  63. videoId: videoInstance.id,
  64. unfederated: options.unfederate === true,
  65. reason: options.reason,
  66. type: VideoBlacklistType.MANUAL
  67. })
  68. blacklist.Video = videoInstance
  69. if (options.unfederate === true) {
  70. await sendDeleteVideo(videoInstance, undefined)
  71. }
  72. if (videoInstance.isLive) {
  73. LiveManager.Instance.stopSessionOfVideo({ videoUUID: videoInstance.uuid, error: LiveVideoError.BLACKLISTED })
  74. }
  75. Notifier.Instance.notifyOnVideoBlacklist(blacklist)
  76. }
  77. async function unblacklistVideo (videoBlacklist: MVideoBlacklist, video: MVideoFullLight) {
  78. const videoBlacklistType = await sequelizeTypescript.transaction(async t => {
  79. const unfederated = videoBlacklist.unfederated
  80. const videoBlacklistType = videoBlacklist.type
  81. await videoBlacklist.destroy({ transaction: t })
  82. video.VideoBlacklist = undefined
  83. // Re federate the video
  84. if (unfederated === true) {
  85. await federateVideoIfNeeded(video, true, t)
  86. }
  87. return videoBlacklistType
  88. })
  89. Notifier.Instance.notifyOnVideoUnblacklist(video)
  90. if (videoBlacklistType === VideoBlacklistType.AUTO_BEFORE_PUBLISHED) {
  91. Notifier.Instance.notifyOnVideoPublishedAfterRemovedFromAutoBlacklist(video)
  92. // Delete on object so new video notifications will send
  93. delete video.VideoBlacklist
  94. Notifier.Instance.notifyOnNewVideoOrLiveIfNeeded(video)
  95. }
  96. }
  97. // ---------------------------------------------------------------------------
  98. export {
  99. autoBlacklistVideoIfNeeded,
  100. blacklistVideo,
  101. unblacklistVideo
  102. }
  103. // ---------------------------------------------------------------------------
  104. function autoBlacklistNeeded (parameters: {
  105. video: MVideoWithBlacklistLight
  106. isRemote: boolean
  107. isNew: boolean
  108. isNewFile: boolean
  109. user?: MUser
  110. }) {
  111. const { user, video, isRemote, isNew, isNewFile } = parameters
  112. // Already blacklisted
  113. if (video.VideoBlacklist) return false
  114. if (!CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED || !user) return false
  115. if (isRemote || (isNew === false && isNewFile === false)) return false
  116. if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)) return false
  117. return true
  118. }