diff options
author | Chocobozzz <me@florianbigard.com> | 2019-12-19 10:35:47 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-12-19 11:34:00 +0100 |
commit | dddc8b1fe0c875f41c88f395b0d55cfea69add2c (patch) | |
tree | 06b84dc99f78d8422fc8eeda8a3372291660aed0 /server/lib | |
parent | 96f6278f3e5b35d6e812176c799088f88729a0b0 (diff) | |
download | PeerTube-dddc8b1fe0c875f41c88f395b0d55cfea69add2c.tar.gz PeerTube-dddc8b1fe0c875f41c88f395b0d55cfea69add2c.tar.zst PeerTube-dddc8b1fe0c875f41c88f395b0d55cfea69add2c.zip |
Don't notify on muted instance
Diffstat (limited to 'server/lib')
-rw-r--r-- | server/lib/notifier.ts | 40 |
1 files changed, 32 insertions, 8 deletions
diff --git a/server/lib/notifier.ts b/server/lib/notifier.ts index b7cc2607d..679b9bcf6 100644 --- a/server/lib/notifier.ts +++ b/server/lib/notifier.ts | |||
@@ -17,14 +17,16 @@ import { | |||
17 | MVideoFullLight | 17 | MVideoFullLight |
18 | } from '../typings/models/video' | 18 | } from '../typings/models/video' |
19 | import { | 19 | import { |
20 | MUser, | 20 | MUser, MUserAccount, |
21 | MUserDefault, | 21 | MUserDefault, |
22 | MUserNotifSettingAccount, | 22 | MUserNotifSettingAccount, |
23 | MUserWithNotificationSetting, | 23 | MUserWithNotificationSetting, |
24 | UserNotificationModelForApi | 24 | UserNotificationModelForApi |
25 | } from '@server/typings/models/user' | 25 | } from '@server/typings/models/user' |
26 | import { MActorFollowFull } from '../typings/models' | 26 | import { MAccountDefault, MActorFollowFull } from '../typings/models' |
27 | import { MVideoImportVideo } from '@server/typings/models/video/video-import' | 27 | import { MVideoImportVideo } from '@server/typings/models/video/video-import' |
28 | import { ServerBlocklistModel } from '@server/models/server/server-blocklist' | ||
29 | import { getServerActor } from '@server/helpers/utils' | ||
28 | 30 | ||
29 | class Notifier { | 31 | class Notifier { |
30 | 32 | ||
@@ -164,8 +166,7 @@ class Notifier { | |||
164 | // Not our user or user comments its own video | 166 | // Not our user or user comments its own video |
165 | if (!user || comment.Account.userId === user.id) return | 167 | if (!user || comment.Account.userId === user.id) return |
166 | 168 | ||
167 | const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, comment.accountId) | 169 | if (await this.isBlockedByServerOrAccount(user, comment.Account)) return |
168 | if (accountMuted) return | ||
169 | 170 | ||
170 | logger.info('Notifying user %s of new comment %s.', user.username, comment.url) | 171 | logger.info('Notifying user %s of new comment %s.', user.username, comment.url) |
171 | 172 | ||
@@ -210,12 +211,22 @@ class Notifier { | |||
210 | 211 | ||
211 | if (users.length === 0) return | 212 | if (users.length === 0) return |
212 | 213 | ||
213 | const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(users.map(u => u.Account.id), comment.accountId) | 214 | const serverAccountId = (await getServerActor()).Account.id |
215 | const sourceAccounts = users.map(u => u.Account.id).concat([ serverAccountId ]) | ||
216 | |||
217 | const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(sourceAccounts, comment.accountId) | ||
218 | const instanceMutedHash = await ServerBlocklistModel.isServerMutedByMulti(sourceAccounts, comment.Account.Actor.serverId) | ||
214 | 219 | ||
215 | logger.info('Notifying %d users of new comment %s.', users.length, comment.url) | 220 | logger.info('Notifying %d users of new comment %s.', users.length, comment.url) |
216 | 221 | ||
217 | function settingGetter (user: MUserNotifSettingAccount) { | 222 | function settingGetter (user: MUserNotifSettingAccount) { |
218 | if (accountMutedHash[user.Account.id] === true) return UserNotificationSettingValue.NONE | 223 | const accountId = user.Account.id |
224 | if ( | ||
225 | accountMutedHash[accountId] === true || instanceMutedHash[accountId] === true || | ||
226 | accountMutedHash[serverAccountId] === true || instanceMutedHash[serverAccountId] === true | ||
227 | ) { | ||
228 | return UserNotificationSettingValue.NONE | ||
229 | } | ||
219 | 230 | ||
220 | return user.NotificationSetting.commentMention | 231 | return user.NotificationSetting.commentMention |
221 | } | 232 | } |
@@ -254,9 +265,9 @@ class Notifier { | |||
254 | if (!user) return | 265 | if (!user) return |
255 | 266 | ||
256 | const followerAccount = actorFollow.ActorFollower.Account | 267 | const followerAccount = actorFollow.ActorFollower.Account |
268 | const followerAccountWithActor = Object.assign(followerAccount, { Actor: actorFollow.ActorFollower }) | ||
257 | 269 | ||
258 | const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, followerAccount.id) | 270 | if (await this.isBlockedByServerOrAccount(user, followerAccountWithActor)) return |
259 | if (accountMuted) return | ||
260 | 271 | ||
261 | logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName()) | 272 | logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName()) |
262 | 273 | ||
@@ -572,6 +583,19 @@ class Notifier { | |||
572 | return value & UserNotificationSettingValue.WEB | 583 | return value & UserNotificationSettingValue.WEB |
573 | } | 584 | } |
574 | 585 | ||
586 | private async isBlockedByServerOrAccount (user: MUserAccount, targetAccount: MAccountDefault) { | ||
587 | const serverAccountId = (await getServerActor()).Account.id | ||
588 | const sourceAccounts = [ serverAccountId, user.Account.id ] | ||
589 | |||
590 | const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(sourceAccounts, targetAccount.id) | ||
591 | if (accountMutedHash[serverAccountId] || accountMutedHash[user.Account.id]) return true | ||
592 | |||
593 | const instanceMutedHash = await ServerBlocklistModel.isServerMutedByMulti(sourceAccounts, targetAccount.Actor.serverId) | ||
594 | if (instanceMutedHash[serverAccountId] || instanceMutedHash[user.Account.id]) return true | ||
595 | |||
596 | return false | ||
597 | } | ||
598 | |||
575 | static get Instance () { | 599 | static get Instance () { |
576 | return this.instance || (this.instance = new this()) | 600 | return this.instance || (this.instance = new this()) |
577 | } | 601 | } |