Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Implement MSC3987
Browse files Browse the repository at this point in the history
  • Loading branch information
S7evinK committed Oct 23, 2023
1 parent 8b3adaf commit 1b7ce13
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 28 deletions.
2 changes: 1 addition & 1 deletion clientapi/clientapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ func TestPushRules(t *testing.T) {
validateFunc: func(t *testing.T, respBody *bytes.Buffer) {
actions := gjson.GetBytes(respBody.Bytes(), "actions").Array()
// only a basic check
assert.Equal(t, 1, len(actions))
assert.Equal(t, 0, len(actions))
},
},
{
Expand Down
2 changes: 0 additions & 2 deletions internal/pushrules/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ func TestActionJSON(t *testing.T) {
Want Action
}{
{Action{Kind: NotifyAction}},
{Action{Kind: DontNotifyAction}},
{Action{Kind: CoalesceAction}},
{Action{Kind: SetTweakAction}},

{Action{Kind: SetTweakAction, Tweak: SoundTweak, Value: "default"}},
Expand Down
10 changes: 4 additions & 6 deletions internal/pushrules/default_override.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var (
RuleID: MRuleMaster,
Default: true,
Enabled: false,
Actions: []*Action{{Kind: DontNotifyAction}},
Actions: []*Action{},
}
mRuleSuppressNoticesDefinition = Rule{
RuleID: MRuleSuppressNotices,
Expand All @@ -43,7 +43,7 @@ var (
Pattern: pointer("m.notice"),
},
},
Actions: []*Action{{Kind: DontNotifyAction}},
Actions: []*Action{},
}
mRuleMemberEventDefinition = Rule{
RuleID: MRuleMemberEvent,
Expand All @@ -56,7 +56,7 @@ var (
Pattern: pointer("m.room.member"),
},
},
Actions: []*Action{{Kind: DontNotifyAction}},
Actions: []*Action{},
}
mRuleContainsDisplayNameDefinition = Rule{
RuleID: MRuleContainsDisplayName,
Expand Down Expand Up @@ -152,9 +152,7 @@ var (
Pattern: pointer("m.reaction"),
},
},
Actions: []*Action{
{Kind: DontNotifyAction},
},
Actions: []*Action{},
}
)

Expand Down
6 changes: 3 additions & 3 deletions internal/pushrules/default_pushrules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func TestDefaultRules(t *testing.T) {
// Default override rules
{
name: ".m.rule.master",
inputBytes: []byte(`{"rule_id":".m.rule.master","default":true,"enabled":false,"actions":["dont_notify"]}`),
inputBytes: []byte(`{"rule_id":".m.rule.master","default":true,"enabled":false,"actions":[]}`),
want: mRuleMasterDefinition,
},
{
name: ".m.rule.suppress_notices",
inputBytes: []byte(`{"rule_id":".m.rule.suppress_notices","default":true,"enabled":true,"conditions":[{"kind":"event_match","key":"content.msgtype","pattern":"m.notice"}],"actions":["dont_notify"]}`),
inputBytes: []byte(`{"rule_id":".m.rule.suppress_notices","default":true,"enabled":true,"conditions":[{"kind":"event_match","key":"content.msgtype","pattern":"m.notice"}],"actions":[]}`),
want: mRuleSuppressNoticesDefinition,
},
{
Expand All @@ -36,7 +36,7 @@ func TestDefaultRules(t *testing.T) {
},
{
name: ".m.rule.member_event",
inputBytes: []byte(`{"rule_id":".m.rule.member_event","default":true,"enabled":true,"conditions":[{"kind":"event_match","key":"type","pattern":"m.room.member"}],"actions":["dont_notify"]}`),
inputBytes: []byte(`{"rule_id":".m.rule.member_event","default":true,"enabled":true,"conditions":[{"kind":"event_match","key":"type","pattern":"m.room.member"}],"actions":[]}`),
want: mRuleMemberEventDefinition,
},
{
Expand Down
5 changes: 1 addition & 4 deletions internal/pushrules/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ func ActionsToTweaks(as []*Action) (ActionKind, map[string]interface{}, error) {

for _, a := range as {
switch a.Kind {
case DontNotifyAction:
// Don't bother processing any further
return DontNotifyAction, nil, nil

case DontNotifyAction: // Ignored
case SetTweakAction:
if tweaks == nil {
tweaks = map[string]interface{}{}
Expand Down
5 changes: 2 additions & 3 deletions internal/pushrules/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ func TestActionsToTweaks(t *testing.T) {
{"empty", nil, UnknownAction, nil},
{"zero", []*Action{{}}, UnknownAction, nil},
{"onlyPrimary", []*Action{{Kind: NotifyAction}}, NotifyAction, nil},
{"onlyPrimaryDontNotify", []*Action{{Kind: DontNotifyAction}}, DontNotifyAction, nil},
{"onlyPrimaryDontNotify", []*Action{}, UnknownAction, nil},
{"onlyTweak", []*Action{{Kind: SetTweakAction, Tweak: HighlightTweak}}, UnknownAction, map[string]interface{}{"highlight": nil}},
{"onlyTweakWithValue", []*Action{{Kind: SetTweakAction, Tweak: SoundTweak, Value: "default"}}, UnknownAction, map[string]interface{}{"sound": "default"}},
{
"all",
[]*Action{
{Kind: CoalesceAction},
{Kind: SetTweakAction, Tweak: HighlightTweak},
{Kind: SetTweakAction, Tweak: SoundTweak, Value: "default"},
},
CoalesceAction,
UnknownAction,
map[string]interface{}{"highlight": nil, "sound": "default"},
},
}
Expand Down
4 changes: 2 additions & 2 deletions userapi/consumers/roomserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,8 @@ func (s *OutputRoomEventConsumer) notifyLocal(ctx context.Context, event *rstype
if err != nil {
return fmt.Errorf("pushrules.ActionsToTweaks: %w", err)
}
// TODO: support coalescing.
if a != pushrules.NotifyAction && a != pushrules.CoalesceAction {

if a != pushrules.NotifyAction {
log.WithFields(log.Fields{
"event_id": event.EventID(),
"room_id": event.RoomID().String(),
Expand Down
10 changes: 3 additions & 7 deletions userapi/consumers/roomserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,8 @@ func Test_evaluatePushRules(t *testing.T) {
{
name: "m.reaction doesn't notify",
eventContent: `{"type":"m.reaction","room_id":"!room:example.com"}`,
wantAction: pushrules.DontNotifyAction,
wantActions: []*pushrules.Action{
{
Kind: pushrules.DontNotifyAction,
},
},
wantAction: "",
wantActions: []*pushrules.Action{},
},
{
name: "m.room.message notifies",
Expand Down Expand Up @@ -136,7 +132,7 @@ func Test_evaluatePushRules(t *testing.T) {
t.Fatalf("expected action to be '%s', got '%s'", tc.wantAction, gotAction)
}
// this is taken from `notifyLocal`
if tc.wantNotify && gotAction != pushrules.NotifyAction && gotAction != pushrules.CoalesceAction {
if tc.wantNotify && gotAction != pushrules.NotifyAction {
t.Fatalf("expected to notify but didn't")
}
})
Expand Down

0 comments on commit 1b7ce13

Please sign in to comment.