Skip to content

portal: silently drop empty Matrix messages instead of erroring - #225

Open
Deln0r wants to merge 1 commit into
mautrix:mainfrom
Deln0r:fix/issue-186-empty-matrix-message
Open

portal: silently drop empty Matrix messages instead of erroring#225
Deln0r wants to merge 1 commit into
mautrix:mainfrom
Deln0r:fix/issue-186-empty-matrix-message

Conversation

@Deln0r

@Deln0r Deln0r commented May 13, 2026

Copy link
Copy Markdown

Closes #186.

Problem

When a Matrix client sends a text/notice/emote with an empty body to a Discord-bridged room, the bridge relays it to Discord verbatim. Discord's API rejects the call with HTTP 400 / code 50006 ("Cannot send an empty message"), and the bridge surfaces an error notice back into the Matrix room. Ten empty messages therefore become twenty (ten originals plus ten error notices), which makes accidental empty sends a cheap room-spam vector.

Fix

Add an errEmptyMatrixMessage sentinel. On the MsgText/MsgEmote/MsgNotice branch of (*Portal).handleMatrixMessage, just after parseMatrixHTML resolves the final Discord content, check whether the content is whitespace-only AND there is no reply embed attached. If so, drop the message before making the API call:

if strings.TrimSpace(sendReq.Content) == "" && len(sendReq.Embeds) == 0 {
    go portal.sendMessageMetrics(evt, errEmptyMatrixMessage, "Ignoring")
    return
}

The metric call is still made with the Ignoring action so the checkpoint is recorded, but sendNotice is false for this error class, so no error message hits the Matrix room. Non-text content (images, files, audio, video) is unaffected: those paths attach a file separately and never reach this check.

Test plan

go build ./... and go vet ./... are clean (with libolm headers available; this matches what CI does).

No new test added because portal_test.go doesn't exist in this repo and a full end-to-end integration test would need a live Discord client. Happy to add a focused unit test for the empty-content branch if you want a regression guard.

When a Matrix client sends a text/notice/emote with an empty body to
a Discord-bridged room, the bridge currently relays it to Discord
verbatim. Discord's API rejects the call with HTTP 400 / code 50006
("Cannot send an empty message"), and the bridge surfaces a notice
back into the Matrix room. Ten empty messages therefore become
twenty (ten originals plus ten error notices), which makes accidental
empty sends a cheap room-spam vector and is generally noisy (see mautrix#186).

Add an errEmptyMatrixMessage sentinel and check it on the
MsgText/MsgEmote/MsgNotice path of (*Portal).handleMatrixMessage,
just after parseMatrixHTML resolves the final Discord content. If the
content is whitespace-only AND there is no reply embed attached, drop
the message before making the API call: the sendMessageMetrics call
is still made with the Ignoring action so the checkpoint is recorded,
but sendNotice is false so no error message hits the room.

Non-text content (images, files, audio, video) is unaffected: those
paths attach a file separately and never reach this check.

Closes mautrix#186.

Signed-off-by: Ian Chechin <ian00chechin@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Error when a Matrix message is empty

1 participant