diff --git a/src/channelManager.js b/src/channelManager.js deleted file mode 100644 index a18b1e9..0000000 --- a/src/channelManager.js +++ /dev/null @@ -1,127 +0,0 @@ -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; - } - - updateUsersState() { - let currentTime = (new Date().getTime()); - for (let pubkey in this.users) { - let user = this.users[pubkey]; - if (currentTime - user.lastSeen > 10*1000) { - user.online = false; - } - } - } - -} - -class ChannelManager { - constructor() { - this.channels = []; - this.events = new Events(); - this.currentChannel = 0; - this.allUsers = new Users(); - } - - addChannel(channelName, type, extraData) { - if(this.getChannel(channelName)) return; - - let channel = {name: channelName, pendingMessages: [], type, ...extraData}; - 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) => { - - const prefix = c.type === 'channel' ? '#' : ''; - - if (c.name === this.channels[this.currentChannel].name) { - return `${prefix}${c.name}`.green; - } - if (c.pendingMessages.length === 0) { - return `${prefix}${c.name}`; - } - return `${prefix}${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/channelManager.ts b/src/channelManager.ts new file mode 100644 index 0000000..6802acf --- /dev/null +++ b/src/channelManager.ts @@ -0,0 +1,92 @@ +const Events = require("events"); +import Users from "./users"; +import colors from "colors"; + +class ChannelManager { + private channels: any[]; + private events: any; + private currentChannel: number; + private allUsers: Users; + + constructor() { + this.channels = []; + this.events = new Events(); + this.currentChannel = 0; + this.allUsers = new Users(); + } + + public addChannel(channelName: string, type: string, extraData?: any) { + if (this.getChannel(channelName)) { + return; + } + + const channel = {name: channelName, pendingMessages: [], type, ...extraData}; + channel.users = new Users(); + this.channels.push(channel); + this.events.emit("update"); + } + + public getChannel(channelName: string) { + return this.channels.find((c) => c.name === channelName); + } + + public getCurrentChannel() { + return this.channels[this.currentChannel]; + } + + public addMessage(channelName: string, message: string, pubkey: string, username: string) { + const 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); + } + const user = this.allUsers.addOrUpdateUserKey(pubkey, username); + channel.users.addUserOrUpdate(user); + + this.events.emit("update"); + } + + public dumpPendingMessages() { + const messages = this.channels[this.currentChannel].pendingMessages.slice(0); + this.channels[this.currentChannel].pendingMessages = []; + return messages; + } + + public switchChannelIndex(index: number) { + if (index < 0) { + return; + } + if (index >= this.channels.length) { + return; + } + this.currentChannel = index; + this.events.emit("update"); + this.events.emit("channelSwitch"); + } + + public getChannelList() { + return this.channels.map((c) => { + const prefix = c.type === "channel" ? "#" : ""; + + if (c.name === this.channels[this.currentChannel].name) { + return colors.green(`${prefix}${c.name}`); + } + if (c.pendingMessages.length === 0) { + return `${prefix}${c.name}`; + } + return `${prefix}${c.name} (${c.pendingMessages.length})`; + }); + } + + public getUsersInCurrentChannel() { + const channel = this.getCurrentChannel(); + const userKeys = channel.users.getUsers(); + const users = userKeys.map((pubkey: string) => { + return this.allUsers.users[pubkey]; + }); + return users; + } +} + +module.exports = ChannelManager; diff --git a/src/user.ts b/src/user.ts new file mode 100644 index 0000000..e9084ea --- /dev/null +++ b/src/user.ts @@ -0,0 +1,16 @@ + +class User { + public pubkey: string; + public username: string; + public online: boolean; + public lastSeen: number; + + constructor(pubkey: string, username: string) { + this.pubkey = pubkey; + this.username = username; + this.online = false; + this.lastSeen = 0; + } +} + +export default User; diff --git a/src/users.ts b/src/users.ts new file mode 100644 index 0000000..6816220 --- /dev/null +++ b/src/users.ts @@ -0,0 +1,43 @@ +import User from "./user"; + +class Users { + public users: any; + + constructor() { + this.users = {}; + } + + public addUserOrUpdate(user: User) { + this.users[user.pubkey] = user; + } + + public addOrUpdateUserKey(pubkey: string, username: string) { + 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]; + } + + public getUsers() { + const userList = []; + for (const pubkey of Object.keys(this.users)) { + userList.push(pubkey); + } + return userList; + } + + public updateUsersState() { + const currentTime = (new Date().getTime()); + for (const pubkey of Object.keys(this.users)) { + const user = this.users[pubkey]; + if (currentTime - user.lastSeen > 10 * 1000) { + user.online = false; + } + } + } + +} + +export default Users;