Preserve name/avatar/topic of linked rooms

This commit is contained in:
Cadence Ember
2025-02-18 01:46:54 +13:00
parent 438714b67e
commit ed417e029f
2 changed files with 7 additions and 3 deletions

View File

@@ -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)

View File

@@ -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"]}