Link instead of upload emoji sprite sheets
This commit is contained in:
@@ -348,25 +348,44 @@ function getUserOrProxyOwnerMention(mxid) {
|
||||
|
||||
/**
|
||||
* At the time of this executing, we know what the end of message emojis are, and we know that at least one of them is unknown.
|
||||
* This function will strip them from the content and generate the correct pending file of the sprite sheet.
|
||||
* This function will strip them from the content and generate a link in it's place for proxying a sprite sheet that discord previews.
|
||||
* @param {string} content
|
||||
* @param {{id: string, filename: string}[]} attachments
|
||||
* @param {({name: string, mxc: string} | {name: string, mxc: string, key: string, iv: string} | {name: string, buffer: Buffer})[]} pendingFiles
|
||||
* @param {(mxc: string) => Promise<Buffer | undefined>} mxcDownloader function that will download the mxc URLs and convert to uncompressed PNG data. use `getAndConvertEmoji` or a mock.
|
||||
*/
|
||||
async function uploadEndOfMessageSpriteSheet(content, attachments, pendingFiles, mxcDownloader) {
|
||||
async function uploadEndOfMessageSpriteSheet(content) {
|
||||
if (!content.includes("<::>")) return content // No unknown emojis, nothing to do
|
||||
// Remove known and unknown emojis from the end of the message
|
||||
const r = /<a?:[a-zA-Z0-9_]*:[0-9]*>\s*$/
|
||||
|
||||
while (content.match(r)) {
|
||||
content = content.replace(r, "")
|
||||
}
|
||||
// Create a sprite sheet of known and unknown emojis from the end of the message
|
||||
const buffer = await emojiSheet.compositeMatrixEmojis(endOfMessageEmojis, mxcDownloader)
|
||||
// Attach it
|
||||
const filename = "emojis.png"
|
||||
attachments.push({id: String(attachments.length), filename})
|
||||
pendingFiles.push({name: filename, buffer})
|
||||
|
||||
let emojiSheetUrl = new URL('emoji/matrix', reg.ooye.bridge_origin)
|
||||
for(let mxc of endOfMessageEmojis) {
|
||||
const emojiSheetUrlString = emojiSheetUrl.toString()
|
||||
|
||||
// Discord will not preview the URL if it is longer than 2000 characters.
|
||||
// Longer URLs can preview but it's very inconsistant so ignoring any additional emojis
|
||||
if(emojiSheetUrlString.length + mxc.length > 2000) {
|
||||
break
|
||||
}
|
||||
|
||||
// Ignoring any additional emojis if it would exceed discords message length
|
||||
if(emojiSheetUrlString.length + mxc.length + content.length > 4000) {
|
||||
break
|
||||
}
|
||||
|
||||
mxUtils.generatePermittedMediaHash(mxc)
|
||||
emojiSheetUrl.searchParams.append('e', mxc)
|
||||
}
|
||||
|
||||
const emojiSheetUrlString = emojiSheetUrl.toString()
|
||||
|
||||
// Discord message length check. Using markdown link with placeholder text since discord displays
|
||||
// the link when there is preceeding text to the emoji
|
||||
const placeholderText = '.'
|
||||
content = content.concat(`[${placeholderText}](${emojiSheetUrlString})`)
|
||||
|
||||
return content
|
||||
}
|
||||
|
||||
@@ -937,7 +956,7 @@ async function eventToMessage(event, guild, channel, di) {
|
||||
if (replyLine && content.startsWith("> ")) content = "\n" + content
|
||||
|
||||
// SPRITE SHEET EMOJIS FEATURE:
|
||||
content = await uploadEndOfMessageSpriteSheet(content, attachments, pendingFiles, di?.mxcDownloader)
|
||||
content = await uploadEndOfMessageSpriteSheet(content)
|
||||
} else {
|
||||
// Looks like we're using the plaintext body!
|
||||
content = event.content.body
|
||||
|
||||
@@ -205,6 +205,19 @@ async function getViaServersQuery(roomID, api) {
|
||||
return qs
|
||||
}
|
||||
|
||||
function generatePermittedMediaHash(mxc) {
|
||||
assert(hasher, "xxhash is not ready yet")
|
||||
const mediaParts = mxc?.match(/^mxc:\/\/([^/]+)\/(\w+)$/)
|
||||
if (!mediaParts) return undefined
|
||||
|
||||
const serverAndMediaID = `${mediaParts[1]}/${mediaParts[2]}`
|
||||
const unsignedHash = hasher.h64(serverAndMediaID)
|
||||
const signedHash = unsignedHash - 0x8000000000000000n // shifting down to signed 64-bit range
|
||||
db.prepare("INSERT OR IGNORE INTO media_proxy (permitted_hash) VALUES (?)").run(signedHash)
|
||||
|
||||
return serverAndMediaID
|
||||
}
|
||||
|
||||
/**
|
||||
* Since the introduction of authenticated media, this can no longer just be the /_matrix/media/r0/download URL
|
||||
* because Discord and Discord users cannot use those URLs. Media now has to be proxied through the bridge.
|
||||
@@ -219,15 +232,8 @@ async function getViaServersQuery(roomID, api) {
|
||||
* @returns {string | undefined}
|
||||
*/
|
||||
function getPublicUrlForMxc(mxc) {
|
||||
assert(hasher, "xxhash is not ready yet")
|
||||
const mediaParts = mxc?.match(/^mxc:\/\/([^/]+)\/(\w+)$/)
|
||||
if (!mediaParts) return undefined
|
||||
|
||||
const serverAndMediaID = `${mediaParts[1]}/${mediaParts[2]}`
|
||||
const unsignedHash = hasher.h64(serverAndMediaID)
|
||||
const signedHash = unsignedHash - 0x8000000000000000n // shifting down to signed 64-bit range
|
||||
db.prepare("INSERT OR IGNORE INTO media_proxy (permitted_hash) VALUES (?)").run(signedHash)
|
||||
|
||||
const serverAndMediaID = generatePermittedMediaHash(mxc);
|
||||
if(!serverAndMediaID) return undefined
|
||||
return `${reg.ooye.bridge_origin}/download/matrix/${serverAndMediaID}`
|
||||
}
|
||||
|
||||
@@ -358,6 +364,7 @@ async function setUserPowerCascade(spaceID, mxid, power, api) {
|
||||
module.exports.bot = bot
|
||||
module.exports.BLOCK_ELEMENTS = BLOCK_ELEMENTS
|
||||
module.exports.eventSenderIsFromDiscord = eventSenderIsFromDiscord
|
||||
module.exports.generatePermittedMediaHash = generatePermittedMediaHash
|
||||
module.exports.getPublicUrlForMxc = getPublicUrlForMxc
|
||||
module.exports.getEventIDHash = getEventIDHash
|
||||
module.exports.MatrixStringBuilder = MatrixStringBuilder
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// @ts-check
|
||||
|
||||
const assert = require("assert/strict")
|
||||
const {defineEventHandler, getValidatedRouterParams, setResponseStatus, setResponseHeader, sendStream, createError, H3Event} = require("h3")
|
||||
const {defineEventHandler, getValidatedRouterParams, setResponseStatus, setResponseHeader, createError, H3Event, getValidatedQuery} = require("h3")
|
||||
const {z} = require("zod")
|
||||
|
||||
/** @type {import("xxhash-wasm").XXHashAPI} */ // @ts-ignore
|
||||
@@ -27,10 +27,8 @@ function getAPI(event) {
|
||||
return event.context.api || sync.require("../../matrix/api")
|
||||
}
|
||||
|
||||
as.router.get(`/download/matrix/:server_name/:media_id`, defineEventHandler(async event => {
|
||||
const params = await getValidatedRouterParams(event, schema.params.parse)
|
||||
|
||||
const serverAndMediaID = `${params.server_name}/${params.media_id}`
|
||||
function verifyMediaHash(serverName, mediaId) {
|
||||
const serverAndMediaID = `${serverName}/${mediaId}`
|
||||
const unsignedHash = hasher.h64(serverAndMediaID)
|
||||
const signedHash = unsignedHash - 0x8000000000000000n // shifting down to signed 64-bit range
|
||||
|
||||
@@ -41,7 +39,12 @@ as.router.get(`/download/matrix/:server_name/:media_id`, defineEventHandler(asyn
|
||||
data: `The file you requested isn't permitted by this media proxy.`
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
as.router.get(`/download/matrix/:server_name/:media_id`, defineEventHandler(async event => {
|
||||
const params = await getValidatedRouterParams(event, schema.params.parse)
|
||||
|
||||
verifyMediaHash(params.server_name, params.media_id)
|
||||
const api = getAPI(event)
|
||||
const res = await api.getMedia(`mxc://${params.server_name}/${params.media_id}`)
|
||||
|
||||
@@ -53,3 +56,34 @@ as.router.get(`/download/matrix/:server_name/:media_id`, defineEventHandler(asyn
|
||||
setResponseHeader(event, "Transfer-Encoding", "chunked")
|
||||
return res.body
|
||||
}))
|
||||
|
||||
const emojiSchema = z.object({
|
||||
'e': z.array(z.string()).or(z.string())
|
||||
})
|
||||
|
||||
const emojiSheet = sync.require("../../m2d/actions/emoji-sheet")
|
||||
const emojiSheetConverter = sync.require("../../m2d/converters/emoji-sheet")
|
||||
|
||||
as.router.get(`/emoji/matrix`, defineEventHandler(async event => {
|
||||
|
||||
const query = await getValidatedQuery(event, emojiSchema.parse)
|
||||
|
||||
let mxcs = query.e
|
||||
if(!Array.isArray(mxcs)) {
|
||||
mxcs = [mxcs]
|
||||
}
|
||||
|
||||
for(let mxc of mxcs) {
|
||||
const mediaParts = mxc.match(/^mxc:\/\/([^/]+)\/(\w+)$/)
|
||||
if (!mediaParts) return undefined
|
||||
verifyMediaHash(mediaParts[1], mediaParts[2])
|
||||
}
|
||||
const buffer = await emojiSheetConverter.compositeMatrixEmojis(mxcs, emojiSheet.getAndConvertEmoji)
|
||||
|
||||
const contentType = 'image/png'
|
||||
|
||||
setResponseStatus(event, 200)
|
||||
setResponseHeader(event, "Content-Type", contentType)
|
||||
setResponseHeader(event, "Transfer-Encoding", "chunked")
|
||||
return buffer
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user