diff --git a/bridge/bridge.go b/bridge/bridge.go index e7a39f63..9cfc1450 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -7,6 +7,7 @@ package bridge import ( + "context" "encoding/json" "errors" "fmt" @@ -219,6 +220,7 @@ type Crypto interface { Stop() Reset(startAfterReset bool) Client() *mautrix.Client + ShareKeys(context.Context) error } func (br *Bridge) GenerateRegistration() { diff --git a/bridge/crypto.go b/bridge/crypto.go index ccdf7fee..065bc017 100644 --- a/bridge/crypto.go +++ b/bridge/crypto.go @@ -436,6 +436,11 @@ func (helper *CryptoHelper) HandleMemberEvent(evt *event.Event) { helper.mach.HandleMemberEvent(0, evt) } +// ShareKeys uploads the given number of one-time-keys to the server. +func (helper *CryptoHelper) ShareKeys(ctx context.Context) error { + return helper.mach.ShareKeys(ctx, -1) +} + type cryptoSyncer struct { *crypto.OlmMachine } diff --git a/crypto/machine.go b/crypto/machine.go index 98da7686..2c9b63c9 100644 --- a/crypto/machine.go +++ b/crypto/machine.go @@ -622,8 +622,8 @@ func (mach *OlmMachine) ShareKeys(ctx context.Context, currentOTKCount int) erro start := time.Now() mach.otkUploadLock.Lock() defer mach.otkUploadLock.Unlock() - if mach.lastOTKUpload.Add(1 * time.Minute).After(start) { - log.Debug().Msg("Checking OTK count from server due to suspiciously close share keys requests") + if mach.lastOTKUpload.Add(1*time.Minute).After(start) || currentOTKCount < 0 { + log.Debug().Msg("Checking OTK count from server due to suspiciously close share keys requests or negative OTK count") resp, err := mach.Client.UploadKeys(&mautrix.ReqUploadKeys{}) if err != nil { return fmt.Errorf("failed to check current OTK counts: %w", err)