diff options
author | Wim <wim@42.be> | 2022-06-11 23:07:42 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-11 23:07:42 +0200 |
commit | 8751fb4bb1eb7cd34ed63be9b3801b8aeac71a1d (patch) | |
tree | 601d2616b05b5b197bd2a3ae7cb245b1a0ea17e7 /vendor/go.mau.fi/whatsmeow/appstate.go | |
parent | 3819062574ac7e4af6a562bf40a425469a7752fb (diff) | |
download | matterbridge-msglm-8751fb4bb1eb7cd34ed63be9b3801b8aeac71a1d.tar.gz matterbridge-msglm-8751fb4bb1eb7cd34ed63be9b3801b8aeac71a1d.tar.bz2 matterbridge-msglm-8751fb4bb1eb7cd34ed63be9b3801b8aeac71a1d.zip |
Update dependencies (#1841)
Diffstat (limited to 'vendor/go.mau.fi/whatsmeow/appstate.go')
-rw-r--r-- | vendor/go.mau.fi/whatsmeow/appstate.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/vendor/go.mau.fi/whatsmeow/appstate.go b/vendor/go.mau.fi/whatsmeow/appstate.go index b9b99f26..ad97bdc2 100644 --- a/vendor/go.mau.fi/whatsmeow/appstate.go +++ b/vendor/go.mau.fi/whatsmeow/appstate.go @@ -7,6 +7,8 @@ package whatsmeow import ( + "encoding/hex" + "errors" "fmt" "time" @@ -53,6 +55,9 @@ func (cli *Client) FetchAppState(name appstate.WAPatchName, fullSync, onlyIfNotS mutations, newState, err := cli.appStateProc.DecodePatches(patches, state, true) if err != nil { + if errors.Is(err, appstate.ErrKeyNotFound) { + go cli.requestMissingAppStateKeys(patches) + } return fmt.Errorf("failed to decode app state %s patches: %w", name, err) } wasFullSync := state.Version == 0 && patches.Snapshot != nil @@ -228,3 +233,42 @@ func (cli *Client) fetchAppStatePatches(name appstate.WAPatchName, fromVersion u } return appstate.ParsePatchList(resp, cli.downloadExternalAppStateBlob) } + +func (cli *Client) requestMissingAppStateKeys(patches *appstate.PatchList) { + cli.appStateKeyRequestsLock.Lock() + rawKeyIDs := cli.appStateProc.GetMissingKeyIDs(patches) + filteredKeyIDs := make([][]byte, 0, len(rawKeyIDs)) + now := time.Now() + for _, keyID := range rawKeyIDs { + stringKeyID := hex.EncodeToString(keyID) + lastRequestTime := cli.appStateKeyRequests[stringKeyID] + if lastRequestTime.IsZero() || lastRequestTime.Add(24*time.Hour).Before(now) { + cli.appStateKeyRequests[stringKeyID] = now + filteredKeyIDs = append(filteredKeyIDs, keyID) + } + } + cli.appStateKeyRequestsLock.Unlock() + cli.requestAppStateKeys(filteredKeyIDs) +} + +func (cli *Client) requestAppStateKeys(rawKeyIDs [][]byte) { + keyIDs := make([]*waProto.AppStateSyncKeyId, len(rawKeyIDs)) + debugKeyIDs := make([]string, len(rawKeyIDs)) + for i, keyID := range rawKeyIDs { + keyIDs[i] = &waProto.AppStateSyncKeyId{KeyId: keyID} + debugKeyIDs[i] = hex.EncodeToString(keyID) + } + msg := &waProto.Message{ + ProtocolMessage: &waProto.ProtocolMessage{ + Type: waProto.ProtocolMessage_APP_STATE_SYNC_KEY_REQUEST.Enum(), + AppStateSyncKeyRequest: &waProto.AppStateSyncKeyRequest{ + KeyIds: keyIDs, + }, + }, + } + cli.Log.Infof("Sending key request for app state keys %+v", debugKeyIDs) + _, err := cli.SendMessage(cli.Store.ID.ToNonAD(), "", msg) + if err != nil { + cli.Log.Warnf("Failed to send app state key request: %v", err) + } +} |