1
0
Fork 0
forked from Mirrors/gomuks

Add sync implementation to headless.go

This commit is contained in:
FIGBERT 2023-08-08 14:22:29 -07:00
parent 704fc53db1
commit b1c940a0a8
No known key found for this signature in database
GPG key ID: 67F1598D607A844B
2 changed files with 57 additions and 19 deletions

View file

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

View file

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