bridge/crypto: add ShareKeys function to CryptoHelper

Signed-off-by: Sumner Evans <sumner@beeper.com>
This commit is contained in:
Sumner Evans 2023-08-03 14:25:56 -06:00
commit c273e40a12
No known key found for this signature in database
GPG key ID: 8904527AB50022FD
3 changed files with 9 additions and 2 deletions

View file

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

View file

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

View file

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