From 5326b7d6be1eacac987ec83a6f38d1b24b5eb0cc Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Tue, 11 Jul 2023 10:10:47 +1200 Subject: [PATCH] get room mentions formatting working --- d2m/converters/message-to-event.js | 8 ++-- d2m/converters/message-to-event.test.js | 4 +- d2m/discord-client.js | 3 +- index.js | 2 +- scripts/save-channel-names-to-db.js | 58 +++++++++++++++++++++++++ test/data.js | 57 ++++++++++++++++++++++++ 6 files changed, 124 insertions(+), 8 deletions(-) create mode 100644 scripts/save-channel-names-to-db.js diff --git a/d2m/converters/message-to-event.js b/d2m/converters/message-to-event.js index fc76bf2..9999df9 100644 --- a/d2m/converters/message-to-event.js +++ b/d2m/converters/message-to-event.js @@ -20,11 +20,11 @@ function getDiscordParseCallbacks(message, useHTML) { } }, channel: node => { - const roomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(node.id) - if (roomID && useHTML) { - return "https://matrix.to/#/" + roomID + 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}` } else { - return "#" + node.id + return `#${nick || name}` } }, role: node => diff --git a/d2m/converters/message-to-event.test.js b/d2m/converters/message-to-event.test.js index a456a94..e3fcb06 100644 --- a/d2m/converters/message-to-event.test.js +++ b/d2m/converters/message-to-event.test.js @@ -27,9 +27,9 @@ test("message2event: simple room mention", async t => { t.deepEqual(events, [{ $type: "m.room.message", msgtype: "m.text", - body: "@crunch god: Tell me about Phil, renowned martial arts master and creator of the Chin Trick", + body: "#main", format: "org.matrix.custom.html", - formatted_body: '@crunch god Tell me about Phil, renowned martial arts master and creator of the Chin Trick' + formatted_body: '#main' }]) }) diff --git a/d2m/discord-client.js b/d2m/discord-client.js index 2092718..91682bd 100644 --- a/d2m/discord-client.js +++ b/d2m/discord-client.js @@ -12,8 +12,9 @@ const discordPackets = sync.require("./discord-packets") class DiscordClient { /** * @param {string} discordToken + * @param {boolean} listen whether to set up the event listeners for OOYE to operate */ - constructor(discordToken) { + constructor(discordToken, listen) { this.discordToken = discordToken this.snow = new SnowTransfer(discordToken) this.cloud = new CloudStorm(discordToken, { diff --git a/index.js b/index.js index 233d518..447e944 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,7 @@ Object.assign(passthrough, {config, sync, db}) const DiscordClient = require("./d2m/discord-client") -const discord = new DiscordClient(config.discordToken) +const discord = new DiscordClient(config.discordToken, true) passthrough.discord = discord const as = require("./m2d/appservice") diff --git a/scripts/save-channel-names-to-db.js b/scripts/save-channel-names-to-db.js new file mode 100644 index 0000000..a70b1bb --- /dev/null +++ b/scripts/save-channel-names-to-db.js @@ -0,0 +1,58 @@ +// @ts-check + +const sqlite = require("better-sqlite3") +const HeatSync = require("heatsync") + +const config = require("../config") +const passthrough = require("../passthrough") +const db = new sqlite("db/ooye.db") + +const sync = new HeatSync({watchFS: false}) + +Object.assign(passthrough, {config, sync, db}) + +const DiscordClient = require("../d2m/discord-client") + +const discord = new DiscordClient(config.discordToken, false) +passthrough.discord = discord + +;(async () => { + await discord.cloud.connect() + console.log("Discord gateway started") + + const f = event => onPacket(discord, event, () => discord.cloud.off("event", f)) + discord.cloud.on("event", f) +})() + +const expectedGuilds = new Set() + +const prepared = db.prepare("UPDATE channel_room SET name = ? WHERE channel_id = ?") + +/** @param {DiscordClient} discord */ +function onPacket(discord, event, unsubscribe) { + if (event.t === "READY") { + for (const obj of event.d.guilds) { + expectedGuilds.add(obj.id) + } + + } else if (event.t === "GUILD_CREATE") { + expectedGuilds.delete(event.d.id) + + // Store the channel. + for (const channel of event.d.channels || []) { + prepared.run(channel.name, channel.id) + } + + // Checked them all? + if (expectedGuilds.size === 0) { + discord.cloud.disconnect() + unsubscribe() + + // I don't know why node keeps running. + setTimeout(() => { + console.log("Stopping now.") + process.exit() + }, 1500).unref() + } + } +} diff --git a/test/data.js b/test/data.js index 52b8770..d2c586a 100644 --- a/test/data.js +++ b/test/data.js @@ -216,6 +216,63 @@ module.exports = { flags: 0, components: [] }, + simple_room_mention: { + type: 0, + tts: false, + timestamp: "2023-07-10T20:04:25.939000+00:00", + referenced_message: null, + pinned: false, + nonce: "1128054139385806848", + mentions: [], + mention_roles: [], + mention_everyone: false, + member: { + roles: [ + "112767366235959296", "118924814567211009", + "204427286542417920", "199995902742626304", + "222168467627835392", "238028326281805825", + "259806643414499328", "265239342648131584", + "271173313575780353", "287733611912757249", + "225744901915148298", "305775031223320577", + "318243902521868288", "348651574924541953", + "349185088157777920", "378402925128712193", + "392141548932038658", "393912152173576203", + "482860581670486028", "495384759074160642", + "638988388740890635", "373336013109461013", + "530220455085473813", "454567553738473472", + "790724320824655873", "1123518980456452097", + "1040735082610167858", "695946570482450442", + "1123460940935991296", "849737964090556488" + ], + premium_since: null, + pending: false, + nick: null, + mute: false, + joined_at: "2015-11-11T09:55:40.321000+00:00", + flags: 0, + deaf: false, + communication_disabled_until: null, + avatar: null + }, + id: "1128054143064494233", + flags: 0, + embeds: [], + edited_timestamp: null, + content: "<#112760669178241024>", + components: [], + channel_id: "266767590641238027", + author: { + username: "kumaccino", + public_flags: 128, + id: "113340068197859328", + global_name: "kumaccino", + discriminator: "0", + avatar_decoration: null, + avatar: "b48302623a12bc7c59a71328f72ccb39" + }, + attachments: [], + guild_id: "112760669178241024" + }, simple_message_link: { id: "1126788210308161626", type: 0,