Skip to content

Commit

Permalink
feat(satori): implement pagniation api
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 9, 2023
1 parent b851cd2 commit f86e7f8
Show file tree
Hide file tree
Showing 17 changed files with 87 additions and 88 deletions.
33 changes: 18 additions & 15 deletions adapters/discord/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,20 @@ export class DiscordBot extends Bot<DiscordBot.Config> {
}

async getMessageList(channelId: string, before?: string) {
const data = await this.internal.getChannelMessages(channelId, { before, limit: 100 })
return { data: await Promise.all(data.reverse().map(data => decodeMessage(this, data, {}, false))) }
const messages = await this.internal.getChannelMessages(channelId, { before, limit: 100 })
const data = await Promise.all(messages.reverse().map(data => decodeMessage(this, data, {}, false)))
return { data, next: data[0]?.messageId }
}

async getUser(userId: string) {
const data = await this.internal.getUser(userId)
return decodeUser(data)
}

async getGuildMemberList(guildId: string) {
const data = await this.internal.listGuildMembers(guildId)
return { data: data.map(v => decodeUser(v.user)) }
async getGuildMemberList(guildId: string, after?: string) {
const users = await this.internal.listGuildMembers(guildId, { after, limit: 1000 })
const data = users.map(v => decodeUser(v.user))
return { data, next: data[data.length - 1]?.userId }
}

async getChannel(channelId: string) {
Expand All @@ -128,14 +130,15 @@ export class DiscordBot extends Bot<DiscordBot.Config> {
return decodeGuild(data)
}

async getGuildList() {
const data = await this.internal.getCurrentUserGuilds()
return data.map(decodeGuild)
async getGuildList(after?: string) {
const guilds = await this.internal.getCurrentUserGuilds({ after, limit: 200 })
const data = guilds.map(decodeGuild)
return { data, next: data[data.length - 1]?.id }
}

async getChannelList(guildId: string) {
const data = await this.internal.getGuildChannels(guildId)
return data.map(decodeChannel)
const channels = await this.internal.getGuildChannels(guildId)
return { data: channels.map(decodeChannel) }
}

createReaction(channelId: string, messageId: string, emoji: string) {
Expand All @@ -158,9 +161,9 @@ export class DiscordBot extends Bot<DiscordBot.Config> {
}
}

async getReactions(channelId: string, messageId: string, emoji: string) {
const data = await this.internal.getReactions(channelId, messageId, emoji)
return data.map(decodeUser)
async getReactionList(channelId: string, messageId: string, emoji: string, after?: string) {
const data = await this.internal.getReactions(channelId, messageId, emoji, { after, limit: 100 })
return { data: data.map(decodeUser), next: data[data.length - 1]?.id }
}

setGuildMemberRole(guildId: string, userId: string, roleId: string) {
Expand All @@ -171,9 +174,9 @@ export class DiscordBot extends Bot<DiscordBot.Config> {
return this.internal.removeGuildMemberRole(guildId, userId, roleId)
}

async getGuildRoles(guildId: string) {
async getGuildRoleList(guildId: string) {
const data = await this.internal.getGuildRoles(guildId)
return data.map(decodeRole)
return { data: data.map(decodeRole) }
}

async createGuildRole(guildId: string, data: Partial<Universal.Role>) {
Expand Down
6 changes: 3 additions & 3 deletions adapters/discord/src/types/guild-member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ export namespace GuildMember {
/** https://discord.com/developers/docs/resources/guild#list-guild-members-query-string-params */
export interface List {
/** max number of members to return (1-1000) */
limit: integer
limit?: integer
/** the highest user id in the previous page */
after: snowflake
after?: snowflake
}

/** https://discord.com/developers/docs/resources/guild#search-guild-members-query-string-params */
export interface Search {
/** Query string to match username(s) and nickname(s) against. */
query: string
/** max number of members to return (1-1000) */
limit: integer
limit?: integer
}

/** https://discord.com/developers/docs/resources/guild#add-guild-member-json-params */
Expand Down
8 changes: 5 additions & 3 deletions adapters/discord/src/types/guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,13 @@ export namespace Guild {
/** https://discord.com/developers/docs/resources/user#get-current-user-guilds-query-string-params */
export interface List {
/** get guilds before this guild ID */
before: snowflake
before?: snowflake
/** get guilds after this guild ID */
after: snowflake
after?: snowflake
/** max number of guilds to return (1-200) */
limit: integer
limit?: integer
/** include approximate member and presence counts in response */
with_counts?: boolean
}

/** https://discord.com/developers/docs/resources/guild#create-guild-json-params */
Expand Down
4 changes: 4 additions & 0 deletions adapters/discord/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ export const decodeUser = (user: Discord.User): Universal.User => ({
})

export const decodeGuild = (data: Discord.Guild): Universal.Guild => ({
id: data.id,
name: data.name,
guildId: data.id,
guildName: data.name,
})

export const decodeChannel = (data: Discord.Channel): Universal.Channel => ({
id: data.id,
name: data.name,
channelId: data.id,
channelName: data.name,
})
Expand Down
10 changes: 5 additions & 5 deletions adapters/kook/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class KookBot<T extends KookBot.Config = KookBot.Config> extends Bot<T> {

async getGuildList() {
const { items } = await this.request<Kook.GuildList>('GET', '/guild/list')
return items.map(adaptGroup)
return { data: items.map(adaptGroup) }
}

async getGuildMemberList(guild_id: string) {
Expand Down Expand Up @@ -130,14 +130,14 @@ export class KookBot<T extends KookBot.Config = KookBot.Config> extends Bot<T> {
}
}

async getReactions(channelId: string, messageId: string, emoji: string): Promise<Universal.User[]> {
async getReactionList(channelId: string, messageId: string, emoji: string) {
let users: Kook.User[]
if (isDirectChannel(channelId)) {
users = await this.internal.getDirectMessageReactionList({ msg_id: messageId, emoji })
} else {
users = await this.internal.getMessageReactionList({ msg_id: messageId, emoji })
}
return users.map(adaptUser)
return { data: users.map(adaptUser) }
}

async setGuildMemberRole(guildId: string, userId: string, roleId: string) {
Expand All @@ -148,9 +148,9 @@ export class KookBot<T extends KookBot.Config = KookBot.Config> extends Bot<T> {
await this.internal.revokeGuildRole({ guild_id: guildId, user_id: userId, role_id: +roleId })
}

async getGuildRoles(guildId: string): Promise<Universal.Role[]> {
async getGuildRoles(guildId: string) {
const { items } = await this.internal.getGuildRoleList({ guild_id: guildId })
return items.map(decodeRole)
return { data: items.map(decodeRole) }
}

async createGuildRole(guildId: string, data: Partial<Universal.Role>) {
Expand Down
2 changes: 2 additions & 0 deletions adapters/kook/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as Kook from './types'
export * from './types'

export const adaptGroup = (data: Kook.Guild): Universal.Guild => ({
id: data.id,
name: data.name,
guildId: data.id,
guildName: data.name,
})
Expand Down
38 changes: 13 additions & 25 deletions adapters/line/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,48 +52,36 @@ export class LineBot extends Bot<LineBot.Config> {
}
}

async getFriendList() {
let userIds: string[] = []
let start: string
do {
const res = await this.internal.getFollowers({
start,
limit: 1000,
})
userIds = userIds.concat(res.userIds)
start = res.next
} while (start)

return userIds.map(v => ({ userId: v }))
async getFriendList(start?: string) {
const { userIds, next } = await this.internal.getFollowers({
start,
limit: 1000,
})
return { data: userIds.map(v => ({ userId: v })), next }
}

async getGuild(guildId: string) {
const res = await this.internal.getGroupSummary(guildId)
return {
id: res.groupId,
name: res.groupName,
guildId: res.groupId,
guildName: res.groupName,
}
}

async getGuildMemberList(guildId: string) {
let userIds: string[] = []
let start: string
do {
const res = await this.internal.getGroupMembersIds(guildId, { start })
userIds = userIds.concat(res.memberIds)
start = res.next
} while (start)

return { data: userIds.map(v => ({ userId: v })) }
async getGuildMemberList(guildId: string, start?: string) {
const { memberIds, next } = await this.internal.getGroupMembersIds(guildId, { start })
return { data: memberIds.map(v => ({ userId: v })), next }
}

async getGuildMember(guildId: string, userId: string) {
const res = await this.internal.getGroupMemberProfile(guildId, userId)
return ({
return {
userId: res.userId,
nickname: res.displayName,
avatar: res.pictureUrl,
})
}
}
}

Expand Down
20 changes: 8 additions & 12 deletions adapters/matrix/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,25 @@ export class MatrixBot extends Bot<MatrixBot.Config> {
}
}

async getFriendList() {
return []
}

async deleteFriend() { }

async getGuild(guildId: string) {
const { channelName } = await this.getChannel(guildId)
return { guildId, guildName: channelName }
const { id, name } = await this.getChannel(guildId)
return { id, name, guildId, guildName: name }
}

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 }
const name = (events.find(event => event.type === 'm.room.name')?.content as Matrix.M_ROOM_NAME)?.name
return { id: channelId, name, channelId, channelName: name }
}

async getGuildList() {
return await Promise.all(this.rooms.map(roomId => this.getGuild(roomId)))
const data = await Promise.all(this.rooms.map(roomId => this.getGuild(roomId)))
return { data }
}

async getChannelList(guildId: string) {
return await Promise.all(this.rooms.map(roomId => this.getChannel(roomId)))
const data = await Promise.all(this.rooms.map(roomId => this.getChannel(roomId)))
return { data }
}

async getGuildMemberList(guildId: 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 @@ -38,7 +38,7 @@ export class BaseBot<T extends BaseBot.Config = BaseBot.Config> extends Bot<T> {

async getFriendList() {
const data = await this.internal.getFriendList()
return data.map(OneBot.adaptUser)
return { data: data.map(OneBot.adaptUser) }
}

async handleFriendRequest(messageId: string, approve: boolean, comment?: string) {
Expand Down
4 changes: 2 additions & 2 deletions adapters/onebot/src/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ export class OneBotBot<T extends OneBotBot.Config = OneBotBot.Config> extends Ba

async getGuildList() {
const data = await this.internal.getGroupList()
return data.map(OneBot.adaptGuild)
return { data: data.map(OneBot.adaptGuild) }
}

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

async getGuildMember(guildId: string, userId: string) {
Expand Down
8 changes: 4 additions & 4 deletions adapters/onebot/src/bot/qqguild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ export class QQGuildBot extends BaseBot {
}

async getChannel(channelId: string, guildId?: string) {
const channels = await this.getChannelList(guildId)
return channels.find((channel) => channel.channelId === channelId)
const { data } = await this.getChannelList(guildId)
return data.find((channel) => channel.id === channelId)
}

async getChannelList(guildId: string) {
const data = await this.internal.getGuildChannelList(guildId, false)
return (data || []).map(OneBot.adaptChannel)
return { data: (data || []).map(OneBot.adaptChannel) }
}

async getGuild(guildId: string) {
Expand All @@ -61,7 +61,7 @@ export class QQGuildBot extends BaseBot {

async getGuildList() {
const data = await this.internal.getGuildList()
return data.map(OneBot.adaptGuild)
return { data: data.map(OneBot.adaptGuild) }
}

async getGuildMember(guildId: string, userId: string) {
Expand Down
8 changes: 8 additions & 0 deletions adapters/onebot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,16 @@ export const adaptGuild = (info: OneBot.GroupInfo | OneBot.GuildBaseInfo): Unive
if ((info as OneBot.GuildBaseInfo).guild_id) {
const guild = info as OneBot.GuildBaseInfo
return {
id: guild.guild_id,
name: guild.guild_name,
guildId: guild.guild_id,
guildName: guild.guild_name,
}
} else {
const group = info as OneBot.GroupInfo
return {
id: group.group_id.toString(),
name: group.group_name,
guildId: group.group_id.toString(),
guildName: group.group_name,
}
Expand All @@ -120,12 +124,16 @@ export const adaptChannel = (info: OneBot.GroupInfo | OneBot.ChannelInfo): Unive
if ((info as OneBot.ChannelInfo).channel_id) {
const channel = info as OneBot.ChannelInfo
return {
id: channel.channel_id,
name: channel.channel_name,
channelId: channel.channel_id.toString(),
channelName: channel.channel_name,
}
} else {
const group = info as OneBot.GroupInfo
return {
id: group.group_id.toString(),
name: group.group_name,
channelId: group.group_id.toString(),
channelName: group.group_name,
}
Expand Down
3 changes: 2 additions & 1 deletion adapters/qqguild/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export class QQGuildBot extends Bot<QQGuildBot.Config> {
}

async getGuildList() {
return this.internal.guilds.then(guilds => guilds.map(adaptGuild))
const guilds = await this.internal.guilds
return { data: guilds.map(adaptGuild) }
}

adaptMessage(msg: QQGuild.Message) {
Expand Down
2 changes: 2 additions & 0 deletions adapters/qqguild/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Universal } from '@satorijs/satori'
import * as QQGuild from '@qq-guild-sdk/core'

export const adaptGuild = (guild: QQGuild.Guild): Universal.Guild => ({
id: guild.id,
name: guild.name,
guildId: guild.id,
guildName: guild.name,
})
Expand Down
2 changes: 1 addition & 1 deletion adapters/slack/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class SlackBot<T extends SlackBot.Config = SlackBot.Config> extends Bot<T
}>('POST', '/conversations.list', {
team_id: guildId,
})
return channels.map(adaptChannel)
return { data: channels.map(adaptChannel) }
}

async getGuild(guildId: string) {
Expand Down
Loading

0 comments on commit f86e7f8

Please sign in to comment.