display known online users; fix sending msgs

This commit is contained in:
Iuri Matias 2018-11-07 07:39:36 -05:00
parent 542b8de985
commit dbadd50739
1 changed files with 62 additions and 14 deletions

View File

@ -7,7 +7,7 @@ const DEFAULT_CHANNEL = "mytest";
var ui = new UI(); var ui = new UI();
class User { class User {
constructor(pubkey) { constructor(pubkey, username) {
this.pubkey = pubkey; this.pubkey = pubkey;
this.username = username; this.username = username;
this.online = false; this.online = false;
@ -20,9 +20,26 @@ class Users {
this.users = {} this.users = {}
} }
addUser(user) { addUserOrUpdate(user) {
this.users[user.pubkey] = user this.users[user.pubkey] = user
} }
addOrUpdateUserKey(pubkey, username) {
if (!this.users[pubkey]) {
this.users[pubkey] = new User(pubkey, username);
}
this.users[pubkey].lastSeen = (new Date().getTime());
this.users[pubkey].online = true;
return this.users[pubkey];
}
getUsers() {
let userList = [];
for (let pubkey in this.users) {
userList.push(pubkey);
}
return userList;
}
} }
class ChannelManager { class ChannelManager {
@ -30,10 +47,13 @@ class ChannelManager {
this.channels = []; this.channels = [];
this.events = new Events(); this.events = new Events();
this.currentChannel = 0; this.currentChannel = 0;
this.allUsers = new Users();
} }
addChannel(channelName) { addChannel(channelName) {
this.channels.push({name: channelName, pendingMessages: []}); let channel = {name: channelName, pendingMessages: []};
channel.users = new Users();
this.channels.push(channel);
this.events.emit("update"); this.events.emit("update");
} }
@ -45,11 +65,15 @@ class ChannelManager {
return this.channels[this.currentChannel]; return this.channels[this.currentChannel];
} }
addMessage(channelName, message) { addMessage(channelName, message, pubkey, username) {
let channel = this.getChannel(channelName); let channel = this.getChannel(channelName);
if (channelName !== this.channels[this.currentChannel].name) { if (channelName !== this.channels[this.currentChannel].name) {
channel.pendingMessages.push(message); channel.pendingMessages.push({pubkey, username, message});
} else {
this.events.emit("newMessage", channelName, username, message);
} }
let user = this.allUsers.addOrUpdateUserKey(pubkey, username);
channel.users.addUserOrUpdate(user);
this.events.emit("update"); this.events.emit("update");
} }
@ -79,21 +103,48 @@ class ChannelManager {
return `#${c.name} (${c.pendingMessages.length})`; return `#${c.name} (${c.pendingMessages.length})`;
}); });
} }
getUsersInCurrentChannel() {
let channel = this.getCurrentChannel();
let user_keys = channel.users.getUsers();
let users = user_keys.map((pubkey) => {
return this.allUsers.users[pubkey];
});
return users;
}
} }
var channels = new ChannelManager(); var channels = new ChannelManager();
channels.events.on('update', () => { channels.events.on('update', () => {
ui.availableChannels(channels.getChannelList()); ui.availableChannels(channels.getChannelList());
}) });
channels.events.on('channelSwitch', () => { channels.events.on('channelSwitch', () => {
ui.logEntry("-------------------"); ui.logEntry("-------------------");
ui.logEntry("now viewing #" + channels.getCurrentChannel().name); ui.logEntry("now viewing #" + channels.getCurrentChannel().name);
channels.dumpPendingMessages().forEach((message) => { channels.dumpPendingMessages().forEach((message) => {
ui.logEntry(message); let msg = (message.username + ">").green + " " + message.message;
ui.logEntry(msg);
}); });
}); });
ui.availableUsers([{name: "iuri", status: "on"}, {name: "rramos", status: "on"}, {name: "barry", status: "on"}, {name: "satoshi", status: "off"}]) channels.events.on('newMessage', (channelName, username, message) => {
let msg = (username + ">").green + " " + message;
ui.logEntry(msg);
});
var updateUsers = function() {
let users = channels.getUsersInCurrentChannel().map((x) => {
return {name: x.username, status: (x.online ? "on" : "offline")}
});
//ui.availableUsers([{name: "iuri", status: "on"}, {name: "rramos", status: "on"}, {name: "barry", status: "on"}, {name: "satoshi", status: "off"}])
ui.availableUsers(users)
}
channels.events.on('update', updateUsers);
channels.events.on('channelSwitch', updateUsers);
ui.logEntry(` ui.logEntry(`
Welcome to Welcome to
@ -120,10 +171,8 @@ status.joinChat(DEFAULT_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];
let message = (data.username + ">").green + " " + msg ;
channels.addMessage(DEFAULT_CHANNEL, message) channels.addMessage(DEFAULT_CHANNEL, msg, data.data.sig, data.username)
ui.logEntry(message);
}); });
}); });
@ -138,9 +187,8 @@ ui.events.on('cmd', (cmd) => {
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];
let message = (data.username + ">").green + " " + msg ;
channels.addMessage(channelName, message) channels.addMessage(channelName, msg, data.data.sig, data.username)
}); });
}) })
@ -152,6 +200,6 @@ ui.events.on('cmd', (cmd) => {
return; return;
} }
status.sendMessage(DEFAULT_CHANNEL, cmd); status.sendMessage(channels.getCurrentChannel().name, cmd);
}) })