From 8751fb4bb1eb7cd34ed63be9b3801b8aeac71a1d Mon Sep 17 00:00:00 2001 From: Wim Date: Sat, 11 Jun 2022 23:07:42 +0200 Subject: Update dependencies (#1841) --- vendor/go.mau.fi/whatsmeow/retry.go | 59 ++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 27 deletions(-) (limited to 'vendor/go.mau.fi/whatsmeow/retry.go') diff --git a/vendor/go.mau.fi/whatsmeow/retry.go b/vendor/go.mau.fi/whatsmeow/retry.go index a5a56d8f..24be9c7a 100644 --- a/vendor/go.mau.fi/whatsmeow/retry.go +++ b/vendor/go.mau.fi/whatsmeow/retry.go @@ -62,7 +62,7 @@ func (cli *Client) getRecentMessage(to types.JID, id types.MessageID) *waProto.M func (cli *Client) getMessageForRetry(receipt *events.Receipt, messageID types.MessageID) (*waProto.Message, error) { msg := cli.getRecentMessage(receipt.Chat, messageID) if msg == nil { - msg = cli.GetMessageForRetry(receipt.Chat, messageID) + msg = cli.GetMessageForRetry(receipt.Sender, receipt.Chat, messageID) if msg == nil { return nil, fmt.Errorf("couldn't find message %s", messageID) } else { @@ -74,6 +74,25 @@ func (cli *Client) getMessageForRetry(receipt *events.Receipt, messageID types.M return proto.Clone(msg).(*waProto.Message), nil } +const recreateSessionTimeout = 1 * time.Hour + +func (cli *Client) shouldRecreateSession(retryCount int, jid types.JID) (reason string, recreate bool) { + cli.sessionRecreateHistoryLock.Lock() + defer cli.sessionRecreateHistoryLock.Unlock() + if !cli.Store.ContainsSession(jid.SignalAddress()) { + cli.sessionRecreateHistory[jid] = time.Now() + return "we don't have a Signal session with them", true + } else if retryCount < 2 { + return "", false + } + prevTime, ok := cli.sessionRecreateHistory[jid] + if !ok || prevTime.Add(recreateSessionTimeout).Before(time.Now()) { + cli.sessionRecreateHistory[jid] = time.Now() + return "retry count > 1 and over an hour since last recreation", true + } + return "", false +} + // handleRetryReceipt handles an incoming retry receipt for an outgoing message. func (cli *Client) handleRetryReceipt(receipt *events.Receipt, node *waBinary.Node) error { retryChild, ok := node.GetOptionalChildByTag("retry") @@ -82,7 +101,7 @@ func (cli *Client) handleRetryReceipt(receipt *events.Receipt, node *waBinary.No } ag := retryChild.AttrGetter() messageID := ag.String("id") - timestamp := time.Unix(ag.Int64("t"), 0) + timestamp := ag.UnixTime("t") retryCount := ag.Int("count") if !ag.OK() { return ag.Error() @@ -113,7 +132,7 @@ func (cli *Client) handleRetryReceipt(receipt *events.Receipt, node *waBinary.No } } - if cli.PreRetryCallback != nil && !cli.PreRetryCallback(receipt, retryCount, msg) { + if cli.PreRetryCallback != nil && !cli.PreRetryCallback(receipt, messageID, retryCount, msg) { cli.Log.Debugf("Cancelled retry receipt in PreRetryCallback") return nil } @@ -129,12 +148,8 @@ func (cli *Client) handleRetryReceipt(receipt *events.Receipt, node *waBinary.No if err != nil { return fmt.Errorf("failed to read prekey bundle in retry receipt: %w", err) } - } else if retryCount >= 2 || !cli.Store.ContainsSession(receipt.Sender.SignalAddress()) { - if retryCount >= 2 { - cli.Log.Debugf("Fetching prekeys for %s due to retry receipt with count>1 but no prekey bundle", receipt.Sender) - } else { - cli.Log.Debugf("Fetching prekeys for %s for handling retry receipt because we don't have a Signal session with them", receipt.Sender) - } + } else if reason, recreate := cli.shouldRecreateSession(retryCount, receipt.Sender); recreate { + cli.Log.Debugf("Fetching prekeys for %s for handling retry receipt with no prekey bundle because %s", receipt.Sender, reason) var keys map[types.JID]preKeyResp keys, err = cli.fetchPreKeys([]types.JID{receipt.Sender}) if err != nil { @@ -148,13 +163,6 @@ func (cli *Client) handleRetryReceipt(receipt *events.Receipt, node *waBinary.No } else if bundle == nil { return fmt.Errorf("didn't get prekey bundle for %s (response size: %d)", senderAD, len(keys)) } - if retryCount > 3 { - cli.Log.Debugf("Erasing existing session for %s due to retry receipt with count>3", receipt.Sender) - err = cli.Store.Sessions.DeleteSession(receipt.Sender.SignalAddress().String()) - if err != nil { - return fmt.Errorf("failed to delete session for %s: %w", senderAD, err) - } - } } encrypted, includeDeviceIdentity, err := cli.encryptMessageForDevice(plaintext, receipt.Sender, bundle) if err != nil { @@ -164,7 +172,7 @@ func (cli *Client) handleRetryReceipt(receipt *events.Receipt, node *waBinary.No attrs := waBinary.Attrs{ "to": node.Attrs["from"], - "type": "text", + "type": getTypeFromMessage(msg), "id": messageID, "t": timestamp.Unix(), } @@ -180,18 +188,15 @@ func (cli *Client) handleRetryReceipt(receipt *events.Receipt, node *waBinary.No if edit, ok := node.Attrs["edit"]; ok { attrs["edit"] = edit } - req := waBinary.Node{ - Tag: "message", - Attrs: attrs, - Content: []waBinary.Node{*encrypted}, - } + content := []waBinary.Node{*encrypted} if includeDeviceIdentity { - err = cli.appendDeviceIdentityNode(&req) - if err != nil { - return fmt.Errorf("failed to add device identity to retry message: %w", err) - } + content = append(content, cli.makeDeviceIdentityNode()) } - err = cli.sendNode(req) + err = cli.sendNode(waBinary.Node{ + Tag: "message", + Attrs: attrs, + Content: content, + }) if err != nil { return fmt.Errorf("failed to send retry message: %w", err) } -- cgit v1.2.3