hicli/sync: cache push rules in memory
Some checks are pending
Go / Lint (latest) (push) Waiting to run
Go / Build (old, libolm) (push) Waiting to run
Go / Build (latest, libolm) (push) Waiting to run
Go / Build (old, goolm) (push) Waiting to run
Go / Build (latest, goolm) (push) Waiting to run

This commit is contained in:
Tulir Asokan 2024-10-13 17:14:33 +03:00
commit 5cccf93cdc
2 changed files with 24 additions and 0 deletions

View file

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

View file

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