From 4d24b2153ba2cdc180f7551e22875b7a0911ef2d Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 8 Nov 2018 08:50:59 -0400 Subject: [PATCH] Allows the reception and sending of messages --- src/channelManager.js | 15 +++-- src/index.js | 143 +++++++++++++++++++++++++++--------------- 2 files changed, 101 insertions(+), 57 deletions(-) diff --git a/src/channelManager.js b/src/channelManager.js index 20128a3..2358740 100644 --- a/src/channelManager.js +++ b/src/channelManager.js @@ -55,8 +55,10 @@ class ChannelManager { this.allUsers = new Users(); } - addChannel(channelName) { - let channel = {name: channelName, pendingMessages: []}; + addChannel(channelName, type, extraData) { + if(this.getChannel(channelName)) return; + + let channel = {name: channelName, pendingMessages: [], type, ...extraData}; channel.users = new Users(); this.channels.push(channel); this.events.emit("update"); @@ -99,13 +101,16 @@ class ChannelManager { getChannelList() { return this.channels.map((c) => { + + const prefix = c.type === 'channel' ? '#' : ''; + if (c.name === this.channels[this.currentChannel].name) { - return `#${c.name}`.green; + return `${prefix}${c.name}`.green; } if (c.pendingMessages.length === 0) { - return `#${c.name}`; + return `${prefix}${c.name}`; } - return `#${c.name} (${c.pendingMessages.length})`; + return `${prefix}${c.name} (${c.pendingMessages.length})`; }); } diff --git a/src/index.js b/src/index.js index ed9d103..02578c9 100644 --- a/src/index.js +++ b/src/index.js @@ -92,69 +92,108 @@ ui.logEntry(` \\/ \\/ \\/ \\_/ `) -ui.logEntry(`Generating Identify....`) -ui.logEntry(`Connecting to Peers....`) -ui.logEntry(`Rejoining Channels....`) -ui.logEntry(`-----------------------------------------------------------`) +ui.logEntry(`Generating Identity....`); +ui.logEntry(`Connecting to Peers....`); +ui.logEntry(`Rejoining Channels....`); -var status = new StatusJS(); -status.connect("ws://localhost:8546"); +(async () => { + const status = new StatusJS(); + + await status.connect("ws://localhost:8546"); -setInterval(function() { - status.sendJsonMessage(channels.getCurrentChannel().name, {type: "ping"}); + ui.logEntry(`PK: ${await status.getPublicKey()}`); + ui.logEntry(`-----------------------------------------------------------`); - channels.allUsers.updateUsersState(); -}, 5 * 1000); + /* + const fs = require('fs'); + fs.writeFile("/tmp/test", await status.getPublicKey(), function(err) { + if(err) { + return console.log(err); + } + }); + */ -status.joinChat(DEFAULT_CHANNEL, () => { - ui.logEntry(("Joined #" + DEFAULT_CHANNEL).green.underline) + setInterval(function() { + const channel = channels.getCurrentChannel(); + if(!channel.pubKey){ + // TODO: JSON message is being displayed in the chat box of status + status.sendJsonMessage(channel.name, {type: "ping"}); + channels.allUsers.updateUsersState(); + } + }, 5 * 1000); - channels.addChannel(DEFAULT_CHANNEL); - status.onMessage(DEFAULT_CHANNEL, (err, data) => { + status.joinChat(DEFAULT_CHANNEL, () => { + ui.logEntry(("Joined #" + DEFAULT_CHANNEL).green.underline) + + channels.addChannel(DEFAULT_CHANNEL, 'channel'); + + status.onMessage(DEFAULT_CHANNEL, (err, data) => { + let msg = JSON.parse(data.payload)[1][0]; + + if (JSON.parse(data.payload)[1][1] === 'content/json') { + handleProtocolMessages(DEFAULT_CHANNEL, data); + } else { + channels.addMessage(DEFAULT_CHANNEL, msg, data.data.sig, data.username) + } + }); + }); + + + status.onMessage((err, data) => { + channels.addChannel(data.username, 'contact', {pubKey: data.data.sig}); let msg = JSON.parse(data.payload)[1][0]; - if (JSON.parse(data.payload)[1][1] === 'content/json') { - handleProtocolMessages(DEFAULT_CHANNEL, data); + handleProtocolMessages(data.username, data); } else { - channels.addMessage(DEFAULT_CHANNEL, msg, data.data.sig, data.username) + channels.addMessage(data.username, msg, data.data.sig, data.username) + } + }) + + ui.events.on('cmd', (cmd) => { + if (cmd.split(' ')[0] === '/join') { + let channelName = cmd.split(' ')[1].replace('#',''); + ui.logEntry("joining " + channelName) + status.joinChat(channelName).then(() => { + ui.logEntry("joined #" + channelName) + + channels.addChannel(channelName, 'channel'); + + status.onMessage(channelName, (err, data) => { + let msg = JSON.parse(data.payload)[1][0]; + + if (JSON.parse(data.payload)[1][1] === 'content/json') { + handleProtocolMessages(channelName, data); + } else { + channels.addMessage(channelName, msg, data.data.sig, data.username) + } + }); + + }) + return; + } + if (cmd.split(' ')[0] === '/s') { + let channelNumber = cmd.split(' ')[1]; + channels.switchChannelIndex(parseInt(channelNumber, 10)); + return; + } + + const channel = channels.getCurrentChannel(); + if(channel.pubKey){ + status.sendMessage(channel.pubKey, cmd); + } else { + status.sendMessage(channel.name, cmd); } }); -}); -ui.events.on('cmd', (cmd) => { - if (cmd.split(' ')[0] === '/join') { - let channelName = cmd.split(' ')[1].replace('#',''); - ui.logEntry("joining " + channelName) - status.joinChat(channelName).then(() => { - ui.logEntry("joined #" + channelName) + ui.events.on('typing', () => { + // TODO: use async.cargo instead and/or a to avoid unnecessary requests + const channel = channels.getCurrentChannel(); + if(!channel.pubKey){ + // TODO: the json message is being displayed in the UI + status.sendJsonMessage(channels.getCurrentChannel().name, {type: "typing"}); + } + }); - channels.addChannel(channelName); - - status.onMessage(channelName, (err, data) => { - let msg = JSON.parse(data.payload)[1][0]; - - if (JSON.parse(data.payload)[1][1] === 'content/json') { - handleProtocolMessages(channelName, data); - } else { - channels.addMessage(channelName, msg, data.data.sig, data.username) - } - }); - - }) - return; - } - if (cmd.split(' ')[0] === '/s') { - let channelNumber = cmd.split(' ')[1]; - channels.switchChannelIndex(parseInt(channelNumber, 10)); - return; - } - - status.sendMessage(channels.getCurrentChannel().name, cmd); -}); - -ui.events.on('typing', () => { - // TODO: use async.cargo instead and/or a to avoid unnecessary requests - status.sendJsonMessage(channels.getCurrentChannel().name, {type: "typing"}); -}); +})();