Skip to content

Commit

Permalink
feat(database): support guild and channel data
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 14, 2024
1 parent a299a43 commit 2a44313
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/database/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@satorijs/plugin-database",
"description": "Database for Satori protocol",
"version": "0.1.0",
"version": "0.1.1",
"type": "module",
"main": "lib/index.cjs",
"module": "lib/index.mjs",
Expand Down
9 changes: 5 additions & 4 deletions packages/database/src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ export class SyncChannel {

private _initTask?: Promise<void>

constructor(public ctx: Context, public bot: Bot, public guildId: string, public channelId: string) {
this._query = { platform: bot.platform, 'channel.id': channelId }
constructor(public ctx: Context, public bot: Bot, public guildId: string, public data: Universal.Channel) {
this._query = { platform: bot.platform, 'channel.id': data.id }
bot.ctx.emit('satori/database/update')
}

private async init() {
logger.debug('init channel %s %s %s', this.bot.platform, this.guildId, this.channelId)
logger.debug('init channel %s %s %s', this.bot.platform, this.guildId, this.data.id)
const data = await this.ctx.database
.select('satori.message')
.where({
Expand Down Expand Up @@ -102,7 +103,7 @@ export class SyncChannel {
}

getMessageList(id: string, dir?: Universal.Direction, limit?: number) {
return this.bot.getMessageList(this.channelId, id, dir, limit, 'asc')
return this.bot.getMessageList(this.data.id, id, dir, limit, 'asc')
}

// TODO handle default limit
Expand Down
8 changes: 3 additions & 5 deletions packages/database/src/guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import { Bot, Universal } from '@satorijs/core'
export class SyncGuild {
public members?: Universal.List<Universal.GuildMember>

constructor(public bot: Bot, public data: Universal.Guild) {}
constructor(public bot: Bot, public data: Universal.Guild) {
bot.ctx.emit('satori/database/update')
}

async getMembers() {
if (this.members) return this.members
return this.members = await this.bot.getGuildMemberList(this.data.id)
}

toJSON(): SyncGuild.Data {
return {}
}
}

export namespace SyncGuild {
Expand Down
18 changes: 10 additions & 8 deletions packages/database/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import {} from 'minato'
import { Bot, Context, Dict, Schema, Service } from '@satorijs/satori'
import * as Universal from '@satorijs/protocol'
import { Bot, Context, Dict, Schema, Service, Universal } from '@satorijs/core'
import { SyncChannel } from './channel'
import { SyncGuild } from './guild'

export * from './types'

declare module '@satorijs/core' {
// https://github.com/typescript-eslint/typescript-eslint/issues/6720
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Satori<C> {
interface Satori {
database: SatoriDatabase
}

interface Events {
'satori/database/update'(): void
}
}

class SatoriDatabase extends Service<SatoriDatabase.Config, Context> {
inject = ['model', 'database']
static inject = ['model', 'database']

_guilds: Dict<SyncGuild> = {}
_channels: Dict<SyncChannel> = {}
Expand Down Expand Up @@ -84,7 +85,7 @@ class SatoriDatabase extends Service<SatoriDatabase.Config, Context> {
const { platform, guildId, channelId } = session
if (session.bot.hidden) return
const key = platform + '/' + guildId + '/' + channelId
this._channels[key] ||= new SyncChannel(this.ctx, session.bot, session.guildId, session.channelId)
this._channels[key] ||= new SyncChannel(this.ctx, session.bot, session.guildId, session.event.channel!)
if (this._channels[key].bot === session.bot) this._channels[key].queue(session)
})

Expand Down Expand Up @@ -139,10 +140,11 @@ class SatoriDatabase extends Service<SatoriDatabase.Config, Context> {
tasks.push((async () => {
for await (const channel of bot.getChannelIter(guild.id)) {
const key = bot.platform + '/' + guild.id + '/' + channel.id
this._channels[key] ||= new SyncChannel(this.ctx, bot, guild.id, channel.id)
this._channels[key] ||= new SyncChannel(this.ctx, bot, guild.id, channel)
}
})())
}
await Promise.all(tasks)
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/database/src/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class Span {
data.push({
...this.channel._query,
sid: this.next.back[0],
flag: $.bitAnd(row.flag, $.bitNot(Message.Flag.BACK)),
flag: $.and(row.flag, $.not(Message.Flag.BACK)),
})
} else {
data.at(-1)!.flag |= Message.Flag.FRONT
Expand All @@ -65,7 +65,7 @@ export class Span {
data.unshift({
...this.channel._query,
sid: this.prev.front[0],
flag: $.bitAnd(row.flag, $.bitNot(Message.Flag.FRONT)),
flag: $.and(row.flag, $.not(Message.Flag.FRONT)),
})
} else {
data.at(0)!.flag |= Message.Flag.BACK
Expand Down

0 comments on commit 2a44313

Please sign in to comment.