1 import { Transaction } from 'sequelize'
2 import { afterCommitIfTransaction } from '@server/helpers/database-utils'
3 import { sequelizeTypescript } from '@server/initializers/database'
10 MVideoWithBlacklistLight
11 } from '@server/types/models'
12 import { UserRight, VideoBlacklistCreate, VideoBlacklistType } from '../../shared/models'
13 import { UserAdminFlag } from '../../shared/models/users/user-flag.model'
14 import { logger } from '../helpers/logger'
15 import { CONFIG } from '../initializers/config'
16 import { VideoBlacklistModel } from '../models/video/video-blacklist'
17 import { sendDeleteVideo } from './activitypub/send'
18 import { federateVideoIfNeeded } from './activitypub/videos'
19 import { LiveManager } from './live-manager'
20 import { Notifier } from './notifier'
21 import { Hooks } from './plugins/hooks'
23 async function autoBlacklistVideoIfNeeded (parameters: {
24 video: MVideoWithBlacklistLight
29 transaction?: Transaction
31 const { video, user, isRemote, isNew, notify = true, transaction } = parameters
32 const doAutoBlacklist = await Hooks.wrapFun(
34 { video, user, isRemote, isNew },
35 'filter:video.auto-blacklist.result'
38 if (!doAutoBlacklist) return false
40 const videoBlacklistToCreate = {
43 reason: 'Auto-blacklisted. Moderator review required.',
44 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
46 const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate<MVideoBlacklistVideo>({
50 defaults: videoBlacklistToCreate,
53 video.VideoBlacklist = videoBlacklist
55 videoBlacklist.Video = video
58 afterCommitIfTransaction(transaction, () => {
59 Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist)
63 logger.info('Video %s auto-blacklisted.', video.uuid)
68 async function blacklistVideo (videoInstance: MVideoAccountLight, options: VideoBlacklistCreate) {
69 const blacklist: MVideoBlacklistVideo = await VideoBlacklistModel.create({
70 videoId: videoInstance.id,
71 unfederated: options.unfederate === true,
72 reason: options.reason,
73 type: VideoBlacklistType.MANUAL
76 blacklist.Video = videoInstance
78 if (options.unfederate === true) {
79 await sendDeleteVideo(videoInstance, undefined)
82 if (videoInstance.isLive) {
83 LiveManager.Instance.stopSessionOf(videoInstance.id)
86 Notifier.Instance.notifyOnVideoBlacklist(blacklist)
89 async function unblacklistVideo (videoBlacklist: MVideoBlacklist, video: MVideoFullLight) {
90 const videoBlacklistType = await sequelizeTypescript.transaction(async t => {
91 const unfederated = videoBlacklist.unfederated
92 const videoBlacklistType = videoBlacklist.type
94 await videoBlacklist.destroy({ transaction: t })
95 video.VideoBlacklist = undefined
97 // Re federate the video
98 if (unfederated === true) {
99 await federateVideoIfNeeded(video, true, t)
102 return videoBlacklistType
105 Notifier.Instance.notifyOnVideoUnblacklist(video)
107 if (videoBlacklistType === VideoBlacklistType.AUTO_BEFORE_PUBLISHED) {
108 Notifier.Instance.notifyOnVideoPublishedAfterRemovedFromAutoBlacklist(video)
110 // Delete on object so new video notifications will send
111 delete video.VideoBlacklist
112 Notifier.Instance.notifyOnNewVideoIfNeeded(video)
116 // ---------------------------------------------------------------------------
119 autoBlacklistVideoIfNeeded,
124 // ---------------------------------------------------------------------------
126 function autoBlacklistNeeded (parameters: {
127 video: MVideoWithBlacklistLight
132 const { user, video, isRemote, isNew } = parameters
134 // Already blacklisted
135 if (video.VideoBlacklist) return false
136 if (!CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED || !user) return false
137 if (isRemote || isNew === false) return false
139 if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)) return false