diff --git a/src/index.js b/src/index.js index a8451b8..6492468 100644 --- a/src/index.js +++ b/src/index.js @@ -7,7 +7,7 @@ const DEFAULT_CHANNEL = "mytest"; var ui = new UI(); class User { - constructor(pubkey) { + constructor(pubkey, username) { this.pubkey = pubkey; this.username = username; this.online = false; @@ -20,9 +20,26 @@ class Users { this.users = {} } - addUser(user) { + addUserOrUpdate(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 { @@ -30,10 +47,13 @@ class ChannelManager { this.channels = []; this.events = new Events(); this.currentChannel = 0; + this.allUsers = new Users(); } 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"); } @@ -45,11 +65,15 @@ class ChannelManager { return this.channels[this.currentChannel]; } - addMessage(channelName, message) { + addMessage(channelName, message, pubkey, username) { let channel = this.getChannel(channelName); 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"); } @@ -79,21 +103,48 @@ class ChannelManager { 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(); + 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); + 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(` Welcome to @@ -120,10 +171,8 @@ status.joinChat(DEFAULT_CHANNEL, () => { status.onMessage(DEFAULT_CHANNEL, (err, data) => { let msg = JSON.parse(data.payload)[1][0]; - let message = (data.username + ">").green + " " + msg ; - channels.addMessage(DEFAULT_CHANNEL, message) - ui.logEntry(message); + channels.addMessage(DEFAULT_CHANNEL, msg, data.data.sig, data.username) }); }); @@ -138,9 +187,8 @@ ui.events.on('cmd', (cmd) => { status.onMessage(channelName, (err, data) => { 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; } - status.sendMessage(DEFAULT_CHANNEL, cmd); + status.sendMessage(channels.getCurrentChannel().name, cmd); })