Skip to content

Commit

Permalink
Improved channel filtering
Browse files Browse the repository at this point in the history
- Added support for advanced channel name matching
- Updated documentation
- Improved internal identifier
  • Loading branch information
Juha Mustonen committed Jan 17, 2018
1 parent 9024702 commit aab703f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Show pulsating circle for each message sent in Slack channel(s)
key | required | description
--------------|----------|---------------
`title` | no | *Textual title to show. Example: '#mychannel'.*
`channel` | no | *Name of the channel to follow. Defaults to all public channels where token has permissions to*
`channel` | no | *Channels to follow, separated with comma. Defaults to all the channels the token has permission to. Supports including/excluding rules by [micromatch](https://github.com/micromatch/micromatch). Leading hash character ignored*
`imageSize` | no | Scaling of image: initial, cover, contain. Default to `initial`
`showImages` | no | Show images or not. Defaults to `true`
`showPulse` | no | Show pulse visualisation on each message or not. Defaults to `false`
Expand All @@ -123,7 +123,7 @@ key | required | description
```javascript
{
type: 'slack.pulse',
channel: 'general',
channel: 'general,!sales',
columns: 2, rows: 1,
x: 1, y: 0
}
Expand All @@ -139,15 +139,15 @@ Show pulsating circle from each message sent in Slack channel(s)
key | required | description
--------------|----------|---------------
`channel` | no | *Name of the channel to follow. Defaults to all public channels where token has permissions to*
`channel` | no | *Channels to follow, separated with comma. Defaults to all the channels the token has permission to. Supports including/excluding rules by [micromatch](https://github.com/micromatch/micromatch). Leading hash character ignored*
`title` | no | *Textual title to show. Example: '#mychannel'.*
### usage
```javascript
{
type: 'slack.pulse',
channel: 'general',
channel: '*',
columns: 2, rows: 1,
x: 1, y: 0
}
Expand All @@ -159,6 +159,11 @@ Distributed under the MIT license
## Changelog
### Release 0.9.0
- Added support for including/excluding channel messages by using advanced globbing channel name rules like: `general, !sales, team-*` (general -channel and all the channels starging with "team-". Explicitly no sales)
- Improved internal identifier
### Release 0.8.0
- Added support for restoring latest message from browser storage
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mozaik-ext-slack",
"version": "0.8.0",
"version": "0.9.0",
"description": "Slack widgets to Mozaik dashboard",
"main": "./components.js",
"scripts": {
Expand Down Expand Up @@ -31,7 +31,9 @@
"d3-ease": "^1.0.0",
"dotenv": "^2.0.0",
"emojilib": "^2.0.2",
"hash.js": "^1.1.3",
"lodash": "^3.10.1",
"micromatch": "^3.1.5",
"pretty-print": "^1.0.0",
"react-classset": "0.0.2",
"react-mixin": "3.0.4",
Expand Down
18 changes: 14 additions & 4 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import _ from 'lodash';
import slack from 'slack';
import emoji from 'emojilib';
import moment from 'moment';
import mm from 'micromatch';
import getFormatRemover from 'slack-remove-formatting';
import EchoClient from './echo.client';

Expand All @@ -16,6 +17,15 @@ const tempDirName = 'images';
let users = null;
let channels = null;

// Check if provided channel name matches with micromatch
// rules (separated with comma) and returns true if match
function matchChannel(channelName, filterString) {
return mm.all(
channelName.replace('#', ''),
filterString.split(',').map(filter => filter.trim())
);
}

function getChannels(token) {
return new Promise((resolve, reject) => {
// Return cached data if available
Expand Down Expand Up @@ -314,9 +324,9 @@ const client = mozaik => {
return;
}

// Filter with params
if (params.channel && params.channel !== channel.name) {
//console.log('Skip', params.channel, 'vs', channel.name, message);
// Filter with params by using micromatch module
// See options in documentation: https://www.npmjs.com/package/micromatch
if (params.channel && !matchChannel(channel.name, params.channel)) {
return;
}

Expand Down Expand Up @@ -371,4 +381,4 @@ function required() {
}

export default client;
export { replaceEmojis };
export { replaceEmojis, matchChannel };
4 changes: 3 additions & 1 deletion src/components/Channel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Mozaik from 'mozaik/browser';
import classNames from 'classnames';
import moment from 'moment';
import _ from 'lodash';
import hash from 'hash.js';
import Since from './Since.jsx';
import Impulse from './Impulse.jsx';

Expand All @@ -28,9 +29,10 @@ function setStoreValue(key, value) {
class Channel extends Component {
constructor(props) {
super(props);
const identifier = hash.sha256().update(`${props.channel || ''}${props.keyword || ''}`).digest('hex');
this.mounted = false;
this.matcher = this.props.keyword ? new RegExp(this.props.keyword, 'i') : null;
this.requestId = `slack.message.${this.props.channel || 'nochannel'}.${this.props.keyword || 'nokeyword'}`;
this.requestId = `slack.message.${identifier}`;
this.state = {
message: getStoreValue(this.requestId),
width: 100,
Expand Down
10 changes: 10 additions & 0 deletions tests/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import proxyquire from 'proxyquire';
import slackMock from './slack.mock';
import mozaikMock from './mozaik.mock';
import emoji from 'emojilib';
import mm from 'micromatch';

process.env.SLACK_TOKEN = 'test';

// Import the tested modules and mock the slack API
import { replaceEmojis } from '../src/client';
const client = proxyquire('../src/client', slackMock).default;
const matchChannel = proxyquire('../src/client', slackMock).matchChannel;

test.cb('slack channel', t => {
process.env.SLACK_PUBLIC_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'mozaik-ext-slack'));
Expand All @@ -30,3 +32,11 @@ test('replace emojis', t => {
t.is(replaceEmojis('testing :smile: :+1:'), `testing ${emoji.lib.smile.char} ${emoji.lib['+1'].char}`);
t.is(replaceEmojis('missing :foo:'), 'missing :foo:');
});

test('filters', t => {
t.true(matchChannel('#general', 'general'));
t.true(matchChannel('#general', '*'));
t.false(matchChannel('team-x', '!team-*, general'));
t.true(matchChannel('#general', '!team-*,general'));
t.false(matchChannel('#foo', '!team-*,general'));
});

0 comments on commit aab703f

Please sign in to comment.