move index to typescript; misc fixes
This commit is contained in:
parent
543c485684
commit
5dff7fb68e
|
@ -13,8 +13,6 @@
|
||||||
"build:node": "npm run babel:node",
|
"build:node": "npm run babel:node",
|
||||||
"clean": "rimraf dist",
|
"clean": "rimraf dist",
|
||||||
"lint": "npm-run-all lint:*",
|
"lint": "npm-run-all lint:*",
|
||||||
"lint:js": "npm-run-all lint:js:*",
|
|
||||||
"lint:js:core": "eslint babel.config.js src/",
|
|
||||||
"lint:ts": "tslint -c tslint.json 'src/**/*.ts'",
|
"lint:ts": "tslint -c tslint.json 'src/**/*.ts'",
|
||||||
"prepublishOnly": "npm-run-all clean build test",
|
"prepublishOnly": "npm-run-all clean build test",
|
||||||
"test": "npm-run-all lint test:*",
|
"test": "npm-run-all lint test:*",
|
||||||
|
|
|
@ -3,10 +3,10 @@ import Users from "./users";
|
||||||
import colors from "colors";
|
import colors from "colors";
|
||||||
|
|
||||||
class ChannelManager {
|
class ChannelManager {
|
||||||
private channels: any[];
|
public channels: any[];
|
||||||
private events: any;
|
public events: any;
|
||||||
|
public allUsers: Users;
|
||||||
private currentChannel: number;
|
private currentChannel: number;
|
||||||
private allUsers: Users;
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.channels = [];
|
this.channels = [];
|
||||||
|
@ -89,4 +89,4 @@ class ChannelManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = ChannelManager;
|
export default ChannelManager;
|
||||||
|
|
|
@ -1,70 +1,72 @@
|
||||||
var StatusJS = require('status-js-api');
|
import StatusJS from "status-js-api";
|
||||||
import UI from './ui';
|
import UI from "./ui";
|
||||||
import ChannelManager from './channelManager';
|
import ChannelManager from "./channelManager";
|
||||||
|
|
||||||
const DEFAULT_CHANNEL = "mytest";
|
const DEFAULT_CHANNEL = "mytest";
|
||||||
const CONTACT_CODE_REGEXP = /^(0x)?[0-9a-f]{130}$/i;
|
const CONTACT_CODE_REGEXP = /^(0x)?[0-9a-f]{130}$/i;
|
||||||
|
|
||||||
let userPubKey;
|
let userPubKey: any;
|
||||||
|
|
||||||
var ui = new UI();
|
const ui = new UI();
|
||||||
|
|
||||||
var channels = new ChannelManager();
|
const channels = new ChannelManager();
|
||||||
|
|
||||||
let usersTyping = {};
|
const usersTyping = {};
|
||||||
|
|
||||||
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: any) => {
|
||||||
let msg = (message.username + ">").green + " " + message.message;
|
const msg = (message.username + ">").green + " " + message.message;
|
||||||
ui.logEntry(msg);
|
ui.logEntry(msg);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
channels.events.on('newMessage', (channelName, username, message) => {
|
channels.events.on("newMessage", (channelName: string, username: string, message: string) => {
|
||||||
let msg = (username + ">").green + " " + message;
|
const msg = (username + ">").green + " " + message;
|
||||||
ui.logEntry(msg);
|
ui.logEntry(msg);
|
||||||
});
|
});
|
||||||
|
|
||||||
var updateUsers = function() {
|
const updateUsers = () => {
|
||||||
let users = channels.getUsersInCurrentChannel().map((x) => {
|
const users = channels.getUsersInCurrentChannel().map((x: any) => {
|
||||||
return {name: x.username, status: (x.online ? "on" : "offline")};
|
return {name: x.username, status: (x.online ? "on" : "offline")};
|
||||||
});
|
});
|
||||||
ui.availableUsers(users);
|
ui.availableUsers(users);
|
||||||
};
|
};
|
||||||
|
|
||||||
var handleProtocolMessages = function(channelName, data) {
|
const handleProtocolMessages = (channelName: string, data: any) => {
|
||||||
// TODO: yes this is ugly, can be moved to the lib level
|
// TODO: yes this is ugly, can be moved to the lib level
|
||||||
let msg = JSON.parse(JSON.parse(data.payload)[1][0]);
|
const msg = JSON.parse(JSON.parse(data.payload)[1][0]);
|
||||||
let fromUser = data.data.sig;
|
const fromUser = data.data.sig;
|
||||||
|
|
||||||
if (msg.type === 'ping') {
|
if (msg.type === "ping") {
|
||||||
let user = channels.allUsers.addOrUpdateUserKey(fromUser, data.username);
|
const user = channels.allUsers.addOrUpdateUserKey(fromUser, data.username);
|
||||||
let channel = channels.getChannel(channelName);
|
const channel = channels.getChannel(channelName);
|
||||||
channel.users.addUserOrUpdate(user);
|
channel.users.addUserOrUpdate(user);
|
||||||
channels.events.emit("update");
|
channels.events.emit("update");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msg.type === 'typing') {
|
if (msg.type === "typing") {
|
||||||
if (fromUser === userPubKey) return; // ignore typing events from self
|
if (fromUser === userPubKey) {
|
||||||
|
return; // ignore typing events from self
|
||||||
|
}
|
||||||
usersTyping[fromUser] = (new Date().getTime());
|
usersTyping[fromUser] = (new Date().getTime());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
channels.events.on('update', updateUsers);
|
channels.events.on("update", updateUsers);
|
||||||
channels.events.on('channelSwitch', updateUsers);
|
channels.events.on("channelSwitch", updateUsers);
|
||||||
|
|
||||||
setInterval(function() {
|
setInterval(() => {
|
||||||
let typingUsers = [];
|
const typingUsers = [];
|
||||||
let currentTime = (new Date().getTime());
|
const currentTime = (new Date().getTime());
|
||||||
for (let pubkey in usersTyping) {
|
for (const pubkey of Object.keys(usersTyping)) {
|
||||||
let lastTyped = usersTyping[pubkey];
|
const lastTyped = usersTyping[pubkey];
|
||||||
if (currentTime - lastTyped > 3*1000 || currentTime < lastTyped) {
|
if (currentTime - lastTyped > 3 * 1000 || currentTime < lastTyped) {
|
||||||
delete usersTyping[pubkey];
|
delete usersTyping[pubkey];
|
||||||
} else {
|
} else {
|
||||||
if (channels.allUsers.users[pubkey]) {
|
if (channels.allUsers.users[pubkey]) {
|
||||||
|
@ -82,8 +84,8 @@ setInterval(function() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.consoleState.setContent(typingUsers.join(', ') + " are typing");
|
ui.consoleState.setContent(typingUsers.join(", ") + " are typing");
|
||||||
}, 0.5*1000);
|
}, 0.5 * 1000);
|
||||||
|
|
||||||
ui.logEntry(`
|
ui.logEntry(`
|
||||||
Welcome to
|
Welcome to
|
||||||
|
@ -109,32 +111,31 @@ ui.logEntry(`Rejoining Channels....`);
|
||||||
ui.logEntry(`PK: ${userPubKey}`);
|
ui.logEntry(`PK: ${userPubKey}`);
|
||||||
ui.logEntry(`-----------------------------------------------------------`);
|
ui.logEntry(`-----------------------------------------------------------`);
|
||||||
|
|
||||||
const fs = require('fs');
|
const fs = require("fs");
|
||||||
fs.writeFile("/tmp/test", await status.getPublicKey(), function(err) {
|
fs.writeFile("/tmp/test", await status.getPublicKey(), (err: any) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return console.log(err);
|
return console.log(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
setInterval(function() {
|
setInterval(() => {
|
||||||
const channel = channels.getCurrentChannel();
|
const channel = channels.getCurrentChannel();
|
||||||
if(!channel.pubKey){
|
if (!channel.pubKey) {
|
||||||
// TODO: JSON message is being displayed in the chat box of status
|
// TODO: JSON message is being displayed in the chat box of status
|
||||||
status.sendJsonMessage(channel.name, {type: "ping"});
|
status.sendJsonMessage(channel.name, {type: "ping"});
|
||||||
channels.allUsers.updateUsersState();
|
channels.allUsers.updateUsersState();
|
||||||
}
|
}
|
||||||
}, 5 * 1000);
|
}, 5 * 1000);
|
||||||
|
|
||||||
|
|
||||||
status.joinChat(DEFAULT_CHANNEL, () => {
|
status.joinChat(DEFAULT_CHANNEL, () => {
|
||||||
ui.logEntry(("Joined #" + DEFAULT_CHANNEL).green.underline);
|
ui.logEntry(("Joined #" + DEFAULT_CHANNEL).green.underline);
|
||||||
|
|
||||||
channels.addChannel(DEFAULT_CHANNEL, 'channel');
|
channels.addChannel(DEFAULT_CHANNEL, "channel");
|
||||||
|
|
||||||
status.onMessage(DEFAULT_CHANNEL, (err, data) => {
|
status.onMessage(DEFAULT_CHANNEL, (err: any, data: any) => {
|
||||||
let msg = JSON.parse(data.payload)[1][0];
|
const msg = JSON.parse(data.payload)[1][0];
|
||||||
|
|
||||||
if (JSON.parse(data.payload)[1][1] === 'content/json') {
|
if (JSON.parse(data.payload)[1][1] === "content/json") {
|
||||||
handleProtocolMessages(DEFAULT_CHANNEL, data);
|
handleProtocolMessages(DEFAULT_CHANNEL, data);
|
||||||
} else {
|
} else {
|
||||||
usersTyping[data.data.sig] = 0; // user is likley no longer typing if a message was received
|
usersTyping[data.data.sig] = 0; // user is likley no longer typing if a message was received
|
||||||
|
@ -143,30 +144,29 @@ ui.logEntry(`Rejoining Channels....`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
status.onMessage((err: any, data: any) => {
|
||||||
status.onMessage((err, data) => {
|
channels.addChannel(data.username, "contact", {pubKey: data.data.sig});
|
||||||
channels.addChannel(data.username, 'contact', {pubKey: data.data.sig});
|
const msg = JSON.parse(data.payload)[1][0];
|
||||||
let msg = JSON.parse(data.payload)[1][0];
|
if (JSON.parse(data.payload)[1][1] === "content/json") {
|
||||||
if (JSON.parse(data.payload)[1][1] === 'content/json') {
|
|
||||||
handleProtocolMessages(data.username, data);
|
handleProtocolMessages(data.username, data);
|
||||||
} else {
|
} else {
|
||||||
channels.addMessage(data.username, msg, data.data.sig, data.username);
|
channels.addMessage(data.username, msg, data.data.sig, data.username);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
ui.events.on('cmd', (cmd) => {
|
ui.events.on("cmd", (cmd: string) => {
|
||||||
if (cmd.split(' ')[0] === '/join') {
|
if (cmd.split(" ")[0] === "/join") {
|
||||||
let channelName = cmd.split(' ')[1].replace('#','');
|
const channelName = cmd.split(" ")[1].replace("#", "");
|
||||||
ui.logEntry("joining " + channelName);
|
ui.logEntry("joining " + channelName);
|
||||||
status.joinChat(channelName).then(() => {
|
status.joinChat(channelName).then(() => {
|
||||||
ui.logEntry("joined #" + channelName);
|
ui.logEntry("joined #" + channelName);
|
||||||
|
|
||||||
channels.addChannel(channelName, 'channel');
|
channels.addChannel(channelName, "channel");
|
||||||
|
|
||||||
status.onMessage(channelName, (err, data) => {
|
status.onMessage(channelName, (err: any, data: any) => {
|
||||||
let msg = JSON.parse(data.payload)[1][0];
|
const msg = JSON.parse(data.payload)[1][0];
|
||||||
|
|
||||||
if (JSON.parse(data.payload)[1][1] === 'content/json') {
|
if (JSON.parse(data.payload)[1][1] === "content/json") {
|
||||||
handleProtocolMessages(channelName, data);
|
handleProtocolMessages(channelName, data);
|
||||||
} else {
|
} else {
|
||||||
channels.addMessage(channelName, msg, data.data.sig, data.username);
|
channels.addMessage(channelName, msg, data.data.sig, data.username);
|
||||||
|
@ -176,14 +176,14 @@ ui.logEntry(`Rejoining Channels....`);
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (cmd.split(' ')[0] === '/s') {
|
if (cmd.split(" ")[0] === "/s") {
|
||||||
let channelNumber = cmd.split(' ')[1];
|
const channelNumber = cmd.split(" ")[1];
|
||||||
channels.switchChannelIndex(parseInt(channelNumber, 10));
|
channels.switchChannelIndex(parseInt(channelNumber, 10));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(cmd.split(' ')[0] === '/msg') {
|
if (cmd.split(" ")[0] === "/msg") {
|
||||||
let destination = cmd.substr(5);
|
const destination = cmd.substr(5);
|
||||||
|
|
||||||
if (!(CONTACT_CODE_REGEXP.test(destination) || (/^[a-z0-9A-Z\s]{4,}$/).test(destination))) {
|
if (!(CONTACT_CODE_REGEXP.test(destination) || (/^[a-z0-9A-Z\s]{4,}$/).test(destination))) {
|
||||||
ui.logEntry(`Invalid account`.red);
|
ui.logEntry(`Invalid account`.red);
|
||||||
|
@ -191,13 +191,13 @@ ui.logEntry(`Rejoining Channels....`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO:resolve ens username
|
// TODO:resolve ens username
|
||||||
const user = Object.values(channels.allUsers.users).find(x => x.username === destination);
|
const user: any = Object.values(channels.allUsers.users).find((x: any) => x.username === destination);
|
||||||
if(user){
|
if (user) {
|
||||||
channels.addChannel(user.username, 'contact', {pubKey: user.pubkey});
|
channels.addChannel(user.username, "contact", {pubKey: user.pubkey});
|
||||||
channels.switchChannelIndex(channels.channels.length - 1);
|
channels.switchChannelIndex(channels.channels.length - 1);
|
||||||
} else {
|
} else {
|
||||||
status.getUserName(destination).then(username => {
|
status.getUserName(destination).then((username: string) => {
|
||||||
channels.addChannel(username, 'contact', {pubKey: destination});
|
channels.addChannel(username, "contact", {pubKey: destination});
|
||||||
channels.switchChannelIndex(channels.channels.length - 1);
|
channels.switchChannelIndex(channels.channels.length - 1);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -206,7 +206,7 @@ ui.logEntry(`Rejoining Channels....`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const channel = channels.getCurrentChannel();
|
const channel = channels.getCurrentChannel();
|
||||||
if(channel.pubKey){
|
if (channel.pubKey) {
|
||||||
status.sendMessage(channel.pubKey, cmd);
|
status.sendMessage(channel.pubKey, cmd);
|
||||||
channels.addMessage(channel.name, cmd, channel.pubKey, userName);
|
channels.addMessage(channel.name, cmd, channel.pubKey, userName);
|
||||||
} else {
|
} else {
|
||||||
|
@ -215,35 +215,35 @@ ui.logEntry(`Rejoining Channels....`);
|
||||||
});
|
});
|
||||||
|
|
||||||
// keep track of each channel typing sent for throttling purposes
|
// keep track of each channel typing sent for throttling purposes
|
||||||
let typingNotificationsTimestamp = {
|
const typingNotificationsTimestamp = {};
|
||||||
};
|
|
||||||
|
|
||||||
ui.events.on('typing', (currentText) => {
|
ui.events.on("typing", (currentText: string) => {
|
||||||
// TODO: use async.cargo instead and/or a to avoid unnecessary requests
|
// TODO: use async.cargo instead and/or a to avoid unnecessary requests
|
||||||
if (currentText[0] === '/') return;
|
if (currentText[0] === "/") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const channel = channels.getCurrentChannel();
|
const channel = channels.getCurrentChannel();
|
||||||
if(!channel.pubKey){
|
if (!channel.pubKey) {
|
||||||
let channelName = channels.getCurrentChannel().name;
|
const channelName = channels.getCurrentChannel().name;
|
||||||
if (!typingNotificationsTimestamp[channelName]) {
|
if (!typingNotificationsTimestamp[channelName]) {
|
||||||
typingNotificationsTimestamp[channelName] = {
|
typingNotificationsTimestamp[channelName] = {
|
||||||
|
lastEvent: 0,
|
||||||
timeout: 0,
|
timeout: 0,
|
||||||
lastEvent: 0
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
let now = (new Date().getTime());
|
const now = (new Date().getTime());
|
||||||
|
|
||||||
clearTimeout(typingNotificationsTimestamp[channelName].timeout);
|
clearTimeout(typingNotificationsTimestamp[channelName].timeout);
|
||||||
if (typingNotificationsTimestamp[channelName].lastEvent === 0 || now - typingNotificationsTimestamp[channelName].lastEvent > 3*1000) {
|
if (typingNotificationsTimestamp[channelName].lastEvent === 0 || now - typingNotificationsTimestamp[channelName].lastEvent > 3 * 1000) {
|
||||||
typingNotificationsTimestamp[channelName].lastEvent = (new Date().getTime());
|
typingNotificationsTimestamp[channelName].lastEvent = (new Date().getTime());
|
||||||
status.sendJsonMessage(channelName, {type: "typing"});
|
status.sendJsonMessage(channelName, {type: "typing"});
|
||||||
}
|
}
|
||||||
|
|
||||||
typingNotificationsTimestamp[channelName].timeout = setTimeout(function() {
|
typingNotificationsTimestamp[channelName].timeout = setTimeout(() => {
|
||||||
typingNotificationsTimestamp[channelName].lastEvent = (new Date().getTime());
|
typingNotificationsTimestamp[channelName].lastEvent = (new Date().getTime());
|
||||||
status.sendJsonMessage(channelName, {type: "typing"});
|
status.sendJsonMessage(channelName, {type: "typing"});
|
||||||
}, 3*1000);
|
}, 3 * 1000);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
declare module "status-js-api";
|
|
@ -3,14 +3,14 @@ const blessed = require("neo-blessed");
|
||||||
const Events = require("events");
|
const Events = require("events");
|
||||||
|
|
||||||
class UI {
|
class UI {
|
||||||
private events: any;
|
public events: any;
|
||||||
private color: string;
|
private color: string;
|
||||||
private minimal: boolean;
|
private minimal: boolean;
|
||||||
private screen: any;
|
private screen: any;
|
||||||
private input: any;
|
private input: any;
|
||||||
private consoleBox: any;
|
private consoleBox: any;
|
||||||
private consoleStateContainer: any;
|
private consoleStateContainer: any;
|
||||||
private consoleState: any;
|
public consoleState: any;
|
||||||
private wrapper: any;
|
private wrapper: any;
|
||||||
private users: any;
|
private users: any;
|
||||||
private channels: any;
|
private channels: any;
|
||||||
|
|
Loading…
Reference in New Issue