diff options
author | Tulir Asokan <tulir@maunium.net> | 2018-03-23 01:00:13 +0200 |
---|---|---|
committer | Tulir Asokan <tulir@maunium.net> | 2018-03-23 01:00:13 +0200 |
commit | 16635dcde7b3402e7eff44864b4dba5a2ddd8a37 (patch) | |
tree | 35c00039aa5c8af833f94623ae717efae7b1ebf9 /matrix | |
parent | a5e7800ab6e0e3ebf38b1a0e809d64afba649024 (diff) |
Refactor PushRuleset#GetActions() and ViewMain event processing
Diffstat (limited to 'matrix')
-rw-r--r-- | matrix/matrix.go | 50 | ||||
-rw-r--r-- | matrix/pushrules/rule.go | 4 | ||||
-rw-r--r-- | matrix/pushrules/ruleset.go | 28 |
3 files changed, 58 insertions, 24 deletions
diff --git a/matrix/matrix.go b/matrix/matrix.go index 9423f63..b062057 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -222,11 +222,16 @@ func (c *Container) NotifyMessage(room *rooms.Room, sender, text string, critica // HandleMessage is the event handler for the m.room.message timeline event. func (c *Container) HandleMessage(evt *gomatrix.Event) { - room, message := c.ui.MainView().ProcessMessageEvent(evt) - if room != nil { - pushRules := c.PushRules().GetActions(room.Room, evt).Should() + roomView := c.ui.MainView().GetRoom(evt.RoomID) + if roomView == nil { + return + } + + message := c.ui.MainView().ProcessMessageEvent(roomView, evt) + if message != nil { + pushRules := c.PushRules().GetActions(roomView.Room, evt).Should() if (pushRules.Notify || !pushRules.NotifySpecified) && evt.Sender != c.config.Session.UserID { - c.NotifyMessage(room.Room, message.Sender, message.Text, pushRules.Highlight) + c.NotifyMessage(roomView.Room, message.Sender, message.Text, pushRules.Highlight) } if pushRules.Highlight { message.TextColor = tcell.ColorYellow @@ -234,7 +239,7 @@ func (c *Container) HandleMessage(evt *gomatrix.Event) { if pushRules.PlaySound { // TODO play sound } - room.AddMessage(message, widget.AppendMessage) + roomView.AddMessage(message, widget.AppendMessage) c.ui.Render() } } @@ -249,6 +254,22 @@ func (c *Container) HandlePushRules(evt *gomatrix.Event) { } } +func (c *Container) processOwnMembershipChange(evt *gomatrix.Event) { + membership, _ := evt.Content["membership"].(string) + prevMembership := "leave" + if evt.Unsigned.PrevContent != nil { + prevMembership, _ = evt.Unsigned.PrevContent["membership"].(string) + } + if membership == prevMembership { + return + } + if membership == "join" { + c.ui.MainView().AddRoom(evt.RoomID) + } else if membership == "leave" { + c.ui.MainView().RemoveRoom(evt.RoomID) + } +} + // HandleMembership is the event handler for the m.room.membership state event. func (c *Container) HandleMembership(evt *gomatrix.Event) { const Hour = 1 * 60 * 60 * 1000 @@ -256,14 +277,23 @@ func (c *Container) HandleMembership(evt *gomatrix.Event) { return } - room, message := c.ui.MainView().ProcessMembershipEvent(evt, true) - if room != nil { + if evt.StateKey != nil && *evt.StateKey == c.config.Session.UserID { + c.processOwnMembershipChange(evt) + } + + roomView := c.ui.MainView().GetRoom(evt.RoomID) + if roomView == nil { + return + } + + message := c.ui.MainView().ProcessMembershipEvent(roomView, evt) + if message != nil { // TODO this shouldn't be necessary - room.Room.UpdateState(evt) + roomView.Room.UpdateState(evt) // TODO This should probably also be in a different place - room.UpdateUserList() + roomView.UpdateUserList() - room.AddMessage(message, widget.AppendMessage) + roomView.AddMessage(message, widget.AppendMessage) c.ui.Render() } } 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 } |