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,69 +92,108 @@ 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");
setInterval(function() { ui.logEntry(`PK: ${await status.getPublicKey()}`);
status.sendJsonMessage(channels.getCurrentChannel().name, {type: "ping"}); 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, () => { setInterval(function() {
ui.logEntry(("Joined #" + DEFAULT_CHANNEL).green.underline) 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]; let msg = JSON.parse(data.payload)[1][0];
if (JSON.parse(data.payload)[1][1] === 'content/json') { if (JSON.parse(data.payload)[1][1] === 'content/json') {
handleProtocolMessages(DEFAULT_CHANNEL, data); handleProtocolMessages(data.username, data);
} else { } 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) => { ui.events.on('typing', () => {
if (cmd.split(' ')[0] === '/join') { // TODO: use async.cargo instead and/or a to avoid unnecessary requests
let channelName = cmd.split(' ')[1].replace('#',''); const channel = channels.getCurrentChannel();
ui.logEntry("joining " + channelName) if(!channel.pubKey){
status.joinChat(channelName).then(() => { // TODO: the json message is being displayed in the UI
ui.logEntry("joined #" + channelName) 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"});
});