]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/video-blacklist.ts
Merge remote-tracking branch 'weblate/develop' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / video-blacklist.ts
index a5c6fcbb29beb174c19c3c8e04595fecc4474e05..cb1ea834c46dc32a0b5f52a875999db98fe961f3 100644 (file)
-import * as sequelize from 'sequelize'
+import { Transaction } from 'sequelize'
+import { afterCommitIfTransaction } from '@server/helpers/database-utils'
+import { sequelizeTypescript } from '@server/initializers/database'
+import {
+  MUser,
+  MVideoAccountLight,
+  MVideoBlacklist,
+  MVideoBlacklistVideo,
+  MVideoFullLight,
+  MVideoWithBlacklistLight
+} from '@server/types/models'
+import { LiveVideoError, UserRight, VideoBlacklistCreate, VideoBlacklistType } from '../../shared/models'
+import { UserAdminFlag } from '../../shared/models/users/user-flag.model'
+import { logger, loggerTagsFactory } from '../helpers/logger'
 import { CONFIG } from '../initializers/config'
-import { VideoBlacklistType, UserRight } from '../../shared/models'
 import { VideoBlacklistModel } from '../models/video/video-blacklist'
-import { UserModel } from '../models/account/user'
-import { VideoModel } from '../models/video/video'
-import { logger } from '../helpers/logger'
+import { sendDeleteVideo } from './activitypub/send'
+import { federateVideoIfNeeded } from './activitypub/videos'
+import { LiveManager } from './live/live-manager'
+import { Notifier } from './notifier'
+import { Hooks } from './plugins/hooks'
 
-async function autoBlacklistVideoIfNeeded (video: VideoModel, user: UserModel, transaction: sequelize.Transaction) {
-  if (!CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED) return false
+const lTags = loggerTagsFactory('blacklist')
 
-  if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST)) return false
+async function autoBlacklistVideoIfNeeded (parameters: {
+  video: MVideoWithBlacklistLight
+  user?: MUser
+  isRemote: boolean
+  isNew: boolean
+  notify?: boolean
+  transaction?: Transaction
+}) {
+  const { video, user, isRemote, isNew, notify = true, transaction } = parameters
+  const doAutoBlacklist = await Hooks.wrapFun(
+    autoBlacklistNeeded,
+    { video, user, isRemote, isNew },
+    'filter:video.auto-blacklist.result'
+  )
+
+  if (!doAutoBlacklist) return false
 
-  const sequelizeOptions = { transaction }
   const videoBlacklistToCreate = {
     videoId: video.id,
     unfederated: true,
     reason: 'Auto-blacklisted. Moderator review required.',
     type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
   }
-  await VideoBlacklistModel.create(videoBlacklistToCreate, sequelizeOptions)
-  logger.info('Video %s auto-blacklisted.', video.uuid)
+  const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate<MVideoBlacklistVideo>({
+    where: {
+      videoId: video.id
+    },
+    defaults: videoBlacklistToCreate,
+    transaction
+  })
+  video.VideoBlacklist = videoBlacklist
+
+  videoBlacklist.Video = video
+
+  if (notify) {
+    afterCommitIfTransaction(transaction, () => {
+      Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist)
+    })
+  }
+
+  logger.info('Video %s auto-blacklisted.', video.uuid, lTags(video.uuid))
 
   return true
 }
 
+async function blacklistVideo (videoInstance: MVideoAccountLight, options: VideoBlacklistCreate) {
+  const blacklist: MVideoBlacklistVideo = await VideoBlacklistModel.create({
+    videoId: videoInstance.id,
+    unfederated: options.unfederate === true,
+    reason: options.reason,
+    type: VideoBlacklistType.MANUAL
+  })
+  blacklist.Video = videoInstance
+
+  if (options.unfederate === true) {
+    await sendDeleteVideo(videoInstance, undefined)
+  }
+
+  if (videoInstance.isLive) {
+    LiveManager.Instance.stopSessionOf(videoInstance.uuid, LiveVideoError.BLACKLISTED)
+  }
+
+  Notifier.Instance.notifyOnVideoBlacklist(blacklist)
+}
+
+async function unblacklistVideo (videoBlacklist: MVideoBlacklist, video: MVideoFullLight) {
+  const videoBlacklistType = await sequelizeTypescript.transaction(async t => {
+    const unfederated = videoBlacklist.unfederated
+    const videoBlacklistType = videoBlacklist.type
+
+    await videoBlacklist.destroy({ transaction: t })
+    video.VideoBlacklist = undefined
+
+    // Re federate the video
+    if (unfederated === true) {
+      await federateVideoIfNeeded(video, true, t)
+    }
+
+    return videoBlacklistType
+  })
+
+  Notifier.Instance.notifyOnVideoUnblacklist(video)
+
+  if (videoBlacklistType === VideoBlacklistType.AUTO_BEFORE_PUBLISHED) {
+    Notifier.Instance.notifyOnVideoPublishedAfterRemovedFromAutoBlacklist(video)
+
+    // Delete on object so new video notifications will send
+    delete video.VideoBlacklist
+    Notifier.Instance.notifyOnNewVideoIfNeeded(video)
+  }
+}
+
 // ---------------------------------------------------------------------------
 
 export {
-  autoBlacklistVideoIfNeeded
+  autoBlacklistVideoIfNeeded,
+  blacklistVideo,
+  unblacklistVideo
+}
+
+// ---------------------------------------------------------------------------
+
+function autoBlacklistNeeded (parameters: {
+  video: MVideoWithBlacklistLight
+  isRemote: boolean
+  isNew: boolean
+  user?: MUser
+}) {
+  const { user, video, isRemote, isNew } = parameters
+
+  // Already blacklisted
+  if (video.VideoBlacklist) return false
+  if (!CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED || !user) return false
+  if (isRemote || isNew === false) return false
+
+  if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)) return false
+
+  return true
 }