← Back to PRs

#7845: Fix Matrix mention detection with URL-encoded user IDs

by emadomedher open 2026-02-03 07:39 View on GitHub →
channel: matrix stale
Fixes #7843 ## Summary Adds support for URL-encoded user IDs in `formatted_body` mention links, fixing intermittent mention detection failures with Element and other Matrix clients. ## Problem Matrix clients URL-encode user IDs in `matrix.to` links: - `@bot:server` becomes `%40bot%3Aserver` Our regex only matched literal `@` characters, missing URL-encoded mentions entirely. ## Solution Added a second regex pattern that: 1. Matches URL-encoded user IDs (`%40...`) 2. Decodes them using `decodeURIComponent()` 3. Adds them to the mention detection set ```typescript // Also check for URL-encoded user IDs (%40 = @, %3A = :) const encodedRegex = /https?:\/\/matrix\.to\/#\/(%40[^"'<>\s]+)/g; let encodedMatch; while ((encodedMatch = encodedRegex.exec(formatted)) !== null) { const decoded = decodeURIComponent(encodedMatch[1]); mentionedUserIds.add(decoded); } ``` ## Why This Happens Matrix clients vary in their URL encoding behavior: - Some clients encode: `%40bot%3Aserver` - Some don't: `@bot:server` - Element appears to do both depending on context This fix handles both cases. ## Testing Tested with: - ✅ Unencoded mentions: `@bot:server` - ✅ URL-encoded mentions: `%40bot%3Aserver` - ✅ Mixed mentions in same message - ✅ Multiple encoded mentions ## Dependencies This PR builds on #7842 (formatted_body mention detection). Both fixes should ideally be merged together for complete Element compatibility. ## References - Related issue: #7843 - Related PR: #7842 (formatted_body support) - URL encoding spec: RFC 3986 <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This change extends Matrix mention detection by looking for user mentions encoded as links in `formatted_body` (an alternate/legacy mention format used by some clients) and treating matches as explicit mentions alongside existing `m.mentions` and regex-based detection. The implementation adds `formatted_body` to the message content type and then checks for `href="…/#/<userId>"` and `href="…/#/<url-encoded userId>"` patterns to determine whether the bot user ID was mentioned. <h3>Confidence Score: 4/5</h3> - This PR is generally safe to merge; changes are small and isolated to mention detection logic. - The update only adds an additional formatted-body check and doesn’t affect message parsing or sending paths. Main risk is functional edge cases across Matrix clients/URL forms rather than runtime safety. - extensions/matrix/src/matrix/monitor/mentions.ts <!-- greptile_other_comments_section --> <sub>(5/5) You can turn off certain types of comments like style [here](https://app.greptile.com/review/github)!</sub> **Context used:** - Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8)) - Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13)) <!-- /greptile_comment -->

Most Similar PRs