]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/notifier.ts
Unify paginator disabling when no result is displayable, fix batch domain add for...
[github/Chocobozzz/PeerTube.git] / server / lib / notifier.ts
index b7cc2607d3160370f36eb5c29192b87027c7506a..63197eee13deaa67e1594f6898824bffd2592b19 100644 (file)
@@ -6,7 +6,6 @@ import { UserModel } from '../models/account/user'
 import { PeerTubeSocket } from './peertube-socket'
 import { CONFIG } from '../initializers/config'
 import { VideoPrivacy, VideoState } from '../../shared/models/videos'
-import * as Bluebird from 'bluebird'
 import { AccountBlocklistModel } from '../models/account/account-blocklist'
 import {
   MCommentOwnerVideo,
@@ -18,26 +17,30 @@ import {
 } from '../typings/models/video'
 import {
   MUser,
+  MUserAccount,
   MUserDefault,
   MUserNotifSettingAccount,
   MUserWithNotificationSetting,
   UserNotificationModelForApi
 } from '@server/typings/models/user'
-import { MActorFollowFull } from '../typings/models'
+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'
 
 class Notifier {
 
   private static instance: Notifier
 
-  private constructor () {}
+  private constructor () {
+  }
 
   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.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: MVideoFullLight): void {
@@ -61,7 +64,9 @@ class Notifier {
     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: MCommentOwnerVideo): void {
@@ -74,17 +79,17 @@ class Notifier {
 
   notifyOnNewVideoAbuse (videoAbuse: MVideoAbuseVideo): void {
     this.notifyModeratorsOfNewVideoAbuse(videoAbuse)
-      .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err }))
+        .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.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 }))
+        .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', videoBlacklist.Video.url, { err }))
   }
 
   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: MVideoFullLight): void {
@@ -94,7 +99,7 @@ class Notifier {
 
   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: MUserDefault): void {
@@ -104,14 +109,14 @@ class Notifier {
 
   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: MActorFollowFull): void {
@@ -164,8 +169,7 @@ 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.isBlockedByServerOrAccount(user, comment.Account)) return
 
     logger.info('Notifying user %s of new comment %s.', user.username, comment.url)
 
@@ -210,12 +214,22 @@ 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: MUserNotifSettingAccount) {
-      if (accountMutedHash[user.Account.id] === true) return UserNotificationSettingValue.NONE
+      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
     }
@@ -254,9 +268,9 @@ class Notifier {
     if (!user) return
 
     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.isBlockedByServerOrAccount(user, followerAccountWithActor)) return
 
     logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName())
 
@@ -537,10 +551,10 @@ class Notifier {
     return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
   }
 
-  private async notify <T extends MUserWithNotificationSetting> (options: {
-    users: T[],
-    notificationCreator: (user: T) => Promise<UserNotificationModelForApi>,
-    emailSender: (emails: string[]) => Promise<any> | Bluebird<any>,
+  private async notify<T extends MUserWithNotificationSetting> (options: {
+    users: T[]
+    notificationCreator: (user: T) => Promise<UserNotificationModelForApi>
+    emailSender: (emails: string[]) => void
     settingGetter: (user: T) => UserNotificationSettingValue
   }) {
     const emails: string[] = []
@@ -558,7 +572,7 @@ class Notifier {
     }
 
     if (emails.length !== 0) {
-      await options.emailSender(emails)
+      options.emailSender(emails)
     }
   }
 
@@ -572,6 +586,19 @@ 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
+  }
+
   static get Instance () {
     return this.instance || (this.instance = new this())
   }