1 import { Transaction } from 'sequelize'
2 import { sequelizeTypescript } from '@server/initializers/database'
9 MVideoWithBlacklistLight
10 } from '@server/typings/models'
11 import { UserRight, VideoBlacklistCreate, VideoBlockType } from '../../shared/models'
12 import { UserAdminFlag } from '../../shared/models/users/user-flag.model'
13 import { logger } from '../helpers/logger'
14 import { CONFIG } from '../initializers/config'
15 import { VideoBlacklistModel } from '../models/video/video-blacklist'
16 import { sendDeleteVideo } from './activitypub/send'
17 import { federateVideoIfNeeded } from './activitypub/videos'
18 import { Notifier } from './notifier'
19 import { Hooks } from './plugins/hooks'
21 async function autoBlacklistVideoIfNeeded (parameters: {
22 video: MVideoWithBlacklistLight
27 transaction?: Transaction
29 const { video, user, isRemote, isNew, notify = true, transaction } = parameters
30 const doAutoBlacklist = await Hooks.wrapFun(
32 { video, user, isRemote, isNew },
33 'filter:video.auto-blacklist.result'
36 if (!doAutoBlacklist) return false
38 const videoBlacklistToCreate = {
41 reason: 'Auto-blacklisted. Moderator review required.',
42 type: VideoBlockType.AUTO_BEFORE_PUBLISHED
44 const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate<MVideoBlacklistVideo>({
48 defaults: videoBlacklistToCreate,
51 video.VideoBlacklist = videoBlacklist
53 videoBlacklist.Video = video
55 if (notify) Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist)
57 logger.info('Video %s auto-blacklisted.', video.uuid)
62 async function blacklistVideo (videoInstance: MVideoAccountLight, options: VideoBlacklistCreate) {
63 const blacklist: MVideoBlacklistVideo = await VideoBlacklistModel.create({
64 videoId: videoInstance.id,
65 unfederated: options.unfederate === true,
66 reason: options.reason,
67 type: VideoBlockType.MANUAL
70 blacklist.Video = videoInstance
72 if (options.unfederate === true) {
73 await sendDeleteVideo(videoInstance, undefined)
76 Notifier.Instance.notifyOnVideoBlacklist(blacklist)
79 async function unblacklistVideo (videoBlacklist: MVideoBlacklist, video: MVideoFullLight) {
80 const videoBlacklistType = await sequelizeTypescript.transaction(async t => {
81 const unfederated = videoBlacklist.unfederated
82 const videoBlacklistType = videoBlacklist.type
84 await videoBlacklist.destroy({ transaction: t })
85 video.VideoBlacklist = undefined
87 // Re federate the video
88 if (unfederated === true) {
89 await federateVideoIfNeeded(video, true, t)
92 return videoBlacklistType
95 Notifier.Instance.notifyOnVideoUnblacklist(video)
97 if (videoBlacklistType === VideoBlockType.AUTO_BEFORE_PUBLISHED) {
98 Notifier.Instance.notifyOnVideoPublishedAfterRemovedFromAutoBlacklist(video)
100 // Delete on object so new video notifications will send
101 delete video.VideoBlacklist
102 Notifier.Instance.notifyOnNewVideoIfNeeded(video)
106 // ---------------------------------------------------------------------------
109 autoBlacklistVideoIfNeeded,
114 // ---------------------------------------------------------------------------
116 function autoBlacklistNeeded (parameters: {
117 video: MVideoWithBlacklistLight
122 const { user, video, isRemote, isNew } = parameters
124 // Already blacklisted
125 if (video.VideoBlacklist) return false
126 if (!CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED || !user) return false
127 if (isRemote || isNew === false) return false
129 if (user.hasRight(UserRight.MANAGE_VIDEO_BLOCKS) || user.hasAdminFlag(UserAdminFlag.BYPASS_VIDEO_AUTO_BLOCK)) return false