]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/video-blacklist.ts
Force live stream termination
[github/Chocobozzz/PeerTube.git] / server / lib / video-blacklist.ts
1 import { Transaction } from 'sequelize'
2 import { afterCommitIfTransaction } from '@server/helpers/database-utils'
3 import { sequelizeTypescript } from '@server/initializers/database'
4 import {
5 MUser,
6 MVideoAccountLight,
7 MVideoBlacklist,
8 MVideoBlacklistVideo,
9 MVideoFullLight,
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'
22
23 const lTags = loggerTagsFactory('blacklist')
24
25 async function autoBlacklistVideoIfNeeded (parameters: {
26 video: MVideoWithBlacklistLight
27 user?: MUser
28 isRemote: boolean
29 isNew: boolean
30 notify?: boolean
31 transaction?: Transaction
32 }) {
33 const { video, user, isRemote, isNew, notify = true, transaction } = parameters
34 const doAutoBlacklist = await Hooks.wrapFun(
35 autoBlacklistNeeded,
36 { video, user, isRemote, isNew },
37 'filter:video.auto-blacklist.result'
38 )
39
40 if (!doAutoBlacklist) return false
41
42 const videoBlacklistToCreate = {
43 videoId: video.id,
44 unfederated: true,
45 reason: 'Auto-blacklisted. Moderator review required.',
46 type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
47 }
48 const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate<MVideoBlacklistVideo>({
49 where: {
50 videoId: video.id
51 },
52 defaults: videoBlacklistToCreate,
53 transaction
54 })
55 video.VideoBlacklist = videoBlacklist
56
57 videoBlacklist.Video = video
58
59 if (notify) {
60 afterCommitIfTransaction(transaction, () => {
61 Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist)
62 })
63 }
64
65 logger.info('Video %s auto-blacklisted.', video.uuid, lTags(video.uuid))
66
67 return true
68 }
69
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
76 })
77 blacklist.Video = videoInstance
78
79 if (options.unfederate === true) {
80 await sendDeleteVideo(videoInstance, undefined)
81 }
82
83 if (videoInstance.isLive) {
84 LiveManager.Instance.stopSessionOf(videoInstance.uuid, LiveVideoError.BLACKLISTED)
85 }
86
87 Notifier.Instance.notifyOnVideoBlacklist(blacklist)
88 }
89
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
94
95 await videoBlacklist.destroy({ transaction: t })
96 video.VideoBlacklist = undefined
97
98 // Re federate the video
99 if (unfederated === true) {
100 await federateVideoIfNeeded(video, true, t)
101 }
102
103 return videoBlacklistType
104 })
105
106 Notifier.Instance.notifyOnVideoUnblacklist(video)
107
108 if (videoBlacklistType === VideoBlacklistType.AUTO_BEFORE_PUBLISHED) {
109 Notifier.Instance.notifyOnVideoPublishedAfterRemovedFromAutoBlacklist(video)
110
111 // Delete on object so new video notifications will send
112 delete video.VideoBlacklist
113 Notifier.Instance.notifyOnNewVideoIfNeeded(video)
114 }
115 }
116
117 // ---------------------------------------------------------------------------
118
119 export {
120 autoBlacklistVideoIfNeeded,
121 blacklistVideo,
122 unblacklistVideo
123 }
124
125 // ---------------------------------------------------------------------------
126
127 function autoBlacklistNeeded (parameters: {
128 video: MVideoWithBlacklistLight
129 isRemote: boolean
130 isNew: boolean
131 user?: MUser
132 }) {
133 const { user, video, isRemote, isNew } = parameters
134
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
139
140 if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)) return false
141
142 return true
143 }