Compare commits

..

2 commits

Author SHA1 Message Date
54dad027e6
test: fix null registration object and validation errors during setup
Running the test suite without a local `registration.yaml` previously
caused a TypeError because the `reg` export defaulted to null. This
injects a base template using `getTemplateRegistration` before applying
test-specific overrides.
2026-03-02 02:26:48 +00:00
Bea
e2d1811df5
fix(d2m): handle expired Discord event and invite links
Wrap the scheduled event lookup in a try-catch block to prevent the
bridge from crashing when it encounters expired or invalid links.
When a 404 or Discord error code 10006 is caught, the bot now
generates a fallback "m.notice" event to inform Matrix users that
the event link has expired.
2026-03-02 02:26:48 +00:00
3 changed files with 38 additions and 4 deletions

View file

@ -769,20 +769,19 @@ async function messageToEvent(message, guild, options = {}, di) {
// Then scheduled events
if (message.content && di?.snow) {
for (const match of [...message.content.matchAll(/discord\.gg\/([A-Za-z0-9]+)\?event=([0-9]{18,})/g)]) { // snowflake has minimum 18 because the events feature is at least that old
// FIX: Wrap the API call in try/catch to handle expired events gracefully
let invite
try {
invite = await di.snow.invite.getInvite(match[1], {guild_scheduled_event_id: match[2]})
} catch (e) {
// Skip expired events and invites
if (e.code === 10006 || e.httpStatus === 404) {
// The event or invite is expired. Render a fallback notice so it's obvious, but don't crash.
console.warn(`[Backfill] Skipped expired scheduled event: ${match[0]}`)
const fallbackBody = `[Expired Scheduled Event: ${match[0]}]`
const fallbackHtml = `<blockquote>Expired Scheduled Event: <a href="https://${match[0]}">${match[0]}</a></blockquote>`
await addTextEvent(fallbackBody, fallbackHtml, "m.notice")
continue
}
throw e // Re-throw unexpected errors (like 429 Rate Limits or 500s)
throw e
}
const event = invite.guild_scheduled_event

View file

@ -1538,6 +1538,38 @@ test("message2event: vc invite event renders embed with room link", async t => {
])
})
test("message2event: expired event invite renders fallback notice", async t => {
const events = await messageToEvent({content: "https://discord.gg/placeholder?event=1381190945646710824"}, {}, {}, {
snow: {
invite: {
getInvite: async () => {
const error = new Error("Unknown Invite")
error.code = 10006
throw error
}
}
}
})
t.deepEqual(events, [
{
$type: "m.room.message",
body: "https://discord.gg/placeholder?event=1381190945646710824",
format: "org.matrix.custom.html",
formatted_body: "<a href=\"https://discord.gg/placeholder?event=1381190945646710824\">https://discord.gg/placeholder?event=1381190945646710824</a>",
"m.mentions": {},
msgtype: "m.text",
},
{
$type: "m.room.message",
msgtype: "m.notice",
body: "[Expired Scheduled Event: discord.gg/placeholder?event=1381190945646710824]",
format: "org.matrix.custom.html",
formatted_body: "<blockquote>Expired Scheduled Event: <a href=\"https://discord.gg/placeholder?event=1381190945646710824\">discord.gg/placeholder?event=1381190945646710824</a></blockquote>",
"m.mentions": {}
}
])
})
test("message2event: channel links are converted even inside lists (parser post-processer descends into list items)", async t => {
let called = 0
const events = await messageToEvent({

View file

@ -13,7 +13,10 @@ const {green} = require("ansi-colors")
const passthrough = require("../src/passthrough")
const db = new sqlite(":memory:")
const {reg} = require("../src/matrix/read-registration")
const readReg = require("../src/matrix/read-registration")
readReg.reg = readReg.getTemplateRegistration("cadence.moe")
const {reg} = readReg
reg.url = "http://localhost:6693"
reg.ooye.discord_token = "Njg0MjgwMTkyNTUzODQ0NzQ3.Xl3zlw.baby"
reg.ooye.server_origin = "https://matrix.cadence.moe" // so that tests will pass even when hard-coded
reg.ooye.server_name = "cadence.moe"