Browse Source

Consider case where device ID is empty and log a warning

Signed-off-by: Nikos Filippakis <me@nfil.dev>
pull/324/head
Nikos Filippakis 4 years ago
parent
commit
689f112d8f
No known key found for this signature in database GPG Key ID: 7110E4356101F017
  1. 32
      clients/bot_client.go
  2. 27
      clients/clients.go

32
clients/bot_client.go

@ -1,6 +1,8 @@
package clients
import (
"time"
"github.com/matrix-org/go-neb/api"
"github.com/matrix-org/go-neb/database"
"github.com/matrix-org/go-neb/matrix"
@ -38,7 +40,11 @@ func (botClient *BotClient) InitOlmMachine(client *mautrix.Client, nebStore *mat
cryptoStore = sqlCryptoStore
cryptoLogger.Debug("Using SQL backend as the crypto store")
} else {
cryptoStore, err = crypto.NewGobStore(client.DeviceID.String() + ".gob")
deviceID := client.DeviceID.String()
if deviceID == "" {
deviceID = "_empty_device_id"
}
cryptoStore, err = crypto.NewGobStore(deviceID + ".gob")
if err != nil {
return
}
@ -111,3 +117,27 @@ func (botClient *BotClient) SendMessageEvent(roomID id.RoomID, evtType mevt.Type
}
return botClient.Client.SendMessageEvent(roomID, evtType, content, extra...)
}
// Sync loops to keep syncing the client with the homeserver by calling the /sync endpoint.
func (botClient *BotClient) Sync() {
// Get the state store up to date
resp, err := botClient.SyncRequest(30000, "", "", true, mevt.PresenceOnline)
if err != nil {
log.WithError(err).Error("Error performing initial sync")
return
}
botClient.stateStore.UpdateStateStore(resp)
for {
if e := botClient.Client.Sync(); e != nil {
log.WithFields(log.Fields{
log.ErrorKey: e,
"user_id": botClient.config.UserID,
}).Error("Fatal Sync() error")
time.Sleep(10 * time.Second)
} else {
log.WithField("user_id", botClient.config.UserID).Info("Stopping Sync()")
return
}
}
}

27
clients/clients.go

@ -6,7 +6,6 @@ import (
"net/http"
"strings"
"sync"
"time"
"github.com/matrix-org/go-neb/api"
"github.com/matrix-org/go-neb/database"
@ -344,6 +343,9 @@ func (c *Clients) initClient(botClient *BotClient) error {
client.Client = c.httpClient
client.DeviceID = config.DeviceID
if client.DeviceID == "" {
log.Warn("Device ID is not set which will result in E2E encryption/decryption not working")
}
botClient.Client = client
syncer := client.Syncer.(*mautrix.DefaultSyncer)
@ -426,28 +428,7 @@ func (c *Clients) initClient(botClient *BotClient) error {
}).Info("Created new client")
if config.Sync {
go func() {
// Get the state store up to date
resp, err := botClient.SyncRequest(30000, "", "", true, mevt.PresenceOnline)
if err != nil {
log.WithError(err).Error("Error performing initial sync")
return
}
botClient.stateStore.UpdateStateStore(resp)
for {
if e := client.Sync(); e != nil {
log.WithFields(log.Fields{
log.ErrorKey: e,
"user_id": config.UserID,
}).Error("Fatal Sync() error")
time.Sleep(10 * time.Second)
} else {
log.WithField("user_id", config.UserID).Info("Stopping Sync()")
return
}
}
}()
go botClient.Sync()
}
return nil

Loading…
Cancel
Save