]>
Commit | Line | Data |
---|---|---|
1 | import { Transaction } from 'sequelize' | |
2 | import { sequelizeTypescript } from '@server/initializers/database' | |
3 | import { | |
4 | MUser, | |
5 | MVideoAccountLight, | |
6 | MVideoBlacklist, | |
7 | MVideoBlacklistVideo, | |
8 | MVideoFullLight, | |
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' | |
21 | ||
22 | async function autoBlacklistVideoIfNeeded (parameters: { | |
23 | video: MVideoWithBlacklistLight | |
24 | user?: MUser | |
25 | isRemote: boolean | |
26 | isNew: boolean | |
27 | notify?: boolean | |
28 | transaction?: Transaction | |
29 | }) { | |
30 | const { video, user, isRemote, isNew, notify = true, transaction } = parameters | |
31 | const doAutoBlacklist = await Hooks.wrapFun( | |
32 | autoBlacklistNeeded, | |
33 | { video, user, isRemote, isNew }, | |
34 | 'filter:video.auto-blacklist.result' | |
35 | ) | |
36 | ||
37 | if (!doAutoBlacklist) return false | |
38 | ||
39 | const videoBlacklistToCreate = { | |
40 | videoId: video.id, | |
41 | unfederated: true, | |
42 | reason: 'Auto-blacklisted. Moderator review required.', | |
43 | type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED | |
44 | } | |
45 | const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate<MVideoBlacklistVideo>({ | |
46 | where: { | |
47 | videoId: video.id | |
48 | }, | |
49 | defaults: videoBlacklistToCreate, | |
50 | transaction | |
51 | }) | |
52 | video.VideoBlacklist = videoBlacklist | |
53 | ||
54 | videoBlacklist.Video = video | |
55 | ||
56 | if (notify) Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist) | |
57 | ||
58 | logger.info('Video %s auto-blacklisted.', video.uuid) | |
59 | ||
60 | return true | |
61 | } | |
62 | ||
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 | |
69 | } | |
70 | ) | |
71 | blacklist.Video = videoInstance | |
72 | ||
73 | if (options.unfederate === true) { | |
74 | await sendDeleteVideo(videoInstance, undefined) | |
75 | } | |
76 | ||
77 | if (videoInstance.isLive) { | |
78 | LiveManager.Instance.stopSessionOf(videoInstance.id) | |
79 | } | |
80 | ||
81 | Notifier.Instance.notifyOnVideoBlacklist(blacklist) | |
82 | } | |
83 | ||
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 | |
88 | ||
89 | await videoBlacklist.destroy({ transaction: t }) | |
90 | video.VideoBlacklist = undefined | |
91 | ||
92 | // Re federate the video | |
93 | if (unfederated === true) { | |
94 | await federateVideoIfNeeded(video, true, t) | |
95 | } | |
96 | ||
97 | return videoBlacklistType | |
98 | }) | |
99 | ||
100 | Notifier.Instance.notifyOnVideoUnblacklist(video) | |
101 | ||
102 | if (videoBlacklistType === VideoBlacklistType.AUTO_BEFORE_PUBLISHED) { | |
103 | Notifier.Instance.notifyOnVideoPublishedAfterRemovedFromAutoBlacklist(video) | |
104 | ||
105 | // Delete on object so new video notifications will send | |
106 | delete video.VideoBlacklist | |
107 | Notifier.Instance.notifyOnNewVideoIfNeeded(video) | |
108 | } | |
109 | } | |
110 | ||
111 | // --------------------------------------------------------------------------- | |
112 | ||
113 | export { | |
114 | autoBlacklistVideoIfNeeded, | |
115 | blacklistVideo, | |
116 | unblacklistVideo | |
117 | } | |
118 | ||
119 | // --------------------------------------------------------------------------- | |
120 | ||
121 | function autoBlacklistNeeded (parameters: { | |
122 | video: MVideoWithBlacklistLight | |
123 | isRemote: boolean | |
124 | isNew: boolean | |
125 | user?: MUser | |
126 | }) { | |
127 | const { user, video, isRemote, isNew } = parameters | |
128 | ||
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 | |
133 | ||
134 | if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)) return false | |
135 | ||
136 | return true | |
137 | } |