Allows the reception and sending of messages
This commit is contained in:
parent
070e0e67cf
commit
4d24b2153b
|
@ -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})`;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
75
src/index.js
75
src/index.js
|
@ -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();
|
||||||
|
|
||||||
setInterval(function() {
|
await status.connect("ws://localhost:8546");
|
||||||
status.sendJsonMessage(channels.getCurrentChannel().name, {type: "ping"});
|
|
||||||
|
|
||||||
|
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() {
|
||||||
|
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];
|
||||||
|
@ -120,16 +137,27 @@ status.joinChat(DEFAULT_CHANNEL, () => {
|
||||||
channels.addMessage(DEFAULT_CHANNEL, msg, data.data.sig, data.username)
|
channels.addMessage(DEFAULT_CHANNEL, msg, data.data.sig, data.username)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
ui.events.on('cmd', (cmd) => {
|
|
||||||
|
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) => {
|
||||||
if (cmd.split(' ')[0] === '/join') {
|
if (cmd.split(' ')[0] === '/join') {
|
||||||
let channelName = cmd.split(' ')[1].replace('#','');
|
let channelName = cmd.split(' ')[1].replace('#','');
|
||||||
ui.logEntry("joining " + channelName)
|
ui.logEntry("joining " + channelName)
|
||||||
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"});
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
})();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue