From b1c940a0a8d0416b170b4c4b49626dccd9b9ad5d Mon Sep 17 00:00:00 2001 From: FIGBERT Date: Tue, 8 Aug 2023 14:22:29 -0700 Subject: [PATCH] Add sync implementation to headless.go --- headless/headless.go | 28 +++++++++++++++++++++++++- matrix/matrix.go | 48 +++++++++++++++++++++++++++----------------- 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/headless/headless.go b/headless/headless.go index b4f0d51..98afca7 100644 --- a/headless/headless.go +++ b/headless/headless.go @@ -1,6 +1,7 @@ package headless import ( + "context" "errors" "fmt" "os" @@ -10,6 +11,7 @@ import ( "maunium.net/go/mautrix/crypto/ssss" "maunium.net/go/gomuks/initialize" + "maunium.net/go/gomuks/matrix" "maunium.net/go/gomuks/ui" ) @@ -76,8 +78,32 @@ func HeadlessInit(conf HeadlessConfig) error { // sync // how? // this does too much: gmx.Matrix().Start() + // + // figbert: + // just looking to perform the initial sync. is gmx.Matrix().Client().Sync() + // the way to go? should i copy+paste the synce initialization from Start() + // and OnLogin()? + // + // tulir: + // not sure if there's any easy way to run a single sync, maybe calling + // Client.FullSyncRequest + Syncer.ProcessSync manually + resp, err := gmx.Matrix().Client().FullSyncRequest(mautrix.ReqSync{ + Timeout: 30000, + Since: "", + FilterID: "", + FullState: true, + SetPresence: gmx.Matrix().Client().SyncPresence, + Context: context.Background(), + StreamResponse: true, + }) + if err != nil { + return err + } - return nil + gmx.Matrix().(*matrix.Container).InitSyncer() + err = gmx.Matrix().(*matrix.Container).ProcessSyncResponse(resp, "") + + return err } func initDirs() (string, string, string, string, error) { diff --git a/matrix/matrix.go b/matrix/matrix.go index 3a9affe..9c2987f 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -381,15 +381,9 @@ func (s StubSyncingModal) SetSteps(i int) {} func (s StubSyncingModal) Step() {} func (s StubSyncingModal) Close() {} -// OnLogin initializes the syncer and updates the room list. -func (c *Container) OnLogin() { - c.cryptoOnLogin() - c.ui.OnLogin() - - c.client.Store = c.config - - debug.Print("Initializing syncer") +func (c *Container) InitSyncer() { c.syncer = NewGomuksSyncer(c.config.Rooms) + if c.crypto != nil { c.syncer.OnSync(c.crypto.ProcessSyncResponse) c.syncer.OnEventType(event.StateMember, func(source mautrix.EventSource, evt *event.Event) { @@ -403,6 +397,7 @@ func (c *Container) OnLogin() { } else { c.syncer.OnEventType(event.EventEncrypted, c.HandleEncryptedUnsupported) } + c.syncer.OnEventType(event.EventMessage, c.HandleMessage) c.syncer.OnEventType(event.EventSticker, c.HandleMessage) c.syncer.OnEventType(event.EventReaction, c.HandleMessage) @@ -418,16 +413,7 @@ func (c *Container) OnLogin() { c.syncer.OnEventType(event.AccountDataPushRules, c.HandlePushRules) c.syncer.OnEventType(event.AccountDataRoomTags, c.HandleTag) c.syncer.OnEventType(AccountDataGomuksPreferences, c.HandlePreferences) - if len(c.config.AuthCache.NextBatch) == 0 { - c.syncer.Progress = c.ui.MainView().OpenSyncingModal() - c.syncer.Progress.SetMessage("Waiting for /sync response from server") - c.syncer.Progress.SetIndeterminate() - c.syncer.FirstDoneCallback = func() { - c.syncer.Progress.Close() - c.syncer.Progress = StubSyncingModal{} - c.syncer.FirstDoneCallback = nil - } - } + c.syncer.InitDoneCallback = func() { debug.Print("Initial sync done") c.config.AuthCache.InitialSyncDone = true @@ -448,7 +434,33 @@ func (c *Container) OnLogin() { runtime.GC() dbg.FreeOSMemory() } + c.client.Syncer = c.syncer +} + +func (c *Container) ProcessSyncResponse(res *mautrix.RespSync, since string) error { + return c.syncer.ProcessResponse(res, since) +} + +// OnLogin initializes the syncer and updates the room list. +func (c *Container) OnLogin() { + c.cryptoOnLogin() + c.ui.OnLogin() + + c.client.Store = c.config + + debug.Print("Initializing syncer") + c.InitSyncer() + if len(c.config.AuthCache.NextBatch) == 0 { + c.syncer.Progress = c.ui.MainView().OpenSyncingModal() + c.syncer.Progress.SetMessage("Waiting for /sync response from server") + c.syncer.Progress.SetIndeterminate() + c.syncer.FirstDoneCallback = func() { + c.syncer.Progress.Close() + c.syncer.Progress = StubSyncingModal{} + c.syncer.FirstDoneCallback = nil + } + } debug.Print("Setting existing rooms") c.ui.MainView().SetRooms(c.config.Rooms)