diff --git a/packages/database/package.json b/packages/database/package.json index dd9c6611..6ef9e58d 100644 --- a/packages/database/package.json +++ b/packages/database/package.json @@ -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", diff --git a/packages/database/src/channel.ts b/packages/database/src/channel.ts index 5af15322..579599d3 100644 --- a/packages/database/src/channel.ts +++ b/packages/database/src/channel.ts @@ -28,12 +28,13 @@ export class SyncChannel { private _initTask?: Promise - 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({ @@ -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 diff --git a/packages/database/src/guild.ts b/packages/database/src/guild.ts index f9e028a9..7293f749 100644 --- a/packages/database/src/guild.ts +++ b/packages/database/src/guild.ts @@ -3,16 +3,14 @@ import { Bot, Universal } from '@satorijs/core' export class SyncGuild { public members?: Universal.List - 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 { diff --git a/packages/database/src/index.ts b/packages/database/src/index.ts index 0c38bfa3..74ac8673 100644 --- a/packages/database/src/index.ts +++ b/packages/database/src/index.ts @@ -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 { + interface Satori { database: SatoriDatabase } + + interface Events { + 'satori/database/update'(): void + } } class SatoriDatabase extends Service { - inject = ['model', 'database'] + static inject = ['model', 'database'] _guilds: Dict = {} _channels: Dict = {} @@ -84,7 +85,7 @@ class SatoriDatabase extends Service { 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) }) @@ -139,10 +140,11 @@ class SatoriDatabase extends Service { 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) } } diff --git a/packages/database/src/span.ts b/packages/database/src/span.ts index f6b29ee5..7d89dd68 100644 --- a/packages/database/src/span.ts +++ b/packages/database/src/span.ts @@ -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 @@ -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