Skip to content

Commit

Permalink
fix: only run action handler.hide when its an action
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Sep 16, 2018
1 parent 9beaae9 commit a2c0ea5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
17 changes: 17 additions & 0 deletions action-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ class ActionCode {
return this.code
}

getRegex() {
if (this.code instanceof RegExp) {
const {source, flags} = this.code
const newSource = `^${source}$`
return new RegExp(newSource, flags)
}
return new RegExp(`^${this.code}$`)
}

exec(value = '') {
return this.getRegex().exec(value)
}

test(value = '') {
return this.getRegex().test(value)
}

concat(action) {
return ActionCode.concat(this.code, action)
}
Expand Down
18 changes: 18 additions & 0 deletions action-code.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,21 @@ test('regex fail anchors', t => {
t.throws(() => new ActionCode(/^42/), /anchor/)
t.throws(() => new ActionCode(/42$/), /anchor/)
})

test('getRegex from regex', t => {
t.deepEqual(new ActionCode(/b/).getRegex(), /^b$/)
})

test('getRegex from non regex', t => {
t.deepEqual(new ActionCode('b').getRegex(), /^b$/)
})

test('exec', t => {
t.deepEqual(new ActionCode('b').exec('c'), null)
t.truthy(new ActionCode('b').exec('b'))
})

test('test', t => {
t.false(new ActionCode('b').test('c'))
t.true(new ActionCode('b').test('b'))
})
18 changes: 16 additions & 2 deletions inline-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,27 @@ class TelegrafInlineMenu {
if (handler.submenu) {
middleware = handler.submenu.middleware(childActionCode.get(), subOptions)
} else {
middlewareOptions.only = async ctx => {
if (ctx.updateType !== 'callback_query') {
return false
}
ctx.match = childActionCode.exec(ctx.callbackQuery.data)
if (!ctx.match) {
return false
}
if (handler.only && !(await handler.only(ctx))) {
return false
}
return true
}

options.log('add action reaction', childActionCode.get(), handler.middleware)
middleware = Composer.action(childActionCode.get(), async (ctx, next) => {
middleware = async (ctx, next) => {
await handler.middleware(ctx, next)
if (handler.setMenuAfter) {
await setMenuFunc(ctx, 'after handler action' + childActionCode.get())
}
})
}
}
} else {
middleware = handler.middleware
Expand Down

0 comments on commit a2c0ea5

Please sign in to comment.