mirror of
https://mau.dev/mautrix/go.git
synced 2026-03-14 14:25:53 +01:00
hicli/sync: cache push rules in memory
This commit is contained in:
parent
e48f081942
commit
5cccf93cdc
2 changed files with 24 additions and 0 deletions
|
|
@ -27,6 +27,7 @@ import (
|
|||
"maunium.net/go/mautrix/crypto/backup"
|
||||
"maunium.net/go/mautrix/hicli/database"
|
||||
"maunium.net/go/mautrix/id"
|
||||
"maunium.net/go/mautrix/pushrules"
|
||||
)
|
||||
|
||||
type HiClient struct {
|
||||
|
|
@ -43,6 +44,8 @@ type HiClient struct {
|
|||
KeyBackupVersion id.KeyBackupVersion
|
||||
KeyBackupKey *backup.MegolmBackupKey
|
||||
|
||||
PushRules atomic.Pointer[pushrules.PushRuleset]
|
||||
|
||||
EventHandler func(evt any)
|
||||
|
||||
firstSyncReceived bool
|
||||
|
|
@ -212,6 +215,7 @@ func (h *HiClient) Sync() {
|
|||
defer cancel()
|
||||
h.stopSync.Store(&cancel)
|
||||
go h.RunRequestQueue(h.Log.WithContext(ctx))
|
||||
go h.LoadPushRules(h.Log.WithContext(ctx))
|
||||
ctx = log.WithContext(ctx)
|
||||
log.Info().Msg("Starting syncing")
|
||||
err := h.Client.SyncWithContext(ctx)
|
||||
|
|
@ -222,6 +226,16 @@ func (h *HiClient) Sync() {
|
|||
}
|
||||
}
|
||||
|
||||
func (h *HiClient) LoadPushRules(ctx context.Context) {
|
||||
rules, err := h.Client.GetPushRules(ctx)
|
||||
if err != nil {
|
||||
zerolog.Ctx(ctx).Err(err).Msg("Failed to load push rules")
|
||||
return
|
||||
}
|
||||
h.PushRules.Store(rules)
|
||||
zerolog.Ctx(ctx).Debug().Msg("Updated push rules from fetch")
|
||||
}
|
||||
|
||||
func (h *HiClient) Stop() {
|
||||
h.Client.StopSync()
|
||||
if fn := h.stopSync.Swap(nil); fn != nil {
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ import (
|
|||
"maunium.net/go/mautrix/event"
|
||||
"maunium.net/go/mautrix/hicli/database"
|
||||
"maunium.net/go/mautrix/id"
|
||||
"maunium.net/go/mautrix/pushrules"
|
||||
)
|
||||
|
||||
type syncContext struct {
|
||||
|
|
@ -103,6 +104,15 @@ func (h *HiClient) processSyncResponse(ctx context.Context, resp *mautrix.RespSy
|
|||
if err != nil {
|
||||
return fmt.Errorf("failed to save account data event %s: %w", evt.Type.Type, err)
|
||||
}
|
||||
if evt.Type == event.AccountDataPushRules {
|
||||
err = evt.Content.ParseRaw(evt.Type)
|
||||
if err != nil {
|
||||
zerolog.Ctx(ctx).Warn().Err(err).Msg("Failed to parse push rules in sync")
|
||||
} else if pushRules, ok := evt.Content.Parsed.(*pushrules.EventContent); ok {
|
||||
h.PushRules.Store(pushRules.Ruleset)
|
||||
zerolog.Ctx(ctx).Debug().Msg("Updated push rules from sync")
|
||||
}
|
||||
}
|
||||
}
|
||||
for roomID, room := range resp.Rooms.Join {
|
||||
err := h.processSyncJoinedRoom(ctx, roomID, room)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue