]>
Commit | Line | Data |
---|---|---|
b4055e1c | 1 | import { Transaction } from 'sequelize' |
80fdaf06 C |
2 | import { sequelizeTypescript } from '@server/initializers/database' |
3 | import { | |
4 | MUser, | |
5 | MVideoAccountLight, | |
6 | MVideoBlacklist, | |
7 | MVideoBlacklistVideo, | |
8 | MVideoFullLight, | |
9 | MVideoWithBlacklistLight | |
10 | } from '@server/typings/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' | |
6dd9de95 | 14 | import { CONFIG } from '../initializers/config' |
7ccddd7b | 15 | import { VideoBlacklistModel } from '../models/video/video-blacklist' |
80fdaf06 C |
16 | import { sendDeleteVideo } from './activitypub/send' |
17 | import { federateVideoIfNeeded } from './activitypub/videos' | |
5b77537c | 18 | import { Notifier } from './notifier' |
80fdaf06 | 19 | import { Hooks } from './plugins/hooks' |
7ccddd7b | 20 | |
6691c522 | 21 | async function autoBlacklistVideoIfNeeded (parameters: { |
a1587156 C |
22 | video: MVideoWithBlacklistLight |
23 | user?: MUser | |
24 | isRemote: boolean | |
25 | isNew: boolean | |
26 | notify?: boolean | |
6691c522 C |
27 | transaction?: Transaction |
28 | }) { | |
5b77537c | 29 | const { video, user, isRemote, isNew, notify = true, transaction } = parameters |
a1587156 | 30 | const doAutoBlacklist = await Hooks.wrapFun( |
89cd1275 | 31 | autoBlacklistNeeded, |
6691c522 | 32 | { video, user, isRemote, isNew }, |
b4055e1c C |
33 | 'filter:video.auto-blacklist.result' |
34 | ) | |
7ccddd7b | 35 | |
b4055e1c | 36 | if (!doAutoBlacklist) return false |
7ccddd7b | 37 | |
7ccddd7b JM |
38 | const videoBlacklistToCreate = { |
39 | videoId: video.id, | |
40 | unfederated: true, | |
41 | reason: 'Auto-blacklisted. Moderator review required.', | |
42 | type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED | |
43 | } | |
8424c402 | 44 | const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate<MVideoBlacklistVideo>({ |
6691c522 C |
45 | where: { |
46 | videoId: video.id | |
47 | }, | |
48 | defaults: videoBlacklistToCreate, | |
49 | transaction | |
50 | }) | |
6691c522 | 51 | video.VideoBlacklist = videoBlacklist |
1eddc9a7 | 52 | |
8424c402 C |
53 | videoBlacklist.Video = video |
54 | ||
55 | if (notify) Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist) | |
5b77537c | 56 | |
7ccddd7b JM |
57 | logger.info('Video %s auto-blacklisted.', video.uuid) |
58 | ||
59 | return true | |
60 | } | |
61 | ||
80fdaf06 C |
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 | ||
a1587156 C |
116 | function autoBlacklistNeeded (parameters: { |
117 | video: MVideoWithBlacklistLight | |
118 | isRemote: boolean | |
119 | isNew: boolean | |
453e83ea | 120 | user?: MUser |
6691c522 C |
121 | }) { |
122 | const { user, video, isRemote, isNew } = parameters | |
b4055e1c | 123 | |
6691c522 C |
124 | // Already blacklisted |
125 | if (video.VideoBlacklist) return false | |
b4055e1c | 126 | if (!CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED || !user) return false |
7c421bb1 | 127 | if (isRemote || isNew === false) return false |
b4055e1c C |
128 | |
129 | if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST)) return false | |
130 | ||
131 | return true | |
132 | } |