m->d: Fix glitched mention when Element disambigs

This commit is contained in:
Cadence Ember 2024-01-22 23:10:33 +13:00
parent a71c9515ec
commit 4591b5ae03
3 changed files with 36 additions and 5 deletions

View File

@ -92,8 +92,10 @@ async function ensureSimJoined(pkMessage, roomID) {
* @param {Ty.PkMessage} pkMessage
*/
async function memberToStateContent(pkMessage) {
const systemname = pkMessage.system.tag || ""
const displayname = (pkMessage.member.display_name || pkMessage.member.name) + systemname
let displayname = (pkMessage.member.display_name || pkMessage.member.name)
if (pkMessage.system.tag) {
displayname = displayname + " " + pkMessage.system.tag
}
const avatar = pkMessage.member.avatar_url || pkMessage.member.webhook_avatar_url || pkMessage.system.avatar_url
const content = {

View File

@ -109,6 +109,8 @@ turndownService.addRule("inlineLink", {
const href = node.getAttribute("href")
let brackets = ["", ""]
if (href.startsWith("https://matrix.to")) brackets = ["<", ">"]
if (href.startsWith("https://matrix.to/#/@")) content = "@" + content
content = content.replace(/ @.*/, "")
return "[" + content + "](" + brackets[0] + href + brackets[1] + ")"
}
})
@ -621,7 +623,7 @@ async function eventToMessage(event, guild, di) {
content = displayNameRunoff + replyLine + content
// Handling written @mentions: we need to look for candidate Discord members to join to the room
let writtenMentionMatch = content.match(/(?:^|[^"<>/A-Za-z0-9])@([A-Za-z][A-Za-z0-9._\[\]\(\)-]+):?/d) // /d flag for indices requires node.js 16+
let writtenMentionMatch = content.match(/(?:^|[^"[<>/A-Za-z0-9])@([A-Za-z][A-Za-z0-9._\[\]\(\)-]+):?/d) // /d flag for indices requires node.js 16+
if (writtenMentionMatch) {
const results = await di.snow.guild.searchGuildMembers(guild.id, {query: writtenMentionMatch[1]})
if (results[0]) {

View File

@ -1925,7 +1925,34 @@ test("event2message: mentioning matrix users works", async t => {
messagesToEdit: [],
messagesToSend: [{
username: "cadence [they]",
content: "I'm just [▲](<https://matrix.to/#/@rnl:cadence.moe>) testing mentions",
content: "I'm just [@▲](<https://matrix.to/#/@rnl:cadence.moe>) testing mentions",
avatar_url: undefined
}]
}
)
})
test("event2message: mentioning matrix users works even when Element disambiguates the user", async t => {
t.deepEqual(
await eventToMessage({
type: "m.room.message",
room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe",
sender: "@cadence:cadence.moe",
content: {
msgtype: "m.text",
body: "unascribed @unascribed:sleeping.town: if you want to run some experimental software, `11864f80cf` branch of OOYE has _vastly_ improved handling of PluralKit users. feel free to try it out, if you find bugs I'd appreciate you letting me know (just tag me at the place in chat where something went wrong)",
format: "org.matrix.custom.html",
formatted_body: "<a href=\"https://matrix.to/#/@unascribed:sleeping.town\">unascribed @unascribed:sleeping.town</a>: if you want to run some experimental software, <code>11864f80cf</code> branch of OOYE has <em>vastly</em> improved handling of PluralKit users. feel free to try it out, if you find bugs I'd appreciate you letting me know (just tag me at the place in chat where something went wrong)"
},
event_id: "$17qTyvkDykSp_4Wkjeuh9Y6j9hPe20ZY_E6V3UKAyUE",
}),
{
ensureJoined: [],
messagesToDelete: [],
messagesToEdit: [],
messagesToSend: [{
username: "cadence [they]",
content: "[@unascribed](<https://matrix.to/#/@unascribed:sleeping.town>) if you want to run some experimental software, `11864f80cf` branch of OOYE has _vastly_ improved handling of PluralKit users. feel free to try it out, if you find bugs I'd appreciate you letting me know (just tag me at the place in chat where something went wrong)",
avatar_url: undefined
}]
}
@ -2264,7 +2291,7 @@ test("event2message: colon after mentions is stripped", async t => {
messagesToEdit: [],
messagesToSend: [{
username: "cadence [they]",
content: "<@114147806469554185> hey, I'm just [▲](<https://matrix.to/#/@rnl:cadence.moe>) testing mentions",
content: "<@114147806469554185> hey, I'm just [@▲](<https://matrix.to/#/@rnl:cadence.moe>) testing mentions",
avatar_url: undefined
}]
}