Skip to content

Commit

Permalink
fix(matrix): same guild and channel, fix koishijs/koishi#1158 (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anillc authored Aug 12, 2023
1 parent fc1cb99 commit 1d7f04c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 25 deletions.
29 changes: 4 additions & 25 deletions adapters/matrix/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ export class MatrixBot extends Bot<MatrixBot.Config> {
async deleteFriend(): Promise<void> { }

async getGuild(guildId: string): Promise<Universal.Guild> {
const events = await this.internal.getState(guildId)
const guildName = (events.find(event => event.type === 'm.room.name')?.content as Matrix.M_ROOM_NAME)?.name
return { guildId, guildName }
const { channelName } = await this.getChannel(guildId)
return { guildId, guildName: channelName }
}

async getChannel(channelId: string): Promise<Universal.Channel> {
Expand All @@ -101,31 +100,11 @@ export class MatrixBot extends Bot<MatrixBot.Config> {
}

async getGuildList(): Promise<Universal.Guild[]> {
const sync = await this.syncRooms()
const joined = sync?.rooms?.join
if (!joined) return []
const result: Universal.Guild[] = []
for (const roomId of Object.keys(joined)) {
const state = await this.internal.getState(roomId)
const create = state.find(state => state.type === 'm.room.create')
const name = state.find(state => state.type === 'm.room.name')?.content as Matrix.M_ROOM_NAME
if (!create) continue
if (create.content['type'] !== 'm.space') continue
result.push({
guildId: roomId,
guildName: name?.name,
})
}
return result
return await Promise.all(this.rooms.map(roomId => this.getGuild(roomId)))
}

async getChannelList(guildId: string): Promise<Universal.Channel[]> {
const state = await this.internal.getState(guildId)
const children = state
.filter(event => event.type === 'm.space.child')
.map(event => event.state_key)
.filter(roomId => this.rooms.includes(roomId))
return await Promise.all(children.map(this.getChannel.bind(this)))
return await Promise.all(this.rooms.map(roomId => this.getChannel(roomId)))
}

async getGuildMemberList(guildId: string): Promise<Universal.GuildMember[]> {
Expand Down
3 changes: 3 additions & 0 deletions adapters/matrix/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ export async function adaptMessage(bot: MatrixBot, event: Matrix.ClientEvent, re
result.subtype = 'group'
result.messageId = event.event_id
result.channelId = event.room_id
result.guildId = event.room_id
result.userId = event.sender
result.timestamp = event.origin_server_ts
result.author = adaptAuthor(bot, event)
result.isDirect = false
const content = event.content as Matrix.M_ROOM_MESSAGE
const reply = content['m.relates_to']?.['m.in_reply_to']
if (reply) {
Expand Down Expand Up @@ -68,6 +70,7 @@ export async function adaptSession(bot: MatrixBot, event: Matrix.ClientEvent): P
session.messageId = event.event_id
session.timestamp = event.origin_server_ts
session.author = adaptAuthor(bot, event)
session.isDirect = false
switch (event.type) {
case 'm.room.redaction': {
session.type = 'message-deleted'
Expand Down

0 comments on commit 1d7f04c

Please sign in to comment.