Skip to content

Commit

Permalink
feat(core): refactor list response type
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 3, 2023
1 parent 7f266cb commit 468597f
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 52 deletions.
8 changes: 3 additions & 5 deletions adapters/discord/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,8 @@ export class DiscordBot extends Bot<DiscordBot.Config> {
}

async getMessageList(channelId: string, before?: string) {
// doesn't include `before` message
// 从旧到新
const data = (await this.internal.getChannelMessages(channelId, { before, limit: 50 })).reverse()
return await Promise.all(data.map(data => decodeMessage(this, data)))
const data = await this.internal.getChannelMessages(channelId, { before, limit: 100 })
return { data: await Promise.all(data.reverse().map(data => decodeMessage(this, data, {}, false))) }
}

async getUser(userId: string) {
Expand All @@ -105,7 +103,7 @@ export class DiscordBot extends Bot<DiscordBot.Config> {

async getGuildMemberList(guildId: string) {
const data = await this.internal.listGuildMembers(guildId)
return data.map(v => decodeUser(v.user))
return { data: data.map(v => decodeUser(v.user)) }
}

async getChannel(channelId: string) {
Expand Down
4 changes: 2 additions & 2 deletions adapters/discord/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const encodeRole = (role: Partial<Universal.Role>): Partial<Discord.Role>
permissions: role.permissions && '' + role.permissions,
})

export async function decodeMessage(bot: DiscordBot, meta: Discord.Message, session: Partial<Session> = {}) {
export async function decodeMessage(bot: DiscordBot, meta: Discord.Message, session: Partial<Session> = {}, reference = true) {
const { platform } = bot

session.messageId = meta.id
Expand Down Expand Up @@ -129,7 +129,7 @@ export async function decodeMessage(bot: DiscordBot, meta: Discord.Message, sess
}
session.elements = h.parse(session.content)
// 遇到过 cross post 的消息在这里不会传消息 id
if (meta.message_reference) {
if (reference && meta.message_reference) {
const { message_id, channel_id } = meta.message_reference
session.quote = await bot.getMessage(channel_id, message_id)
}
Expand Down
2 changes: 1 addition & 1 deletion adapters/kook/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class KookBot<T extends KookBot.Config = KookBot.Config> extends Bot<T> {

async getGuildMemberList(guild_id: string) {
const { items } = await this.request<Kook.GuildUserList>('GET', '/guild/user-list', { guild_id })
return items.map(adaptAuthor)
return { data: items.map(adaptAuthor) }
}

async setGroupNickname(guild_id: string, user_id: string, nickname: string) {
Expand Down
14 changes: 7 additions & 7 deletions adapters/line/src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bot, Context, Logger, Quester, Schema, Universal } from '@satorijs/satori'
import { Bot, Context, Logger, Quester, Schema } from '@satorijs/satori'
import { HttpServer } from './http'
import { Internal } from './types'
import { LineMessageEncoder } from './message'
Expand Down Expand Up @@ -43,7 +43,7 @@ export class LineBot extends Bot<LineBot.Config> {
}

// https://developers.line.biz/en/reference/messaging-api/#get-profile
async getSelf(): Promise<Universal.User> {
async getSelf() {
const { userId, displayName, pictureUrl } = await this.internal.getBotInfo()
return {
userId,
Expand All @@ -52,7 +52,7 @@ export class LineBot extends Bot<LineBot.Config> {
}
}

async getFriendList(): Promise<Universal.User[]> {
async getFriendList() {
let userIds: string[] = []
let start: string
do {
Expand All @@ -67,15 +67,15 @@ export class LineBot extends Bot<LineBot.Config> {
return userIds.map(v => ({ userId: v }))
}

async getGuild(guildId: string): Promise<Universal.Guild> {
async getGuild(guildId: string) {
const res = await this.internal.getGroupSummary(guildId)
return {
guildId: res.groupId,
guildName: res.groupName,
}
}

async getGuildMemberList(guildId: string): Promise<Universal.GuildMember[]> {
async getGuildMemberList(guildId: string) {
let userIds: string[] = []
let start: string
do {
Expand All @@ -84,10 +84,10 @@ export class LineBot extends Bot<LineBot.Config> {
start = res.next
} while (start)

return userIds.map(v => ({ userId: v }))
return { data: userIds.map(v => ({ userId: v })) }
}

async getGuildMember(guildId: string, userId: string): Promise<Universal.GuildMember> {
async getGuildMember(guildId: string, userId: string) {
const res = await this.internal.getGroupMemberProfile(guildId, userId)
return ({
userId: res.userId,
Expand Down
31 changes: 16 additions & 15 deletions adapters/matrix/src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bot, Context, omit, Quester, Schema, Universal } from '@satorijs/satori'
import { Bot, Context, omit, Quester, Schema } from '@satorijs/satori'
import { HttpAdapter } from './http'
import { MatrixMessageEncoder } from './message'
import * as Matrix from './types'
Expand Down Expand Up @@ -57,7 +57,7 @@ export class MatrixBot extends Bot<MatrixBot.Config> {
}))
}

async getMessage(channelId: string, messageId: string): Promise<Universal.Message> {
async getMessage(channelId: string, messageId: string) {
const event = await this.internal.getEvent(channelId, messageId)
return await adaptMessage(this, event)
}
Expand All @@ -70,7 +70,7 @@ export class MatrixBot extends Bot<MatrixBot.Config> {
return await this.getUser(this.userId)
}

async getUser(userId: string): Promise<Universal.User> {
async getUser(userId: string) {
const profile = await this.internal.getProfile(userId)
let avatar: string
if (profile.avatar_url) avatar = this.internal.getAssetUrl(profile.avatar_url)
Expand All @@ -82,35 +82,35 @@ export class MatrixBot extends Bot<MatrixBot.Config> {
}
}

async getFriendList(): Promise<Universal.User[]> {
async getFriendList() {
return []
}

async deleteFriend(): Promise<void> { }
async deleteFriend() { }

async getGuild(guildId: string): Promise<Universal.Guild> {
async getGuild(guildId: string) {
const { channelName } = await this.getChannel(guildId)
return { guildId, guildName: channelName }
}

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

async getGuildList(): Promise<Universal.Guild[]> {
async getGuildList() {
return await Promise.all(this.rooms.map(roomId => this.getGuild(roomId)))
}

async getChannelList(guildId: string): Promise<Universal.Channel[]> {
async getChannelList(guildId: string) {
return await Promise.all(this.rooms.map(roomId => this.getChannel(roomId)))
}

async getGuildMemberList(guildId: string): Promise<Universal.GuildMember[]> {
async getGuildMemberList(guildId: string) {
const state = await this.internal.getState(guildId)
const levels = state.find(event => event.type === 'm.room.power_levels').content as Matrix.M_ROOM_POWER_LEVELS
return state
const data = state
.filter(event => event.type === 'm.room.member')
.map(event => {
const content = event.content as Matrix.M_ROOM_MEMBER
Expand All @@ -123,17 +123,18 @@ export class MatrixBot extends Bot<MatrixBot.Config> {
roles: [levels.users[event.state_key].toString()],
}
})
return { data }
}

async getGuildMember(guildId: string, userId: string): Promise<Universal.GuildMember> {
return (await this.getGuildMemberList(guildId)).find(user => user.userId === userId)
async getGuildMember(guildId: string, userId: string) {
return (await this.getGuildMemberList(guildId)).data.find(user => user.userId === userId)
}

async createReaction(channelId: string, messageId: string, emoji: string): Promise<void> {
async createReaction(channelId: string, messageId: string, emoji: string) {
await this.internal.sendReaction(channelId, messageId, emoji)
}

async handleFriendRequest(): Promise<void> { }
async handleFriendRequest() { }

// as utils.ts commented, messageId is roomId
async handleGuildRequest(messageId: string, approve: boolean, commit: string) {
Expand Down
2 changes: 1 addition & 1 deletion adapters/onebot/src/bot/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class BaseBot<T extends BaseBot.Config = BaseBot.Config> extends Bot<T> {
}

// 从旧到新
return await Promise.all(list.map(item => OneBot.adaptMessage(this, item)))
return { data: await Promise.all(list.map(item => OneBot.adaptMessage(this, item))) }
}
}

Expand Down
2 changes: 1 addition & 1 deletion adapters/onebot/src/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class OneBotBot<T extends OneBotBot.Config = OneBotBot.Config> extends Ba

async getGuildMemberList(guildId: string) {
const data = await this.internal.getGroupMemberList(guildId)
return data.map(OneBot.adaptGuildMember)
return { data: data.map(OneBot.adaptGuildMember) }
}

async kickGuildMember(guildId: string, userId: string, permanent?: boolean) {
Expand Down
2 changes: 1 addition & 1 deletion adapters/onebot/src/bot/qqguild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ export class QQGuildBot extends BaseBot {
if (data.finished) break
nextToken = data.next_token
}
return list
return { data: list }
}
}
32 changes: 16 additions & 16 deletions adapters/slack/src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bot, Context, Fragment, Quester, Schema, SendOptions, Universal } from '@satorijs/satori'
import { Bot, Context, Fragment, Quester, Schema, SendOptions } from '@satorijs/satori'
import { WsClient } from './ws'
import { HttpServer } from './http'
import { adaptChannel, adaptGuild, adaptMessage, adaptUser } from './utils'
Expand Down Expand Up @@ -49,14 +49,14 @@ export class SlackBot<T extends SlackBot.Config = SlackBot.Config> extends Bot<T
}
}

async deleteMessage(channelId: string, messageId: string): Promise<void> {
async deleteMessage(channelId: string, messageId: string) {
await this.internal.chatDelete(Token.BOT, {
channel: channelId,
ts: Number(messageId),
})
}

async getMessage(channelId: string, messageId: string): Promise<Universal.Message> {
async getMessage(channelId: string, messageId: string) {
const msg = await this.internal.conversationsHistory(Token.BOT, {
channel: channelId,
oldest: Number(messageId),
Expand All @@ -67,17 +67,17 @@ export class SlackBot<T extends SlackBot.Config = SlackBot.Config> extends Bot<T
return adaptMessage(this, msg.messages[0])
}

async getMessageList(channelId: string, before?: string): Promise<Universal.Message[]> {
async getMessageList(channelId: string, before?: string) {
const msg = await this.request<{
messages: GenericMessageEvent[]
}>('POST', '/conversations.history', {
channel: channelId,
latest: before,
})
return Promise.all(msg.messages.map(v => adaptMessage(this, v)))
return { data: await Promise.all(msg.messages.map(v => adaptMessage(this, v))) }
}

async getUser(userId: string, guildId?: string): Promise<Universal.User> {
async getUser(userId: string, guildId?: string) {
// users:read
// @TODO guildId
const { user } = await this.request<{ user: SlackUser }>('POST', '/users.info', {
Expand All @@ -86,13 +86,13 @@ export class SlackBot<T extends SlackBot.Config = SlackBot.Config> extends Bot<T
return adaptUser(user)
}

async getGuildMemberList(guildId: string): Promise<Universal.GuildMember[]> {
async getGuildMemberList(guildId: string) {
// users:read
const { members } = await this.request<{ members: SlackUser[] }>('POST', '/users.list')
return members.map(adaptUser)
return { data: members.map(adaptUser) }
}

async getChannel(channelId: string, guildId?: string): Promise<Universal.Channel> {
async getChannel(channelId: string, guildId?: string) {
const { channel } = await this.request<{
channel: SlackChannel
}>('POST', '/conversations.info', {
Expand All @@ -101,7 +101,7 @@ export class SlackBot<T extends SlackBot.Config = SlackBot.Config> extends Bot<T
return adaptChannel(channel)
}

async getChannelList(guildId: string): Promise<Universal.Channel[]> {
async getChannelList(guildId: string) {
const { channels } = await this.request<{
channels: SlackChannel[]
}>('POST', '/conversations.list', {
Expand All @@ -110,14 +110,14 @@ export class SlackBot<T extends SlackBot.Config = SlackBot.Config> extends Bot<T
return channels.map(adaptChannel)
}

async getGuild(guildId: string): Promise<Universal.Guild> {
async getGuild(guildId: string) {
const { team } = await this.request<{ team: SlackTeam }>('POST', '/team.info', {
team_id: guildId,
})
return adaptGuild(team)
}

async getGuildMember(guildId: string, userId: string): Promise<Universal.GuildMember> {
async getGuildMember(guildId: string, userId: string) {
const { user } = await this.request<{ user: SlackUser }>('POST', '/users.info', {
user: userId,
})
Expand All @@ -127,7 +127,7 @@ export class SlackBot<T extends SlackBot.Config = SlackBot.Config> extends Bot<T
}
}

async sendPrivateMessage(channelId: string, content: Fragment, options?: SendOptions): Promise<string[]> {
async sendPrivateMessage(channelId: string, content: Fragment, options?: SendOptions) {
// "channels:write,groups:write,mpim:write,im:write",
const { channel } = await this.internal.conversationsOpen(Token.BOT, {
users: channelId,
Expand All @@ -136,7 +136,7 @@ export class SlackBot<T extends SlackBot.Config = SlackBot.Config> extends Bot<T
return this.sendMessage(channel.id, content, undefined, options)
}

async getReactions(channelId: string, messageId: string, emoji: string): Promise<Universal.User[]> {
async getReactions(channelId: string, messageId: string, emoji: string) {
const { message } = await this.internal.reactionsGet(Token.BOT, {
channel: channelId,
timestamp: messageId,
Expand All @@ -147,7 +147,7 @@ export class SlackBot<T extends SlackBot.Config = SlackBot.Config> extends Bot<T
})) ?? []
}

async createReaction(channelId: string, messageId: string, emoji: string): Promise<void> {
async createReaction(channelId: string, messageId: string, emoji: string) {
// reactions.write
await this.internal.reactionsAdd(Token.BOT, {
channel: channelId,
Expand All @@ -156,7 +156,7 @@ export class SlackBot<T extends SlackBot.Config = SlackBot.Config> extends Bot<T
})
}

async clearReaction(channelId: string, messageId: string, emoji?: string): Promise<void> {
async clearReaction(channelId: string, messageId: string, emoji?: string) {
const { message } = await this.internal.reactionsGet(Token.BOT, {
channel: channelId,
timestamp: messageId,
Expand Down
2 changes: 1 addition & 1 deletion adapters/telegram/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export class TelegramBot<T extends TelegramBot.Config = TelegramBot.Config> exte
const data = await this.internal.getChatAdministrators({ chat_id })
const users = data.map(adaptGuildMember)
await Promise.all(users.map(this.setAvatarUrl.bind(this)))
return users
return { data: users }
}

async kickGuildMember(chat_id: string, user_id: string | number, permanent?: boolean) {
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/universal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ export namespace Universal {
'channel.mute': 'muteChannel',
}

export interface List<T> {
data: T[]
}

export interface Methods {
// message
sendMessage(channelId: string, content: Element.Fragment, guildId?: string, options?: SendOptions): Promise<string[]>
// sendMessage(session: Session.Payload, content: segment.Fragment, options?: SendOptions): Promise<string[]>
sendPrivateMessage(userId: string, content: Element.Fragment, options?: SendOptions): Promise<string[]>
// sendPrivateMessage(session: Session.Payload, content: segment.Fragment, options?: SendOptions): Promise<string[]>
getMessage(channelId: string, messageId: string): Promise<Message>
getMessageList(channelId: string, before?: string): Promise<Message[]>
getMessageList(channelId: string, before?: string, limit?: number): Promise<List<Message>>
editMessage(channelId: string, messageId: string, content: Element.Fragment): Promise<void>
deleteMessage(channelId: string, messageId: string): Promise<void>

Expand All @@ -57,7 +61,7 @@ export namespace Universal {

// guild member
getGuildMember(guildId: string, userId: string): Promise<GuildMember>
getGuildMemberList(guildId: string): Promise<GuildMember[]>
getGuildMemberList(guildId: string): Promise<List<GuildMember>>
kickGuildMember(guildId: string, userId: string, permanent?: boolean): Promise<void>
muteGuildMember(guildId: string, userId: string, duration: number, reason?: string): Promise<void>

Expand Down

0 comments on commit 468597f

Please sign in to comment.