From 91bb38eaa34fc3cbfba9b8080b00b3ca257b947a Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 24 May 2020 15:33:26 +0300 Subject: [PATCH] Wait for response even after message send timeout --- go.mod | 2 +- go.sum | 2 ++ portal.go | 48 +++++++++++++++++++++++++++++++++++++----------- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 4a5cfae..b82d90b 100644 --- a/go.mod +++ b/go.mod @@ -18,4 +18,4 @@ require ( maunium.net/go/mautrix v0.4.5 ) -replace github.com/Rhymen/go-whatsapp => github.com/tulir/go-whatsapp v0.2.6 +replace github.com/Rhymen/go-whatsapp => github.com/tulir/go-whatsapp v0.2.7 diff --git a/go.sum b/go.sum index f9b7366..d07db35 100644 --- a/go.sum +++ b/go.sum @@ -57,6 +57,8 @@ github.com/tulir/go-whatsapp v0.2.5 h1:io7nw/b7/lQH5q9jeRkUD8Kw+teoANe16ZTh/JK7D github.com/tulir/go-whatsapp v0.2.5/go.mod h1:gyw9zGup1/Y3ZQUueZaqz3iR/WX9a2Lth4aqEbXjkok= github.com/tulir/go-whatsapp v0.2.6 h1:d58cqz/iqcCDeT+uFjLso8oSgMTYqoxGhGhGOyyHBro= github.com/tulir/go-whatsapp v0.2.6/go.mod h1:gyw9zGup1/Y3ZQUueZaqz3iR/WX9a2Lth4aqEbXjkok= +github.com/tulir/go-whatsapp v0.2.7 h1:4K5sTxQWPeqDjE7scO39fSemJR7BMOWOF/9IpEg7Zig= +github.com/tulir/go-whatsapp v0.2.7/go.mod h1:gyw9zGup1/Y3ZQUueZaqz3iR/WX9a2Lth4aqEbXjkok= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/image v0.0.0-20200430140353-33d19683fad8 h1:6WW6V3x1P/jokJBpRQYUJnMHRP6isStQwCozxnU7XQw= diff --git a/portal.go b/portal.go index 7d675d5..b0e2eb1 100644 --- a/portal.go +++ b/portal.go @@ -1405,17 +1405,34 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) { } portal.markHandled(sender, info, evt.ID) portal.log.Debugln("Sending event", evt.ID, "to WhatsApp") - _, err = sender.Conn.Send(info) + + errChan := make(chan error, 1) + go sender.Conn.SendRaw(info, errChan) + + var errorSendResp *mautrix.RespSendEvent + select { + case err = <-errChan: + case <-time.After(time.Duration(portal.bridge.Config.Bridge.ConnectionTimeout) * time.Second): + portal.log.Warnfln("Response when bridging Matrix event %s is taking long to arrive", evt.ID) + errorSendResp, err = portal.sendMainIntentMessage(event.MessageEventContent{ + MsgType: event.MsgNotice, + Body: fmt.Sprintf("\u26a0 Your message may not have been bridged: %v", err), + }) + if err != nil { + portal.log.Warnfln("Failed to send bridging failure message:", err) + } + err = <-errChan + } if err != nil { portal.log.Errorfln("Error handling Matrix event %s: %v", evt.ID, err) - msg := format.RenderMarkdown(fmt.Sprintf("\u26a0 Your message may not have been bridged: %v", err), false, false) - msg.MsgType = event.MsgNotice - _, err := portal.sendMainIntentMessage(msg) - if err != nil { - portal.log.Errorln("Failed to send bridging failure message:", err) - } } else { - portal.log.Debugln("Handled Matrix event:", evt) + portal.log.Debugln("Handled Matrix event %s", evt.ID) + } + if errorSendResp != nil { + _, err = portal.MainIntent().RedactEvent(portal.MXID, errorSendResp.EventID) + if err != nil { + portal.log.Warnfln("Failed to redact timeout warning message %s: %v", errorSendResp.EventID, err) + } } } @@ -1452,11 +1469,20 @@ func (portal *Portal) HandleMatrixRedaction(sender *User, evt *event.Event) { }, Status: &status, } - _, err := sender.Conn.Send(info) + errChan := make(chan error, 1) + go sender.Conn.SendRaw(info, errChan) + + var err error + select { + case err = <-errChan: + case <-time.After(time.Duration(portal.bridge.Config.Bridge.ConnectionTimeout) * time.Second): + portal.log.Warnfln("Response when bridging Matrix redaction %s is taking long to arrive", evt.ID) + err = <-errChan + } if err != nil { - portal.log.Errorfln("Error handling Matrix redaction: %s: %v", evt.ID, err) + portal.log.Errorfln("Error handling Matrix redaction %s: %v", evt.ID, err) } else { - portal.log.Debugln("Handled Matrix redaction:", evt) + portal.log.Debugln("Handled Matrix redaction %s of %s", evt.ID, evt.Redacts) } }