Browse Source

Fix bridging error messages in private chats

Tulir Asokan 6 years ago
parent
commit
5af374e6b0
2 changed files with 21 additions and 11 deletions
  1. 0 11
      matrix.go
  2. 21 0
      portal.go

+ 0 - 11
matrix.go

@@ -190,17 +190,6 @@ func (mx *MatrixHandler) HandleMessage(evt *mautrix.Event) {
 		}
 	}
 
-	if !user.HasSession() {
-		mx.log.Debugln("Ignoring message from", user.MXID, "in", evt.RoomID, "as user has no session")
-		return
-	} else if !user.IsConnected() {
-		msg := format.RenderMarkdown(fmt.Sprintf("\u26a0 You are not connected to WhatsApp, so your message was not bridged. " +
-			"Use `%s reconnect` to reconnect.", mx.bridge.Config.Bridge.CommandPrefix))
-		msg.MsgType = mautrix.MsgNotice
-		_, _ = mx.bridge.Bot.SendMessageEvent(roomID, mautrix.EventMessage, msg)
-		return
-	}
-
 	portal := mx.bridge.GetPortalByMXID(roomID)
 	if portal != nil {
 		portal.HandleMatrixMessage(user, evt)

+ 21 - 0
portal.go

@@ -35,6 +35,7 @@ import (
 
 	"github.com/Rhymen/go-whatsapp"
 	waProto "github.com/Rhymen/go-whatsapp/binary/proto"
+	"maunium.net/go/mautrix/format"
 
 	log "maunium.net/go/maulogger/v2"
 
@@ -1081,9 +1082,29 @@ type MediaUpload struct {
 	Thumbnail     []byte
 }
 
+func (portal *Portal) sendMatrixConnectionError(sender *User, eventID string) bool {
+	if !sender.HasSession() {
+		portal.log.Debugln("Ignoring event", eventID, "from", sender.MXID, "as user has no session")
+		return true
+	} else if !sender.IsConnected() {
+		portal.log.Debugln("Ignoring event", eventID, "from", sender.MXID, "as user is not connected")
+		msg := format.RenderMarkdown(fmt.Sprintf("\u26a0 You are not connected to WhatsApp, so your message was not bridged. " +
+			"Use `%s reconnect` to reconnect.", portal.bridge.Config.Bridge.CommandPrefix))
+		msg.MsgType = mautrix.MsgNotice
+		_, err := portal.MainIntent().SendMessageEvent(portal.MXID, mautrix.EventMessage, msg)
+		if err != nil {
+			portal.log.Errorln("Failed to send bridging failure message:", err)
+		}
+		return true
+	}
+	return false
+}
+
 func (portal *Portal) HandleMatrixMessage(sender *User, evt *mautrix.Event) {
 	if portal.IsPrivateChat() && sender.JID != portal.Key.Receiver {
 		return
+	} else if portal.sendMatrixConnectionError(sender, evt.ID) {
+		return
 	}
 	portal.log.Debugfln("Received event %s", evt.ID)