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