X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fnotifier.ts;h=740c274d72116cd1b4edf0c310bffffbb03c8be6;hb=fae6e4da8f516a9d6c3bad9bf6f35811ccacbad8;hp=c1e63fa8faad41de2f5cc3c67a27e257e46d255f;hpb=6dd9de95dfa39bd5c1faed00d1dbd52cd112bae0;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/notifier.ts b/server/lib/notifier.ts index c1e63fa8f..740c274d7 100644 --- a/server/lib/notifier.ts +++ b/server/lib/notifier.ts @@ -1,36 +1,46 @@ +import { AccountModel } from '@server/models/account/account' +import { getServerActor } from '@server/models/application/application' +import { ServerBlocklistModel } from '@server/models/server/server-blocklist' +import { + MUser, + MUserAccount, + MUserDefault, + MUserNotifSettingAccount, + MUserWithNotificationSetting, + UserNotificationModelForApi +} from '@server/types/models/user' +import { MVideoBlacklistLightVideo, MVideoBlacklistVideo } from '@server/types/models/video/video-blacklist' +import { MVideoImportVideo } from '@server/types/models/video/video-import' +import { UserAbuse } from '@shared/models' import { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users' +import { VideoPrivacy, VideoState } from '../../shared/models/videos' import { logger } from '../helpers/logger' -import { VideoModel } from '../models/video/video' -import { Emailer } from './emailer' -import { UserNotificationModel } from '../models/account/user-notification' -import { VideoCommentModel } from '../models/video/video-comment' -import { UserModel } from '../models/account/user' -import { PeerTubeSocket } from './peertube-socket' import { CONFIG } from '../initializers/config' -import { VideoPrivacy, VideoState } from '../../shared/models/videos' -import { VideoAbuseModel } from '../models/video/video-abuse' -import { VideoBlacklistModel } from '../models/video/video-blacklist' -import * as Bluebird from 'bluebird' -import { VideoImportModel } from '../models/video/video-import' import { AccountBlocklistModel } from '../models/account/account-blocklist' -import { ActorFollowModel } from '../models/activitypub/actor-follow' -import { AccountModel } from '../models/account/account' +import { UserModel } from '../models/account/user' +import { UserNotificationModel } from '../models/account/user-notification' +import { MAbuseFull, MAbuseMessage, MAccountServer, MActorFollowFull } from '../types/models' +import { MCommentOwnerVideo, MVideoAccountLight, MVideoFullLight } from '../types/models/video' +import { isBlockedByServerOrAccount } from './blocklist' +import { Emailer } from './emailer' +import { PeerTubeSocket } from './peertube-socket' class Notifier { private static instance: Notifier - private constructor () {} + private constructor () { + } - notifyOnNewVideo (video: VideoModel): void { + notifyOnNewVideoIfNeeded (video: MVideoAccountLight): void { // Only notify on public and published videos which are not blacklisted - if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED || video.VideoBlacklist) return + if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED || video.isBlacklisted()) return this.notifySubscribersOfNewVideo(video) - .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err })) + .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err })) } - notifyOnVideoPublishedAfterTranscoding (video: VideoModel): void { + notifyOnVideoPublishedAfterTranscoding (video: MVideoFullLight): void { // don't notify if didn't wait for transcoding or video is still blacklisted/waiting for scheduled update if (!video.waitTranscoding || video.VideoBlacklist || video.ScheduleVideoUpdate) return @@ -38,7 +48,7 @@ class Notifier { .catch(err => logger.error('Cannot notify owner that its video %s has been published after transcoding.', video.url, { err })) } - notifyOnVideoPublishedAfterScheduledUpdate (video: VideoModel): void { + notifyOnVideoPublishedAfterScheduledUpdate (video: MVideoFullLight): void { // don't notify if video is still blacklisted or waiting for transcoding if (video.VideoBlacklist || (video.waitTranscoding && video.state !== VideoState.PUBLISHED)) return @@ -46,15 +56,17 @@ class Notifier { .catch(err => logger.error('Cannot notify owner that its video %s has been published after scheduled update.', video.url, { err })) } - notifyOnVideoPublishedAfterRemovedFromAutoBlacklist (video: VideoModel): void { + notifyOnVideoPublishedAfterRemovedFromAutoBlacklist (video: MVideoFullLight): void { // don't notify if video is still waiting for transcoding or scheduled update if (video.ScheduleVideoUpdate || (video.waitTranscoding && video.state !== VideoState.PUBLISHED)) return this.notifyOwnedVideoHasBeenPublished(video) - .catch(err => logger.error('Cannot notify owner that its video %s has been published after removed from auto-blacklist.', video.url, { err })) // tslint:disable-line:max-line-length + .catch(err => { + logger.error('Cannot notify owner that its video %s has been published after removed from auto-blacklist.', video.url, { err }) + }) } - notifyOnNewComment (comment: VideoCommentModel): void { + notifyOnNewComment (comment: MCommentOwnerVideo): void { this.notifyVideoOwnerOfNewComment(comment) .catch(err => logger.error('Cannot notify video owner of new comment %s.', comment.url, { err })) @@ -62,67 +74,88 @@ class Notifier { .catch(err => logger.error('Cannot notify mentions of comment %s.', comment.url, { err })) } - notifyOnNewVideoAbuse (videoAbuse: VideoAbuseModel): void { - this.notifyModeratorsOfNewVideoAbuse(videoAbuse) - .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err })) + notifyOnNewAbuse (parameters: { abuse: UserAbuse, abuseInstance: MAbuseFull, reporter: string }): void { + this.notifyModeratorsOfNewAbuse(parameters) + .catch(err => logger.error('Cannot notify of new abuse %d.', parameters.abuseInstance.id, { err })) } - notifyOnVideoAutoBlacklist (video: VideoModel): void { - this.notifyModeratorsOfVideoAutoBlacklist(video) - .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', video.url, { err })) + notifyOnVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo): void { + this.notifyModeratorsOfVideoAutoBlacklist(videoBlacklist) + .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', videoBlacklist.Video.url, { err })) } - notifyOnVideoBlacklist (videoBlacklist: VideoBlacklistModel): void { + notifyOnVideoBlacklist (videoBlacklist: MVideoBlacklistVideo): void { this.notifyVideoOwnerOfBlacklist(videoBlacklist) - .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err })) + .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err })) } - notifyOnVideoUnblacklist (video: VideoModel): void { + notifyOnVideoUnblacklist (video: MVideoFullLight): void { this.notifyVideoOwnerOfUnblacklist(video) .catch(err => logger.error('Cannot notify video owner of unblacklist of %s.', video.url, { err })) } - notifyOnFinishedVideoImport (videoImport: VideoImportModel, success: boolean): void { + notifyOnFinishedVideoImport (videoImport: MVideoImportVideo, success: boolean): void { this.notifyOwnerVideoImportIsFinished(videoImport, success) - .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err })) + .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err })) } - notifyOnNewUserRegistration (user: UserModel): void { + notifyOnNewUserRegistration (user: MUserDefault): void { this.notifyModeratorsOfNewUserRegistration(user) .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err })) } - notifyOfNewUserFollow (actorFollow: ActorFollowModel): void { + notifyOfNewUserFollow (actorFollow: MActorFollowFull): void { this.notifyUserOfNewActorFollow(actorFollow) - .catch(err => { - logger.error( - 'Cannot notify owner of channel %s of a new follow by %s.', - actorFollow.ActorFollowing.VideoChannel.getDisplayName(), - actorFollow.ActorFollower.Account.getDisplayName(), - { err } - ) - }) + .catch(err => { + logger.error( + 'Cannot notify owner of channel %s of a new follow by %s.', + actorFollow.ActorFollowing.VideoChannel.getDisplayName(), + actorFollow.ActorFollower.Account.getDisplayName(), + { err } + ) + }) } - notifyOfNewInstanceFollow (actorFollow: ActorFollowModel): void { + notifyOfNewInstanceFollow (actorFollow: MActorFollowFull): void { this.notifyAdminsOfNewInstanceFollow(actorFollow) .catch(err => { logger.error('Cannot notify administrators of new follower %s.', actorFollow.ActorFollower.url, { err }) }) } - private async notifySubscribersOfNewVideo (video: VideoModel) { + notifyOfAutoInstanceFollowing (actorFollow: MActorFollowFull): void { + this.notifyAdminsOfAutoInstanceFollowing(actorFollow) + .catch(err => { + logger.error('Cannot notify administrators of auto instance following %s.', actorFollow.ActorFollowing.url, { err }) + }) + } + + notifyOnAbuseStateChange (abuse: MAbuseFull): void { + this.notifyReporterOfAbuseStateChange(abuse) + .catch(err => { + logger.error('Cannot notify reporter of abuse %d state change.', abuse.id, { err }) + }) + } + + notifyOnAbuseMessage (abuse: MAbuseFull, message: MAbuseMessage): void { + this.notifyOfNewAbuseMessage(abuse, message) + .catch(err => { + logger.error('Cannot notify on new abuse %d message.', abuse.id, { err }) + }) + } + + private async notifySubscribersOfNewVideo (video: MVideoAccountLight) { // List all followers that are users const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId) logger.info('Notifying %d users of new video %s.', users.length, video.url) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.newVideoFromSubscription } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION, userId: user.id, videoId: video.id @@ -139,7 +172,7 @@ class Notifier { return this.notify({ users, settingGetter, notificationCreator, emailSender }) } - private async notifyVideoOwnerOfNewComment (comment: VideoCommentModel) { + private async notifyVideoOwnerOfNewComment (comment: MCommentOwnerVideo) { if (comment.Video.isOwned() === false) return const user = await UserModel.loadByVideoId(comment.videoId) @@ -147,17 +180,16 @@ class Notifier { // Not our user or user comments its own video if (!user || comment.Account.userId === user.id) return - const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, comment.accountId) - if (accountMuted) return + if (await this.isBlockedByServerOrUser(comment.Account, user)) return logger.info('Notifying user %s of new comment %s.', user.username, comment.url) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.newCommentOnMyVideo } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, userId: user.id, commentId: comment.id @@ -174,7 +206,7 @@ class Notifier { return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) } - private async notifyOfCommentMention (comment: VideoCommentModel) { + private async notifyOfCommentMention (comment: MCommentOwnerVideo) { const extractedUsernames = comment.extractMentions() logger.debug( 'Extracted %d username from comment %s.', extractedUsernames.length, comment.url, @@ -193,18 +225,28 @@ class Notifier { if (users.length === 0) return - const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(users.map(u => u.Account.id), comment.accountId) + const serverAccountId = (await getServerActor()).Account.id + const sourceAccounts = users.map(u => u.Account.id).concat([ serverAccountId ]) + + const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(sourceAccounts, comment.accountId) + const instanceMutedHash = await ServerBlocklistModel.isServerMutedByMulti(sourceAccounts, comment.Account.Actor.serverId) logger.info('Notifying %d users of new comment %s.', users.length, comment.url) - function settingGetter (user: UserModel) { - if (accountMutedHash[user.Account.id] === true) return UserNotificationSettingValue.NONE + function settingGetter (user: MUserNotifSettingAccount) { + const accountId = user.Account.id + if ( + accountMutedHash[accountId] === true || instanceMutedHash[accountId] === true || + accountMutedHash[serverAccountId] === true || instanceMutedHash[serverAccountId] === true + ) { + return UserNotificationSettingValue.NONE + } return user.NotificationSetting.commentMention } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserNotifSettingAccount) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.COMMENT_MENTION, userId: user.id, commentId: comment.id @@ -221,7 +263,7 @@ class Notifier { return this.notify({ users, settingGetter, notificationCreator, emailSender }) } - private async notifyUserOfNewActorFollow (actorFollow: ActorFollowModel) { + private async notifyUserOfNewActorFollow (actorFollow: MActorFollowFull) { if (actorFollow.ActorFollowing.isOwned() === false) return // Account follows one of our account? @@ -236,22 +278,19 @@ class Notifier { if (!user) return - if (!actorFollow.ActorFollower.Account || !actorFollow.ActorFollower.Account.name) { - actorFollow.ActorFollower.Account = await actorFollow.ActorFollower.$get('Account') as AccountModel - } const followerAccount = actorFollow.ActorFollower.Account + const followerAccountWithActor = Object.assign(followerAccount, { Actor: actorFollow.ActorFollower }) - const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, followerAccount.id) - if (accountMuted) return + if (await this.isBlockedByServerOrUser(followerAccountWithActor, user)) return logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName()) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.newFollow } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.NEW_FOLLOW, userId: user.id, actorFollowId: actorFollow.id @@ -268,17 +307,20 @@ class Notifier { return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) } - private async notifyAdminsOfNewInstanceFollow (actorFollow: ActorFollowModel) { + private async notifyAdminsOfNewInstanceFollow (actorFollow: MActorFollowFull) { const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW) + const follower = Object.assign(actorFollow.ActorFollower.Account, { Actor: actorFollow.ActorFollower }) + if (await this.isBlockedByServerOrUser(follower)) return + logger.info('Notifying %d administrators of new instance follower: %s.', admins.length, actorFollow.ActorFollower.url) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.newInstanceFollower } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.NEW_INSTANCE_FOLLOWER, userId: user.id, actorFollowId: actorFollow.id @@ -295,74 +337,203 @@ class Notifier { return this.notify({ users: admins, settingGetter, notificationCreator, emailSender }) } - private async notifyModeratorsOfNewVideoAbuse (videoAbuse: VideoAbuseModel) { - const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES) + private async notifyAdminsOfAutoInstanceFollowing (actorFollow: MActorFollowFull) { + const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW) + + logger.info('Notifying %d administrators of auto instance following: %s.', admins.length, actorFollow.ActorFollowing.url) + + function settingGetter (user: MUserWithNotificationSetting) { + return user.NotificationSetting.autoInstanceFollowing + } + + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ + type: UserNotificationType.AUTO_INSTANCE_FOLLOWING, + userId: user.id, + actorFollowId: actorFollow.id + }) + notification.ActorFollow = actorFollow + + return notification + } + + function emailSender (emails: string[]) { + return Emailer.Instance.addAutoInstanceFollowingNotification(emails, actorFollow) + } + + return this.notify({ users: admins, settingGetter, notificationCreator, emailSender }) + } + + private async notifyModeratorsOfNewAbuse (parameters: { + abuse: UserAbuse + abuseInstance: MAbuseFull + reporter: string + }) { + const { abuse, abuseInstance } = parameters + + const moderators = await UserModel.listWithRight(UserRight.MANAGE_ABUSES) if (moderators.length === 0) return - logger.info('Notifying %s user/moderators of new video abuse %s.', moderators.length, videoAbuse.Video.url) + const url = this.getAbuseUrl(abuseInstance) + + logger.info('Notifying %s user/moderators of new abuse %s.', moderators.length, url) - function settingGetter (user: UserModel) { - return user.NotificationSetting.videoAbuseAsModerator + function settingGetter (user: MUserWithNotificationSetting) { + return user.NotificationSetting.abuseAsModerator } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ - type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS, + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ + type: UserNotificationType.NEW_ABUSE_FOR_MODERATORS, userId: user.id, - videoAbuseId: videoAbuse.id + abuseId: abuse.id }) - notification.VideoAbuse = videoAbuse + notification.Abuse = abuseInstance return notification } function emailSender (emails: string[]) { - return Emailer.Instance.addVideoAbuseModeratorsNotification(emails, videoAbuse) + return Emailer.Instance.addAbuseModeratorsNotification(emails, parameters) } return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) } - private async notifyModeratorsOfVideoAutoBlacklist (video: VideoModel) { + private async notifyReporterOfAbuseStateChange (abuse: MAbuseFull) { + // Only notify our users + if (abuse.ReporterAccount.isOwned() !== true) return + + const url = this.getAbuseUrl(abuse) + + logger.info('Notifying reporter of abuse % of state change.', url) + + const reporter = await UserModel.loadByAccountActorId(abuse.ReporterAccount.actorId) + + function settingGetter (user: MUserWithNotificationSetting) { + return user.NotificationSetting.abuseStateChange + } + + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ + type: UserNotificationType.ABUSE_STATE_CHANGE, + userId: user.id, + abuseId: abuse.id + }) + notification.Abuse = abuse + + return notification + } + + function emailSender (emails: string[]) { + return Emailer.Instance.addAbuseStateChangeNotification(emails, abuse) + } + + return this.notify({ users: [ reporter ], settingGetter, notificationCreator, emailSender }) + } + + private async notifyOfNewAbuseMessage (abuse: MAbuseFull, message: MAbuseMessage) { + const url = this.getAbuseUrl(abuse) + logger.info('Notifying reporter and moderators of new abuse message on %s.', url) + + const accountMessage = await AccountModel.load(message.accountId) + + function settingGetter (user: MUserWithNotificationSetting) { + return user.NotificationSetting.abuseNewMessage + } + + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ + type: UserNotificationType.ABUSE_NEW_MESSAGE, + userId: user.id, + abuseId: abuse.id + }) + notification.Abuse = abuse + + return notification + } + + function emailSenderReporter (emails: string[]) { + return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'reporter', abuse, message, accountMessage }) + } + + function emailSenderModerators (emails: string[]) { + return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'moderator', abuse, message, accountMessage }) + } + + async function buildReporterOptions () { + // Only notify our users + if (abuse.ReporterAccount.isOwned() !== true) return undefined + + const reporter = await UserModel.loadByAccountActorId(abuse.ReporterAccount.actorId) + // Don't notify my own message + if (reporter.Account.id === message.accountId) return undefined + + return { users: [ reporter ], settingGetter, notificationCreator, emailSender: emailSenderReporter } + } + + async function buildModeratorsOptions () { + let moderators = await UserModel.listWithRight(UserRight.MANAGE_ABUSES) + // Don't notify my own message + moderators = moderators.filter(m => m.Account.id !== message.accountId) + + if (moderators.length === 0) return undefined + + return { users: moderators, settingGetter, notificationCreator, emailSender: emailSenderModerators } + } + + const options = await Promise.all([ + buildReporterOptions(), + buildModeratorsOptions() + ]) + + return Promise.all( + options + .filter(opt => !!opt) + .map(opt => this.notify(opt)) + ) + } + + private async notifyModeratorsOfVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo) { const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST) if (moderators.length === 0) return - logger.info('Notifying %s moderators of video auto-blacklist %s.', moderators.length, video.url) + logger.info('Notifying %s moderators of video auto-blacklist %s.', moderators.length, videoBlacklist.Video.url) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.videoAutoBlacklistAsModerator } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS, userId: user.id, - videoId: video.id + videoBlacklistId: videoBlacklist.id }) - notification.Video = video + notification.VideoBlacklist = videoBlacklist return notification } function emailSender (emails: string[]) { - return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, video) + return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, videoBlacklist) } return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) } - private async notifyVideoOwnerOfBlacklist (videoBlacklist: VideoBlacklistModel) { + private async notifyVideoOwnerOfBlacklist (videoBlacklist: MVideoBlacklistVideo) { const user = await UserModel.loadByVideoId(videoBlacklist.videoId) if (!user) return logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.blacklistOnMyVideo } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.BLACKLIST_ON_MY_VIDEO, userId: user.id, videoBlacklistId: videoBlacklist.id @@ -379,18 +550,18 @@ class Notifier { return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) } - private async notifyVideoOwnerOfUnblacklist (video: VideoModel) { + private async notifyVideoOwnerOfUnblacklist (video: MVideoFullLight) { const user = await UserModel.loadByVideoId(video.id) if (!user) return logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.blacklistOnMyVideo } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO, userId: user.id, videoId: video.id @@ -407,18 +578,18 @@ class Notifier { return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) } - private async notifyOwnedVideoHasBeenPublished (video: VideoModel) { + private async notifyOwnedVideoHasBeenPublished (video: MVideoFullLight) { const user = await UserModel.loadByVideoId(video.id) if (!user) return logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.myVideoPublished } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.MY_VIDEO_PUBLISHED, userId: user.id, videoId: video.id @@ -435,18 +606,18 @@ class Notifier { return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) } - private async notifyOwnerVideoImportIsFinished (videoImport: VideoImportModel, success: boolean) { + private async notifyOwnerVideoImportIsFinished (videoImport: MVideoImportVideo, success: boolean) { const user = await UserModel.loadByVideoImportId(videoImport.id) if (!user) return logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier()) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.myVideoImportFinished } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR, userId: user.id, videoImportId: videoImport.id @@ -465,21 +636,21 @@ class Notifier { return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) } - private async notifyModeratorsOfNewUserRegistration (registeredUser: UserModel) { + private async notifyModeratorsOfNewUserRegistration (registeredUser: MUserDefault) { const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS) if (moderators.length === 0) return logger.info( 'Notifying %s moderators of new user registration of %s.', - moderators.length, registeredUser.Account.Actor.preferredUsername + moderators.length, registeredUser.username ) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.newUserRegistration } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.NEW_USER_REGISTRATION, userId: user.id, accountId: registeredUser.Account.id @@ -496,11 +667,11 @@ class Notifier { return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) } - private async notify (options: { - users: UserModel[], - notificationCreator: (user: UserModel) => Promise, - emailSender: (emails: string[]) => Promise | Bluebird, - settingGetter: (user: UserModel) => UserNotificationSettingValue + private async notify (options: { + users: T[] + notificationCreator: (user: T) => Promise + emailSender: (emails: string[]) => void + settingGetter: (user: T) => UserNotificationSettingValue }) { const emails: string[] = [] @@ -517,11 +688,11 @@ class Notifier { } if (emails.length !== 0) { - await options.emailSender(emails) + options.emailSender(emails) } } - private isEmailEnabled (user: UserModel, value: UserNotificationSettingValue) { + private isEmailEnabled (user: MUser, value: UserNotificationSettingValue) { if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false return value & UserNotificationSettingValue.EMAIL @@ -531,6 +702,16 @@ class Notifier { return value & UserNotificationSettingValue.WEB } + private isBlockedByServerOrUser (targetAccount: MAccountServer, user?: MUserAccount) { + return isBlockedByServerOrAccount(targetAccount, user?.Account) + } + + private getAbuseUrl (abuse: MAbuseFull) { + return abuse.VideoAbuse?.Video?.url || + abuse.VideoCommentAbuse?.VideoComment?.url || + abuse.FlaggedAccount.Actor.url + } + static get Instance () { return this.instance || (this.instance = new this()) }