浏览代码

Wait for response even after message send timeout

Tulir Asokan 5 年之前
父节点
当前提交
91bb38eaa3
共有 3 个文件被更改,包括 39 次插入11 次删除
  1. 1 1
      go.mod
  2. 2 0
      go.sum
  3. 36 10
      portal.go

+ 1 - 1
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

+ 2 - 0
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=

+ 36 - 10
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)
+	} else {
+		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.Errorln("Failed to send bridging failure message:", err)
+			portal.log.Warnfln("Failed to redact timeout warning message %s: %v", errorSendResp.EventID, err)
 		}
-	} else {
-		portal.log.Debugln("Handled Matrix event:", evt)
 	}
 }
 
@@ -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)
 	}
 }