diff --git a/api/v1/gateway/index.js b/api/v1/gateway/index.js index 275ff02..9249cb0 100644 --- a/api/v1/gateway/index.js +++ b/api/v1/gateway/index.js @@ -152,7 +152,7 @@ GatewayServer.prototype.eventSetup = function() { console.log(`[*] [gateway] [handshake] Got yoo from ${socket.user.username}, connection is finally completed!`); socket.isConnected = true; - socket.on('message', ({ category, content }) => { + socket.on('message', ({ category, content, nickAuthor }) => { if (!category || !content || !socket.joinedCategories || !socket.isConnected || !socket.user || !(typeof content === 'string') || !(typeof category._id === 'string')) return; content = content.trim(); if (!content || content === '' || content === ' ' || content.length >= 2000) return; @@ -161,12 +161,11 @@ GatewayServer.prototype.eventSetup = function() { return; } - // TODO: When/if category permissions are added, check if the user has permissions for that category const categoryTitle = socket.joinedCategories[category._id]; if (!categoryTitle || !(typeof categoryTitle === 'string')) return; - const messageObject = { + let messageObject = { author: { username: socket.user.username, _id: socket.user._id, @@ -180,6 +179,17 @@ GatewayServer.prototype.eventSetup = function() { _id: uuid.v4() }; + if (nickAuthor && nickAuthor.username && (typeof nickAuthor.username) === 'string' && nickAuthor.username.length <= 32 && nickAuthor.username.length >= 3) { + if (socket.user.permissionLevel === config.roleMap.BOT) { + messageObject = { + nickAuthor: { + username: nickAuthor.username + }, + ...messageObject + } + } + } + if (messageObject.content.startsWith(this._commandPrefix)) { this._processCommand(socket, messageObject); return; diff --git a/app/app.html b/app/app.html index 9990150..5ef28cc 100755 --- a/app/app.html +++ b/app/app.html @@ -164,7 +164,8 @@
- {{ post.author.username }} + {{ post.author.username }} + {{ post.nickAuthor.username }} (from bot "{{ post.author.username }}") {{ post.content }} diff --git a/app/resources/js/app.js b/app/resources/js/app.js index 9aeb6b9..706fb80 100755 --- a/app/resources/js/app.js +++ b/app/resources/js/app.js @@ -259,19 +259,29 @@ const app = new Vue({ }); }); }, + shouldMergeMessage: function(messageObject, categoryMessageList) { + const lastMessageIndex = categoryMessageList.length-1; + const lastMessage = categoryMessageList[lastMessageIndex]; + + if (!lastMessage) return; + if (lastMessage.author._id === messageObject.author._id) { + if (lastMessage.nickAuthor && messageObject.nickAuthor && lastMessage.nickAuthor.username === messageObject.nickAuthor.username) { + return true; + } + } + if (lastMessage.author._id === messageObject.author.id) return true; + }, processMessage: async function(messageObject) { if (!this.messages[messageObject.category._id]) this.$set(this.messages, messageObject.category._id, []); const categoryMessageList = this.messages[messageObject.category._id]; - const lastMessageIndex = categoryMessageList.length-1; - const lastMessage = categoryMessageList[lastMessageIndex]; - if (lastMessage && lastMessage.author._id === messageObject.author._id) { - categoryMessageList[lastMessageIndex].content += `\n${messageObject.content}` + + if (this.shouldMergeMessage(messageObject, categoryMessageList)) { + categoryMessageList[lastMessageIndex].content += `\n${messageObject.content}`; } else { this.messages[messageObject.category._id].push(messageObject); } - if (messageObject.category._id === this.selection.category._id) { this.$nextTick(() => { // TODO: When the user presses back, actually undo this scroll cause its annoying to scroll back up in the category list diff --git a/config.js b/config.js index 38600f1..585f456 100755 --- a/config.js +++ b/config.js @@ -3,13 +3,14 @@ module.exports = { mainServerPort: 3000, }, address: 'localhost', - mongoUrl: 'mongodb://localhost:27017/app', + mongoUrl: 'mongodb://192.168.0.105:27017/app', bcryptRounds: 10, roleMap: { 'BANNED': 0, 'RESTRICTED': 1, 'USER': 2, - 'ADMIN': 3 + 'BOT': 3, + 'ADMIN': 4 }, gatewayStillNotConnectedTimeoutMS: 15*1000 }; \ No newline at end of file diff --git a/libbrainlet/gatewayconnection.js b/libbrainlet/gatewayconnection.js index e818ec5..c5b6d06 100644 --- a/libbrainlet/gatewayconnection.js +++ b/libbrainlet/gatewayconnection.js @@ -59,10 +59,20 @@ GatewayConnection.prototype.connect = function(token) { this.socket.on('clientListUpdate', (e) => this.emit('clientListUpdate', e)); }; -GatewayConnection.prototype.sendMessage = function(categoryId, content) { +GatewayConnection.prototype.sendMessage = function(categoryId, content, nickAuthor=undefined) { if (!this.isConnected) return 1; if (content.length >= 2000) return 1; + if (nickAuthor) { + this.socket.emit('message', { + category: { + _id: categoryId + }, + nickAuthor, + content + }); + return; + } this.socket.emit('message', { category: { _id: categoryId diff --git a/libbrainlet/test.js b/libbrainlet/test.js index fa65387..83607ff 100644 --- a/libbrainlet/test.js +++ b/libbrainlet/test.js @@ -5,13 +5,18 @@ const main = async () => { throwErrors: true }); - await client.setToken('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3QiLCJpYXQiOjE2MDcwMjc5MzIsImV4cCI6MTYwNzAzODczMn0.9sHWxfPetp5efm11kaJP8wzsFfWjntVJ6COdqKGEuX4'); + await client.setToken('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InVzZXIiLCJpYXQiOjE2MDcxNzkyMzAsImV4cCI6MTYwNzE5MDAzMH0.X7KJA-8KmcdwTORXXVZcQTId3iz4_c7H7FYgF2X6nq8'); await client.gatewayConnect(); client.gateway.on('connect', () => { client.gateway.subscribeToCategoryChat('5fc829314e96e00725c17fd8'); - client.gateway.on('message', console.log); + client.gateway.on('message', (e) => { + if (e.author._id === client.user._id) return; + client.gateway.sendMessage('5fc829314e96e00725c17fd8', e.content, { + username: e.author.username + }); + }); }); };