Skip to content

Commit

Permalink
chat: implement message tags, closes #57
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Feb 14, 2021
1 parent 28f6e0a commit fe914f9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/SocketServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ class SocketServer {
this.clientActions = {
sendChat: (user, message) => {
debug('sendChat', user, message);
this.uw.chat.send(user, message);
if (typeof message === 'string') {
this.uw.chat.send(user, { message });
} else {
this.uw.chat.send(user, message);
}
},
vote: (user, direction) => {
socketVote(this.uw, user.id, direction);
Expand All @@ -162,7 +166,18 @@ class SocketServer {

this.clientActionSchemas = new Map();
this.clientActionSchemas.set('sendChat', ajv.compile({
type: 'string',
oneOf: [{
type: 'string',
}, {
type: 'object',
properties: {
message: { type: 'string' },
tags: {
type: 'object',
},
},
required: ['message'],
}],
}));
this.clientActionSchemas.set('vote', ajv.compile({
type: 'integer',
Expand Down
15 changes: 14 additions & 1 deletion src/plugins/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,30 @@ class Chat {
return message.slice(0, this.options.maxLength);
}

async send(user, message) {
async send(user, { message, tags }) {
const { acl } = this.uw;

if (await this.isMuted(user)) {
return;
}

const filteredTags = {};
if (tags && Object.keys(tags).length > 0) {
const permissions = await acl.getAllPermissions(user);
Object.entries(tags).forEach(([tagName, value]) => {
if (permissions.includes(`chat.tags.${tagName}`)) {
filteredTags[tagName] = value;
}
});
}

this.chatID += 1;

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

0 comments on commit fe914f9

Please sign in to comment.