Skip to content

Commit

Permalink
feat(plugin-common): polyfill $user and $group
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 18, 2020
1 parent 2eac63b commit 7769a50
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
20 changes: 14 additions & 6 deletions packages/plugin-common/src/contextify.ts
Expand Up @@ -18,13 +18,14 @@ export default function apply (ctx: Context) {
if (!message) return meta.$send('请输入要发送的文本。')
if (!options.user && !options.group && !options.discuss) return meta.$send('请提供新的上下文。')

let user = meta.$user
if (options.user) {
const id = +options.user
if (id !== meta.userId) {
const user = await ctx.database.getUser(id, -1, ['authority'])
if (!user) return meta.$send('未找到用户。')
if (meta.$user.authority <= user.authority) return meta.$send('权限不足。')
user = await ctx.database.observeUser(id)
if (meta.$user.authority <= user.authority) {
return meta.$send('权限不足。')
}

newMeta.userId = id
newMeta.sender = {
sex: 'unknown',
Expand All @@ -34,14 +35,21 @@ export default function apply (ctx: Context) {
}
}

Object.defineProperty(newMeta, '$user', { value: user, writable: true })

if (options.discuss) {
newMeta.discussId = +options.discuss
newMeta.messageType = 'discuss'
} else if (options.group) {
newMeta.groupId = +options.group
const id = +options.group
newMeta.groupId = id
newMeta.messageType = 'group'
newMeta.subType = options.type || 'normal'
} else if (options.user) {
Object.defineProperty(newMeta, '$group', {
value: await ctx.database.observeGroup(id),
writable: true,
})
} else {
newMeta.messageType = 'private'
newMeta.subType = options.type || 'other'
}
Expand Down
33 changes: 33 additions & 0 deletions packages/plugin-common/tests/contextify.spec.ts
@@ -0,0 +1,33 @@
import { MockedApp } from 'koishi-test-utils'
import contextify from '../src/contextify'
import 'koishi-database-memory'

const app = new MockedApp({ database: { memory: {} } })
const session = app.createSession('user', 123)

app.plugin(contextify)
app.command('show-context')
.action(({ meta }) => {
return meta.$send(`${meta.userId},${meta.$ctxType},${meta.$ctxId},${meta.$user?.id},${meta.$group?.id}`)
})

beforeAll(async () => {
await app.start()
await app.database.getUser(123, 3)
await app.database.getUser(456, 2)
await app.database.getUser(789, 4)
})

test('check input', async () => {
await session.shouldHaveReply('ctxf -u 456', '请输入要发送的文本。')
await session.shouldHaveReply('ctxf show-context', '请提供新的上下文。')
await session.shouldHaveReply('ctxf -u 789 show-context', '权限不足。')
})

test('contextify', async () => {
await session.shouldHaveReply('ctxf -u 456 show-context', '456,user,456,456,undefined')
await session.shouldHaveReply('ctxf -g 654 show-context', '123,group,654,123,654')
await session.shouldHaveReply('ctxf -d 654 show-context', '123,discuss,654,123,undefined')
await session.shouldHaveReply('ctxf -u 456 -g 654 show-context', '456,group,654,456,654')
await session.shouldHaveReply('ctxf -u 456 -d 654 show-context', '456,discuss,654,456,undefined')
})
4 changes: 2 additions & 2 deletions packages/plugin-common/tests/exit.spec.ts
Expand Up @@ -12,13 +12,13 @@ const mock = process.exit = jest.fn<never, [number?]>()
beforeEach(() => mock.mockClear())
afterAll(() => process.exit = processExit)

test('basic support', async () => {
test('terminate', async () => {
await session.shouldHaveNoResponse(`koishi, 关机`)
expect(mock).toBeCalledTimes(1)
expect(mock).toBeCalledWith(0)
})

test('basic support', async () => {
test('restart', async () => {
await session.shouldHaveNoResponse(`koishi, 重启`)
expect(mock).toBeCalledTimes(1)
expect(mock).toBeCalledWith(-1)
Expand Down

0 comments on commit 7769a50

Please sign in to comment.