diff options
Diffstat (limited to 'matrix/pushrules')
-rw-r--r-- | matrix/pushrules/condition.go | 29 | ||||
-rw-r--r-- | matrix/pushrules/condition_test.go | 10 | ||||
-rw-r--r-- | matrix/pushrules/pushrules.go | 24 | ||||
-rw-r--r-- | matrix/pushrules/pushrules_test.go | 4 | ||||
-rw-r--r-- | matrix/pushrules/rule.go | 17 | ||||
-rw-r--r-- | matrix/pushrules/ruleset.go | 4 |
6 files changed, 42 insertions, 46 deletions
diff --git a/matrix/pushrules/condition.go b/matrix/pushrules/condition.go index 08958a3..bacab56 100644 --- a/matrix/pushrules/condition.go +++ b/matrix/pushrules/condition.go @@ -21,16 +21,15 @@ import ( "strconv" "strings" - "maunium.net/go/gomatrix" + "maunium.net/go/mautrix" "maunium.net/go/gomuks/lib/glob" - "maunium.net/go/gomuks/matrix/rooms" ) // Room is an interface with the functions that are needed for processing room-specific push conditions type Room interface { - GetMember(mxid string) *rooms.Member - GetMembers() map[string]*rooms.Member - GetSessionOwner() *rooms.Member + GetMember(mxid string) *mautrix.Member + GetMembers() map[string]*mautrix.Member + GetSessionOwner() string } // PushCondKind is the type of a push condition. @@ -60,7 +59,7 @@ type PushCondition struct { var MemberCountFilterRegex = regexp.MustCompile("^(==|[<>]=?)?([0-9]+)$") // Match checks if this condition is fulfilled for the given event in the given room. -func (cond *PushCondition) Match(room Room, event *gomatrix.Event) bool { +func (cond *PushCondition) Match(room Room, event *mautrix.Event) bool { switch cond.Kind { case KindEventMatch: return cond.matchValue(room, event) @@ -73,7 +72,7 @@ func (cond *PushCondition) Match(room Room, event *gomatrix.Event) bool { } } -func (cond *PushCondition) matchValue(room Room, event *gomatrix.Event) bool { +func (cond *PushCondition) matchValue(room Room, event *mautrix.Event) bool { index := strings.IndexRune(cond.Key, '.') key := cond.Key subkey := "" @@ -89,7 +88,7 @@ func (cond *PushCondition) matchValue(room Room, event *gomatrix.Event) bool { switch key { case "type": - return pattern.MatchString(event.Type) + return pattern.MatchString(event.Type.String()) case "sender": return pattern.MatchString(event.Sender) case "room_id": @@ -100,23 +99,23 @@ func (cond *PushCondition) matchValue(room Room, event *gomatrix.Event) bool { } return pattern.MatchString(*event.StateKey) case "content": - val, _ := event.Content[subkey].(string) + val, _ := event.Content.Raw[subkey].(string) return pattern.MatchString(val) default: return false } } -func (cond *PushCondition) matchDisplayName(room Room, event *gomatrix.Event) bool { - member := room.GetSessionOwner() - if member == nil || member.UserID == event.Sender { +func (cond *PushCondition) matchDisplayName(room Room, event *mautrix.Event) bool { + ownerID := room.GetSessionOwner() + if ownerID == event.Sender { return false } - text, _ := event.Content["body"].(string) - return strings.Contains(text, member.DisplayName) + member := room.GetMember(ownerID) + return strings.Contains(event.Content.Body, member.Displayname) } -func (cond *PushCondition) matchMemberCount(room Room, event *gomatrix.Event) bool { +func (cond *PushCondition) matchMemberCount(room Room, event *mautrix.Event) bool { group := MemberCountFilterRegex.FindStringSubmatch(cond.MemberCountCondition) if len(group) != 3 { return false diff --git a/matrix/pushrules/condition_test.go b/matrix/pushrules/condition_test.go index 7fd06ee..750f2c7 100644 --- a/matrix/pushrules/condition_test.go +++ b/matrix/pushrules/condition_test.go @@ -21,7 +21,7 @@ import ( "testing" "github.com/stretchr/testify/assert" - "maunium.net/go/gomatrix" + "maunium.net/go/mautrix" "maunium.net/go/gomuks/matrix/pushrules" "maunium.net/go/gomuks/matrix/rooms" ) @@ -30,7 +30,7 @@ var ( blankTestRoom *rooms.Room displaynameTestRoom pushrules.Room - countConditionTestEvent *gomatrix.Event + countConditionTestEvent *mautrix.Event displaynamePushCondition *pushrules.PushCondition ) @@ -38,7 +38,7 @@ var ( func init() { blankTestRoom = rooms.NewRoom("!fakeroom:maunium.net", "@tulir:maunium.net") - countConditionTestEvent = &gomatrix.Event{ + countConditionTestEvent = &mautrix.Event{ Sender: "@tulir:maunium.net", Type: "m.room.message", Timestamp: 1523791120, @@ -56,8 +56,8 @@ func init() { } } -func newFakeEvent(evtType string, content map[string]interface{}) *gomatrix.Event { - return &gomatrix.Event{ +func newFakeEvent(evtType string, content map[string]interface{}) *mautrix.Event { + return &mautrix.Event{ Sender: "@tulir:maunium.net", Type: evtType, Timestamp: 1523791120, diff --git a/matrix/pushrules/pushrules.go b/matrix/pushrules/pushrules.go index 876713b..643f2f2 100644 --- a/matrix/pushrules/pushrules.go +++ b/matrix/pushrules/pushrules.go @@ -4,16 +4,16 @@ import ( "encoding/json" "net/url" - "maunium.net/go/gomatrix" + "maunium.net/go/mautrix" ) // GetPushRules returns the push notification rules for the global scope. -func GetPushRules(client *gomatrix.Client) (*PushRuleset, error) { +func GetPushRules(client *mautrix.Client) (*PushRuleset, error) { return GetScopedPushRules(client, "global") } // GetScopedPushRules returns the push notification rules for the given scope. -func GetScopedPushRules(client *gomatrix.Client, scope string) (resp *PushRuleset, err error) { +func GetScopedPushRules(client *mautrix.Client, scope string) (resp *PushRuleset, err error) { u, _ := url.Parse(client.BuildURL("pushrules", scope)) // client.BuildURL returns the URL without a trailing slash, but the pushrules endpoint requires the slash. u.Path += "/" @@ -21,19 +21,17 @@ func GetScopedPushRules(client *gomatrix.Client, scope string) (resp *PushRulese return } -// EventToPushRules converts a m.push_rules event to a PushRuleset by passing the data through JSON. -func EventToPushRules(event *gomatrix.Event) (*PushRuleset, error) { - content, _ := event.Content["global"] - raw, err := json.Marshal(content) - if err != nil { - return nil, err - } +type contentWithRuleset struct { + Ruleset *PushRuleset `json:"global"` +} - ruleset := &PushRuleset{} - err = json.Unmarshal(raw, ruleset) +// EventToPushRules converts a m.push_rules event to a PushRuleset by passing the data through JSON. +func EventToPushRules(event *mautrix.Event) (*PushRuleset, error) { + content := &contentWithRuleset{} + err := json.Unmarshal(event.Content.VeryRaw, content) if err != nil { return nil, err } - return ruleset, nil + return content.Ruleset, nil } diff --git a/matrix/pushrules/pushrules_test.go b/matrix/pushrules/pushrules_test.go index 09698ac..73fa787 100644 --- a/matrix/pushrules/pushrules_test.go +++ b/matrix/pushrules/pushrules_test.go @@ -21,7 +21,7 @@ import ( "testing" "github.com/stretchr/testify/assert" - "maunium.net/go/gomatrix" + "maunium.net/go/mautrix" "maunium.net/go/gomuks/matrix/pushrules" ) @@ -33,7 +33,7 @@ func init() { } func TestEventToPushRules(t *testing.T) { - event := &gomatrix.Event{ + event := &mautrix.Event{ Type: "m.push_rules", Timestamp: 1523380910, Content: mapExamplePushRules, diff --git a/matrix/pushrules/rule.go b/matrix/pushrules/rule.go index 5ede895..62318ac 100644 --- a/matrix/pushrules/rule.go +++ b/matrix/pushrules/rule.go @@ -18,7 +18,7 @@ package pushrules import ( "encoding/gob" - "maunium.net/go/gomatrix" + "maunium.net/go/mautrix" "maunium.net/go/gomuks/lib/glob" ) @@ -28,7 +28,7 @@ func init() { } type PushRuleCollection interface { - GetActions(room Room, event *gomatrix.Event) PushActionArray + GetActions(room Room, event *mautrix.Event) PushActionArray } type PushRuleArray []*PushRule @@ -40,7 +40,7 @@ func (rules PushRuleArray) SetType(typ PushRuleType) PushRuleArray { return rules } -func (rules PushRuleArray) GetActions(room Room, event *gomatrix.Event) PushActionArray { +func (rules PushRuleArray) GetActions(room Room, event *mautrix.Event) PushActionArray { for _, rule := range rules { if !rule.Match(room, event) { continue @@ -67,7 +67,7 @@ func (rules PushRuleArray) SetTypeAndMap(typ PushRuleType) PushRuleMap { return data } -func (ruleMap PushRuleMap) GetActions(room Room, event *gomatrix.Event) PushActionArray { +func (ruleMap PushRuleMap) GetActions(room Room, event *mautrix.Event) PushActionArray { var rule *PushRule var found bool switch ruleMap.Type { @@ -122,7 +122,7 @@ type PushRule struct { Pattern string `json:"pattern,omitempty"` } -func (rule *PushRule) Match(room Room, event *gomatrix.Event) bool { +func (rule *PushRule) Match(room Room, event *mautrix.Event) bool { if !rule.Enabled { return false } @@ -140,7 +140,7 @@ func (rule *PushRule) Match(room Room, event *gomatrix.Event) bool { } } -func (rule *PushRule) matchConditions(room Room, event *gomatrix.Event) bool { +func (rule *PushRule) matchConditions(room Room, event *mautrix.Event) bool { for _, cond := range rule.Conditions { if !cond.Match(room, event) { return false @@ -149,11 +149,10 @@ func (rule *PushRule) matchConditions(room Room, event *gomatrix.Event) bool { return true } -func (rule *PushRule) matchPattern(room Room, event *gomatrix.Event) bool { +func (rule *PushRule) matchPattern(room Room, event *mautrix.Event) bool { pattern, err := glob.Compile(rule.Pattern) if err != nil { return false } - text, _ := event.Content["body"].(string) - return pattern.MatchString(text) + return pattern.MatchString(event.Content.Body) } diff --git a/matrix/pushrules/ruleset.go b/matrix/pushrules/ruleset.go index 940025f..366702e 100644 --- a/matrix/pushrules/ruleset.go +++ b/matrix/pushrules/ruleset.go @@ -19,7 +19,7 @@ package pushrules import ( "encoding/json" - "maunium.net/go/gomatrix" + "maunium.net/go/mautrix" ) type PushRuleset struct { @@ -80,7 +80,7 @@ var DefaultPushActions = make(PushActionArray, 0) // GetActions matches the given event against all of the push rule // collections in this push ruleset in the order of priority as // specified in spec section 11.12.1.4. -func (rs *PushRuleset) GetActions(room Room, event *gomatrix.Event) (match PushActionArray) { +func (rs *PushRuleset) GetActions(room Room, event *mautrix.Event) (match PushActionArray) { // Add push rule collections to array in priority order arrays := []PushRuleCollection{rs.Override, rs.Content, rs.Room, rs.Sender, rs.Underride} // Loop until one of the push rule collections matches the room/event combo. |