]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/notifier.ts
Use 3 tables to represent abuses
[github/Chocobozzz/PeerTube.git] / server / lib / notifier.ts
index 63197eee13deaa67e1594f6898824bffd2592b19..40cff66d224d4798b0feedf5ce9f6bc86232d434 100644 (file)
@@ -1,20 +1,5 @@
-import { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users'
-import { logger } from '../helpers/logger'
-import { Emailer } from './emailer'
-import { UserNotificationModel } from '../models/account/user-notification'
-import { UserModel } from '../models/account/user'
-import { PeerTubeSocket } from './peertube-socket'
-import { CONFIG } from '../initializers/config'
-import { VideoPrivacy, VideoState } from '../../shared/models/videos'
-import { AccountBlocklistModel } from '../models/account/account-blocklist'
-import {
-  MCommentOwnerVideo,
-  MVideoAbuseVideo,
-  MVideoAccountLight,
-  MVideoBlacklistLightVideo,
-  MVideoBlacklistVideo,
-  MVideoFullLight
-} from '../typings/models/video'
+import { getServerActor } from '@server/models/application/application'
+import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
 import {
   MUser,
   MUserAccount,
@@ -22,11 +7,22 @@ import {
   MUserNotifSettingAccount,
   MUserWithNotificationSetting,
   UserNotificationModelForApi
-} from '@server/typings/models/user'
-import { MAccountDefault, MActorFollowFull } from '../typings/models'
-import { MVideoImportVideo } from '@server/typings/models/video/video-import'
-import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
-import { getServerActor } from '@server/helpers/utils'
+} 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 { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users'
+import { VideoPrivacy, VideoState } from '../../shared/models/videos'
+import { logger } from '../helpers/logger'
+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, MAbuseVideo, 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 {
 
@@ -77,9 +73,9 @@ class Notifier {
         .catch(err => logger.error('Cannot notify mentions of comment %s.', comment.url, { err }))
   }
 
-  notifyOnNewVideoAbuse (videoAbuse: MVideoAbuseVideo): void {
-    this.notifyModeratorsOfNewVideoAbuse(videoAbuse)
-        .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err }))
+  notifyOnNewAbuse (parameters: { abuse: Abuse, abuseInstance: MAbuseFull, reporter: string }): void {
+    this.notifyModeratorsOfNewAbuse(parameters)
+        .catch(err => logger.error('Cannot notify of new abuse %d.', parameters.abuseInstance.id, { err }))
   }
 
   notifyOnVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo): void {
@@ -169,7 +165,7 @@ class Notifier {
     // Not our user or user comments its own video
     if (!user || comment.Account.userId === user.id) return
 
-    if (await this.isBlockedByServerOrAccount(user, comment.Account)) return
+    if (await this.isBlockedByServerOrUser(comment.Account, user)) return
 
     logger.info('Notifying user %s of new comment %s.', user.username, comment.url)
 
@@ -270,7 +266,7 @@ class Notifier {
     const followerAccount = actorFollow.ActorFollower.Account
     const followerAccountWithActor = Object.assign(followerAccount, { Actor: actorFollow.ActorFollower })
 
-    if (await this.isBlockedByServerOrAccount(user, followerAccountWithActor)) return
+    if (await this.isBlockedByServerOrUser(followerAccountWithActor, user)) return
 
     logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName())
 
@@ -299,6 +295,9 @@ class Notifier {
   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) {
@@ -350,29 +349,37 @@ class Notifier {
     return this.notify({ users: admins, settingGetter, notificationCreator, emailSender })
   }
 
-  private async notifyModeratorsOfNewVideoAbuse (videoAbuse: MVideoAbuseVideo) {
-    const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES)
+  private async notifyModeratorsOfNewAbuse (parameters: {
+    abuse: Abuse
+    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 = abuseInstance.VideoAbuse?.Video?.url || abuseInstance.VideoCommentAbuse?.VideoComment?.url
+
+    logger.info('Notifying %s user/moderators of new abuse %s.', moderators.length, url)
 
     function settingGetter (user: MUserWithNotificationSetting) {
       return user.NotificationSetting.videoAbuseAsModerator
     }
 
     async function notificationCreator (user: MUserWithNotificationSetting) {
-      const notification: UserNotificationModelForApi = await UserNotificationModel.create<UserNotificationModelForApi>({
+      const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
         type: UserNotificationType.NEW_VIDEO_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 })
@@ -586,17 +593,8 @@ class Notifier {
     return value & UserNotificationSettingValue.WEB
   }
 
-  private async isBlockedByServerOrAccount (user: MUserAccount, targetAccount: MAccountDefault) {
-    const serverAccountId = (await getServerActor()).Account.id
-    const sourceAccounts = [ serverAccountId, user.Account.id ]
-
-    const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(sourceAccounts, targetAccount.id)
-    if (accountMutedHash[serverAccountId] || accountMutedHash[user.Account.id]) return true
-
-    const instanceMutedHash = await ServerBlocklistModel.isServerMutedByMulti(sourceAccounts, targetAccount.Actor.serverId)
-    if (instanceMutedHash[serverAccountId] || instanceMutedHash[user.Account.id]) return true
-
-    return false
+  private isBlockedByServerOrUser (targetAccount: MAccountServer, user?: MUserAccount) {
+    return isBlockedByServerOrAccount(targetAccount, user?.Account)
   }
 
   static get Instance () {