support switching channels; keep track of pending messages and display them when switching

This commit is contained in:
Iuri Matias 2018-11-07 06:29:35 -05:00
parent 9d60c6304d
commit 542b8de985
4 changed files with 2251 additions and 2459 deletions

974
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -19,6 +19,6 @@
"dependencies": { "dependencies": {
"colors": "^1.3.2", "colors": "^1.3.2",
"neo-blessed": "^0.2.0", "neo-blessed": "^0.2.0",
"status-js-api": "^1.0.1" "status-js-api": "../status-js"
} }
} }

View File

@ -1,11 +1,98 @@
var UI = require('./ui.js') var UI = require('./ui.js')
var StatusJS = require('status-js-api') var StatusJS = require('status-js-api')
var Events = require('events');
const DEFAULT_CHANNEL = "mytest"; const DEFAULT_CHANNEL = "mytest";
var ui = new UI(); var ui = new UI();
let channels = []; class User {
constructor(pubkey) {
this.pubkey = pubkey;
this.username = username;
this.online = false;
this.lastSeen = 0;
}
}
class Users {
constructor() {
this.users = {}
}
addUser(user) {
this.users[user.pubkey] = user
}
}
class ChannelManager {
constructor() {
this.channels = [];
this.events = new Events();
this.currentChannel = 0;
}
addChannel(channelName) {
this.channels.push({name: channelName, pendingMessages: []});
this.events.emit("update");
}
getChannel(channelName) {
return this.channels.find(c => c.name === channelName);
}
getCurrentChannel() {
return this.channels[this.currentChannel];
}
addMessage(channelName, message) {
let channel = this.getChannel(channelName);
if (channelName !== this.channels[this.currentChannel].name) {
channel.pendingMessages.push(message);
}
this.events.emit("update");
}
dumpPendingMessages() {
let messages = this.channels[this.currentChannel].pendingMessages.slice(0);
this.channels[this.currentChannel].pendingMessages = [];
return messages;
}
switchChannelIndex(index) {
if (index < 0) return;
if (index >= this.channels.length) return;
this.currentChannel = index;
this.events.emit("update");
this.events.emit("channelSwitch");
}
getChannelList() {
return this.channels.map((c) => {
if (c.name === this.channels[this.currentChannel].name) {
return `#${c.name}`.green;
}
if (c.pendingMessages.length === 0) {
return `#${c.name}`;
}
return `#${c.name} (${c.pendingMessages.length})`;
});
}
}
var channels = new ChannelManager();
channels.events.on('update', () => {
ui.availableChannels(channels.getChannelList());
})
channels.events.on('channelSwitch', () => {
ui.logEntry("-------------------");
ui.logEntry("now viewing #" + channels.getCurrentChannel().name);
channels.dumpPendingMessages().forEach((message) => {
ui.logEntry(message);
});
});
ui.availableUsers([{name: "iuri", status: "on"}, {name: "rramos", status: "on"}, {name: "barry", status: "on"}, {name: "satoshi", status: "off"}]) ui.availableUsers([{name: "iuri", status: "on"}, {name: "rramos", status: "on"}, {name: "barry", status: "on"}, {name: "satoshi", status: "off"}])
ui.logEntry(` ui.logEntry(`
@ -29,12 +116,14 @@ status.connect("ws://localhost:8546");
status.joinChat(DEFAULT_CHANNEL, () => { status.joinChat(DEFAULT_CHANNEL, () => {
ui.logEntry(("Joined #" + DEFAULT_CHANNEL).green.underline) ui.logEntry(("Joined #" + DEFAULT_CHANNEL).green.underline)
channels.push('#' + DEFAULT_CHANNEL); channels.addChannel(DEFAULT_CHANNEL);
ui.availableChannels(channels);
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];
ui.logEntry((data.username + ">").green + " " + msg); let message = (data.username + ">").green + " " + msg ;
channels.addMessage(DEFAULT_CHANNEL, message)
ui.logEntry(message);
}); });
}); });
@ -45,11 +134,24 @@ ui.events.on('cmd', (cmd) => {
status.joinChat(channelName).then(() => { status.joinChat(channelName).then(() => {
ui.logEntry("joined #" + channelName) ui.logEntry("joined #" + channelName)
channels.push('#' + channelName); channels.addChannel(channelName);
ui.availableChannels(channels);
status.onMessage(channelName, (err, data) => {
let msg = JSON.parse(data.payload)[1][0];
let message = (data.username + ">").green + " " + msg ;
channels.addMessage(channelName, message)
});
}) })
return; return;
} }
if (cmd.split(' ')[0] === '/s') {
let channelNumber = cmd.split(' ')[1];
channels.switchChannelIndex(parseInt(channelNumber, 10));
return;
}
status.sendMessage(DEFAULT_CHANNEL, cmd); status.sendMessage(DEFAULT_CHANNEL, cmd);
}) })

View File

@ -56,7 +56,7 @@ class UI {
} }
availableChannels(channels) { availableChannels(channels) {
this.channels.setContent(channels.join('\n')); this.channels.setContent(channels.map((c, i) => `(${i}) ${c}`).join('\n'));
this.screen.render(); this.screen.render();
} }