Customise format for Klipy GIFs

This commit is contained in:
Cadence Ember
2026-02-04 00:45:23 +13:00
parent c73800f785
commit b52b2de205
4 changed files with 104 additions and 4 deletions

View File

@@ -630,6 +630,16 @@ async function messageToEvent(message, guild, options = {}, di) {
message.content = `added a new emoji, ${message.content} :${name}:`
}
// Send Klipy GIFs in customised form
let isKlipyGIF = false
let isOnlyKlipyGIF = false
if (message.embeds?.length === 1 && message.embeds[0].provider?.name === "Klipy" && message.embeds[0].video?.url) {
isKlipyGIF = true
if (message.content.match(/^https?:\/\/klipy\.com[^ \n]+$/)) {
isOnlyKlipyGIF = true
}
}
// Forwarded content appears first
if (message.message_reference?.type === DiscordTypes.MessageReferenceType.Forward && message.message_snapshots?.length) {
// Forwarded notice
@@ -682,7 +692,7 @@ async function messageToEvent(message, guild, options = {}, di) {
}
// Then text content
if (message.content) {
if (message.content && !isOnlyKlipyGIF) {
// Mentions scenario 3: scan the message content for written @mentions of matrix users. Allows for up to one space between @ and mention.
let content = message.content
if (options.scanTextForMentions !== false) {
@@ -905,6 +915,20 @@ async function messageToEvent(message, guild, options = {}, di) {
// Start building up a replica ("rep") of the embed in Discord-markdown format, which we will convert into both plaintext and formatted body at once
const rep = new mxUtils.MatrixStringBuilder()
if (isKlipyGIF) {
rep.add("[GIF] ", "➿ ")
if (embed.title) {
rep.add(`${embed.title} ${embed.video.url}`, tag`<a href="${embed.video.url}">${embed.title}</a>`)
} else {
rep.add(embed.video.url)
}
let {body, formatted_body: html} = rep.get()
html = `<blockquote>${html}</blockquote>`
await addTextEvent(body, html, "m.text")
continue
}
// Provider
if (embed.provider?.name && embed.provider.name !== "Tenor") {
if (embed.provider.url) {

View File

@@ -346,6 +346,18 @@ test("message2event embeds: tenor gif should show a video link without a provide
}])
})
test("message2event embeds: klipy gif should send in customised format", async t => {
const events = await messageToEvent(data.message_with_embeds.klipy_gif, data.guild.general, {}, {})
t.deepEqual(events, [{
$type: "m.room.message",
msgtype: "m.text",
body: "[GIF] Cute Corgi Waddle https://static.klipy.com/ii/d7aec6f6f171607374b2065c836f92f4/5b/5b/7ndEhcilPNKJ8O.mp4",
format: "org.matrix.custom.html",
formatted_body: "<blockquote>➿ <a href=\"https://static.klipy.com/ii/d7aec6f6f171607374b2065c836f92f4/5b/5b/7ndEhcilPNKJ8O.mp4\">Cute Corgi Waddle</a></blockquote>",
"m.mentions": {}
}])
})
test("message2event embeds: if discord creates an embed preview for a discord channel link, don't copy that embed", async t => {
const events = await messageToEvent(data.message_with_embeds.discord_server_included_punctuation_bad_discord, data.guild.general, {}, {
api: {

View File

@@ -2,6 +2,7 @@
const assert = require("assert").strict
const Ty = require("../types")
const {tag} = require("@cloudrac3r/html-template-tag")
const passthrough = require("../passthrough")
const {db} = passthrough
@@ -72,7 +73,7 @@ class MatrixStringBuilder {
*/
add(body, formattedBody, condition = true) {
if (condition) {
if (formattedBody == undefined) formattedBody = body
if (formattedBody == undefined) formattedBody = tag`${body}`
this.body += body
this.formattedBody += formattedBody
}
@@ -86,7 +87,7 @@ class MatrixStringBuilder {
*/
addLine(body, formattedBody, condition = true) {
if (condition) {
if (formattedBody == undefined) formattedBody = body
if (formattedBody == undefined) formattedBody = tag`${body}`
if (this.body.length && this.body.slice(-1) !== "\n") this.body += "\n"
this.body += body
const match = this.formattedBody.match(/<\/?([a-zA-Z]+[a-zA-Z0-9]*)[^>]*>\s*$/)
@@ -103,7 +104,7 @@ class MatrixStringBuilder {
*/
addParagraph(body, formattedBody, condition = true) {
if (condition) {
if (formattedBody == undefined) formattedBody = body
if (formattedBody == undefined) formattedBody = tag`${body}`
if (this.body.length && this.body.slice(-1) !== "\n") this.body += "\n\n"
this.body += body
const match = formattedBody.match(/^<([a-zA-Z]+[a-zA-Z0-9]*)/)