]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/video-blacklist.ts
Refactor playlist creation for lives
[github/Chocobozzz/PeerTube.git] / server / lib / video-blacklist.ts
index 3b90b1b9415982071d6deff43dacbeb18ec47b59..fd5837a3a19bd394611586dc07142722beb7fe4c 100644 (file)
@@ -1,12 +1,26 @@
 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 { UserRight, VideoBlacklistType } from '../../shared/models'
 import { VideoBlacklistModel } from '../models/video/video-blacklist'
-import { logger } from '../helpers/logger'
-import { UserAdminFlag } from '../../shared/models/users/user-flag.model'
-import { Hooks } from './plugins/hooks'
+import { sendDeleteVideo } from './activitypub/send'
+import { federateVideoIfNeeded } from './activitypub/videos'
+import { LiveManager } from './live/live-manager'
 import { Notifier } from './notifier'
-import { MUser, MVideoBlacklistVideo, MVideoWithBlacklistLight } from '@server/typings/models'
+import { Hooks } from './plugins/hooks'
+
+const lTags = loggerTagsFactory('blacklist')
 
 async function autoBlacklistVideoIfNeeded (parameters: {
   video: MVideoWithBlacklistLight
@@ -42,13 +56,74 @@ async function autoBlacklistVideoIfNeeded (parameters: {
 
   videoBlacklist.Video = video
 
-  if (notify) Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist)
+  if (notify) {
+    afterCommitIfTransaction(transaction, () => {
+      Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist)
+    })
+  }
 
-  logger.info('Video %s auto-blacklisted.', video.uuid)
+  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.id, 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,
+  blacklistVideo,
+  unblacklistVideo
+}
+
+// ---------------------------------------------------------------------------
+
 function autoBlacklistNeeded (parameters: {
   video: MVideoWithBlacklistLight
   isRemote: boolean
@@ -62,13 +137,7 @@ function autoBlacklistNeeded (parameters: {
   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.BY_PASS_VIDEO_AUTO_BLACKLIST)) return false
+  if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)) return false
 
   return true
 }
-
-// ---------------------------------------------------------------------------
-
-export {
-  autoBlacklistVideoIfNeeded
-}