Generate embeds for invites with events

This commit is contained in:
Cadence Ember
2025-06-08 21:52:28 +12:00
parent c50d238552
commit ab396bd581
5 changed files with 421 additions and 5 deletions

6
package-lock.json generated
View File

@@ -2950,9 +2950,9 @@
}
},
"node_modules/tar-fs": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-2.1.2.tgz",
"integrity": "sha512-EsaAXwxmx8UB7FRKqeozqEPop69DXcmYwTQwXvyAPF352HJsPdkVhvTaDPYqfNgruveJIJy3TA2l+2zj8LJIJA==",
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-2.1.3.tgz",
"integrity": "sha512-090nwYJDmlhwFwEW3QQl+vaNnxsO2yVsd45eTKRBzSzu+hlb1w2K9inVq5b0ngXuLVqQ4ApvsUHHnu/zQNkWAg==",
"license": "MIT",
"dependencies": {
"chownr": "^1.1.1",

View File

@@ -42,7 +42,7 @@ async function sendMessage(message, channel, guild, row) {
}
}
const events = await messageToEvent.messageToEvent(message, guild, {}, {api})
const events = await messageToEvent.messageToEvent(message, guild, {}, {api, snow: discord.snow})
const eventIDs = []
if (events.length) {
db.prepare("INSERT OR IGNORE INTO message_channel (message_id, channel_id) VALUES (?, ?)").run(message.id, message.channel_id)

View File

@@ -204,7 +204,7 @@ async function attachmentToEvent(mentions, attachment) {
* - includeEditFallbackStar: false
* - alwaysReturnFormattedBody: false - formatted_body will be skipped if it is the same as body because the message is plaintext. if you want the formatted_body to be returned anyway, for example to merge it with another message, then set this to true.
* - scanTextForMentions: true - needs to be set to false when converting forwarded messages etc which may be from a different channel that can't be scanned.
* @param {{api: import("../../matrix/api")}} di simple-as-nails dependency injection for the matrix API
* @param {{api: import("../../matrix/api"), snow?: import("snowtransfer").SnowTransfer}} di simple-as-nails dependency injection for the matrix API
*/
async function messageToEvent(message, guild, options = {}, di) {
const events = []
@@ -608,6 +608,49 @@ async function messageToEvent(message, guild, options = {}, di) {
await addTextEvent(body, html, msgtype)
}
// 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
const invite = await di.snow.invite.getInvite(match[1], {guild_scheduled_event_id: match[2]})
const event = invite.guild_scheduled_event
if (!event) continue // the event ID provided was not valid
const formatter = new Intl.DateTimeFormat("en-NZ", {month: "long", day: "numeric", hour: "numeric", minute: "2-digit", timeZoneName: "shortGeneric"}) // 9 June at 3:00 pm NZT
const rep = new mxUtils.MatrixStringBuilder()
// Add time
if (event.scheduled_end_time) {
// @ts-ignore - no definition available for formatRange
rep.addParagraph(`Scheduled Event - ${formatter.formatRange(new Date(event.scheduled_start_time), new Date(event.scheduled_end_time))}`)
} else {
rep.addParagraph(`Scheduled Event - ${formatter.format(new Date(event.scheduled_start_time))}`)
}
// Add details
rep.addLine(`## ${event.name}`, tag`<strong>${event.name}</strong>`)
if (event.description) rep.addLine(event.description)
// Add location
if (event.entity_metadata?.location) {
rep.addParagraph(`📍 ${event.entity_metadata.location}`)
} else if (invite.channel?.name) {
const roomID = select("channel_room", "room_id", {channel_id: invite.channel.id}).pluck().get()
if (roomID) {
const via = await getViaServersMemo(roomID)
rep.addParagraph(`🔊 ${invite.channel.name} - https://matrix.to/#/${roomID}?${via}`, tag`🔊 ${invite.channel.name} - <a href="https://matrix.to/#/${roomID}?${via}">${invite.channel.name}</a>`)
} else {
rep.addParagraph(`🔊 ${invite.channel.name}`)
}
}
// Send like an embed
let {body, formatted_body: html} = rep.get()
body = body.split("\n").map(l => "| " + l).join("\n")
html = `<blockquote>${html}</blockquote>`
await addTextEvent(body, html, "m.notice")
}
}
// Then attachments
if (message.attachments) {
const attachmentEvents = await Promise.all(message.attachments.map(attachmentToEvent.bind(null, mentions)))

View File

@@ -1165,3 +1165,131 @@ test("message2event: don't scan forwarded messages for mentions", async t => {
}
])
})
test("message2event: invite no details embed if no event", async t => {
const events = await messageToEvent({content: "https://discord.gg/placeholder?event=1381190945646710824"}, {}, {}, {
snow: {
invite: {
getInvite: async () => ({...data.invite.irl, guild_scheduled_event: null})
}
}
})
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",
}
])
})
test("message2event: irl invite event renders embed", async t => {
const events = await messageToEvent({content: "https://discord.gg/placeholder?event=1381190945646710824"}, {}, {}, {
snow: {
invite: {
getInvite: async () => data.invite.irl
}
}
})
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: `| Scheduled Event - 8 June at 10:00pm NZT9 June at 12:00am NZT`
+ `\n| ## forest exploration`
+ `\n| `
+ `\n| 📍 the dark forest`,
format: "org.matrix.custom.html",
formatted_body: `<blockquote><p>Scheduled Event - 8 June at 10:00pm NZT9 June at 12:00am NZT</p>`
+ `<strong>forest exploration</strong>`
+ `<p>📍 the dark forest</p></blockquote>`,
"m.mentions": {}
}
])
})
test("message2event: vc invite event renders embed", async t => {
const events = await messageToEvent({content: "https://discord.gg/placeholder?event=1381174024801095751"}, {}, {}, {
snow: {
invite: {
getInvite: async () => data.invite.vc
}
}
})
t.deepEqual(events, [
{
$type: "m.room.message",
body: "https://discord.gg/placeholder?event=1381174024801095751",
format: "org.matrix.custom.html",
formatted_body: "<a href=\"https://discord.gg/placeholder?event=1381174024801095751\">https://discord.gg/placeholder?event=1381174024801095751</a>",
"m.mentions": {},
msgtype: "m.text",
},
{
$type: "m.room.message",
msgtype: "m.notice",
body: `| Scheduled Event - 9 June at 3:00 pm NZT`
+ `\n| ## Cooking (Netrunners)`
+ `\n| Short circuited brain interfaces actually just means your brain is medium rare, yum.`
+ `\n| `
+ `\n| 🔊 Cooking`,
format: "org.matrix.custom.html",
formatted_body: `<blockquote><p>Scheduled Event - 9 June at 3:00 pm NZT</p>`
+ `<strong>Cooking (Netrunners)</strong><br>Short circuited brain interfaces actually just means your brain is medium rare, yum.`
+ `<p>🔊 Cooking</p></blockquote>`,
"m.mentions": {}
}
])
})
test("message2event: vc invite event renders embed with room link", async t => {
const events = await messageToEvent({content: "https://discord.gg/placeholder?event=1381174024801095751"}, {}, {}, {
api: {
getJoinedMembers: async () => ({
joined: {
"@_ooye_bot:cadence.moe": {display_name: null, avatar_url: null},
}
})
},
snow: {
invite: {
getInvite: async () => data.invite.known_vc
}
}
})
t.deepEqual(events, [
{
$type: "m.room.message",
body: "https://discord.gg/placeholder?event=1381174024801095751",
format: "org.matrix.custom.html",
formatted_body: "<a href=\"https://discord.gg/placeholder?event=1381174024801095751\">https://discord.gg/placeholder?event=1381174024801095751</a>",
"m.mentions": {},
msgtype: "m.text",
},
{
$type: "m.room.message",
msgtype: "m.notice",
body: `| Scheduled Event - 9 June at 3:00 pm NZT`
+ `\n| ## Cooking (Netrunners)`
+ `\n| Short circuited brain interfaces actually just means your brain is medium rare, yum.`
+ `\n| `
+ `\n| 🔊 Hey. - https://matrix.to/#/!FuDZhlOAtqswlyxzeR:cadence.moe?via=cadence.moe`,
format: "org.matrix.custom.html",
formatted_body: `<blockquote><p>Scheduled Event - 9 June at 3:00 pm NZT</p>`
+ `<strong>Cooking (Netrunners)</strong><br>Short circuited brain interfaces actually just means your brain is medium rare, yum.`
+ `<p>🔊 Hey. - <a href="https://matrix.to/#/!FuDZhlOAtqswlyxzeR:cadence.moe?via=cadence.moe">Hey.</a></p></blockquote>`,
"m.mentions": {}
}
])
})

View File

@@ -4858,5 +4858,250 @@ module.exports = {
application_id: "1109360903096369153",
guild_id: "497159726455455754"
}
},
invite: {
irl: {
type: 0,
code: 'placeholder',
inviter: {
id: '772659086046658620',
username: 'cadence.worm',
avatar: '466df0c98b1af1e1388f595b4c1ad1b9',
discriminator: '0',
public_flags: 0,
flags: 0,
banner: null,
accent_color: 4534897,
global_name: 'cadence',
avatar_decoration_data: null,
collectibles: null,
banner_color: '#453271',
clan: null,
primary_guild: null
},
expires_at: '2025-06-15T08:39:43+00:00',
guild: {
id: '1338114140941586518',
name: 'self service',
splash: null,
banner: null,
description: null,
icon: null,
features: [],
verification_level: 0,
vanity_url_code: null,
nsfw_level: 0,
nsfw: false,
premium_subscription_count: 0,
premium_tier: 0
},
guild_id: '1338114140941586518',
channel: { id: '1338114141658939517', type: 0, name: 'general' },
guild_scheduled_event: {
id: '1381190945646710824',
guild_id: '1338114140941586518',
name: 'forest exploration',
description: '',
channel_id: null,
creator_id: '772659086046658620',
image: null,
scheduled_start_time: '2025-06-08T10:00:00.161000+00:00',
scheduled_end_time: '2025-06-08T12:00:00.161000+00:00',
status: 1,
entity_type: 3,
entity_id: null,
recurrence_rule: null,
user_count: 1,
privacy_level: 2,
sku_ids: [],
user_rsvp: null,
guild_scheduled_event_exceptions: [],
entity_metadata: { location: 'the dark forest' }
},
profile: {
id: '1338114140941586518',
name: 'self service',
icon_hash: null,
member_count: 2,
online_count: 1,
description: null,
banner_hash: null,
game_application_ids: [],
game_activity: {},
tag: null,
badge: 0,
badge_color_primary: '#ff0000',
badge_color_secondary: '#800000',
badge_hash: null,
traits: [],
features: [],
visibility: 2,
custom_banner_hash: null,
premium_subscription_count: 0,
premium_tier: 0
}
},
vc: {
type: 0,
code: 'placeholder',
inviter: {
id: '1024720274928697384',
username: '1024720274928697384',
avatar: '040a0652f1c76af3b71bb2c58ee0057b',
discriminator: '0',
public_flags: 0,
flags: 0,
banner: null,
accent_color: 4259841,
global_name: 'Regalia, Goddess of OH GOD OH FU',
avatar_decoration_data: null,
collectibles: null,
banner_color: '#410001',
clan: null,
primary_guild: null
},
expires_at: '2025-06-15T07:32:30+00:00',
guild: {
id: '1340545485542391879',
name: 'VRCooking',
splash: null,
banner: null,
description: null,
icon: '8e1948b83d79c11ccb32b9e54a5d85fd',
features: [ 'SOUNDBOARD', 'ACTIVITY_FEED_DISABLED_BY_USER' ],
verification_level: 0,
vanity_url_code: null,
nsfw_level: 0,
nsfw: false,
premium_subscription_count: 0,
premium_tier: 0
},
guild_id: '1340545485542391879',
channel: { id: '1368144987707019306', type: 2, name: 'Cooking' },
guild_scheduled_event: {
id: '1381174024801095751',
guild_id: '1340545485542391879',
name: 'Cooking (Netrunners)',
description: 'Short circuited brain interfaces actually just means your brain is medium rare, yum.',
channel_id: '1368144987707019306',
creator_id: '1024720274928697384',
image: null,
scheduled_start_time: '2025-06-09T03:00:00+00:00',
scheduled_end_time: null,
status: 1,
entity_type: 2,
entity_id: null,
recurrence_rule: null,
user_count: 2,
privacy_level: 2,
sku_ids: [],
user_rsvp: null,
guild_scheduled_event_exceptions: [],
entity_metadata: {}
},
profile: {
id: '1340545485542391879',
name: 'VRCooking',
icon_hash: '8e1948b83d79c11ccb32b9e54a5d85fd',
member_count: 18,
online_count: 13,
description: null,
banner_hash: null,
game_application_ids: [],
game_activity: {},
tag: null,
badge: 0,
badge_color_primary: '#ff0000',
badge_color_secondary: '#800000',
badge_hash: null,
traits: [],
features: [],
visibility: 2,
custom_banner_hash: null,
premium_subscription_count: 0,
premium_tier: 0
}
},
known_vc: {
type: 0,
code: 'placeholder',
inviter: {
id: '1024720274928697384',
username: '1024720274928697384',
avatar: '040a0652f1c76af3b71bb2c58ee0057b',
discriminator: '0',
public_flags: 0,
flags: 0,
banner: null,
accent_color: 4259841,
global_name: 'Regalia, Goddess of OH GOD OH FU',
avatar_decoration_data: null,
collectibles: null,
banner_color: '#410001',
clan: null,
primary_guild: null
},
expires_at: '2025-06-15T07:32:30+00:00',
guild: {
id: '112760669178241024',
name: 'Psychonauts 3',
splash: null,
banner: null,
description: null,
icon: '8e1948b83d79c11ccb32b9e54a5d85fd',
features: [ 'SOUNDBOARD', 'ACTIVITY_FEED_DISABLED_BY_USER' ],
verification_level: 0,
vanity_url_code: null,
nsfw_level: 0,
nsfw: false,
premium_subscription_count: 0,
premium_tier: 0
},
guild_id: '112760669178241024',
channel: { id: '1162005314908999790', type: 0, name: 'Hey.' },
guild_scheduled_event: {
id: '1381174024801095751',
guild_id: '112760669178241024',
name: 'Cooking (Netrunners)',
description: 'Short circuited brain interfaces actually just means your brain is medium rare, yum.',
channel_id: '1162005314908999790',
creator_id: '1024720274928697384',
image: null,
scheduled_start_time: '2025-06-09T03:00:00+00:00',
scheduled_end_time: null,
status: 1,
entity_type: 2,
entity_id: null,
recurrence_rule: null,
user_count: 2,
privacy_level: 2,
sku_ids: [],
user_rsvp: null,
guild_scheduled_event_exceptions: [],
entity_metadata: {}
},
profile: {
id: '112760669178241024',
name: 'Psychonauts 3',
icon_hash: '8e1948b83d79c11ccb32b9e54a5d85fd',
member_count: 18,
online_count: 13,
description: null,
banner_hash: null,
game_application_ids: [],
game_activity: {},
tag: null,
badge: 0,
badge_color_primary: '#ff0000',
badge_color_secondary: '#800000',
badge_hash: null,
traits: [],
features: [],
visibility: 2,
custom_banner_hash: null,
premium_subscription_count: 0,
premium_tier: 0
}
}
}
}