From 16635dcde7b3402e7eff44864b4dba5a2ddd8a37 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 23 Mar 2018 01:00:13 +0200 Subject: Refactor PushRuleset#GetActions() and ViewMain event processing --- matrix/pushrules/rule.go | 4 ++++ matrix/pushrules/ruleset.go | 28 ++++++++++++++-------------- 2 files changed, 18 insertions(+), 14 deletions(-) (limited to 'matrix/pushrules') diff --git a/matrix/pushrules/rule.go b/matrix/pushrules/rule.go index 933d493..dd8a4d3 100644 --- a/matrix/pushrules/rule.go +++ b/matrix/pushrules/rule.go @@ -22,6 +22,10 @@ import ( "maunium.net/go/gomuks/matrix/rooms" ) +type PushRuleCollection interface { + GetActions(room *rooms.Room, event *gomatrix.Event) PushActionArray +} + type PushRuleArray []*PushRule func (rules PushRuleArray) setType(typ PushRuleType) PushRuleArray { diff --git a/matrix/pushrules/ruleset.go b/matrix/pushrules/ruleset.go index a8d212d..b533f94 100644 --- a/matrix/pushrules/ruleset.go +++ b/matrix/pushrules/ruleset.go @@ -65,23 +65,23 @@ func (rs *PushRuleset) MarshalJSON() ([]byte, error) { return json.Marshal(&data) } +// DefaultPushActions is the value returned if none of the rule +// collections in a Ruleset match the event given to GetActions() 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 *rooms.Room, event *gomatrix.Event) (match PushActionArray) { - if match = rs.Override.GetActions(room, event); match != nil { - return - } - if match = rs.Content.GetActions(room, event); match != nil { - return - } - if match = rs.Room.GetActions(room, event); match != nil { - return - } - if match = rs.Sender.GetActions(room, event); match != nil { - return - } - if match = rs.Underride.GetActions(room, event); match != nil { - return + // 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. + for _, pra := range arrays { + if match = pra.GetActions(room, event); match != nil { + // Match found, return it. + return + } } + // No match found, return default actions. return DefaultPushActions } -- cgit v1.2.3