matrix: URIs #112

Merged
cadence merged 8 commits from mschae/out-of-your-element:matrix-uris into main 2026-11-28 03:44:30 +00:00
Showing only changes of commit c06356a771 - Show all commits

convert matrix: uris to matrix.to links during m->d

mini-bomba 2026-11-18 21:04:29 +01:00
No known key found for this signature in database
GPG key ID: 30F8B138B4794886

View file

@ -557,6 +557,83 @@ function serialiseReplyContent(doc) {
return body.textContent return body.textContent
} }
/**
* Replace all matrix: uris in the input string with matrix.to links
*
* @param {string} input
* @returns {string}
*/
function convertMatrixUris(input) {
// this regex is kinda broad, but i'm not really sure how to reasonably make it stricter
// we run the match through the url parser anyway, so if we get a bad match, it should just fail? hopefully?
return input.replace(/(^|\s|")(matrix:[^\s"]+)/g, (match, before, uri) => {
// technically the url might be html-encoded, but since we're gonna splice the url back together,
// and none of the characters that might get html-encoded are actually important to us, it doesn't really matter
// (most of the case it's the query string, and we just copy it over anyway)
//
// try to parse the uri, if we fail just bail
let parsed;
try {
parsed = new URL(uri)
} catch {
return match
}
// the spec permits the url to contain an authority segment, even though it is currently unused
// in the case where someone does include the authority segment, the pathname will start with a /
// otherwise there will be no leading /
// if there is a leading /, remove it
const path = parsed.pathname.slice(parsed.pathname[0] === "/" ? 1 : 0)
// the path is expected to contain an even number of segments
// if it doesn't, bail
const segments = path.split("/")
if (segments.length % 2 !== 0) {
return match
}
// we can actually convert matrix: uris to matrix.to links in quite a straight forward way:
// - take pairs of path segments, the first one is the type, the other is an identifier without the sigil
// - map type to an appropriate sigil
// - concatenate with the identifier
// - and that's your new matrix.to path segment
// after transcribing the entire path, add the query at the end of the url
const resultSegments = []
while (segments.length > 0) {
const type = segments.shift()
const id = segments.shift()
let sigil
switch(type) {
case "r":
case "room":
sigil = "#"
break
case "roomid":
sigil = "!"
break
case "e":
case "event":
sigil = "$"
break
case "u":
case "user":
sigil = "@"
break
default:
// unknown type! bail
return match
}
resultSegments.push(`${encodeURIComponent(sigil)}${id}`)
}
// now splice the path back together, add the query, and we're done!
// (and don't forget to add the character before the url that we might've matched)
return `${before}https://matrix.to/#/${resultSegments.join("/")}${parsed.search}`
})
}
/** /**
* @param {Ty.Event.Outer_M_Room_Message | Ty.Event.Outer_M_Room_Message_File | Ty.Event.Outer_M_Sticker | Ty.Event.Outer_M_Room_Message_Encrypted_File | Ty.Event.Outer_Org_Matrix_Msc3381_Poll_Start | Ty.Event.Outer_Org_Matrix_Msc3381_Poll_End} event * @param {Ty.Event.Outer_M_Room_Message | Ty.Event.Outer_M_Room_Message_File | Ty.Event.Outer_M_Sticker | Ty.Event.Outer_M_Room_Message_Encrypted_File | Ty.Event.Outer_Org_Matrix_Msc3381_Poll_Start | Ty.Event.Outer_Org_Matrix_Msc3381_Poll_End} event
* @param {DiscordTypes.APIGuild} guild * @param {DiscordTypes.APIGuild} guild
@ -913,6 +990,9 @@ async function eventToMessage(event, guild, channel, di) {
input = `* ${displayName} ${input}` input = `* ${displayName} ${input}`
} }
// convert matrix: schema uris to "legacy" matrix.to links (thanks, sable)
input = convertMatrixUris(input)
// Handling mentions of Discord users // Handling mentions of Discord users
input = input.replace(/("https:\/\/matrix.to\/#\/((?:@|%40)[^"]+)")/g, (whole, attributeValue, mxid) => { input = input.replace(/("https:\/\/matrix.to\/#\/((?:@|%40)[^"]+)")/g, (whole, attributeValue, mxid) => {
mxid = decodeURIComponent(mxid) mxid = decodeURIComponent(mxid)
@ -1114,6 +1194,7 @@ async function eventToMessage(event, guild, channel, di) {
content = `* ${displayName} ${content}` content = `* ${displayName} ${content}`
} }
content = convertMatrixUris(content) // convert matrix: schema uris to "legacy" matrix.to links (thanks, sable)
content = await handleRoomOrMessageLinks(content, di) // Replace matrix.to links with discord.com equivalents where possible content = await handleRoomOrMessageLinks(content, di) // Replace matrix.to links with discord.com equivalents where possible
let offset = 0 let offset = 0