Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(matrix): same guild and channel, fix koishijs/koishi#1158 #147

Merged
merged 1 commit into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 4 additions & 25 deletions adapters/matrix/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,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 @@ -99,31 +98,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
Loading