]>
Commit | Line | Data |
---|---|---|
668b7f09 | 1 | import { AccountModel } from '@server/models/account/account' |
696d83fd C |
2 | import { getServerActor } from '@server/models/application/application' |
3 | import { ServerBlocklistModel } from '@server/models/server/server-blocklist' | |
4 | import { | |
5 | MUser, | |
6 | MUserAccount, | |
7 | MUserDefault, | |
8 | MUserNotifSettingAccount, | |
9 | MUserWithNotificationSetting, | |
10 | UserNotificationModelForApi | |
26d6bf65 | 11 | } from '@server/types/models/user' |
d95d1559 | 12 | import { MVideoBlacklistLightVideo, MVideoBlacklistVideo } from '@server/types/models/video/video-blacklist' |
26d6bf65 | 13 | import { MVideoImportVideo } from '@server/types/models/video/video-import' |
edbc9325 | 14 | import { UserAbuse } from '@shared/models' |
cef534ed | 15 | import { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users' |
d95d1559 | 16 | import { VideoPrivacy, VideoState } from '../../shared/models/videos' |
cef534ed | 17 | import { logger } from '../helpers/logger' |
6dd9de95 | 18 | import { CONFIG } from '../initializers/config' |
dc133480 | 19 | import { AccountBlocklistModel } from '../models/account/account-blocklist' |
696d83fd C |
20 | import { UserModel } from '../models/account/user' |
21 | import { UserNotificationModel } from '../models/account/user-notification' | |
32a18cbf | 22 | import { MAbuseFull, MAbuseMessage, MAccountServer, MActorFollowFull, MApplication, MPlugin } from '../types/models' |
d95d1559 | 23 | import { MCommentOwnerVideo, MVideoAccountLight, MVideoFullLight } from '../types/models/video' |
696d83fd C |
24 | import { isBlockedByServerOrAccount } from './blocklist' |
25 | import { Emailer } from './emailer' | |
26 | import { PeerTubeSocket } from './peertube-socket' | |
cef534ed C |
27 | |
28 | class Notifier { | |
29 | ||
30 | private static instance: Notifier | |
31 | ||
a1587156 C |
32 | private constructor () { |
33 | } | |
cef534ed | 34 | |
453e83ea | 35 | notifyOnNewVideoIfNeeded (video: MVideoAccountLight): void { |
7ccddd7b | 36 | // Only notify on public and published videos which are not blacklisted |
5b77537c | 37 | if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED || video.isBlacklisted()) return |
cef534ed C |
38 | |
39 | this.notifySubscribersOfNewVideo(video) | |
a1587156 | 40 | .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err })) |
cef534ed C |
41 | } |
42 | ||
453e83ea | 43 | notifyOnVideoPublishedAfterTranscoding (video: MVideoFullLight): void { |
7ccddd7b JM |
44 | // don't notify if didn't wait for transcoding or video is still blacklisted/waiting for scheduled update |
45 | if (!video.waitTranscoding || video.VideoBlacklist || video.ScheduleVideoUpdate) return | |
dc133480 C |
46 | |
47 | this.notifyOwnedVideoHasBeenPublished(video) | |
7ccddd7b JM |
48 | .catch(err => logger.error('Cannot notify owner that its video %s has been published after transcoding.', video.url, { err })) |
49 | } | |
50 | ||
453e83ea | 51 | notifyOnVideoPublishedAfterScheduledUpdate (video: MVideoFullLight): void { |
7ccddd7b JM |
52 | // don't notify if video is still blacklisted or waiting for transcoding |
53 | if (video.VideoBlacklist || (video.waitTranscoding && video.state !== VideoState.PUBLISHED)) return | |
54 | ||
55 | this.notifyOwnedVideoHasBeenPublished(video) | |
56 | .catch(err => logger.error('Cannot notify owner that its video %s has been published after scheduled update.', video.url, { err })) | |
57 | } | |
58 | ||
453e83ea | 59 | notifyOnVideoPublishedAfterRemovedFromAutoBlacklist (video: MVideoFullLight): void { |
7ccddd7b JM |
60 | // don't notify if video is still waiting for transcoding or scheduled update |
61 | if (video.ScheduleVideoUpdate || (video.waitTranscoding && video.state !== VideoState.PUBLISHED)) return | |
62 | ||
63 | this.notifyOwnedVideoHasBeenPublished(video) | |
a1587156 C |
64 | .catch(err => { |
65 | logger.error('Cannot notify owner that its video %s has been published after removed from auto-blacklist.', video.url, { err }) | |
66 | }) | |
dc133480 C |
67 | } |
68 | ||
453e83ea | 69 | notifyOnNewComment (comment: MCommentOwnerVideo): void { |
cef534ed | 70 | this.notifyVideoOwnerOfNewComment(comment) |
f7cc67b4 C |
71 | .catch(err => logger.error('Cannot notify video owner of new comment %s.', comment.url, { err })) |
72 | ||
73 | this.notifyOfCommentMention(comment) | |
74 | .catch(err => logger.error('Cannot notify mentions of comment %s.', comment.url, { err })) | |
cef534ed C |
75 | } |
76 | ||
edbc9325 | 77 | notifyOnNewAbuse (parameters: { abuse: UserAbuse, abuseInstance: MAbuseFull, reporter: string }): void { |
d95d1559 C |
78 | this.notifyModeratorsOfNewAbuse(parameters) |
79 | .catch(err => logger.error('Cannot notify of new abuse %d.', parameters.abuseInstance.id, { err })) | |
cef534ed C |
80 | } |
81 | ||
8424c402 C |
82 | notifyOnVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo): void { |
83 | this.notifyModeratorsOfVideoAutoBlacklist(videoBlacklist) | |
a1587156 | 84 | .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', videoBlacklist.Video.url, { err })) |
7ccddd7b JM |
85 | } |
86 | ||
453e83ea | 87 | notifyOnVideoBlacklist (videoBlacklist: MVideoBlacklistVideo): void { |
cef534ed | 88 | this.notifyVideoOwnerOfBlacklist(videoBlacklist) |
a1587156 | 89 | .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err })) |
cef534ed C |
90 | } |
91 | ||
8424c402 | 92 | notifyOnVideoUnblacklist (video: MVideoFullLight): void { |
cef534ed | 93 | this.notifyVideoOwnerOfUnblacklist(video) |
7ccddd7b | 94 | .catch(err => logger.error('Cannot notify video owner of unblacklist of %s.', video.url, { err })) |
cef534ed C |
95 | } |
96 | ||
453e83ea | 97 | notifyOnFinishedVideoImport (videoImport: MVideoImportVideo, success: boolean): void { |
dc133480 | 98 | this.notifyOwnerVideoImportIsFinished(videoImport, success) |
a1587156 | 99 | .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err })) |
dc133480 C |
100 | } |
101 | ||
8424c402 | 102 | notifyOnNewUserRegistration (user: MUserDefault): void { |
f7cc67b4 C |
103 | this.notifyModeratorsOfNewUserRegistration(user) |
104 | .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err })) | |
105 | } | |
106 | ||
8424c402 | 107 | notifyOfNewUserFollow (actorFollow: MActorFollowFull): void { |
f7cc67b4 | 108 | this.notifyUserOfNewActorFollow(actorFollow) |
a1587156 C |
109 | .catch(err => { |
110 | logger.error( | |
111 | 'Cannot notify owner of channel %s of a new follow by %s.', | |
112 | actorFollow.ActorFollowing.VideoChannel.getDisplayName(), | |
113 | actorFollow.ActorFollower.Account.getDisplayName(), | |
114 | { err } | |
115 | ) | |
116 | }) | |
f7cc67b4 C |
117 | } |
118 | ||
8424c402 | 119 | notifyOfNewInstanceFollow (actorFollow: MActorFollowFull): void { |
883993c8 C |
120 | this.notifyAdminsOfNewInstanceFollow(actorFollow) |
121 | .catch(err => { | |
122 | logger.error('Cannot notify administrators of new follower %s.', actorFollow.ActorFollower.url, { err }) | |
123 | }) | |
124 | } | |
125 | ||
8424c402 C |
126 | notifyOfAutoInstanceFollowing (actorFollow: MActorFollowFull): void { |
127 | this.notifyAdminsOfAutoInstanceFollowing(actorFollow) | |
128 | .catch(err => { | |
129 | logger.error('Cannot notify administrators of auto instance following %s.', actorFollow.ActorFollowing.url, { err }) | |
130 | }) | |
131 | } | |
132 | ||
594d3e48 C |
133 | notifyOnAbuseStateChange (abuse: MAbuseFull): void { |
134 | this.notifyReporterOfAbuseStateChange(abuse) | |
135 | .catch(err => { | |
136 | logger.error('Cannot notify reporter of abuse %d state change.', abuse.id, { err }) | |
137 | }) | |
138 | } | |
139 | ||
d573926e | 140 | notifyOnAbuseMessage (abuse: MAbuseFull, message: MAbuseMessage): void { |
594d3e48 C |
141 | this.notifyOfNewAbuseMessage(abuse, message) |
142 | .catch(err => { | |
143 | logger.error('Cannot notify on new abuse %d message.', abuse.id, { err }) | |
144 | }) | |
145 | } | |
146 | ||
32a18cbf C |
147 | notifyOfNewPeerTubeVersion (application: MApplication, latestVersion: string) { |
148 | this.notifyAdminsOfNewPeerTubeVersion(application, latestVersion) | |
149 | .catch(err => { | |
150 | logger.error('Cannot notify on new PeerTubeb version %s.', latestVersion, { err }) | |
151 | }) | |
152 | } | |
153 | ||
154 | notifyOfNewPluginVersion (plugin: MPlugin) { | |
155 | this.notifyAdminsOfNewPluginVersion(plugin) | |
156 | .catch(err => { | |
157 | logger.error('Cannot notify on new plugin version %s.', plugin.name, { err }) | |
158 | }) | |
159 | } | |
160 | ||
453e83ea | 161 | private async notifySubscribersOfNewVideo (video: MVideoAccountLight) { |
cef534ed C |
162 | // List all followers that are users |
163 | const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId) | |
164 | ||
165 | logger.info('Notifying %d users of new video %s.', users.length, video.url) | |
166 | ||
8424c402 | 167 | function settingGetter (user: MUserWithNotificationSetting) { |
cef534ed C |
168 | return user.NotificationSetting.newVideoFromSubscription |
169 | } | |
170 | ||
8424c402 C |
171 | async function notificationCreator (user: MUserWithNotificationSetting) { |
172 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
cef534ed C |
173 | type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION, |
174 | userId: user.id, | |
175 | videoId: video.id | |
176 | }) | |
8424c402 | 177 | notification.Video = video |
cef534ed C |
178 | |
179 | return notification | |
180 | } | |
181 | ||
182 | function emailSender (emails: string[]) { | |
183 | return Emailer.Instance.addNewVideoFromSubscriberNotification(emails, video) | |
184 | } | |
185 | ||
186 | return this.notify({ users, settingGetter, notificationCreator, emailSender }) | |
187 | } | |
188 | ||
453e83ea | 189 | private async notifyVideoOwnerOfNewComment (comment: MCommentOwnerVideo) { |
f7cc67b4 C |
190 | if (comment.Video.isOwned() === false) return |
191 | ||
cef534ed C |
192 | const user = await UserModel.loadByVideoId(comment.videoId) |
193 | ||
194 | // Not our user or user comments its own video | |
195 | if (!user || comment.Account.userId === user.id) return | |
196 | ||
696d83fd | 197 | if (await this.isBlockedByServerOrUser(comment.Account, user)) return |
dc133480 | 198 | |
cef534ed C |
199 | logger.info('Notifying user %s of new comment %s.', user.username, comment.url) |
200 | ||
8424c402 | 201 | function settingGetter (user: MUserWithNotificationSetting) { |
cef534ed C |
202 | return user.NotificationSetting.newCommentOnMyVideo |
203 | } | |
204 | ||
8424c402 C |
205 | async function notificationCreator (user: MUserWithNotificationSetting) { |
206 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
cef534ed C |
207 | type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, |
208 | userId: user.id, | |
209 | commentId: comment.id | |
210 | }) | |
8424c402 | 211 | notification.Comment = comment |
cef534ed C |
212 | |
213 | return notification | |
214 | } | |
215 | ||
216 | function emailSender (emails: string[]) { | |
217 | return Emailer.Instance.addNewCommentOnMyVideoNotification(emails, comment) | |
218 | } | |
219 | ||
220 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
221 | } | |
222 | ||
453e83ea | 223 | private async notifyOfCommentMention (comment: MCommentOwnerVideo) { |
41d71344 C |
224 | const extractedUsernames = comment.extractMentions() |
225 | logger.debug( | |
226 | 'Extracted %d username from comment %s.', extractedUsernames.length, comment.url, | |
227 | { usernames: extractedUsernames, text: comment.text } | |
228 | ) | |
1f6d57e3 | 229 | |
41d71344 | 230 | let users = await UserModel.listByUsernames(extractedUsernames) |
f7cc67b4 C |
231 | |
232 | if (comment.Video.isOwned()) { | |
233 | const userException = await UserModel.loadByVideoId(comment.videoId) | |
234 | users = users.filter(u => u.id !== userException.id) | |
235 | } | |
236 | ||
237 | // Don't notify if I mentioned myself | |
238 | users = users.filter(u => u.Account.id !== comment.accountId) | |
239 | ||
cef534ed C |
240 | if (users.length === 0) return |
241 | ||
dddc8b1f C |
242 | const serverAccountId = (await getServerActor()).Account.id |
243 | const sourceAccounts = users.map(u => u.Account.id).concat([ serverAccountId ]) | |
244 | ||
245 | const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(sourceAccounts, comment.accountId) | |
246 | const instanceMutedHash = await ServerBlocklistModel.isServerMutedByMulti(sourceAccounts, comment.Account.Actor.serverId) | |
f7cc67b4 C |
247 | |
248 | logger.info('Notifying %d users of new comment %s.', users.length, comment.url) | |
249 | ||
8424c402 | 250 | function settingGetter (user: MUserNotifSettingAccount) { |
dddc8b1f C |
251 | const accountId = user.Account.id |
252 | if ( | |
253 | accountMutedHash[accountId] === true || instanceMutedHash[accountId] === true || | |
254 | accountMutedHash[serverAccountId] === true || instanceMutedHash[serverAccountId] === true | |
255 | ) { | |
256 | return UserNotificationSettingValue.NONE | |
257 | } | |
f7cc67b4 C |
258 | |
259 | return user.NotificationSetting.commentMention | |
260 | } | |
261 | ||
8424c402 C |
262 | async function notificationCreator (user: MUserNotifSettingAccount) { |
263 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
f7cc67b4 C |
264 | type: UserNotificationType.COMMENT_MENTION, |
265 | userId: user.id, | |
266 | commentId: comment.id | |
267 | }) | |
8424c402 | 268 | notification.Comment = comment |
f7cc67b4 C |
269 | |
270 | return notification | |
271 | } | |
272 | ||
273 | function emailSender (emails: string[]) { | |
274 | return Emailer.Instance.addNewCommentMentionNotification(emails, comment) | |
275 | } | |
276 | ||
277 | return this.notify({ users, settingGetter, notificationCreator, emailSender }) | |
278 | } | |
279 | ||
8424c402 | 280 | private async notifyUserOfNewActorFollow (actorFollow: MActorFollowFull) { |
f7cc67b4 C |
281 | if (actorFollow.ActorFollowing.isOwned() === false) return |
282 | ||
283 | // Account follows one of our account? | |
284 | let followType: 'account' | 'channel' = 'channel' | |
285 | let user = await UserModel.loadByChannelActorId(actorFollow.ActorFollowing.id) | |
286 | ||
287 | // Account follows one of our channel? | |
288 | if (!user) { | |
289 | user = await UserModel.loadByAccountActorId(actorFollow.ActorFollowing.id) | |
290 | followType = 'account' | |
291 | } | |
292 | ||
293 | if (!user) return | |
294 | ||
f7cc67b4 | 295 | const followerAccount = actorFollow.ActorFollower.Account |
dddc8b1f | 296 | const followerAccountWithActor = Object.assign(followerAccount, { Actor: actorFollow.ActorFollower }) |
f7cc67b4 | 297 | |
696d83fd | 298 | if (await this.isBlockedByServerOrUser(followerAccountWithActor, user)) return |
f7cc67b4 C |
299 | |
300 | logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName()) | |
301 | ||
8424c402 | 302 | function settingGetter (user: MUserWithNotificationSetting) { |
f7cc67b4 C |
303 | return user.NotificationSetting.newFollow |
304 | } | |
305 | ||
8424c402 C |
306 | async function notificationCreator (user: MUserWithNotificationSetting) { |
307 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
f7cc67b4 C |
308 | type: UserNotificationType.NEW_FOLLOW, |
309 | userId: user.id, | |
310 | actorFollowId: actorFollow.id | |
311 | }) | |
8424c402 | 312 | notification.ActorFollow = actorFollow |
f7cc67b4 C |
313 | |
314 | return notification | |
315 | } | |
316 | ||
317 | function emailSender (emails: string[]) { | |
318 | return Emailer.Instance.addNewFollowNotification(emails, actorFollow, followType) | |
319 | } | |
320 | ||
321 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
322 | } | |
323 | ||
8424c402 | 324 | private async notifyAdminsOfNewInstanceFollow (actorFollow: MActorFollowFull) { |
883993c8 C |
325 | const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW) |
326 | ||
696d83fd C |
327 | const follower = Object.assign(actorFollow.ActorFollower.Account, { Actor: actorFollow.ActorFollower }) |
328 | if (await this.isBlockedByServerOrUser(follower)) return | |
329 | ||
883993c8 C |
330 | logger.info('Notifying %d administrators of new instance follower: %s.', admins.length, actorFollow.ActorFollower.url) |
331 | ||
8424c402 | 332 | function settingGetter (user: MUserWithNotificationSetting) { |
883993c8 C |
333 | return user.NotificationSetting.newInstanceFollower |
334 | } | |
335 | ||
8424c402 C |
336 | async function notificationCreator (user: MUserWithNotificationSetting) { |
337 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
883993c8 C |
338 | type: UserNotificationType.NEW_INSTANCE_FOLLOWER, |
339 | userId: user.id, | |
340 | actorFollowId: actorFollow.id | |
341 | }) | |
8424c402 | 342 | notification.ActorFollow = actorFollow |
883993c8 C |
343 | |
344 | return notification | |
345 | } | |
346 | ||
347 | function emailSender (emails: string[]) { | |
348 | return Emailer.Instance.addNewInstanceFollowerNotification(emails, actorFollow) | |
349 | } | |
350 | ||
351 | return this.notify({ users: admins, settingGetter, notificationCreator, emailSender }) | |
352 | } | |
353 | ||
8424c402 C |
354 | private async notifyAdminsOfAutoInstanceFollowing (actorFollow: MActorFollowFull) { |
355 | const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW) | |
356 | ||
357 | logger.info('Notifying %d administrators of auto instance following: %s.', admins.length, actorFollow.ActorFollowing.url) | |
358 | ||
359 | function settingGetter (user: MUserWithNotificationSetting) { | |
360 | return user.NotificationSetting.autoInstanceFollowing | |
361 | } | |
362 | ||
363 | async function notificationCreator (user: MUserWithNotificationSetting) { | |
364 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
365 | type: UserNotificationType.AUTO_INSTANCE_FOLLOWING, | |
366 | userId: user.id, | |
367 | actorFollowId: actorFollow.id | |
368 | }) | |
369 | notification.ActorFollow = actorFollow | |
370 | ||
371 | return notification | |
372 | } | |
373 | ||
374 | function emailSender (emails: string[]) { | |
375 | return Emailer.Instance.addAutoInstanceFollowingNotification(emails, actorFollow) | |
376 | } | |
377 | ||
378 | return this.notify({ users: admins, settingGetter, notificationCreator, emailSender }) | |
379 | } | |
380 | ||
d95d1559 | 381 | private async notifyModeratorsOfNewAbuse (parameters: { |
edbc9325 | 382 | abuse: UserAbuse |
d95d1559 | 383 | abuseInstance: MAbuseFull |
df4c603d RK |
384 | reporter: string |
385 | }) { | |
d95d1559 C |
386 | const { abuse, abuseInstance } = parameters |
387 | ||
388 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_ABUSES) | |
f7cc67b4 C |
389 | if (moderators.length === 0) return |
390 | ||
594d3e48 | 391 | const url = this.getAbuseUrl(abuseInstance) |
d95d1559 C |
392 | |
393 | logger.info('Notifying %s user/moderators of new abuse %s.', moderators.length, url) | |
cef534ed | 394 | |
8424c402 | 395 | function settingGetter (user: MUserWithNotificationSetting) { |
4f32032f | 396 | return user.NotificationSetting.abuseAsModerator |
cef534ed C |
397 | } |
398 | ||
8424c402 | 399 | async function notificationCreator (user: MUserWithNotificationSetting) { |
d95d1559 | 400 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ |
310b5219 | 401 | type: UserNotificationType.NEW_ABUSE_FOR_MODERATORS, |
cef534ed | 402 | userId: user.id, |
d95d1559 | 403 | abuseId: abuse.id |
cef534ed | 404 | }) |
d95d1559 | 405 | notification.Abuse = abuseInstance |
cef534ed C |
406 | |
407 | return notification | |
408 | } | |
409 | ||
410 | function emailSender (emails: string[]) { | |
d95d1559 | 411 | return Emailer.Instance.addAbuseModeratorsNotification(emails, parameters) |
cef534ed C |
412 | } |
413 | ||
f7cc67b4 | 414 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) |
cef534ed C |
415 | } |
416 | ||
594d3e48 C |
417 | private async notifyReporterOfAbuseStateChange (abuse: MAbuseFull) { |
418 | // Only notify our users | |
419 | if (abuse.ReporterAccount.isOwned() !== true) return | |
420 | ||
421 | const url = this.getAbuseUrl(abuse) | |
422 | ||
423 | logger.info('Notifying reporter of abuse % of state change.', url) | |
424 | ||
425 | const reporter = await UserModel.loadByAccountActorId(abuse.ReporterAccount.actorId) | |
426 | ||
427 | function settingGetter (user: MUserWithNotificationSetting) { | |
428 | return user.NotificationSetting.abuseStateChange | |
429 | } | |
430 | ||
431 | async function notificationCreator (user: MUserWithNotificationSetting) { | |
432 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
433 | type: UserNotificationType.ABUSE_STATE_CHANGE, | |
434 | userId: user.id, | |
435 | abuseId: abuse.id | |
436 | }) | |
437 | notification.Abuse = abuse | |
438 | ||
439 | return notification | |
440 | } | |
441 | ||
442 | function emailSender (emails: string[]) { | |
443 | return Emailer.Instance.addAbuseStateChangeNotification(emails, abuse) | |
444 | } | |
445 | ||
446 | return this.notify({ users: [ reporter ], settingGetter, notificationCreator, emailSender }) | |
447 | } | |
448 | ||
449 | private async notifyOfNewAbuseMessage (abuse: MAbuseFull, message: MAbuseMessage) { | |
450 | const url = this.getAbuseUrl(abuse) | |
451 | logger.info('Notifying reporter and moderators of new abuse message on %s.', url) | |
452 | ||
d573926e C |
453 | const accountMessage = await AccountModel.load(message.accountId) |
454 | ||
594d3e48 C |
455 | function settingGetter (user: MUserWithNotificationSetting) { |
456 | return user.NotificationSetting.abuseNewMessage | |
457 | } | |
458 | ||
459 | async function notificationCreator (user: MUserWithNotificationSetting) { | |
460 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
461 | type: UserNotificationType.ABUSE_NEW_MESSAGE, | |
462 | userId: user.id, | |
463 | abuseId: abuse.id | |
464 | }) | |
465 | notification.Abuse = abuse | |
466 | ||
467 | return notification | |
468 | } | |
469 | ||
470 | function emailSenderReporter (emails: string[]) { | |
d573926e | 471 | return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'reporter', abuse, message, accountMessage }) |
594d3e48 C |
472 | } |
473 | ||
474 | function emailSenderModerators (emails: string[]) { | |
d573926e | 475 | return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'moderator', abuse, message, accountMessage }) |
594d3e48 C |
476 | } |
477 | ||
478 | async function buildReporterOptions () { | |
479 | // Only notify our users | |
f98c3952 | 480 | if (abuse.ReporterAccount.isOwned() !== true) return undefined |
594d3e48 C |
481 | |
482 | const reporter = await UserModel.loadByAccountActorId(abuse.ReporterAccount.actorId) | |
483 | // Don't notify my own message | |
f98c3952 | 484 | if (reporter.Account.id === message.accountId) return undefined |
594d3e48 C |
485 | |
486 | return { users: [ reporter ], settingGetter, notificationCreator, emailSender: emailSenderReporter } | |
487 | } | |
488 | ||
489 | async function buildModeratorsOptions () { | |
490 | let moderators = await UserModel.listWithRight(UserRight.MANAGE_ABUSES) | |
491 | // Don't notify my own message | |
492 | moderators = moderators.filter(m => m.Account.id !== message.accountId) | |
493 | ||
f98c3952 | 494 | if (moderators.length === 0) return undefined |
594d3e48 C |
495 | |
496 | return { users: moderators, settingGetter, notificationCreator, emailSender: emailSenderModerators } | |
497 | } | |
498 | ||
b1771455 | 499 | const options = await Promise.all([ |
594d3e48 C |
500 | buildReporterOptions(), |
501 | buildModeratorsOptions() | |
502 | ]) | |
503 | ||
b1771455 | 504 | return Promise.all( |
505 | options | |
f98c3952 C |
506 | .filter(opt => !!opt) |
507 | .map(opt => this.notify(opt)) | |
b1771455 | 508 | ) |
594d3e48 C |
509 | } |
510 | ||
8424c402 | 511 | private async notifyModeratorsOfVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo) { |
7ccddd7b JM |
512 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST) |
513 | if (moderators.length === 0) return | |
514 | ||
8424c402 | 515 | logger.info('Notifying %s moderators of video auto-blacklist %s.', moderators.length, videoBlacklist.Video.url) |
7ccddd7b | 516 | |
8424c402 | 517 | function settingGetter (user: MUserWithNotificationSetting) { |
7ccddd7b JM |
518 | return user.NotificationSetting.videoAutoBlacklistAsModerator |
519 | } | |
7ccddd7b | 520 | |
8424c402 C |
521 | async function notificationCreator (user: MUserWithNotificationSetting) { |
522 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
7ccddd7b JM |
523 | type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS, |
524 | userId: user.id, | |
8424c402 | 525 | videoBlacklistId: videoBlacklist.id |
7ccddd7b | 526 | }) |
8424c402 | 527 | notification.VideoBlacklist = videoBlacklist |
7ccddd7b JM |
528 | |
529 | return notification | |
530 | } | |
531 | ||
532 | function emailSender (emails: string[]) { | |
8424c402 | 533 | return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, videoBlacklist) |
7ccddd7b JM |
534 | } |
535 | ||
536 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) | |
537 | } | |
538 | ||
453e83ea | 539 | private async notifyVideoOwnerOfBlacklist (videoBlacklist: MVideoBlacklistVideo) { |
cef534ed C |
540 | const user = await UserModel.loadByVideoId(videoBlacklist.videoId) |
541 | if (!user) return | |
542 | ||
543 | logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url) | |
544 | ||
8424c402 | 545 | function settingGetter (user: MUserWithNotificationSetting) { |
cef534ed C |
546 | return user.NotificationSetting.blacklistOnMyVideo |
547 | } | |
548 | ||
8424c402 C |
549 | async function notificationCreator (user: MUserWithNotificationSetting) { |
550 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
cef534ed C |
551 | type: UserNotificationType.BLACKLIST_ON_MY_VIDEO, |
552 | userId: user.id, | |
553 | videoBlacklistId: videoBlacklist.id | |
554 | }) | |
8424c402 | 555 | notification.VideoBlacklist = videoBlacklist |
cef534ed C |
556 | |
557 | return notification | |
558 | } | |
559 | ||
560 | function emailSender (emails: string[]) { | |
561 | return Emailer.Instance.addVideoBlacklistNotification(emails, videoBlacklist) | |
562 | } | |
563 | ||
564 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
565 | } | |
566 | ||
8424c402 | 567 | private async notifyVideoOwnerOfUnblacklist (video: MVideoFullLight) { |
cef534ed C |
568 | const user = await UserModel.loadByVideoId(video.id) |
569 | if (!user) return | |
570 | ||
571 | logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url) | |
572 | ||
8424c402 | 573 | function settingGetter (user: MUserWithNotificationSetting) { |
cef534ed C |
574 | return user.NotificationSetting.blacklistOnMyVideo |
575 | } | |
576 | ||
8424c402 C |
577 | async function notificationCreator (user: MUserWithNotificationSetting) { |
578 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
cef534ed C |
579 | type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO, |
580 | userId: user.id, | |
581 | videoId: video.id | |
582 | }) | |
8424c402 | 583 | notification.Video = video |
cef534ed C |
584 | |
585 | return notification | |
586 | } | |
587 | ||
588 | function emailSender (emails: string[]) { | |
589 | return Emailer.Instance.addVideoUnblacklistNotification(emails, video) | |
590 | } | |
591 | ||
592 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
593 | } | |
594 | ||
453e83ea | 595 | private async notifyOwnedVideoHasBeenPublished (video: MVideoFullLight) { |
dc133480 C |
596 | const user = await UserModel.loadByVideoId(video.id) |
597 | if (!user) return | |
598 | ||
599 | logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url) | |
600 | ||
8424c402 | 601 | function settingGetter (user: MUserWithNotificationSetting) { |
dc133480 C |
602 | return user.NotificationSetting.myVideoPublished |
603 | } | |
604 | ||
8424c402 C |
605 | async function notificationCreator (user: MUserWithNotificationSetting) { |
606 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
dc133480 C |
607 | type: UserNotificationType.MY_VIDEO_PUBLISHED, |
608 | userId: user.id, | |
609 | videoId: video.id | |
610 | }) | |
8424c402 | 611 | notification.Video = video |
dc133480 C |
612 | |
613 | return notification | |
614 | } | |
615 | ||
616 | function emailSender (emails: string[]) { | |
617 | return Emailer.Instance.myVideoPublishedNotification(emails, video) | |
618 | } | |
619 | ||
620 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
621 | } | |
622 | ||
453e83ea | 623 | private async notifyOwnerVideoImportIsFinished (videoImport: MVideoImportVideo, success: boolean) { |
dc133480 C |
624 | const user = await UserModel.loadByVideoImportId(videoImport.id) |
625 | if (!user) return | |
626 | ||
627 | logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier()) | |
628 | ||
8424c402 | 629 | function settingGetter (user: MUserWithNotificationSetting) { |
dc133480 C |
630 | return user.NotificationSetting.myVideoImportFinished |
631 | } | |
632 | ||
8424c402 C |
633 | async function notificationCreator (user: MUserWithNotificationSetting) { |
634 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
dc133480 C |
635 | type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR, |
636 | userId: user.id, | |
637 | videoImportId: videoImport.id | |
638 | }) | |
8424c402 | 639 | notification.VideoImport = videoImport |
dc133480 C |
640 | |
641 | return notification | |
642 | } | |
643 | ||
644 | function emailSender (emails: string[]) { | |
645 | return success | |
646 | ? Emailer.Instance.myVideoImportSuccessNotification(emails, videoImport) | |
647 | : Emailer.Instance.myVideoImportErrorNotification(emails, videoImport) | |
648 | } | |
649 | ||
650 | return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) | |
651 | } | |
652 | ||
8424c402 | 653 | private async notifyModeratorsOfNewUserRegistration (registeredUser: MUserDefault) { |
f7cc67b4 C |
654 | const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS) |
655 | if (moderators.length === 0) return | |
656 | ||
657 | logger.info( | |
658 | 'Notifying %s moderators of new user registration of %s.', | |
453e83ea | 659 | moderators.length, registeredUser.username |
f7cc67b4 C |
660 | ) |
661 | ||
8424c402 | 662 | function settingGetter (user: MUserWithNotificationSetting) { |
f7cc67b4 C |
663 | return user.NotificationSetting.newUserRegistration |
664 | } | |
665 | ||
8424c402 C |
666 | async function notificationCreator (user: MUserWithNotificationSetting) { |
667 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
f7cc67b4 C |
668 | type: UserNotificationType.NEW_USER_REGISTRATION, |
669 | userId: user.id, | |
670 | accountId: registeredUser.Account.id | |
671 | }) | |
8424c402 | 672 | notification.Account = registeredUser.Account |
f7cc67b4 C |
673 | |
674 | return notification | |
675 | } | |
676 | ||
677 | function emailSender (emails: string[]) { | |
678 | return Emailer.Instance.addNewUserRegistrationNotification(emails, registeredUser) | |
679 | } | |
680 | ||
681 | return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) | |
682 | } | |
683 | ||
32a18cbf C |
684 | private async notifyAdminsOfNewPeerTubeVersion (application: MApplication, latestVersion: string) { |
685 | // Use the debug right to know who is an administrator | |
686 | const admins = await UserModel.listWithRight(UserRight.MANAGE_DEBUG) | |
687 | if (admins.length === 0) return | |
688 | ||
689 | logger.info('Notifying %s admins of new PeerTube version %s.', admins.length, latestVersion) | |
690 | ||
691 | function settingGetter (user: MUserWithNotificationSetting) { | |
692 | return user.NotificationSetting.newPeerTubeVersion | |
693 | } | |
694 | ||
695 | async function notificationCreator (user: MUserWithNotificationSetting) { | |
696 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
697 | type: UserNotificationType.NEW_PEERTUBE_VERSION, | |
698 | userId: user.id, | |
699 | applicationId: application.id | |
700 | }) | |
701 | notification.Application = application | |
702 | ||
703 | return notification | |
704 | } | |
705 | ||
706 | function emailSender (emails: string[]) { | |
707 | return Emailer.Instance.addNewPeerTubeVersionNotification(emails, latestVersion) | |
708 | } | |
709 | ||
710 | return this.notify({ users: admins, settingGetter, notificationCreator, emailSender }) | |
711 | } | |
712 | ||
713 | private async notifyAdminsOfNewPluginVersion (plugin: MPlugin) { | |
714 | // Use the debug right to know who is an administrator | |
715 | const admins = await UserModel.listWithRight(UserRight.MANAGE_DEBUG) | |
716 | if (admins.length === 0) return | |
717 | ||
718 | logger.info('Notifying %s admins of new plugin version %s@%s.', admins.length, plugin.name, plugin.latestVersion) | |
719 | ||
720 | function settingGetter (user: MUserWithNotificationSetting) { | |
721 | return user.NotificationSetting.newPluginVersion | |
722 | } | |
723 | ||
724 | async function notificationCreator (user: MUserWithNotificationSetting) { | |
725 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
726 | type: UserNotificationType.NEW_PLUGIN_VERSION, | |
727 | userId: user.id, | |
728 | pluginId: plugin.id | |
729 | }) | |
730 | notification.Plugin = plugin | |
731 | ||
732 | return notification | |
733 | } | |
734 | ||
735 | function emailSender (emails: string[]) { | |
736 | return Emailer.Instance.addNewPlugionVersionNotification(emails, plugin) | |
737 | } | |
738 | ||
739 | return this.notify({ users: admins, settingGetter, notificationCreator, emailSender }) | |
740 | } | |
741 | ||
a1587156 C |
742 | private async notify<T extends MUserWithNotificationSetting> (options: { |
743 | users: T[] | |
744 | notificationCreator: (user: T) => Promise<UserNotificationModelForApi> | |
745 | emailSender: (emails: string[]) => void | |
8424c402 | 746 | settingGetter: (user: T) => UserNotificationSettingValue |
cef534ed C |
747 | }) { |
748 | const emails: string[] = [] | |
749 | ||
750 | for (const user of options.users) { | |
751 | if (this.isWebNotificationEnabled(options.settingGetter(user))) { | |
752 | const notification = await options.notificationCreator(user) | |
753 | ||
754 | PeerTubeSocket.Instance.sendNotification(user.id, notification) | |
755 | } | |
756 | ||
757 | if (this.isEmailEnabled(user, options.settingGetter(user))) { | |
758 | emails.push(user.email) | |
759 | } | |
760 | } | |
761 | ||
762 | if (emails.length !== 0) { | |
a1587156 | 763 | options.emailSender(emails) |
cef534ed C |
764 | } |
765 | } | |
766 | ||
453e83ea | 767 | private isEmailEnabled (user: MUser, value: UserNotificationSettingValue) { |
1ed9b8ee | 768 | if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false |
cef534ed | 769 | |
2f1548fd | 770 | return value & UserNotificationSettingValue.EMAIL |
cef534ed C |
771 | } |
772 | ||
773 | private isWebNotificationEnabled (value: UserNotificationSettingValue) { | |
2f1548fd | 774 | return value & UserNotificationSettingValue.WEB |
cef534ed C |
775 | } |
776 | ||
696d83fd C |
777 | private isBlockedByServerOrUser (targetAccount: MAccountServer, user?: MUserAccount) { |
778 | return isBlockedByServerOrAccount(targetAccount, user?.Account) | |
dddc8b1f C |
779 | } |
780 | ||
594d3e48 C |
781 | private getAbuseUrl (abuse: MAbuseFull) { |
782 | return abuse.VideoAbuse?.Video?.url || | |
783 | abuse.VideoCommentAbuse?.VideoComment?.url || | |
784 | abuse.FlaggedAccount.Actor.url | |
785 | } | |
786 | ||
cef534ed C |
787 | static get Instance () { |
788 | return this.instance || (this.instance = new this()) | |
789 | } | |
790 | } | |
791 | ||
792 | // --------------------------------------------------------------------------- | |
793 | ||
794 | export { | |
795 | Notifier | |
796 | } |