Skip to content

Commit

Permalink
fix: url contain - treat as channel id
Browse files Browse the repository at this point in the history
  • Loading branch information
fengkx committed Mar 20, 2020
1 parent b9bd5c2 commit 7cabb1e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ package.json
package-lock.json
node_modules
logs
coverage
15 changes: 14 additions & 1 deletion middlewares/is-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ const errors = require('../utils/errors');
const USERS = require('../proxies/users');
const config = require('../config');

/**
* Check if using for channel
* @param {string} text message recived
* @return {boolean} whether contain channel id (can be start with @ or a number start with -)
*/
function checkChannelId(text) {
const textPart = text.split(/\s+/);
if (textPart.length > 1 && textPart[1].match(/^(@\w+)|(-\d+)$/)) {
return true;
}
return false;
}

module.exports = async (ctx, next) => {
ctx.state.chat = await ctx.getChat();
const chat = ctx.state.chat;
Expand Down Expand Up @@ -32,7 +45,7 @@ module.exports = async (ctx, next) => {
} else if (
ctx.message && // button respone without message
ctx.message.text &&
ctx.message.text.search(/(@\w+)|(-\d+)/) !== -1
checkChannelId(ctx.message.text)
) {
// for channel subscription in private chat
const channelId = ctx.message.text.match(/(@\w+)|(-\d+)/)[0];
Expand Down
12 changes: 12 additions & 0 deletions test/is-admin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,29 @@ test('sub channel with @', async () => {
const ctx = pass('/sub @testChannel http://test.test', 666);
await isAdmin(ctx, () => {});
expect(ctx).toHaveProperty('state.chat.id', channelId);
expect(ctx.message.text).not.toMatch(/@\w+/);
});

test('sub channel without @', async () => {
const channelId = 666;
const ctx = pass('/sub -123456 http://test.test', channelId);
await isAdmin(ctx, () => {});
expect(ctx).toHaveProperty('state.chat.id', channelId);
expect(ctx.message.text).not.toMatch(/-\d+/);
});

test('bot no admin in channel', async () => {
const channelId = 666;
const ctx = noAdmin('/sub -123456 http://test.test', channelId);
await expect(isAdmin(ctx, () => {})).rejects.toThrow(ErrClass);
});

test('url contain -', async () => {
const channelId = 666;
const ctx = pass(
'/sub http://ip-243-184bpo123.com/kuaidi100/141abc',
channelId
);
await isAdmin(ctx, () => {});
expect(ctx).toHaveProperty('state.chat.id', 233233233);
});

0 comments on commit 7cabb1e

Please sign in to comment.