2020-10-23 14:15:14 +00:00
|
|
|
const lsm = require("./lsm.js")
|
2020-10-19 13:05:16 +00:00
|
|
|
|
|
|
|
function resolveMxc(url, size, method) {
|
2020-10-29 04:28:12 +00:00
|
|
|
let [server, id] = url.match(/^mxc:\/\/([^/]+)\/(.*)/).slice(1)
|
|
|
|
id = id.replace(/#.*$/, "")
|
2020-10-19 13:05:16 +00:00
|
|
|
if (size && method) {
|
|
|
|
return `${lsm.get("domain")}/_matrix/media/r0/thumbnail/${server}/${id}?width=${size}&height=${size}&method=${method}`
|
|
|
|
} else {
|
|
|
|
return `${lsm.get("domain")}/_matrix/media/r0/download/${server}/${id}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 11:19:56 +00:00
|
|
|
function extractLocalpart(mxid) {
|
|
|
|
// try to extract the localpart from the mxid
|
|
|
|
let match = mxid.match(/^@([^:]+):/)
|
|
|
|
if (match) {
|
|
|
|
return match[1]
|
|
|
|
}
|
|
|
|
// localpart extraction failed, use the whole mxid
|
|
|
|
return mxid
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractDisplayName(stateEvent) {
|
|
|
|
const mxid = stateEvent.state_key
|
|
|
|
// see if a display name is set
|
|
|
|
if (stateEvent.content.displayname) {
|
|
|
|
return stateEvent.content.displayname
|
|
|
|
}
|
|
|
|
// fall back to the mxid
|
|
|
|
return extractLocalpart(mxid)
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
resolveMxc,
|
|
|
|
extractLocalpart,
|
|
|
|
extractDisplayName
|
|
|
|
}
|