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 { LiveVideoError, UserRight, VideoBlacklistCreate, VideoBlacklistType } from '../../shared/models'
13 import { UserAdminFlag } from '../../shared/models/users/user-flag.model'
14 import { logger, loggerTagsFactory } 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/live-manager'
20 import { Notifier } from './notifier'
21 import { Hooks } from './plugins/hooks'
23 const lTags = loggerTagsFactory('blacklist')
25 async function autoBlacklistVideoIfNeeded (parameters: {
26 video: MVideoWithBlacklistLight
31 transaction?: Transaction
33 const { video, user, isRemote, isNew, notify = true, transaction } = parameters
34 const doAutoBlacklist = await Hooks.wrapFun(
36 { video, user, isRemote, isNew },
37 'filter:video.auto-blacklist.result'
40 if (!doAutoBlacklist) return false
42 const videoBlacklistToCreate = {
45 reason: 'Auto-blacklisted. Moderator review required.',
46 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
48 const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate<MVideoBlacklistVideo>({
52 defaults: videoBlacklistToCreate,
55 video.VideoBlacklist = videoBlacklist
57 videoBlacklist.Video = video
60 afterCommitIfTransaction(transaction, () => {
61 Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist)
65 logger.info('Video %s auto-blacklisted.', video.uuid, lTags(video.uuid))
70 async function blacklistVideo (videoInstance: MVideoAccountLight, options: VideoBlacklistCreate) {
71 const blacklist: MVideoBlacklistVideo = await VideoBlacklistModel.create({
72 videoId: videoInstance.id,
73 unfederated: options.unfederate === true,
74 reason: options.reason,
75 type: VideoBlacklistType.MANUAL
77 blacklist.Video = videoInstance
79 if (options.unfederate === true) {
80 await sendDeleteVideo(videoInstance, undefined)
83 if (videoInstance.isLive) {
84 LiveManager.Instance.stopSessionOf(videoInstance.id, LiveVideoError.BLACKLISTED)
87 Notifier.Instance.notifyOnVideoBlacklist(blacklist)
90 async function unblacklistVideo (videoBlacklist: MVideoBlacklist, video: MVideoFullLight) {
91 const videoBlacklistType = await sequelizeTypescript.transaction(async t => {
92 const unfederated = videoBlacklist.unfederated
93 const videoBlacklistType = videoBlacklist.type
95 await videoBlacklist.destroy({ transaction: t })
96 video.VideoBlacklist = undefined
98 // Re federate the video
99 if (unfederated === true) {
100 await federateVideoIfNeeded(video, true, t)
103 return videoBlacklistType
106 Notifier.Instance.notifyOnVideoUnblacklist(video)
108 if (videoBlacklistType === VideoBlacklistType.AUTO_BEFORE_PUBLISHED) {
109 Notifier.Instance.notifyOnVideoPublishedAfterRemovedFromAutoBlacklist(video)
111 // Delete on object so new video notifications will send
112 delete video.VideoBlacklist
113 Notifier.Instance.notifyOnNewVideoIfNeeded(video)
117 // ---------------------------------------------------------------------------
120 autoBlacklistVideoIfNeeded,
125 // ---------------------------------------------------------------------------
127 function autoBlacklistNeeded (parameters: {
128 video: MVideoWithBlacklistLight
133 const { user, video, isRemote, isNew } = parameters
135 // Already blacklisted
136 if (video.VideoBlacklist) return false
137 if (!CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED || !user) return false
138 if (isRemote || isNew === false) return false
140 if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)) return false