]>
Commit | Line | Data |
---|---|---|
1 | import { Transaction } from 'sequelize' | |
2 | import { CONFIG } from '../initializers/config' | |
3 | import { UserRight, VideoBlacklistType } from '../../shared/models' | |
4 | import { VideoBlacklistModel } from '../models/video/video-blacklist' | |
5 | import { UserModel } from '../models/account/user' | |
6 | import { VideoModel } from '../models/video/video' | |
7 | import { logger } from '../helpers/logger' | |
8 | import { UserAdminFlag } from '../../shared/models/users/user-flag.model' | |
9 | import { Hooks } from './plugins/hooks' | |
10 | import { Notifier } from './notifier' | |
11 | ||
12 | async function autoBlacklistVideoIfNeeded (parameters: { | |
13 | video: VideoModel, | |
14 | user?: UserModel, | |
15 | isRemote: boolean, | |
16 | isNew: boolean, | |
17 | notify?: boolean, | |
18 | transaction?: Transaction | |
19 | }) { | |
20 | const { video, user, isRemote, isNew, notify = true, transaction } = parameters | |
21 | const doAutoBlacklist = await Hooks.wrapPromiseFun( | |
22 | autoBlacklistNeeded, | |
23 | { video, user, isRemote, isNew }, | |
24 | 'filter:video.auto-blacklist.result' | |
25 | ) | |
26 | ||
27 | if (!doAutoBlacklist) return false | |
28 | ||
29 | const videoBlacklistToCreate = { | |
30 | videoId: video.id, | |
31 | unfederated: true, | |
32 | reason: 'Auto-blacklisted. Moderator review required.', | |
33 | type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED | |
34 | } | |
35 | const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate({ | |
36 | where: { | |
37 | videoId: video.id | |
38 | }, | |
39 | defaults: videoBlacklistToCreate, | |
40 | transaction | |
41 | }) | |
42 | video.VideoBlacklist = videoBlacklist | |
43 | ||
44 | if (notify) Notifier.Instance.notifyOnVideoAutoBlacklist(video) | |
45 | ||
46 | logger.info('Video %s auto-blacklisted.', video.uuid) | |
47 | ||
48 | return true | |
49 | } | |
50 | ||
51 | async function autoBlacklistNeeded (parameters: { | |
52 | video: VideoModel, | |
53 | isRemote: boolean, | |
54 | isNew: boolean, | |
55 | user?: UserModel | |
56 | }) { | |
57 | const { user, video, isRemote, isNew } = parameters | |
58 | ||
59 | // Already blacklisted | |
60 | if (video.VideoBlacklist) return false | |
61 | if (!CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED || !user) return false | |
62 | if (isRemote || isNew === false) return false | |
63 | ||
64 | if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST)) return false | |
65 | ||
66 | return true | |
67 | } | |
68 | ||
69 | // --------------------------------------------------------------------------- | |
70 | ||
71 | export { | |
72 | autoBlacklistVideoIfNeeded | |
73 | } |