Skip to content

Commit

Permalink
chat: Implement message tags. Closes u-wave#57
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Apr 9, 2017
1 parent 3d5d39e commit 42d87d0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"debug": "^2.6.0",
"escape-string-regexp": "^1.0.5",
"ioredis": "^2.4.0",
"is-empty-object": "^1.1.1",
"lodash": "^4.16.3",
"mongoose": "^4.1.6",
"mongoose-model-decorators": "^0.3.1",
Expand Down
32 changes: 30 additions & 2 deletions src/plugins/chat.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
import Promise from 'bluebird';
import isEmpty from 'is-empty-object';

/**
* Filter a message tags object to remove tags that cannot be used by the given
* user.
*/
function filterAllowedTags(user, tags) {
if (isEmpty(tags)) {
return {};
}

const allowedTagNames = Promise.all(Object.keys(tags))
.filter(tagName => user.can(`chat.tags.${tagName}`));

return allowedTagNames.reduce((obj, tagName) => {
// eslint-disable-next-line no-param-reassign
obj[tagName] = tags[tagName];
return obj;
});
}

const defaultOptions = {
maxLength: 300
};
Expand Down Expand Up @@ -44,18 +66,24 @@ export class Chat {
return message.slice(0, this.options.maxLength);
}

async send(user, message) {
/**
* Send a chat message.
*/
async send(user, message, tags = {}) {
if (await this.isMuted(user)) {
return;
}

const allowedTags = await filterAllowedTags(user, tags);

this.chatID += 1;

this.uw.publish('chat:message', {
id: `${user.id}-${this.chatID}`,
userID: user.id,
message: this.truncate(message),
timestamp: Date.now()
timestamp: Date.now(),
tags: allowedTags
});
}

Expand Down

0 comments on commit 42d87d0

Please sign in to comment.