]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/notifier.ts
chore: add manifest in light build
[github/Chocobozzz/PeerTube.git] / server / lib / notifier.ts
index c567e1c200f945c4e604385854b2e5a2488b599a..740c274d72116cd1b4edf0c310bffffbb03c8be6 100644 (file)
@@ -1,3 +1,4 @@
+import { AccountModel } from '@server/models/account/account'
 import { getServerActor } from '@server/models/application/application'
 import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
 import {
@@ -10,7 +11,7 @@ import {
 } 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 { Abuse } from '@shared/models'
+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'
@@ -18,7 +19,7 @@ import { CONFIG } from '../initializers/config'
 import { AccountBlocklistModel } from '../models/account/account-blocklist'
 import { UserModel } from '../models/account/user'
 import { UserNotificationModel } from '../models/account/user-notification'
-import { MAbuseFull, MAccountServer, MActorFollowFull } from '../types/models'
+import { MAbuseFull, MAbuseMessage, MAccountServer, MActorFollowFull } from '../types/models'
 import { MCommentOwnerVideo, MVideoAccountLight, MVideoFullLight } from '../types/models/video'
 import { isBlockedByServerOrAccount } from './blocklist'
 import { Emailer } from './emailer'
@@ -73,7 +74,7 @@ class Notifier {
         .catch(err => logger.error('Cannot notify mentions of comment %s.', comment.url, { err }))
   }
 
-  notifyOnNewAbuse (parameters: { abuse: Abuse, abuseInstance: MAbuseFull, reporter: string }): void {
+  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 }))
   }
@@ -129,6 +130,20 @@ class Notifier {
         })
   }
 
+  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)
@@ -350,7 +365,7 @@ class Notifier {
   }
 
   private async notifyModeratorsOfNewAbuse (parameters: {
-    abuse: Abuse
+    abuse: UserAbuse
     abuseInstance: MAbuseFull
     reporter: string
   }) {
@@ -359,9 +374,7 @@ class Notifier {
     const moderators = await UserModel.listWithRight(UserRight.MANAGE_ABUSES)
     if (moderators.length === 0) return
 
-    const url = abuseInstance.VideoAbuse?.Video?.url ||
-                abuseInstance.VideoCommentAbuse?.VideoComment?.url ||
-                abuseInstance.FlaggedAccount.Actor.url
+    const url = this.getAbuseUrl(abuseInstance)
 
     logger.info('Notifying %s user/moderators of new abuse %s.', moderators.length, url)
 
@@ -387,6 +400,100 @@ class Notifier {
     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
@@ -599,6 +706,12 @@ class Notifier {
     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())
   }