matrix: URIs #112
2 changed files with 1199 additions and 127 deletions
|
|
@ -557,6 +557,72 @@ function serialiseReplyContent(doc) {
|
|||
return body.textContent
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace all matrix: uris in the input string with matrix.to links
|
||||
*
|
||||
* @param {string} input
|
||||
* @returns {string}
|
||||
*/
|
||||
function convertMatrixUris(input) {
|
||||
return input.replace(/(?<![\/:@?#])\b(matrix:[\w\/=?:\.&%-]+)/gi, (match, 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
|
||||
|
||||
if (type === "r" || type === "room") {
|
||||
sigil = "#"
|
||||
} else if (type === "roomid") {
|
||||
sigil = "!"
|
||||
} else if (type === "e" || type === "event") {
|
||||
sigil = "$"
|
||||
} else if (type === "u" || type === "user") {
|
||||
sigil = "@"
|
||||
} else {
|
||||
// unknown type! bail
|
||||
return match
|
||||
}
|
||||
|
||||
resultSegments.push(`${sigil}${id}`)
|
||||
}
|
||||
|
||||
// now splice the path back together, add the query, and we're done!
|
||||
return `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 {DiscordTypes.APIGuild} guild
|
||||
|
|
@ -913,6 +979,9 @@ async function eventToMessage(event, guild, channel, di) {
|
|||
input = `* ${displayName} ${input}`
|
||||
}
|
||||
|
||||
// convert matrix: schema uris to "legacy" matrix.to links (thanks, sable)
|
||||
input = convertMatrixUris(input)
|
||||
|
||||
// Handling mentions of Discord users
|
||||
input = input.replace(/("https:\/\/matrix.to\/#\/((?:@|%40)[^"]+)")/g, (whole, attributeValue, mxid) => {
|
||||
mxid = decodeURIComponent(mxid)
|
||||
|
|
@ -1114,6 +1183,7 @@ async function eventToMessage(event, guild, channel, di) {
|
|||
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
|
||||
|
||||
let offset = 0
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue