From d666c0aedb3ad97b78ad85ef4a915ce6317548fe Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Mon, 21 Aug 2023 08:07:05 +1200 Subject: [PATCH] attempt to support THREAD_CREATE but it explodes --- d2m/converters/message-to-event.js | 10 ++++++---- d2m/discord-packets.js | 10 ++++++++++ d2m/event-dispatcher.js | 13 ++++++++++++- notes.md | 10 ++++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/d2m/converters/message-to-event.js b/d2m/converters/message-to-event.js index 3808beb0..c34b389f 100644 --- a/d2m/converters/message-to-event.js +++ b/d2m/converters/message-to-event.js @@ -27,11 +27,13 @@ function getDiscordParseCallbacks(message, useHTML) { }, /** @param {{id: string, type: "discordChannel"}} node */ channel: node => { - const {room_id, name, nick} = db.prepare("SELECT room_id, name, nick FROM channel_room WHERE channel_id = ?").get(node.id) - if (room_id && useHTML) { - return `#${nick || name}` + const row = db.prepare("SELECT room_id, name, nick FROM channel_room WHERE channel_id = ?").get(node.id) + if (!row) { + return `<#${node.id}>` // fallback for when this channel is not bridged + } else if (useHTML) { + return `#${row.nick || row.name}` } else { - return `#${nick || name}` + return `#${row.nick || row.name}` } }, /** @param {{animated: boolean, name: string, id: string, type: "discordEmoji"}} node */ diff --git a/d2m/discord-packets.js b/d2m/discord-packets.js index f1723219..776d4b1b 100644 --- a/d2m/discord-packets.js +++ b/d2m/discord-packets.js @@ -44,6 +44,10 @@ const utils = { eventDispatcher.checkMissedMessages(client, message.d) + } else if (message.t === "THREAD_CREATE") { + client.channels.set(message.d.id, message.d) + + } else if (message.t === "CHANNEL_UPDATE" || message.t === "THREAD_UPDATE") { client.channels.set(message.d.id, message.d) @@ -81,13 +85,19 @@ const utils = { if (message.t === "CHANNEL_UPDATE") { await eventDispatcher.onChannelOrThreadUpdate(client, message.d, false) + } else if (message.t === "THREAD_CREATE") { + console.log(message) + // await eventDispatcher.onThreadCreate(client, message.d) + } else if (message.t === "THREAD_UPDATE") { await eventDispatcher.onChannelOrThreadUpdate(client, message.d, true) } else if (message.t === "MESSAGE_CREATE") { + console.log(message) await eventDispatcher.onMessageCreate(client, message.d) } else if (message.t === "MESSAGE_UPDATE") { + console.log(message) await eventDispatcher.onMessageUpdate(client, message.d) } else if (message.t === "MESSAGE_DELETE") { diff --git a/d2m/event-dispatcher.js b/d2m/event-dispatcher.js index ad2a2d2f..154cb349 100644 --- a/d2m/event-dispatcher.js +++ b/d2m/event-dispatcher.js @@ -101,7 +101,18 @@ module.exports = { } }, - /** + /** + * @param {import("./discord-client")} client + * @param {import("discord-api-types/v10").APIChannel} thread + */ + async onThreadCreate(client, thread) { + console.log(thread) + const parentRoomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").get(thread.parent_id) + if (!parentRoomID) return // Not interested in a thread if we aren't interested in its wider channel + await createRoom.syncRoom(thread.id) + }, + + /** * @param {import("./discord-client")} client * @param {import("discord-api-types/v10").GatewayChannelUpdateDispatchData} channelOrThread * @param {boolean} isThread diff --git a/notes.md b/notes.md index 1dcbcd7f..dbc5ecda 100644 --- a/notes.md +++ b/notes.md @@ -9,6 +9,16 @@ A database will be used to store the discord id to matrix event id mapping. Tabl There needs to be a way to easily manually trigger something later. For example, it should be easy to manually retry sending a message, or check all members for changes, etc. +## Discord's gateway when a new thread is created from an existing message: + +1. Regular MESSAGE_CREATE of the message that it's going to branch off in the future. Example ID -6423 +2. It MESSAGE_UPDATEd the ID -6423 with this whole data: {id:-6423,flags: 32,channel_id:-2084,guild_id:-1727} (ID is the message ID it's branching off, channel ID is the parent channel containing the message ID it's branching off) +3. It THREAD_CREATEd and gave us a channel object with type 11 (public thread) and parent ID -2084 and ID -6423. +4. It MESSAGE_CREATEd type 21 with blank content and a message reference pointing towards channel -2084 message -6423. (That's the message it branched from in the parent channel.) This MESSAGE_CREATE got ID -4631 (a new ID). Apart from that it's a regular message object. +5. Finally, as the first "real" message in that thread (which a user must send to create that thread!) it sent a regular message object with a new message ID and a channel ID of -6423. + +When viewing this thread, it shows the message branched from at the top, and then the first "real" message right underneath, as separate groups. + ## Current manual process for setting up a server 1. Call createSpace.createSpace(discord.guilds.get(GUILD_ID))