Skip to content

Commit

Permalink
feat(database): fix sync logic
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 12, 2024
1 parent 7fada5b commit 163cf5b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
31 changes: 21 additions & 10 deletions packages/database/src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Bot, pick, Universal } from '@satorijs/core'
import { Channel, Guild, Login } from './types'
import { Channel, Guild, LoginSync } from './types'
import { SyncChannel } from './channel'
import SatoriDatabase from '.'

interface CachedBot extends Bot {
sync: Login['sync']
sync: LoginSync
}

class CachedBot {
Expand Down Expand Up @@ -33,13 +33,16 @@ class CachedBot {
}
const data: Partial<Guild>[] = []
for await (const guild of self.getGuildIter()) {
data.push({ platform: this.platform, ...pick(guild, ['id', 'name', 'avatar']) })
data.push(pick(guild, ['id', 'name', 'avatar']))
}
await this.sd.ctx.database.set('satori.login', this._query, {
sync: {
guildListAt: Date.now(),
},
guildSyncs: data.map((guild) => ({ guild })) as any,
guildSyncs: data.map((guild) => ({ guild: { $create: {
platform: this.platform,
...guild,
} } })) as any,
})
return { data }
}
Expand All @@ -58,7 +61,7 @@ class CachedBot {
id: guildId,
},
})
if (sync!.channelListAt >= this.sync.onlineAt) {
if (sync?.channelListAt >= this.sync.onlineAt) {
const data = await this.sd.ctx.database.get('satori.channel', {
guild: { id: guildId },
syncs: {
Expand All @@ -71,16 +74,20 @@ class CachedBot {
}
const data: Partial<Channel>[] = []
for await (const channel of self.getChannelIter(guildId)) {
data.push({ platform: this.platform, ...pick(channel, ['id', 'name', 'type']) })
data.push(pick(channel, ['id', 'name', 'type', 'parentId', 'position']))
}
await this.sd.ctx.database.set('satori.login', this._query, {
guildSyncs: {
$create: {
guild: { id: guildId },
guild: { id: guildId, platform: this.platform },
channelListAt: Date.now(),
},
},
channelSyncs: data.map((channel) => ({ channel })) as any,
channelSyncs: data.map((channel) => ({ channel: { $create: {
platform: this.platform,
guild: { id: guildId },
...channel,
} } })) as any,
})
return { data }
}
Expand All @@ -102,11 +109,15 @@ class CachedBot {
if (channel.bot.sid !== this.sid) continue
channel.hasLatest = false
}
const update: Partial<LoginSync> = {
onlineAt: Date.now(),
}
const [login] = await this.ctx.database.get('satori.login', this._query)
this.sync = login?.sync || { onlineAt: Date.now() }
this.sync = login?.sync || {}
Object.assign(this.sync, update)
await this.ctx.database.upsert('satori.login', [{
...this._query,
sync: this.sync,
sync: update,
}])
}
}
Expand Down
8 changes: 8 additions & 0 deletions packages/database/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ class SatoriDatabase extends Service<SatoriDatabase.Config> {
'id': 'char(255)',
'platform': 'char(255)',
'name': 'char(255)',
'type': 'integer',
'parentId': 'char(255)',
'position': 'integer',
'guild': {
type: 'manyToOne',
table: 'satori.guild',
target: 'channels',
},
}, {
primary: ['id', 'platform'],
})
Expand Down
7 changes: 2 additions & 5 deletions packages/database/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,15 @@ declare module '@satorijs/protocol' {
interface Message {
sid?: bigint
}

interface Login {
sync: LoginSync
}
}

export interface Login extends Universal.Login {
sync: LoginSync
guildSyncs: GuildSync[]
channelSyncs: ChannelSync[]
}

interface LoginSync {
export interface LoginSync {
onlineAt: number
guildListAt: number
}
Expand Down
2 changes: 1 addition & 1 deletion packages/status/client/config.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const data = useRpc<Data>()
const bots = computed(() => {
return Object.values(data.value.bots || {}).filter(bot => {
return bot.paths?.includes(ctx.get('manager')!.current.value!.id)
return bot.paths?.includes(ctx.get('manager')!.currentEntry!.id)
})
})
Expand Down

0 comments on commit 163cf5b

Please sign in to comment.