From 5aef6baaab705cd4f2e0d7dee642308097b588c0 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Tue, 5 Sep 2023 00:59:10 +1200 Subject: [PATCH] add migration process for stuff already bridged --- d2m/converters/edit-to-changes.test.js | 4 ++-- scripts/migrate-from-old-bridge.js | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/d2m/converters/edit-to-changes.test.js b/d2m/converters/edit-to-changes.test.js index 022b3960..d1644fdd 100644 --- a/d2m/converters/edit-to-changes.test.js +++ b/d2m/converters/edit-to-changes.test.js @@ -38,11 +38,11 @@ test("edit2changes: bot response", async t => { resolve({ joined: { "@cadence:cadence.moe": { - display_name: "cadence [they]", + displayname: "cadence [they]", avatar_url: "whatever" }, "@_ooye_botrac4r:cadence.moe": { - display_name: "botrac4r", + displayname: "botrac4r", avatar_url: "whatever" } } diff --git a/scripts/migrate-from-old-bridge.js b/scripts/migrate-from-old-bridge.js index 4504aea0..cd327295 100644 --- a/scripts/migrate-from-old-bridge.js +++ b/scripts/migrate-from-old-bridge.js @@ -54,7 +54,7 @@ const sema = new Semaphore() function onPacket(discord, event) { if (event.t === "GUILD_CREATE") { const guild = event.d - if (["1100319549670301727", "112760669178241024", "497159726455455754"].includes(guild.id)) return + if (!["112760669178241024"].includes(guild.id)) return sema.request(() => migrateGuild(guild)) } } @@ -108,8 +108,21 @@ async function migrateGuild(guild) { }) // Step 4: Add old rooms to new database; they are now also the new rooms - db.prepare("REPLACE INTO channel_room (channel_id, room_id, name) VALUES (?, ?, ?)").run(channel.id, row.matrix_id, channel.name) - console.log(`-- -- Added to database`) + // Make sure it wasn't already set up for the new bridge and bridged somewhere else pre-migration... + const preMigrationRow = db.prepare("SELECT room_id, nick, custom_avatar FROM channel_room WHERE channel_id = ?").get(channel.id) + if (preMigrationRow) { + // Ok, so we're going to delete this row from the database and then add the new proper row. + // But we want to copy over any previous custom settings like nick and avatar. + // (By the way, thread_parent is always null here because thread rooms would never be migrated because they are not in the old bridge.) + db.transaction(() => { + db.prepare("DELETE FROM channel_room WHERE channel_id = ?").run(channel.id) + db.prepare("INSERT INTO channel_room (channel_id, room_id, name, nick, custom_avatar) VALUES (?, ?, ?, ?, ?)").run(channel.id, row.matrix_id, channel.name, preMigrationRow.nick, preMigrationRow.custom_avatar) + console.log(`-- -- Added to database (transferred properties from previous OOYE room)`) + })() + } else { + db.prepare("REPLACE INTO channel_room (channel_id, room_id, name) VALUES (?, ?, ?)").run(channel.id, row.matrix_id, channel.name) + console.log(`-- -- Added to database`) + } // Step 5: Call syncRoom for each room await createRoom.syncRoom(row.discord_channel)