Skip to content

Commit

Permalink
teach: fix auth 3 not able to set new writer as self
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 25, 2020
1 parent e278cbd commit 5f9349f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
9 changes: 5 additions & 4 deletions packages/plugin-teach/src/plugins/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ export default function apply(ctx: Context, config: Dialogue.Config) {
// 当使用 -w 时需要原作者权限高于目标用户
// 锁定的问答需要 4 级权限才能修改
ctx.on('dialogue/permit', ({ session, target, options, authMap }, { writer, flag }) => {
const { substitute, writer: newWriter } = options, { authority } = session.$user
const { substitute, writer: newWriter } = options
const { id, authority } = session.$user
return (
(newWriter && authority <= authMap[newWriter]) ||
(newWriter && authority <= authMap[newWriter] && newWriter !== id) ||
((flag & Dialogue.Flag.frozen) && authority < 4) ||
(writer !== session.$user.id && (
(writer !== id && (
(target && authority < 3) || (
(substitute || (flag & Dialogue.Flag.substitute)) &&
(authority <= (authMap[writer] || 2))
Expand All @@ -114,7 +115,7 @@ export default function apply(ctx: Context, config: Dialogue.Config) {

ctx.on('dialogue/detail-short', ({ flag }, output) => {
if (flag & Dialogue.Flag.frozen) output.push('锁定')
if (flag & Dialogue.Flag.substitute) output.push('教学者执行')
if (flag & Dialogue.Flag.substitute) output.push('代行')
})

ctx.on('dialogue/before-search', ({ options }, test) => {
Expand Down
28 changes: 26 additions & 2 deletions packages/plugin-teach/tests/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ describe('Plugin Teach', () => {
})

describe('writer', () => {
const { app, u2, u3g1, u4g2 } = createEnvironment({ useWriter: true })
const { app, u2, u2g1, u3g1, u4g2 } = createEnvironment({ useWriter: true })

it('teach with writer', async () => {
app.command('test').action(({ session }) => '' + session.userId)

it('create writer', async () => {
// 当自身未设置 name 时使用 session.sender
u3g1.meta.sender.nickname = 'nick3'
await u3g1.shouldHaveReply('# foo bar', '问答已添加,编号为 1。')
Expand All @@ -202,6 +204,7 @@ describe('Plugin Teach', () => {
it('modify writer', async () => {
await u2.shouldHaveReply('#1 -W', '问答 1 因权限过低无法修改。')
await u4g2.shouldHaveReply('#1 -w foo', '参数 -w, --writer 错误,请检查指令语法。')
await u4g2.shouldHaveReply('#1 -w [CQ:at,qq=500]', '指定的目标用户不存在。')
await u4g2.shouldHaveReply('#1 -w [CQ:at,qq=200]', '问答 1 已成功修改。')

// 实在找不到名字就只显示 QQ 号
Expand All @@ -218,5 +221,26 @@ describe('Plugin Teach', () => {
await u2.shouldHaveReply('#1', DETAIL_HEAD.slice(0, -1))
await u2.shouldHaveReply('#1 -p 0', '问答 1 因权限过低无法修改。')
})

it('frozen', async () => {
await u3g1.shouldHaveReply('# foo baz -f', '权限不足。')
await u4g2.shouldHaveReply('# foo bar -f', '修改了已存在的问答,编号为 1。')
await u3g1.shouldHaveReply('# foo bar -p 0', '问答 1 因权限过低无法修改。')
await u3g1.shouldHaveReply('#1', DETAIL_HEAD + '此问答已锁定。')
await u3g1.shouldHaveReply('## foo', SEARCH_HEAD + '1. [锁定] bar')
await u4g2.shouldHaveReply('#1 -F', '问答 1 已成功修改。')
})

it('substitute', async () => {
u2g1.meta.sender.nickname = 'nick2'
const DETAIL_HEAD = '编号为 1 的问答信息:\n问题:foo\n回答:%s:%{test}\n'
await u3g1.shouldHaveReply('#1 ~ %s:%{test}', '问答 1 已成功修改。')
await u2g1.shouldHaveReply('foo', 'nick2:200')
await u3g1.shouldHaveReply('#1 -s', '问答 1 已成功修改。')
await u3g1.shouldHaveReply('## foo', SEARCH_HEAD + '1. [代行] %s:%{test}')
await u3g1.shouldHaveReply('#1 -w [CQ:at,qq=300]', '问答 1 已成功修改。')
await u3g1.shouldHaveReply('#1', DETAIL_HEAD + '来源:user3 (300)\n回答中的指令由教学者代行。')
await u2g1.shouldHaveReply('foo', 'nick2:300')
})
})
})

0 comments on commit 5f9349f

Please sign in to comment.