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