From f06f0048808fa3e01249fe77c496f4565f51f6fd Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 7 Nov 2018 07:42:19 -0500 Subject: [PATCH] move channelManager to its own file --- src/channelManager.js | 111 ++++++++++++++++++++++++++++++++++++++++ src/index.js | 114 ++---------------------------------------- 2 files changed, 114 insertions(+), 111 deletions(-) create mode 100644 src/channelManager.js diff --git a/src/channelManager.js b/src/channelManager.js new file mode 100644 index 0000000..4b11919 --- /dev/null +++ b/src/channelManager.js @@ -0,0 +1,111 @@ +var Events = require('events'); + +class User { + constructor(pubkey, username) { + this.pubkey = pubkey; + this.username = username; + this.online = false; + this.lastSeen = 0; + } +} + +class Users { + constructor() { + this.users = {} + } + + 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 { + constructor() { + this.channels = []; + this.events = new Events(); + this.currentChannel = 0; + this.allUsers = new Users(); + } + + addChannel(channelName) { + let channel = {name: channelName, pendingMessages: []}; + channel.users = new Users(); + this.channels.push(channel); + this.events.emit("update"); + } + + getChannel(channelName) { + return this.channels.find(c => c.name === channelName); + } + + getCurrentChannel() { + return this.channels[this.currentChannel]; + } + + addMessage(channelName, message, pubkey, username) { + let channel = this.getChannel(channelName); + if (channelName !== this.channels[this.currentChannel].name) { + 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"); + } + + 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})`; + }); + } + + getUsersInCurrentChannel() { + let channel = this.getCurrentChannel(); + let user_keys = channel.users.getUsers(); + let users = user_keys.map((pubkey) => { + return this.allUsers.users[pubkey]; + }); + return users; + } +} + +module.exports = ChannelManager; diff --git a/src/index.js b/src/index.js index 6492468..b1ffff1 100644 --- a/src/index.js +++ b/src/index.js @@ -1,119 +1,11 @@ -var UI = require('./ui.js') -var StatusJS = require('status-js-api') -var Events = require('events'); +var UI = require('./ui.js'); +var StatusJS = require('status-js-api'); +var ChannelManager = require('./channelManager.js'); const DEFAULT_CHANNEL = "mytest"; var ui = new UI(); -class User { - constructor(pubkey, username) { - this.pubkey = pubkey; - this.username = username; - this.online = false; - this.lastSeen = 0; - } -} - -class Users { - constructor() { - this.users = {} - } - - 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 { - constructor() { - this.channels = []; - this.events = new Events(); - this.currentChannel = 0; - this.allUsers = new Users(); - } - - addChannel(channelName) { - let channel = {name: channelName, pendingMessages: []}; - channel.users = new Users(); - this.channels.push(channel); - this.events.emit("update"); - } - - getChannel(channelName) { - return this.channels.find(c => c.name === channelName); - } - - getCurrentChannel() { - return this.channels[this.currentChannel]; - } - - addMessage(channelName, message, pubkey, username) { - let channel = this.getChannel(channelName); - if (channelName !== this.channels[this.currentChannel].name) { - 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"); - } - - 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})`; - }); - } - - 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', () => {