Clear push rules when clearing cache and listen to push rule updates

This commit is contained in:
Tulir Asokan 2018-03-21 20:01:52 +02:00
parent efbce4c363
commit b4902d4edb
4 changed files with 31 additions and 1 deletions

View File

@ -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()

View File

@ -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

View File

@ -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 {

View File

@ -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 {