@@ -36,6 +36,7 @@ import {
36
36
PushRuleCondition ,
37
37
PushRuleKind ,
38
38
PushRuleSet ,
39
+ RuleId ,
39
40
TweakName ,
40
41
} from "./@types/PushRules" ;
41
42
import { EventType } from "./@types/event" ;
@@ -70,6 +71,36 @@ const DEFAULT_OVERRIDE_RULES: IPushRule[] = [
70
71
] ,
71
72
actions : [ PushRuleActionName . DontNotify ] ,
72
73
} ,
74
+ {
75
+ rule_id : RuleId . IsUserMention ,
76
+ default : true ,
77
+ enabled : true ,
78
+ conditions : [
79
+ {
80
+ kind : ConditionKind . EventPropertyContains ,
81
+ key : "content.org\\.matrix\\.msc3952\\.mentions.user_ids" ,
82
+ value : "" , // The user ID is dynamically added in rewriteDefaultRules.
83
+ } ,
84
+ ] ,
85
+ actions : [ PushRuleActionName . Notify , { set_tweak : TweakName . Highlight } ] ,
86
+ } ,
87
+ {
88
+ rule_id : RuleId . IsRoomMention ,
89
+ default : true ,
90
+ enabled : true ,
91
+ conditions : [
92
+ {
93
+ kind : ConditionKind . EventPropertyIs ,
94
+ key : "content.org\\.matrix\\.msc3952\\.mentions.room" ,
95
+ value : true ,
96
+ } ,
97
+ {
98
+ kind : ConditionKind . SenderNotificationPermission ,
99
+ key : "room" ,
100
+ } ,
101
+ ] ,
102
+ actions : [ PushRuleActionName . Notify , { set_tweak : TweakName . Highlight } ] ,
103
+ } ,
73
104
{
74
105
// For homeservers which don't support MSC3786 yet
75
106
rule_id : ".org.matrix.msc3786.rule.room.server_acl" ,
@@ -160,9 +191,10 @@ export class PushProcessor {
160
191
* where applicable. Useful for upgrading push rules to more strict
161
192
* conditions when the server is falling behind on defaults.
162
193
* @param incomingRules - The client's existing push rules
194
+ * @param userId - The Matrix ID of the client.
163
195
* @returns The rewritten rules
164
196
*/
165
- public static rewriteDefaultRules ( incomingRules : IPushRules ) : IPushRules {
197
+ public static rewriteDefaultRules ( incomingRules : IPushRules , userId : string | undefined = undefined ) : IPushRules {
166
198
let newRules : IPushRules = JSON . parse ( JSON . stringify ( incomingRules ) ) ; // deep clone
167
199
168
200
// These lines are mostly to make the tests happy. We shouldn't run into these
@@ -174,8 +206,22 @@ export class PushProcessor {
174
206
175
207
// Merge the client-level defaults with the ones from the server
176
208
const globalOverrides = newRules . global . override ;
177
- for ( const override of DEFAULT_OVERRIDE_RULES ) {
178
- const existingRule = globalOverrides . find ( ( r ) => r . rule_id === override . rule_id ) ;
209
+ for ( const originalOverride of DEFAULT_OVERRIDE_RULES ) {
210
+ const existingRule = globalOverrides . find ( ( r ) => r . rule_id === originalOverride . rule_id ) ;
211
+
212
+ // Dynamically add the user ID as the value for the is_user_mention rule.
213
+ let override : IPushRule ;
214
+ if ( originalOverride . rule_id === RuleId . IsUserMention ) {
215
+ // If the user ID wasn't provided, skip the rule.
216
+ if ( ! userId ) {
217
+ continue ;
218
+ }
219
+
220
+ override = JSON . parse ( JSON . stringify ( originalOverride ) ) ; // deep clone
221
+ override . conditions ! [ 0 ] . value = userId ;
222
+ } else {
223
+ override = originalOverride ;
224
+ }
179
225
180
226
if ( existingRule ) {
181
227
// Copy over the actions, default, and conditions. Don't touch the user's preference.
@@ -668,6 +714,17 @@ export class PushProcessor {
668
714
}
669
715
670
716
public ruleMatchesEvent ( rule : Partial < IPushRule > & Pick < IPushRule , "conditions" > , ev : MatrixEvent ) : boolean {
717
+ // Disable the deprecated mentions push rules if the new mentions property exists.
718
+ if (
719
+ this . client . supportsIntentionalMentions ( ) &&
720
+ ev . getContent ( ) [ "org.matrix.msc3952.mentions" ] !== undefined &&
721
+ ( rule . rule_id === RuleId . ContainsUserName ||
722
+ rule . rule_id === RuleId . ContainsDisplayName ||
723
+ rule . rule_id === RuleId . AtRoomNotification )
724
+ ) {
725
+ return false ;
726
+ }
727
+
671
728
return ! rule . conditions ?. some ( ( cond ) => ! this . eventFulfillsCondition ( cond , ev ) ) ;
672
729
}
673
730
0 commit comments