diff --git a/src/web/routes/link.js b/src/web/routes/link.js index 628ff1c7..32262780 100644 --- a/src/web/routes/link.js +++ b/src/web/routes/link.js @@ -164,8 +164,11 @@ as.router.post("/api/link", defineEventHandler(async event => { const selfPowerLevel = powerLevelsStateContent?.users?.[me] || powerLevelsStateContent?.users_default || 0 if (selfPowerLevel < (powerLevelsStateContent?.state_default || 50) || selfPowerLevel < 100) throw createError({status: 400, message: "Bad Request", data: "OOYE needs power level 100 (admin) in the target Matrix room"}) - // Insert database entry - db.prepare("INSERT INTO channel_room (channel_id, room_id, name, guild_id) VALUES (?, ?, ?, ?)").run(channel.id, parsedBody.matrix, channel.name, guildID) + // Insert database entry, but keep the room's existing properties if they are set + const nick = await api.getStateEvent(parsedBody.matrix, "m.room.name", "").then(content => content.name || null).catch(() => null) + const avatar = await api.getStateEvent(parsedBody.matrix, "m.room.avatar", "").then(content => content.url || null).catch(() => null) + const topic = await api.getStateEvent(parsedBody.matrix, "m.room.topic", "").then(content => content.topic || null).catch(() => null) + db.prepare("INSERT INTO channel_room (channel_id, room_id, name, guild_id, nick, custom_avatar, custom_topic) VALUES (?, ?, ?, ?, ?, ?, ?)").run(channel.id, parsedBody.matrix, channel.name, guildID, nick, avatar, topic) // Sync room data and space child await createRoom.syncRoom(parsedBody.discord) diff --git a/src/web/routes/link.test.js b/src/web/routes/link.test.js index b99de042..664acaaa 100644 --- a/src/web/routes/link.test.js +++ b/src/web/routes/link.test.js @@ -508,12 +508,13 @@ test("web link room: successfully calls createRoom", async t => { return roomID }, async getStateEvent(roomID, type, key) { - called++ if (type === "m.room.power_levels") { + called++ t.equal(roomID, "!NDbIqNpJyPvfKRnNcr:cadence.moe") t.equal(key, "") return {users: {"@_ooye_bot:cadence.moe": 100}} } else if (type === "m.space.child") { + called++ t.equal(roomID, "!zTMspHVUBhFLLSdmnS:cadence.moe") t.equal(key, "!NDbIqNpJyPvfKRnNcr:cadence.moe") return {via: ["cadence.moe"]}