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