Skip to content

Commit

Permalink
feat(lark): unwrap response.data
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Nov 25, 2024
1 parent e3b0a75 commit 8550490
Show file tree
Hide file tree
Showing 5 changed files with 2,539 additions and 4,020 deletions.
26 changes: 13 additions & 13 deletions adapters/lark/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,46 +89,46 @@ export class LarkBot<C extends Context = Context> extends Bot<C, LarkBot.Config>

async getMessage(channelId: string, messageId: string, recursive = true) {
const data = await this.internal.getImMessage(messageId)
const message = await Utils.decodeMessage(this, data.data.items[0], recursive)
const message = await Utils.decodeMessage(this, data.items[0], recursive)
const im = await this.internal.getImChat(channelId)
message.channel.type = im.data.chat_mode === 'p2p' ? Universal.Channel.Type.DIRECT : Universal.Channel.Type.TEXT
message.channel.type = im.chat_mode === 'p2p' ? Universal.Channel.Type.DIRECT : Universal.Channel.Type.TEXT
return message
}

async getMessageList(channelId: string, before?: string) {
const { data: messages } = await this.internal.listImMessage({ container_id_type: 'chat', container_id: channelId, page_token: before })
const messages = await this.internal.listImMessage({ container_id_type: 'chat', container_id: channelId, page_token: before })
const data = await Promise.all(messages.items.reverse().map(data => Utils.decodeMessage(this, data)))
return { data, next: data[0]?.id }
}

async getUser(userId: string, guildId?: string) {
const data = await this.internal.getContactUser(userId)
return Utils.decodeUser(data.data.user)
return Utils.decodeUser(data.user)
}

async getChannel(channelId: string) {
const { data } = await this.internal.getImChat(channelId)
return Utils.decodeChannel(channelId, data)
const chat = await this.internal.getImChat(channelId)
return Utils.decodeChannel(channelId, chat)
}

async getChannelList(guildId: string) {
return { data: [await this.getChannel(guildId)] }
}

async getGuild(guildId: string) {
const { data } = await this.internal.getImChat(guildId)
return Utils.decodeGuild(data)
const chat = await this.internal.getImChat(guildId)
return Utils.decodeGuild(chat)
}

async getGuildList(after?: string) {
const { data: guilds } = await this.internal.listImChat({ page_token: after })
return { data: guilds.items.map(Utils.decodeGuild), next: guilds.page_token }
const chats = await this.internal.listImChat({ page_token: after })
return { data: chats.items.map(Utils.decodeGuild), next: chats.page_token }
}

async getGuildMemberList(guildId: string, after?: string) {
const { data: users } = await this.internal.getImChatMembers(guildId, { page_token: after })
const data = users.items.map(v => ({ user: { id: v.member_id, name: v.name }, name: v.name }))
return { data, next: users.page_token }
const members = await this.internal.getImChatMembers(guildId, { page_token: after })
const data = members.items.map(v => ({ user: { id: v.member_id, name: v.name }, name: v.name }))
return { data, next: members.page_token }
}
}

Expand Down
14 changes: 7 additions & 7 deletions adapters/lark/src/message.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context, Dict, h, MessageEncoder } from '@satorijs/core'
import { LarkBot } from './bot'
import { BaseResponse, Lark, MessageContent } from './types'
import { Lark, MessageContent } from './types'
import { extractIdType } from './utils'

export class LarkMessageEncoder<C extends Context = Context> extends MessageEncoder<C, LarkBot<C>> {
Expand All @@ -13,7 +13,7 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco

async post(data?: any) {
try {
let resp: BaseResponse & { data?: Lark.Message }
let resp: Lark.Message
if (this.quote?.id) {
resp = await this.bot.internal.replyImMessage(this.quote.id, {
...data,
Expand All @@ -26,9 +26,9 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
})
}
const session = this.bot.session()
session.messageId = resp.data.message_id
session.timestamp = Number(resp.data.create_time) * 1000
session.userId = resp.data.sender.id
session.messageId = resp.message_id
session.timestamp = Number(resp.create_time) * 1000
session.userId = resp.sender.id
session.channelId = this.channelId
session.guildId = this.guildId
session.app.emit(session, 'send', session)
Expand Down Expand Up @@ -96,7 +96,7 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
const payload = new FormData()
payload.append('image', new Blob([data], { type }), filename)
payload.append('image_type', 'message')
const { data: { image_key } } = await this.bot.internal.createImImage(payload)
const { image_key } = await this.bot.internal.createImImage(payload)
return image_key
}

Expand Down Expand Up @@ -126,7 +126,7 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
}
}

const { data: { file_key } } = await this.bot.internal.createImFile(payload)
const { file_key } = await this.bot.internal.createImFile(payload)
await this.post({
msg_type: _type === 'video' ? 'media' : _type,
content: JSON.stringify({ file_key }),
Expand Down
Loading

0 comments on commit 8550490

Please sign in to comment.