]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/video-blacklist.ts
Merge remote-tracking branch 'weblate/develop' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / video-blacklist.ts
CommitLineData
b4055e1c 1import { Transaction } from 'sequelize'
f0484f7a 2import { afterCommitIfTransaction } from '@server/helpers/database-utils'
80fdaf06
C
3import { sequelizeTypescript } from '@server/initializers/database'
4import {
5 MUser,
6 MVideoAccountLight,
7 MVideoBlacklist,
8 MVideoBlacklistVideo,
9 MVideoFullLight,
10 MVideoWithBlacklistLight
26d6bf65 11} from '@server/types/models'
3487330d 12import { UserRight, VideoBlacklistCreate, VideoBlacklistType } from '../../shared/models'
80fdaf06 13import { UserAdminFlag } from '../../shared/models/users/user-flag.model'
452b3bea 14import { logger, loggerTagsFactory } from '../helpers/logger'
6dd9de95 15import { CONFIG } from '../initializers/config'
7ccddd7b 16import { VideoBlacklistModel } from '../models/video/video-blacklist'
80fdaf06
C
17import { sendDeleteVideo } from './activitypub/send'
18import { federateVideoIfNeeded } from './activitypub/videos'
f0484f7a 19import { LiveManager } from './live-manager'
5b77537c 20import { Notifier } from './notifier'
80fdaf06 21import { Hooks } from './plugins/hooks'
7ccddd7b 22
452b3bea
C
23const lTags = loggerTagsFactory('blacklist')
24
6691c522 25async function autoBlacklistVideoIfNeeded (parameters: {
a1587156
C
26 video: MVideoWithBlacklistLight
27 user?: MUser
28 isRemote: boolean
29 isNew: boolean
30 notify?: boolean
6691c522
C
31 transaction?: Transaction
32}) {
5b77537c 33 const { video, user, isRemote, isNew, notify = true, transaction } = parameters
a1587156 34 const doAutoBlacklist = await Hooks.wrapFun(
89cd1275 35 autoBlacklistNeeded,
6691c522 36 { video, user, isRemote, isNew },
b4055e1c
C
37 'filter:video.auto-blacklist.result'
38 )
7ccddd7b 39
b4055e1c 40 if (!doAutoBlacklist) return false
7ccddd7b 41
7ccddd7b
JM
42 const videoBlacklistToCreate = {
43 videoId: video.id,
44 unfederated: true,
45 reason: 'Auto-blacklisted. Moderator review required.',
3487330d 46 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
7ccddd7b 47 }
8424c402 48 const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate<MVideoBlacklistVideo>({
6691c522
C
49 where: {
50 videoId: video.id
51 },
52 defaults: videoBlacklistToCreate,
53 transaction
54 })
6691c522 55 video.VideoBlacklist = videoBlacklist
1eddc9a7 56
8424c402
C
57 videoBlacklist.Video = video
58
f0484f7a
C
59 if (notify) {
60 afterCommitIfTransaction(transaction, () => {
61 Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist)
62 })
63 }
5b77537c 64
452b3bea 65 logger.info('Video %s auto-blacklisted.', video.uuid, lTags(video.uuid))
7ccddd7b
JM
66
67 return true
68}
69
80fdaf06
C
70async 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,
3487330d 75 type: VideoBlacklistType.MANUAL
80fdaf06
C
76 }
77 )
78 blacklist.Video = videoInstance
79
80 if (options.unfederate === true) {
81 await sendDeleteVideo(videoInstance, undefined)
82 }
83
a5cf76af
C
84 if (videoInstance.isLive) {
85 LiveManager.Instance.stopSessionOf(videoInstance.id)
86 }
87
80fdaf06
C
88 Notifier.Instance.notifyOnVideoBlacklist(blacklist)
89}
90
91async function unblacklistVideo (videoBlacklist: MVideoBlacklist, video: MVideoFullLight) {
92 const videoBlacklistType = await sequelizeTypescript.transaction(async t => {
93 const unfederated = videoBlacklist.unfederated
94 const videoBlacklistType = videoBlacklist.type
95
96 await videoBlacklist.destroy({ transaction: t })
97 video.VideoBlacklist = undefined
98
99 // Re federate the video
100 if (unfederated === true) {
101 await federateVideoIfNeeded(video, true, t)
102 }
103
104 return videoBlacklistType
105 })
106
107 Notifier.Instance.notifyOnVideoUnblacklist(video)
108
3487330d 109 if (videoBlacklistType === VideoBlacklistType.AUTO_BEFORE_PUBLISHED) {
80fdaf06
C
110 Notifier.Instance.notifyOnVideoPublishedAfterRemovedFromAutoBlacklist(video)
111
112 // Delete on object so new video notifications will send
113 delete video.VideoBlacklist
114 Notifier.Instance.notifyOnNewVideoIfNeeded(video)
115 }
116}
117
118// ---------------------------------------------------------------------------
119
120export {
121 autoBlacklistVideoIfNeeded,
122 blacklistVideo,
123 unblacklistVideo
124}
125
126// ---------------------------------------------------------------------------
127
a1587156
C
128function autoBlacklistNeeded (parameters: {
129 video: MVideoWithBlacklistLight
130 isRemote: boolean
131 isNew: boolean
453e83ea 132 user?: MUser
6691c522
C
133}) {
134 const { user, video, isRemote, isNew } = parameters
b4055e1c 135
6691c522
C
136 // Already blacklisted
137 if (video.VideoBlacklist) return false
b4055e1c 138 if (!CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED || !user) return false
7c421bb1 139 if (isRemote || isNew === false) return false
b4055e1c 140
3487330d 141 if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)) return false
b4055e1c
C
142
143 return true
144}