aboutsummaryrefslogtreecommitdiff
path: root/matrix/pushrules
diff options
context:
space:
mode:
Diffstat (limited to 'matrix/pushrules')
-rw-r--r--matrix/pushrules/rule.go6
-rw-r--r--matrix/pushrules/rule_array_test.go293
-rw-r--r--matrix/pushrules/ruleset.go14
3 files changed, 303 insertions, 10 deletions
diff --git a/matrix/pushrules/rule.go b/matrix/pushrules/rule.go
index 5c32a05..ec51d24 100644
--- a/matrix/pushrules/rule.go
+++ b/matrix/pushrules/rule.go
@@ -27,7 +27,7 @@ type PushRuleCollection interface {
type PushRuleArray []*PushRule
-func (rules PushRuleArray) setType(typ PushRuleType) PushRuleArray {
+func (rules PushRuleArray) SetType(typ PushRuleType) PushRuleArray {
for _, rule := range rules {
rule.Type = typ
}
@@ -49,7 +49,7 @@ type PushRuleMap struct {
Type PushRuleType
}
-func (rules PushRuleArray) setTypeAndMap(typ PushRuleType) PushRuleMap {
+func (rules PushRuleArray) SetTypeAndMap(typ PushRuleType) PushRuleMap {
data := PushRuleMap{
Map: make(map[string]*PushRule),
Type: typ,
@@ -76,7 +76,7 @@ func (ruleMap PushRuleMap) GetActions(room Room, event *gomatrix.Event) PushActi
return nil
}
-func (ruleMap PushRuleMap) unmap() PushRuleArray {
+func (ruleMap PushRuleMap) Unmap() PushRuleArray {
array := make(PushRuleArray, len(ruleMap.Map))
index := 0
for _, rule := range ruleMap.Map {
diff --git a/matrix/pushrules/rule_array_test.go b/matrix/pushrules/rule_array_test.go
new file mode 100644
index 0000000..fdecd9c
--- /dev/null
+++ b/matrix/pushrules/rule_array_test.go
@@ -0,0 +1,293 @@
+// gomuks - A terminal Matrix client written in Go.
+// Copyright (C) 2018 Tulir Asokan
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+package pushrules_test
+
+import (
+ "testing"
+ "maunium.net/go/gomuks/matrix/pushrules"
+ "github.com/stretchr/testify/assert"
+)
+
+func TestPushRuleArray_GetActions_FirstMatchReturns(t *testing.T) {
+ cond1 := newMatchPushCondition("content.msgtype", "m.emote")
+ cond2 := newMatchPushCondition("content.body", "no match")
+ actions1 := pushrules.PushActionArray{
+ {Action: pushrules.ActionNotify},
+ {Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakHighlight},
+ {Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakSound, Value: "ping"},
+ }
+ rule1 := &pushrules.PushRule{
+ Type: pushrules.OverrideRule,
+ Enabled: true,
+ Conditions: []*pushrules.PushCondition{cond1, cond2},
+ Actions: actions1,
+ }
+
+ actions2 := pushrules.PushActionArray{
+ {Action: pushrules.ActionDontNotify},
+ {Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakHighlight, Value: false},
+ {Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakSound, Value: "pong"},
+ }
+ rule2 := &pushrules.PushRule{
+ Type: pushrules.RoomRule,
+ Enabled: true,
+ RuleID: "!fakeroom:maunium.net",
+ Actions: actions2,
+ }
+
+ actions3 := pushrules.PushActionArray{
+ {Action: pushrules.ActionCoalesce},
+ }
+ rule3 := &pushrules.PushRule{
+ Type: pushrules.SenderRule,
+ Enabled: true,
+ RuleID: "@tulir:maunium.net",
+ Actions: actions3,
+ }
+
+ rules := pushrules.PushRuleArray{rule1, rule2, rule3}
+
+ event := newFakeEvent("m.room.message", map[string]interface{}{
+ "msgtype": "m.emote",
+ "body": "is testing pushrules",
+ })
+ assert.Equal(t, rules.GetActions(blankTestRoom, event), actions2)
+}
+
+func TestPushRuleArray_GetActions_NoMatchesIsNil(t *testing.T) {
+ cond1 := newMatchPushCondition("content.msgtype", "m.emote")
+ cond2 := newMatchPushCondition("content.body", "no match")
+ actions1 := pushrules.PushActionArray{
+ {Action: pushrules.ActionNotify},
+ {Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakHighlight},
+ {Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakSound, Value: "ping"},
+ }
+ rule1 := &pushrules.PushRule{
+ Type: pushrules.OverrideRule,
+ Enabled: true,
+ Conditions: []*pushrules.PushCondition{cond1, cond2},
+ Actions: actions1,
+ }
+
+ actions2 := pushrules.PushActionArray{
+ {Action: pushrules.ActionDontNotify},
+ {Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakHighlight, Value: false},
+ {Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakSound, Value: "pong"},
+ }
+ rule2 := &pushrules.PushRule{
+ Type: pushrules.RoomRule,
+ Enabled: true,
+ RuleID: "!realroom:maunium.net",
+ Actions: actions2,
+ }
+
+ actions3 := pushrules.PushActionArray{
+ {Action: pushrules.ActionCoalesce},
+ }
+ rule3 := &pushrules.PushRule{
+ Type: pushrules.SenderRule,
+ Enabled: true,
+ RuleID: "@otheruser:maunium.net",
+ Actions: actions3,
+ }
+
+ rules := pushrules.PushRuleArray{rule1, rule2, rule3}
+
+ event := newFakeEvent("m.room.message", map[string]interface{}{
+ "msgtype": "m.emote",
+ "body": "is testing pushrules",
+ })
+ assert.Nil(t, rules.GetActions(blankTestRoom, event))
+}
+
+func TestPushRuleMap_GetActions_RoomRuleExists(t *testing.T) {
+ actions1 := pushrules.PushActionArray{
+ {Action: pushrules.ActionDontNotify},
+ {Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakHighlight, Value: false},
+ {Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakSound, Value: "pong"},
+ }
+ rule1 := &pushrules.PushRule{
+ Type: pushrules.RoomRule,
+ Enabled: true,
+ RuleID: "!realroom:maunium.net",
+ Actions: actions1,
+ }
+
+ actions2 := pushrules.PushActionArray{
+ {Action: pushrules.ActionNotify},
+ }
+ rule2 := &pushrules.PushRule{
+ Type: pushrules.RoomRule,
+ Enabled: true,
+ RuleID: "!thirdroom:maunium.net",
+ Actions: actions2,
+ }
+
+ actions3 := pushrules.PushActionArray{
+ {Action: pushrules.ActionCoalesce},
+ }
+ rule3 := &pushrules.PushRule{
+ Type: pushrules.RoomRule,
+ Enabled: true,
+ RuleID: "!fakeroom:maunium.net",
+ Actions: actions3,
+ }
+
+ rules := pushrules.PushRuleMap{
+ Map: map[string]*pushrules.PushRule{
+ rule1.RuleID: rule1,
+ rule2.RuleID: rule2,
+ rule3.RuleID: rule3,
+ },
+ Type: pushrules.RoomRule,
+ }
+
+ event := newFakeEvent("m.room.message", map[string]interface{}{
+ "msgtype": "m.emote",
+ "body": "is testing pushrules",
+ })
+ assert.Equal(t, rules.GetActions(blankTestRoom, event), actions3)
+}
+
+func TestPushRuleMap_GetActions_RoomRuleDoesntExist(t *testing.T) {
+ actions1 := pushrules.PushActionArray{
+ {Action: pushrules.ActionDontNotify},
+ {Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakHighlight, Value: false},
+ {Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakSound, Value: "pong"},
+ }
+ rule1 := &pushrules.PushRule{
+ Type: pushrules.RoomRule,
+ Enabled: true,
+ RuleID: "!realroom:maunium.net",
+ Actions: actions1,
+ }
+
+ actions2 := pushrules.PushActionArray{
+ {Action: pushrules.ActionNotify},
+ }
+ rule2 := &pushrules.PushRule{
+ Type: pushrules.RoomRule,
+ Enabled: true,
+ RuleID: "!thirdroom:maunium.net",
+ Actions: actions2,
+ }
+
+ rules := pushrules.PushRuleMap{
+ Map: map[string]*pushrules.PushRule{
+ rule1.RuleID: rule1,
+ rule2.RuleID: rule2,
+ },
+ Type: pushrules.RoomRule,
+ }
+
+ event := newFakeEvent("m.room.message", map[string]interface{}{
+ "msgtype": "m.emote",
+ "body": "is testing pushrules",
+ })
+ assert.Nil(t, rules.GetActions(blankTestRoom, event))
+}
+
+func TestPushRuleMap_GetActions_SenderRuleExists(t *testing.T) {
+ actions1 := pushrules.PushActionArray{
+ {Action: pushrules.ActionDontNotify},
+ {Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakHighlight, Value: false},
+ {Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakSound, Value: "pong"},
+ }
+ rule1 := &pushrules.PushRule{
+ Type: pushrules.SenderRule,
+ Enabled: true,
+ RuleID: "@tulir:maunium.net",
+ Actions: actions1,
+ }
+
+ actions2 := pushrules.PushActionArray{
+ {Action: pushrules.ActionNotify},
+ }
+ rule2 := &pushrules.PushRule{
+ Type: pushrules.SenderRule,
+ Enabled: true,
+ RuleID: "@someone:maunium.net",
+ Actions: actions2,
+ }
+
+ actions3 := pushrules.PushActionArray{
+ {Action: pushrules.ActionCoalesce},
+ }
+ rule3 := &pushrules.PushRule{
+ Type: pushrules.SenderRule,
+ Enabled: true,
+ RuleID: "@otheruser:matrix.org",
+ Actions: actions3,
+ }
+
+ rules := pushrules.PushRuleMap{
+ Map: map[string]*pushrules.PushRule{
+ rule1.RuleID: rule1,
+ rule2.RuleID: rule2,
+ rule3.RuleID: rule3,
+ },
+ Type: pushrules.SenderRule,
+ }
+
+ event := newFakeEvent("m.room.message", map[string]interface{}{
+ "msgtype": "m.emote",
+ "body": "is testing pushrules",
+ })
+ assert.Equal(t, rules.GetActions(blankTestRoom, event), actions1)
+}
+
+func TestPushRuleArray_SetTypeAndMap(t *testing.T) {
+ actions1 := pushrules.PushActionArray{
+ {Action: pushrules.ActionDontNotify},
+ {Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakHighlight, Value: false},
+ {Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakSound, Value: "pong"},
+ }
+ rule1 := &pushrules.PushRule{
+ Enabled: true,
+ RuleID: "@tulir:maunium.net",
+ Actions: actions1,
+ }
+
+ actions2 := pushrules.PushActionArray{
+ {Action: pushrules.ActionNotify},
+ }
+ rule2 := &pushrules.PushRule{
+ Enabled: true,
+ RuleID: "@someone:maunium.net",
+ Actions: actions2,
+ }
+
+ actions3 := pushrules.PushActionArray{
+ {Action: pushrules.ActionCoalesce},
+ }
+ rule3 := &pushrules.PushRule{
+ Enabled: true,
+ RuleID: "@otheruser:matrix.org",
+ Actions: actions3,
+ }
+
+ ruleArray := pushrules.PushRuleArray{rule1, rule2, rule3}
+ ruleMap := ruleArray.SetTypeAndMap(pushrules.SenderRule)
+ assert.Equal(t, pushrules.SenderRule, ruleMap.Type)
+ for _, rule := range ruleArray {
+ assert.Equal(t, rule, ruleMap.Map[rule.RuleID])
+ }
+ newRuleArray := ruleMap.Unmap()
+ for _, rule := range ruleArray {
+ assert.Contains(t, newRuleArray, rule)
+ }
+}
diff --git a/matrix/pushrules/ruleset.go b/matrix/pushrules/ruleset.go
index 8402dc9..940025f 100644
--- a/matrix/pushrules/ruleset.go
+++ b/matrix/pushrules/ruleset.go
@@ -53,11 +53,11 @@ func (rs *PushRuleset) UnmarshalJSON(raw []byte) (err error) {
return
}
- rs.Override = data.Override.setType(OverrideRule)
- rs.Content = data.Content.setType(ContentRule)
- rs.Room = data.Room.setTypeAndMap(RoomRule)
- rs.Sender = data.Sender.setTypeAndMap(SenderRule)
- rs.Underride = data.Underride.setType(UnderrideRule)
+ rs.Override = data.Override.SetType(OverrideRule)
+ rs.Content = data.Content.SetType(ContentRule)
+ rs.Room = data.Room.SetTypeAndMap(RoomRule)
+ rs.Sender = data.Sender.SetTypeAndMap(SenderRule)
+ rs.Underride = data.Underride.SetType(UnderrideRule)
return
}
@@ -66,8 +66,8 @@ func (rs *PushRuleset) MarshalJSON() ([]byte, error) {
data := rawPushRuleset{
Override: rs.Override,
Content: rs.Content,
- Room: rs.Room.unmap(),
- Sender: rs.Sender.unmap(),
+ Room: rs.Room.Unmap(),
+ Sender: rs.Sender.Unmap(),
Underride: rs.Underride,
}
return json.Marshal(&data)