Skip to content

Commit 163cf5b

Browse files
committed
feat(database): fix sync logic
1 parent 7fada5b commit 163cf5b

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

packages/database/src/bot.ts

+21-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Bot, pick, Universal } from '@satorijs/core'
2-
import { Channel, Guild, Login } from './types'
2+
import { Channel, Guild, LoginSync } from './types'
33
import { SyncChannel } from './channel'
44
import SatoriDatabase from '.'
55

66
interface CachedBot extends Bot {
7-
sync: Login['sync']
7+
sync: LoginSync
88
}
99

1010
class CachedBot {
@@ -33,13 +33,16 @@ class CachedBot {
3333
}
3434
const data: Partial<Guild>[] = []
3535
for await (const guild of self.getGuildIter()) {
36-
data.push({ platform: this.platform, ...pick(guild, ['id', 'name', 'avatar']) })
36+
data.push(pick(guild, ['id', 'name', 'avatar']))
3737
}
3838
await this.sd.ctx.database.set('satori.login', this._query, {
3939
sync: {
4040
guildListAt: Date.now(),
4141
},
42-
guildSyncs: data.map((guild) => ({ guild })) as any,
42+
guildSyncs: data.map((guild) => ({ guild: { $create: {
43+
platform: this.platform,
44+
...guild,
45+
} } })) as any,
4346
})
4447
return { data }
4548
}
@@ -58,7 +61,7 @@ class CachedBot {
5861
id: guildId,
5962
},
6063
})
61-
if (sync!.channelListAt >= this.sync.onlineAt) {
64+
if (sync?.channelListAt >= this.sync.onlineAt) {
6265
const data = await this.sd.ctx.database.get('satori.channel', {
6366
guild: { id: guildId },
6467
syncs: {
@@ -71,16 +74,20 @@ class CachedBot {
7174
}
7275
const data: Partial<Channel>[] = []
7376
for await (const channel of self.getChannelIter(guildId)) {
74-
data.push({ platform: this.platform, ...pick(channel, ['id', 'name', 'type']) })
77+
data.push(pick(channel, ['id', 'name', 'type', 'parentId', 'position']))
7578
}
7679
await this.sd.ctx.database.set('satori.login', this._query, {
7780
guildSyncs: {
7881
$create: {
79-
guild: { id: guildId },
82+
guild: { id: guildId, platform: this.platform },
8083
channelListAt: Date.now(),
8184
},
8285
},
83-
channelSyncs: data.map((channel) => ({ channel })) as any,
86+
channelSyncs: data.map((channel) => ({ channel: { $create: {
87+
platform: this.platform,
88+
guild: { id: guildId },
89+
...channel,
90+
} } })) as any,
8491
})
8592
return { data }
8693
}
@@ -102,11 +109,15 @@ class CachedBot {
102109
if (channel.bot.sid !== this.sid) continue
103110
channel.hasLatest = false
104111
}
112+
const update: Partial<LoginSync> = {
113+
onlineAt: Date.now(),
114+
}
105115
const [login] = await this.ctx.database.get('satori.login', this._query)
106-
this.sync = login?.sync || { onlineAt: Date.now() }
116+
this.sync = login?.sync || {}
117+
Object.assign(this.sync, update)
107118
await this.ctx.database.upsert('satori.login', [{
108119
...this._query,
109-
sync: this.sync,
120+
sync: update,
110121
}])
111122
}
112123
}

packages/database/src/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ class SatoriDatabase extends Service<SatoriDatabase.Config> {
5757
'id': 'char(255)',
5858
'platform': 'char(255)',
5959
'name': 'char(255)',
60+
'type': 'integer',
61+
'parentId': 'char(255)',
62+
'position': 'integer',
63+
'guild': {
64+
type: 'manyToOne',
65+
table: 'satori.guild',
66+
target: 'channels',
67+
},
6068
}, {
6169
primary: ['id', 'platform'],
6270
})

packages/database/src/types.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,15 @@ declare module '@satorijs/protocol' {
1818
interface Message {
1919
sid?: bigint
2020
}
21-
22-
interface Login {
23-
sync: LoginSync
24-
}
2521
}
2622

2723
export interface Login extends Universal.Login {
24+
sync: LoginSync
2825
guildSyncs: GuildSync[]
2926
channelSyncs: ChannelSync[]
3027
}
3128

32-
interface LoginSync {
29+
export interface LoginSync {
3330
onlineAt: number
3431
guildListAt: number
3532
}

packages/status/client/config.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const data = useRpc<Data>()
2222
2323
const bots = computed(() => {
2424
return Object.values(data.value.bots || {}).filter(bot => {
25-
return bot.paths?.includes(ctx.get('manager')!.current.value!.id)
25+
return bot.paths?.includes(ctx.get('manager')!.currentEntry!.id)
2626
})
2727
})
2828

0 commit comments

Comments
 (0)