+ private async notifyUserOfNewActorFollow (actorFollow: MActorFollowFull) {
+ if (actorFollow.ActorFollowing.isOwned() === false) return
+
+ // Account follows one of our account?
+ let followType: 'account' | 'channel' = 'channel'
+ let user = await UserModel.loadByChannelActorId(actorFollow.ActorFollowing.id)
+
+ // Account follows one of our channel?
+ if (!user) {
+ user = await UserModel.loadByAccountActorId(actorFollow.ActorFollowing.id)
+ followType = 'account'
+ }
+
+ if (!user) return
+
+ const followerAccount = actorFollow.ActorFollower.Account
+ const followerAccountWithActor = Object.assign(followerAccount, { Actor: actorFollow.ActorFollower })
+
+ if (await this.isBlockedByServerOrUser(followerAccountWithActor, user)) return
+
+ logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName())
+
+ function settingGetter (user: MUserWithNotificationSetting) {
+ return user.NotificationSetting.newFollow
+ }
+
+ async function notificationCreator (user: MUserWithNotificationSetting) {
+ const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
+ type: UserNotificationType.NEW_FOLLOW,
+ userId: user.id,
+ actorFollowId: actorFollow.id
+ })
+ notification.ActorFollow = actorFollow
+
+ return notification
+ }
+
+ function emailSender (emails: string[]) {
+ return Emailer.Instance.addNewFollowNotification(emails, actorFollow, followType)
+ }
+
+ return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
+ }
+
+ 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: MUserWithNotificationSetting) {
+ return user.NotificationSetting.newInstanceFollower
+ }
+
+ async function notificationCreator (user: MUserWithNotificationSetting) {
+ const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
+ type: UserNotificationType.NEW_INSTANCE_FOLLOWER,
+ userId: user.id,
+ actorFollowId: actorFollow.id
+ })
+ notification.ActorFollow = actorFollow
+
+ return notification
+ }
+
+ function emailSender (emails: string[]) {
+ return Emailer.Instance.addNewInstanceFollowerNotification(emails, actorFollow)
+ }
+
+ return this.notify({ users: admins, settingGetter, notificationCreator, emailSender })
+ }
+
+ 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<UserNotificationModelForApi>({
+ 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
+
+ const url = this.getAbuseUrl(abuseInstance)
+
+ logger.info('Notifying %s user/moderators of new abuse %s.', moderators.length, url)
+
+ function settingGetter (user: MUserWithNotificationSetting) {
+ return user.NotificationSetting.abuseAsModerator
+ }
+
+ async function notificationCreator (user: MUserWithNotificationSetting) {
+ const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
+ type: UserNotificationType.NEW_ABUSE_FOR_MODERATORS,
+ userId: user.id,
+ abuseId: abuse.id
+ })
+ notification.Abuse = abuseInstance
+
+ return notification
+ }
+
+ function emailSender (emails: string[]) {
+ return Emailer.Instance.addAbuseModeratorsNotification(emails, parameters)
+ }
+
+ return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
+ }
+
+ 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<UserNotificationModelForApi>({
+ 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<UserNotificationModelForApi>({
+ 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, videoBlacklist.Video.url)
+
+ function settingGetter (user: MUserWithNotificationSetting) {
+ return user.NotificationSetting.videoAutoBlacklistAsModerator
+ }
+
+ async function notificationCreator (user: MUserWithNotificationSetting) {
+ const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
+ type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS,
+ userId: user.id,
+ videoBlacklistId: videoBlacklist.id
+ })
+ notification.VideoBlacklist = videoBlacklist
+
+ return notification
+ }
+
+ function emailSender (emails: string[]) {
+ return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, videoBlacklist)
+ }
+
+ return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
+ }
+
+ private async notifyVideoOwnerOfBlacklist (videoBlacklist: MVideoBlacklistVideo) {