diff options
author | Tulir Asokan <tulir@maunium.net> | 2018-03-21 20:01:52 +0200 |
---|---|---|
committer | Tulir Asokan <tulir@maunium.net> | 2018-03-21 20:01:52 +0200 |
commit | b4902d4edb27baf59b21747117d93db4e0e4e96c (patch) | |
tree | 5779f4e1e2aad23e46e1b57a84b1cee2b311ab3c | |
parent | efbce4c363b743817b4a368d6011c7aac9b8b176 (diff) |
Clear push rules when clearing cache and listen to push rule updates
-rw-r--r-- | config/session.go | 1 | ||||
-rw-r--r-- | matrix/ext/pushrules.go | 16 | ||||
-rw-r--r-- | matrix/matrix.go | 12 | ||||
-rw-r--r-- | matrix/sync.go | 3 |
4 files changed, 31 insertions, 1 deletions
diff --git a/config/session.go b/config/session.go index 2f2ff7c..631c954 100644 --- a/config/session.go +++ b/config/session.go @@ -52,6 +52,7 @@ func (config *Config) NewSession(mxid string) *Session { func (s *Session) Clear() { s.Rooms = make(map[string]*rooms.Room) + s.PushRules = nil s.NextBatch = "" s.FilterID = "" s.Save() diff --git a/matrix/ext/pushrules.go b/matrix/ext/pushrules.go index c156873..ccccbb0 100644 --- a/matrix/ext/pushrules.go +++ b/matrix/ext/pushrules.go @@ -26,6 +26,22 @@ func GetScopedPushRules(client *gomatrix.Client, scope string) (resp *PushRulese return } +func EventToPushRules(event *gomatrix.Event) (*PushRuleset, error) { + content, _ := event.Content["global"] + raw, err := json.Marshal(content) + if err != nil { + return nil, err + } + + ruleset := &PushRuleset{} + err = json.Unmarshal(raw, ruleset) + if err != nil { + return nil, err + } + + return ruleset, nil +} + type PushRuleset struct { Override PushRuleArray Content PushRuleArray diff --git a/matrix/matrix.go b/matrix/matrix.go index 4ec6d96..6ab922c 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -121,6 +121,7 @@ func (c *Container) Client() *gomatrix.Client { } func (c *Container) UpdatePushRules() { + debug.Print("Updating push rules...") resp, err := gomx_ext.GetPushRules(c.client) if err != nil { debug.Print("Failed to fetch push rules:", err) @@ -162,10 +163,10 @@ func (c *Container) OnLogin() { syncer.OnEventType("m.room.message", c.HandleMessage) syncer.OnEventType("m.room.member", c.HandleMembership) syncer.OnEventType("m.typing", c.HandleTyping) + syncer.OnEventType("m.push_rules", c.HandlePushRules) c.client.Syncer = syncer c.UpdateRoomList() - c.UpdatePushRules() } func (c *Container) Start() { @@ -221,6 +222,15 @@ func (c *Container) HandleMessage(evt *gomatrix.Event) { } } +func (c *Container) HandlePushRules(evt *gomatrix.Event) { + debug.Print("Received updated push rules") + var err error + c.config.Session.PushRules, err = gomx_ext.EventToPushRules(evt) + if err != nil { + debug.Print("Failed to convert event to push rules:", err) + } +} + func (c *Container) HandleMembership(evt *gomatrix.Event) { const Hour = 1 * 60 * 60 * 1000 if evt.Unsigned.Age > Hour { diff --git a/matrix/sync.go b/matrix/sync.go index ab5d047..9eeb87a 100644 --- a/matrix/sync.go +++ b/matrix/sync.go @@ -41,6 +41,9 @@ func (s *GomuksSyncer) ProcessResponse(res *gomatrix.RespSync, since string) (er for _, event := range res.Presence.Events { s.notifyListeners(event) } + for _, event := range res.AccountData.Events { + s.notifyListeners(event) + } for roomID, roomData := range res.Rooms.Join { room := s.Session.GetRoom(roomID) for _, event := range roomData.State.Events { |