Skip to content

Commit

Permalink
fix(core): fix selector functions may escape plugin context
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 20, 2021
1 parent dd86bc5 commit a1f11d6
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions packages/koishi-core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,12 @@ export class Context {
protected _router: Router
protected _database: Database

public user = this.createSelector('userId')
public self = this.createSelector('selfId')
public group = this.createSelector('groupId')
public channel = this.createSelector('channelId')
public platform = this.createSelector('platform')

protected constructor(public filter: Filter, public app?: App, private _plugin: Plugin = null) {}

[inspect.custom]() {
return `Context {}`
const plugin = this._plugin
const name = !plugin ? 'root' : typeof plugin === 'object' && plugin.name || 'unknown'
return `Context <${name}>`
}

private createSelector<K extends keyof Session>(key: K) {
Expand All @@ -75,6 +71,26 @@ export class Context {
return selector
}

get user() {
return this.createSelector('userId')
}

get self() {
return this.createSelector('selfId')
}

get group() {
return this.createSelector('groupId')
}

get channel() {
return this.createSelector('channelId')
}

get platform() {
return this.createSelector('platform')
}

get private() {
return this.unselect('groupId').user
}
Expand Down

0 comments on commit a1f11d6

Please sign in to comment.