Allows the reception and sending of messages

This commit is contained in:
Richard Ramos 2018-11-08 08:50:59 -04:00
parent 070e0e67cf
commit 4d24b2153b
2 changed files with 101 additions and 57 deletions

View File

@ -55,8 +55,10 @@ class ChannelManager {
this.allUsers = new Users(); this.allUsers = new Users();
} }
addChannel(channelName) { addChannel(channelName, type, extraData) {
let channel = {name: channelName, pendingMessages: []}; if(this.getChannel(channelName)) return;
let channel = {name: channelName, pendingMessages: [], type, ...extraData};
channel.users = new Users(); channel.users = new Users();
this.channels.push(channel); this.channels.push(channel);
this.events.emit("update"); this.events.emit("update");
@ -99,13 +101,16 @@ class ChannelManager {
getChannelList() { getChannelList() {
return this.channels.map((c) => { return this.channels.map((c) => {
const prefix = c.type === 'channel' ? '#' : '';
if (c.name === this.channels[this.currentChannel].name) { if (c.name === this.channels[this.currentChannel].name) {
return `#${c.name}`.green; return `${prefix}${c.name}`.green;
} }
if (c.pendingMessages.length === 0) { 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})`;
}); });
} }

View File

@ -92,24 +92,41 @@ ui.logEntry(`
\\/ \\/ \\/ \\_/ \\/ \\/ \\/ \\_/
`) `)
ui.logEntry(`Generating Identify....`) ui.logEntry(`Generating Identity....`);
ui.logEntry(`Connecting to Peers....`) ui.logEntry(`Connecting to Peers....`);
ui.logEntry(`Rejoining Channels....`) ui.logEntry(`Rejoining Channels....`);
ui.logEntry(`-----------------------------------------------------------`)
var status = new StatusJS(); (async () => {
status.connect("ws://localhost:8546"); const status = new StatusJS();
await status.connect("ws://localhost:8546");
ui.logEntry(`PK: ${await status.getPublicKey()}`);
ui.logEntry(`-----------------------------------------------------------`);
/*
const fs = require('fs');
fs.writeFile("/tmp/test", await status.getPublicKey(), function(err) {
if(err) {
return console.log(err);
}
});
*/
setInterval(function() { setInterval(function() {
status.sendJsonMessage(channels.getCurrentChannel().name, {type: "ping"}); 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(); channels.allUsers.updateUsersState();
}
}, 5 * 1000); }, 5 * 1000);
status.joinChat(DEFAULT_CHANNEL, () => { status.joinChat(DEFAULT_CHANNEL, () => {
ui.logEntry(("Joined #" + DEFAULT_CHANNEL).green.underline) ui.logEntry(("Joined #" + DEFAULT_CHANNEL).green.underline)
channels.addChannel(DEFAULT_CHANNEL); channels.addChannel(DEFAULT_CHANNEL, 'channel');
status.onMessage(DEFAULT_CHANNEL, (err, data) => { status.onMessage(DEFAULT_CHANNEL, (err, data) => {
let msg = JSON.parse(data.payload)[1][0]; let msg = JSON.parse(data.payload)[1][0];
@ -122,6 +139,17 @@ status.joinChat(DEFAULT_CHANNEL, () => {
}); });
}); });
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(data.username, data);
} else {
channels.addMessage(data.username, msg, data.data.sig, data.username)
}
})
ui.events.on('cmd', (cmd) => { ui.events.on('cmd', (cmd) => {
if (cmd.split(' ')[0] === '/join') { if (cmd.split(' ')[0] === '/join') {
let channelName = cmd.split(' ')[1].replace('#',''); let channelName = cmd.split(' ')[1].replace('#','');
@ -129,7 +157,7 @@ ui.events.on('cmd', (cmd) => {
status.joinChat(channelName).then(() => { status.joinChat(channelName).then(() => {
ui.logEntry("joined #" + channelName) ui.logEntry("joined #" + channelName)
channels.addChannel(channelName); channels.addChannel(channelName, 'channel');
status.onMessage(channelName, (err, data) => { status.onMessage(channelName, (err, data) => {
let msg = JSON.parse(data.payload)[1][0]; let msg = JSON.parse(data.payload)[1][0];
@ -150,11 +178,22 @@ ui.events.on('cmd', (cmd) => {
return; return;
} }
status.sendMessage(channels.getCurrentChannel().name, cmd); const channel = channels.getCurrentChannel();
if(channel.pubKey){
status.sendMessage(channel.pubKey, cmd);
} else {
status.sendMessage(channel.name, cmd);
}
}); });
ui.events.on('typing', () => { ui.events.on('typing', () => {
// TODO: use async.cargo instead and/or a to avoid unnecessary requests // 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"}); status.sendJsonMessage(channels.getCurrentChannel().name, {type: "typing"});
}
}); });
})();