diff --git a/d2m/actions/announce-thread.js b/d2m/actions/announce-thread.js index 760905ae..e52a2a38 100644 --- a/d2m/actions/announce-thread.js +++ b/d2m/actions/announce-thread.js @@ -15,7 +15,7 @@ const api = sync.require("../../matrix/api") * @param {import("discord-api-types/v10").APIThreadChannel} thread */ async function announceThread(parentRoomID, threadRoomID, thread) { - const creatorMxid = select("sim", "mxid", "WHERE discord_id = ?").pluck().get(thread.owner_id) + const creatorMxid = select("sim", "mxid", "WHERE user_id = ?").pluck().get(thread.owner_id) const content = await threadToAnnouncement.threadToAnnouncement(parentRoomID, threadRoomID, creatorMxid, thread, {api}) diff --git a/d2m/actions/register-user.js b/d2m/actions/register-user.js index 05f85180..dc3995dd 100644 --- a/d2m/actions/register-user.js +++ b/d2m/actions/register-user.js @@ -29,7 +29,7 @@ async function createSim(user) { // Save chosen name in the database forever // Making this database change right away so that in a concurrent registration, the 2nd registration will already have generated a different localpart because it can see this row when it generates - db.prepare("INSERT INTO sim (discord_id, sim_name, localpart, mxid) VALUES (?, ?, ?, ?)").run(user.id, simName, localpart, mxid) + db.prepare("INSERT INTO sim (user_id, sim_name, localpart, mxid) VALUES (?, ?, ?, ?)").run(user.id, simName, localpart, mxid) // Register matrix user with that name try { @@ -37,7 +37,7 @@ async function createSim(user) { } catch (e) { // If user creation fails, manually undo the database change. Still isn't perfect, but should help. // (A transaction would be preferable, but I don't think it's safe to leave transaction open across event loop ticks.) - db.prepare("DELETE FROM sim WHERE discord_id = ?").run(user.id) + db.prepare("DELETE FROM sim WHERE user_id = ?").run(user.id) throw e } return mxid @@ -51,7 +51,7 @@ async function createSim(user) { */ async function ensureSim(user) { let mxid = null - const existing = select("sim", "mxid", "WHERE discord_id = ?").pluck().get(user.id) + const existing = select("sim", "mxid", "WHERE user_id = ?").pluck().get(user.id) if (existing) { mxid = existing } else { @@ -164,7 +164,7 @@ async function syncAllUsersInRoom(roomID) { assert.ok(typeof guildID === "string") for (const mxid of mxids) { - const userID = select("sim", "discord_id", "WHERE mxid = ?").pluck().get(mxid) + const userID = select("sim", "user_id", "WHERE mxid = ?").pluck().get(mxid) assert.ok(typeof userID === "string") /** @ts-ignore @type {Required} */ diff --git a/d2m/actions/remove-reaction.js b/d2m/actions/remove-reaction.js index b8188675..d84531e4 100644 --- a/d2m/actions/remove-reaction.js +++ b/d2m/actions/remove-reaction.js @@ -42,7 +42,7 @@ async function removeReaction(data) { } if (!lookingAtMatrixReaction && !wantToRemoveMatrixReaction) { // We are removing a Discord user's reaction, so we just make the sim user remove it. - const mxid = select("sim", "mxid", "WHERE discord_id = ?").pluck().get(data.user_id) + const mxid = select("sim", "mxid", "WHERE user_id = ?").pluck().get(data.user_id) if (mxid === event.sender) { await api.redactEvent(roomID, event.event_id, mxid) } diff --git a/d2m/converters/edit-to-changes.js b/d2m/converters/edit-to-changes.js index ac072f83..9144ccba 100644 --- a/d2m/converters/edit-to-changes.js +++ b/d2m/converters/edit-to-changes.js @@ -22,7 +22,7 @@ async function editToChanges(message, guild, api) { // Figure out what events we will be replacing const roomID = select("channel_room", "room_id", "WHERE channel_id = ?").pluck().get(message.channel_id) - let senderMxid = select("sim", "mxid", "WHERE discord_id = ?").pluck().get(message.author.id) || null + let senderMxid = select("sim", "mxid", "WHERE user_id = ?").pluck().get(message.author.id) || null if (senderMxid) { const senderIsInRoom = select("sim_member", "mxid", "WHERE room_id = ? AND mxid = ?").get(roomID, senderMxid) if (!senderIsInRoom) { diff --git a/d2m/converters/lottie.js b/d2m/converters/lottie.js index 1d200a4d..a00f98f1 100644 --- a/d2m/converters/lottie.js +++ b/d2m/converters/lottie.js @@ -35,11 +35,11 @@ const Rlottie = (async () => { /** * @param {DiscordTypes.APIStickerItem} stickerItem - * @returns {Promise<{mxc: string, info: typeof INFO}>} + * @returns {Promise<{mxc_url: string, info: typeof INFO}>} */ async function convert(stickerItem) { - const existingMxc = select("lottie", "mxc", "WHERE id = ?").pluck().get(stickerItem.id) - if (existingMxc) return {mxc: existingMxc, info: INFO} + const existingMxc = select("lottie", "mxc_url", "WHERE sticker_id = ?").pluck().get(stickerItem.id) + if (existingMxc) return {mxc_url: existingMxc, info: INFO} const r = await Rlottie const res = await fetch(file.DISCORD_IMAGES_BASE + file.sticker(stickerItem)) if (res.status !== 200) throw new Error("Sticker data file not found.") @@ -67,8 +67,8 @@ async function convert(stickerItem) { } }) assert(root.content_uri) - db.prepare("INSERT INTO lottie (id, mxc) VALUES (?, ?)").run(stickerItem.id, root.content_uri) - return {mxc: root.content_uri, info: INFO} + db.prepare("INSERT INTO lottie (sticker_id, mxc_url) VALUES (?, ?)").run(stickerItem.id, root.content_uri) + return {mxc_url: root.content_uri, info: INFO} } module.exports.convert = convert diff --git a/d2m/converters/message-to-event.js b/d2m/converters/message-to-event.js index f0dc8fe8..62a032f9 100644 --- a/d2m/converters/message-to-event.js +++ b/d2m/converters/message-to-event.js @@ -19,7 +19,7 @@ function getDiscordParseCallbacks(message, useHTML) { return { /** @param {{id: string, type: "discordUser"}} node */ user: node => { - const mxid = select("sim", "mxid", "WHERE discord_id = ?").pluck().get(node.id) + const mxid = select("sim", "mxid", "WHERE user_id = ?").pluck().get(node.id) const username = message.mentions.find(ment => ment.id === node.id)?.username || node.id if (mxid && useHTML) { return `@${username}` @@ -405,13 +405,13 @@ async function messageToEvent(message, guild, options = {}, di) { const format = file.stickerFormat.get(stickerItem.format_type) if (format?.mime === "lottie") { try { - const {mxc, info} = await lottie.convert(stickerItem) + const {mxc_url, info} = await lottie.convert(stickerItem) return { $type: "m.sticker", "m.mentions": mentions, body: stickerItem.name, info, - url: mxc + url: mxc_url } } catch (e) { return { diff --git a/d2m/converters/user-to-mxid.js b/d2m/converters/user-to-mxid.js index 45379668..8514fc9f 100644 --- a/d2m/converters/user-to-mxid.js +++ b/d2m/converters/user-to-mxid.js @@ -53,7 +53,7 @@ function userToSimName(user) { assert.notEqual(user.discriminator, "0000", "cannot create user for a webhook") // 1. Is sim user already registered? - const existing = select("sim", "sim_name", "WHERE discord_id = ?").pluck().get(user.id) + const existing = select("sim", "sim_name", "WHERE user_id = ?").pluck().get(user.id) if (existing) return existing // 2. Register based on username (could be new or old format) diff --git a/d2m/event-dispatcher.js b/d2m/event-dispatcher.js index f19437ff..0fd1da50 100644 --- a/d2m/event-dispatcher.js +++ b/d2m/event-dispatcher.js @@ -251,7 +251,7 @@ module.exports = { async onTypingStart(client, data) { const roomID = select("channel_room", "room_id", "WHERE channel_id = ?").pluck().get(data.channel_id) if (!roomID) return - const mxid = from("sim").join("sim_member", "mxid").and("WHERE discord_id = ? AND room_id = ?").pluck("mxid").get(data.user_id, roomID) + const mxid = from("sim").join("sim_member", "mxid").and("WHERE user_id = ? AND room_id = ?").pluck("mxid").get(data.user_id, roomID) if (!mxid) return // Each Discord user triggers the notification every 8 seconds as long as they remain typing. // Discord does not send typing stopped events, so typing only stops if the timeout is reached or if the user sends their message. diff --git a/db/migrations/0003-distinguish-column-names.sql b/db/migrations/0003-distinguish-column-names.sql new file mode 100644 index 00000000..4ffe0ac8 --- /dev/null +++ b/db/migrations/0003-distinguish-column-names.sql @@ -0,0 +1,15 @@ +BEGIN TRANSACTION; + +-- Rename mxc to mxc_url for consistency + +ALTER TABLE lottie RENAME COLUMN mxc TO mxc_url; + +-- Rename id to sticker_id so joins make sense in the future + +ALTER TABLE lottie RENAME COLUMN id TO sticker_id; + +-- Rename discord_id to user_id so joins make sense in the future + +ALTER TABLE sim RENAME COLUMN discord_id TO user_id; + +COMMIT; diff --git a/db/orm-utils.d.ts b/db/orm-utils.d.ts index e0c2cb41..a0cd8174 100644 --- a/db/orm-utils.d.ts +++ b/db/orm-utils.d.ts @@ -28,8 +28,8 @@ export type Models = { } lottie: { - id: string - mxc: string + sticker_id: string + mxc_url: string } member_cache: { @@ -45,7 +45,7 @@ export type Models = { } sim: { - discord_id: string + user_id: string sim_name: string localpart: string mxid: string diff --git a/db/orm.test.js b/db/orm.test.js index ef5e2fb8..7b8237b9 100644 --- a/db/orm.test.js +++ b/db/orm.test.js @@ -26,6 +26,6 @@ test("orm: from: get pluck works", t => { }) test("orm: from: join and pluck works", t => { - const mxid = from("sim").join("sim_member", "mxid").and("WHERE discord_id = ? AND room_id = ?").pluck("mxid").get("771520384671416320", "!hYnGGlPHlbujVVfktC:cadence.moe") + const mxid = from("sim").join("sim_member", "mxid").and("WHERE user_id = ? AND room_id = ?").pluck("mxid").get("771520384671416320", "!hYnGGlPHlbujVVfktC:cadence.moe") t.equal(mxid, "@_ooye_bojack_horseman:cadence.moe") }) diff --git a/m2d/converters/event-to-message.js b/m2d/converters/event-to-message.js index 9ff810b8..ae676457 100644 --- a/m2d/converters/event-to-message.js +++ b/m2d/converters/event-to-message.js @@ -324,7 +324,7 @@ async function eventToMessage(event, guild, di) { } const sender = repliedToEvent.sender const senderName = sender.match(/@([^:]*)/)?.[1] || sender - const authorID = select("sim", "discord_id", "WHERE mxid = ?").pluck().get(repliedToEvent.sender) + const authorID = select("sim", "user_id", "WHERE mxid = ?").pluck().get(repliedToEvent.sender) if (authorID) { replyLine += `<@${authorID}>` } else { @@ -367,7 +367,7 @@ async function eventToMessage(event, guild, di) { // Handling mentions of Discord users input = input.replace(/("https:\/\/matrix.to\/#\/(@[^"]+)")>/g, (whole, attributeValue, mxid) => { if (!utils.eventSenderIsFromDiscord(mxid)) return whole - const userID = select("sim", "discord_id", "WHERE mxid = ?").pluck().get(mxid) + const userID = select("sim", "user_id", "WHERE mxid = ?").pluck().get(mxid) if (!userID) return whole return `${attributeValue} data-user-id="${userID}">` }) diff --git a/scripts/seed.js b/scripts/seed.js index 10d12e68..3b247683 100644 --- a/scripts/seed.js +++ b/scripts/seed.js @@ -41,5 +41,5 @@ const utils = require("../m2d/converters/utils") await api.profileSetAvatarUrl(mxid, avatarUrl) // add initial rows to database, like adding the bot to sim... - db.prepare("INSERT INTO sim (discord_id, sim_name, localpart, mxid) VALUES (?, ?, ?, ?)").run("0", reg.sender_localpart.slice(reg.ooye.namespace_prefix.length), reg.sender_localpart, mxid) + db.prepare("INSERT INTO sim (user_id, sim_name, localpart, mxid) VALUES (?, ?, ?, ?)").run("0", reg.sender_localpart.slice(reg.ooye.namespace_prefix.length), reg.sender_localpart, mxid) })() diff --git a/test/ooye-test-data.sql b/test/ooye-test-data.sql index 70d02edd..681ec915 100644 --- a/test/ooye-test-data.sql +++ b/test/ooye-test-data.sql @@ -10,7 +10,7 @@ INSERT INTO channel_room (channel_id, room_id, name, nick, thread_parent, custom ('1100319550446252084', '!BnKuBPCvyfOkhcUjEu:cadence.moe', 'worm-farm', NULL, NULL, NULL), ('297272183716052993', '!rEOspnYqdOalaIFniV:cadence.moe', 'general', NULL, NULL, NULL); -INSERT INTO sim (discord_id, sim_name, localpart, mxid) VALUES +INSERT INTO sim (user_id, sim_name, localpart, mxid) VALUES ('0', 'bot', '_ooye_bot', '@_ooye_bot:cadence.moe'), ('820865262526005258', 'crunch_god', '_ooye_crunch_god', '@_ooye_crunch_god:cadence.moe'), ('771520384671416320', 'bojack_horseman', '_ooye_bojack_horseman', '@_ooye_bojack_horseman:cadence.moe'),