From fe914f97dbeb4a1ba77b368c7f62e6770db9551e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Sun, 14 Feb 2021 13:35:50 +0100 Subject: [PATCH] chat: implement message tags, closes #57 --- src/SocketServer.js | 19 +++++++++++++++++-- src/plugins/chat.js | 15 ++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/SocketServer.js b/src/SocketServer.js index b8b47bc2..5be149c7 100644 --- a/src/SocketServer.js +++ b/src/SocketServer.js @@ -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); @@ -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', diff --git a/src/plugins/chat.js b/src/plugins/chat.js index cf58fe01..7a85a0bc 100644 --- a/src/plugins/chat.js +++ b/src/plugins/chat.js @@ -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(), }); }