Skip to content

Commit

Permalink
fix(sandbox): fix authorize error, close koishijs#136
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 27, 2023
1 parent 5fa0e0e commit b86a4a2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/sandbox/client/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
@click="config.panelType = key">{{ name }}</span>
</template>
</div>

<keep-alive>
<k-empty key="empty" v-if="!users.length">
<div>点击「添加用户」开始体验</div>
Expand Down
2 changes: 1 addition & 1 deletion packages/sandbox/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@koishijs/plugin-sandbox",
"description": "Test Your Virtual Bot in Console",
"version": "2.7.1",
"version": "2.7.3",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand Down
30 changes: 14 additions & 16 deletions packages/sandbox/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import { SandboxBot } from './bot'
import zh from './locales/zh.yml'

declare module 'koishi' {
interface User {
sandbox: string
}

namespace Session {
interface Payload {
client: Client
Expand Down Expand Up @@ -55,32 +51,34 @@ export class UserProvider extends DataService<Dict<User>> {
authority: 1,
...data,
})
return this.observe(user, users)
this.observe(name, user, users)
} else if (!data) {
await this.ctx.database.remove('user', users[name].id)
await this.ctx.database.remove('binding', { platform: 'sandbox', pid: name })
delete users[name]
this.ctx.$internal._userCache.set('sandbox', 'sandbox:' + name, null)
return this.ctx.database.remove('user', { sandbox: name })
} else {
Object.assign(users[name], data)
return users[name].$update()
}
Object.assign(users[name], data)
return users[name].$update()
}, { authority: 4 })
}

observe(user: User, users: Dict<User.Observed>) {
const uid = 'sandbox:' + user.sandbox
users[user.sandbox] = observe(user, async (diff) => {
await this.ctx.database.setUser('sandbox', user.sandbox, diff)
observe(name: string, user: User, users: Dict<User.Observed>) {
users[name] = observe(user, async (diff) => {
await this.ctx.database.setUser('sandbox', name, diff)
this.refresh()
})
this.ctx.$internal._userCache.set('sandbox', uid, users[user.sandbox])
this.ctx.$internal._userCache.set('sandbox', 'sandbox:' + name, users[name])
}

async prepare() {
const data = await this.ctx.database.get('binding', { platform: 'sandbox' }, ['aid'])
const data = await this.ctx.database.get('binding', { platform: 'sandbox' }, ['pid', 'aid'])
const users = await this.ctx.database.get('user', data.map(({ aid }) => aid))
const result: Dict<User.Observed> = {}
for (const user of users) {
this.observe(user, result)
for (const { aid, pid } of data) {
const user = users.find(u => u.id === aid)
if (user) this.observe(pid, user, result)
}
return result
}
Expand Down

0 comments on commit b86a4a2

Please sign in to comment.