1 import { Transaction } from 'sequelize'
2 import { sequelizeTypescript } from '@server/initializers/database'
9 MVideoWithBlacklistLight
10 } from '@server/types/models'
11 import { UserRight, VideoBlacklistCreate, VideoBlacklistType } 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'
20 import { LiveManager } from './live-manager'
22 async function autoBlacklistVideoIfNeeded (parameters: {
23 video: MVideoWithBlacklistLight
28 transaction?: Transaction
30 const { video, user, isRemote, isNew, notify = true, transaction } = parameters
31 const doAutoBlacklist = await Hooks.wrapFun(
33 { video, user, isRemote, isNew },
34 'filter:video.auto-blacklist.result'
37 if (!doAutoBlacklist) return false
39 const videoBlacklistToCreate = {
42 reason: 'Auto-blacklisted. Moderator review required.',
43 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
45 const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate<MVideoBlacklistVideo>({
49 defaults: videoBlacklistToCreate,
52 video.VideoBlacklist = videoBlacklist
54 videoBlacklist.Video = video
56 if (notify) Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist)
58 logger.info('Video %s auto-blacklisted.', video.uuid)
63 async function blacklistVideo (videoInstance: MVideoAccountLight, options: VideoBlacklistCreate) {
64 const blacklist: MVideoBlacklistVideo = await VideoBlacklistModel.create({
65 videoId: videoInstance.id,
66 unfederated: options.unfederate === true,
67 reason: options.reason,
68 type: VideoBlacklistType.MANUAL
71 blacklist.Video = videoInstance
73 if (options.unfederate === true) {
74 await sendDeleteVideo(videoInstance, undefined)
77 if (videoInstance.isLive) {
78 LiveManager.Instance.stopSessionOf(videoInstance.id)
81 Notifier.Instance.notifyOnVideoBlacklist(blacklist)
84 async function unblacklistVideo (videoBlacklist: MVideoBlacklist, video: MVideoFullLight) {
85 const videoBlacklistType = await sequelizeTypescript.transaction(async t => {
86 const unfederated = videoBlacklist.unfederated
87 const videoBlacklistType = videoBlacklist.type
89 await videoBlacklist.destroy({ transaction: t })
90 video.VideoBlacklist = undefined
92 // Re federate the video
93 if (unfederated === true) {
94 await federateVideoIfNeeded(video, true, t)
97 return videoBlacklistType
100 Notifier.Instance.notifyOnVideoUnblacklist(video)
102 if (videoBlacklistType === VideoBlacklistType.AUTO_BEFORE_PUBLISHED) {
103 Notifier.Instance.notifyOnVideoPublishedAfterRemovedFromAutoBlacklist(video)
105 // Delete on object so new video notifications will send
106 delete video.VideoBlacklist
107 Notifier.Instance.notifyOnNewVideoIfNeeded(video)
111 // ---------------------------------------------------------------------------
114 autoBlacklistVideoIfNeeded,
119 // ---------------------------------------------------------------------------
121 function autoBlacklistNeeded (parameters: {
122 video: MVideoWithBlacklistLight
127 const { user, video, isRemote, isNew } = parameters
129 // Already blacklisted
130 if (video.VideoBlacklist) return false
131 if (!CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED || !user) return false
132 if (isRemote || isNew === false) return false
134 if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)) return false