#7845: Fix Matrix mention detection with URL-encoded user IDs
channel: matrix
stale
Cluster:
Model Reasoning Fixes
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
#7842: Fix Matrix mention detection for Element client (formatted_body links)
by emadomedher · 2026-02-03
91.6%
#20286: Matrix: render LaTeX for Element Desktop by emitting data-mx-maths ...
by eloklam · 2026-02-18
73.5%
#12028: fix: normalize // to / for Matrix client escaped commands
by githabideri · 2026-02-08
73.3%
#10721: fix for matrix media: destructure downloadContent return value in m...
by mklasen · 2026-02-06
73.3%
#20278: Fix/matrix missing bot sdk dependency
by saurav470 · 2026-02-18
72.3%
#22389: Fix: Matrix plugin not sending images from content blocks
by fryccerGit · 2026-02-21
72.0%
#11774: fix: add guards for undefined mentionRegexes arrays
by ikvyk · 2026-02-08
70.9%
#10606: fix(matrix): keep room IDs without :server suffix as-is during reso...
by majorminors · 2026-02-06
70.5%
#19294: fix: normalize room ID case in Matrix config lookup
by MisterGuy420 · 2026-02-17
70.3%
#13430: fix(discord): properly handle bot-sent @everyone mentions
by wassimbensalem · 2026-02-10
70.2%