diff --git a/matrix.go b/matrix.go index e15d795..b9f2ca8 100644 --- a/matrix.go +++ b/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) diff --git a/portal.go b/portal.go index 4773094..9807239 100644 --- a/portal.go +++ b/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)