forked from cadence/out-of-your-element
Compare commits
2 commits
f76974a8c0
...
54dad027e6
| Author | SHA1 | Date | |
|---|---|---|---|
| 54dad027e6 | |||
| e2d1811df5 |
3 changed files with 38 additions and 4 deletions
|
|
@ -769,20 +769,19 @@ async function messageToEvent(message, guild, options = {}, di) {
|
||||||
// Then scheduled events
|
// Then scheduled events
|
||||||
if (message.content && di?.snow) {
|
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
|
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
|
let invite
|
||||||
try {
|
try {
|
||||||
invite = await di.snow.invite.getInvite(match[1], {guild_scheduled_event_id: match[2]})
|
invite = await di.snow.invite.getInvite(match[1], {guild_scheduled_event_id: match[2]})
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
// Skip expired events and invites
|
||||||
if (e.code === 10006 || e.httpStatus === 404) {
|
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]}`)
|
console.warn(`[Backfill] Skipped expired scheduled event: ${match[0]}`)
|
||||||
const fallbackBody = `[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>`
|
const fallbackHtml = `<blockquote>Expired Scheduled Event: <a href="https://${match[0]}">${match[0]}</a></blockquote>`
|
||||||
await addTextEvent(fallbackBody, fallbackHtml, "m.notice")
|
await addTextEvent(fallbackBody, fallbackHtml, "m.notice")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
throw e // Re-throw unexpected errors (like 429 Rate Limits or 500s)
|
throw e
|
||||||
}
|
}
|
||||||
|
|
||||||
const event = invite.guild_scheduled_event
|
const event = invite.guild_scheduled_event
|
||||||
|
|
|
||||||
|
|
@ -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 => {
|
test("message2event: channel links are converted even inside lists (parser post-processer descends into list items)", async t => {
|
||||||
let called = 0
|
let called = 0
|
||||||
const events = await messageToEvent({
|
const events = await messageToEvent({
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,10 @@ const {green} = require("ansi-colors")
|
||||||
const passthrough = require("../src/passthrough")
|
const passthrough = require("../src/passthrough")
|
||||||
const db = new sqlite(":memory:")
|
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.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_origin = "https://matrix.cadence.moe" // so that tests will pass even when hard-coded
|
||||||
reg.ooye.server_name = "cadence.moe"
|
reg.ooye.server_name = "cadence.moe"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue