1 import { Transaction } from 'sequelize'
2 import { CONFIG } from '../initializers/config'
3 import { UserRight, VideoBlacklistType } from '../../shared/models'
4 import { VideoBlacklistModel } from '../models/video/video-blacklist'
5 import { logger } from '../helpers/logger'
6 import { UserAdminFlag } from '../../shared/models/users/user-flag.model'
7 import { Hooks } from './plugins/hooks'
8 import { Notifier } from './notifier'
9 import { MUser, MVideoBlacklist, MVideoWithBlacklistLight } from '@server/typings/models'
11 async function autoBlacklistVideoIfNeeded (parameters: {
12 video: MVideoWithBlacklistLight,
17 transaction?: Transaction
19 const { video, user, isRemote, isNew, notify = true, transaction } = parameters
20 const doAutoBlacklist = await Hooks.wrapPromiseFun(
22 { video, user, isRemote, isNew },
23 'filter:video.auto-blacklist.result'
26 if (!doAutoBlacklist) return false
28 const videoBlacklistToCreate = {
31 reason: 'Auto-blacklisted. Moderator review required.',
32 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
34 const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate<MVideoBlacklist>({
38 defaults: videoBlacklistToCreate,
41 video.VideoBlacklist = videoBlacklist
43 if (notify) Notifier.Instance.notifyOnVideoAutoBlacklist(video)
45 logger.info('Video %s auto-blacklisted.', video.uuid)
50 async function autoBlacklistNeeded (parameters: {
51 video: MVideoWithBlacklistLight,
56 const { user, video, isRemote, isNew } = parameters
58 // Already blacklisted
59 if (video.VideoBlacklist) return false
60 if (!CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED || !user) return false
61 if (isRemote || isNew === false) return false
63 if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST)) return false
68 // ---------------------------------------------------------------------------
71 autoBlacklistVideoIfNeeded