convert channelManager to typescript
This commit is contained in:
parent
04f16d71fa
commit
a924549479
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
Loading…
Reference in New Issue