move index to typescript; misc fixes

This commit is contained in:
Iuri Matias 2018-12-03 23:06:20 -05:00
parent 543c485684
commit 5dff7fb68e
5 changed files with 84 additions and 85 deletions

View File

@ -13,8 +13,6 @@
"build:node": "npm run babel:node",
"clean": "rimraf dist",
"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'",
"prepublishOnly": "npm-run-all clean build test",
"test": "npm-run-all lint test:*",

View File

@ -3,10 +3,10 @@ import Users from "./users";
import colors from "colors";
class ChannelManager {
private channels: any[];
private events: any;
public channels: any[];
public events: any;
public allUsers: Users;
private currentChannel: number;
private allUsers: Users;
constructor() {
this.channels = [];
@ -89,4 +89,4 @@ class ChannelManager {
}
}
module.exports = ChannelManager;
export default ChannelManager;

View File

@ -1,69 +1,71 @@
var StatusJS = require('status-js-api');
import UI from './ui';
import ChannelManager from './channelManager';
import StatusJS from "status-js-api";
import UI from "./ui";
import ChannelManager from "./channelManager";
const DEFAULT_CHANNEL = "mytest";
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());
});
channels.events.on('channelSwitch', () => {
channels.events.on("channelSwitch", () => {
ui.logEntry("-------------------");
ui.logEntry("now viewing #" + channels.getCurrentChannel().name);
channels.dumpPendingMessages().forEach((message) => {
let msg = (message.username + ">").green + " " + message.message;
channels.dumpPendingMessages().forEach((message: any) => {
const msg = (message.username + ">").green + " " + message.message;
ui.logEntry(msg);
});
});
channels.events.on('newMessage', (channelName, username, message) => {
let msg = (username + ">").green + " " + message;
channels.events.on("newMessage", (channelName: string, username: string, message: string) => {
const msg = (username + ">").green + " " + message;
ui.logEntry(msg);
});
var updateUsers = function() {
let users = channels.getUsersInCurrentChannel().map((x) => {
const updateUsers = () => {
const users = channels.getUsersInCurrentChannel().map((x: any) => {
return {name: x.username, status: (x.online ? "on" : "offline")};
});
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
let msg = JSON.parse(JSON.parse(data.payload)[1][0]);
let fromUser = data.data.sig;
const msg = JSON.parse(JSON.parse(data.payload)[1][0]);
const fromUser = data.data.sig;
if (msg.type === 'ping') {
let user = channels.allUsers.addOrUpdateUserKey(fromUser, data.username);
let channel = channels.getChannel(channelName);
if (msg.type === "ping") {
const user = channels.allUsers.addOrUpdateUserKey(fromUser, data.username);
const channel = channels.getChannel(channelName);
channel.users.addUserOrUpdate(user);
channels.events.emit("update");
}
if (msg.type === 'typing') {
if (fromUser === userPubKey) return; // ignore typing events from self
if (msg.type === "typing") {
if (fromUser === userPubKey) {
return; // ignore typing events from self
}
usersTyping[fromUser] = (new Date().getTime());
}
};
channels.events.on('update', updateUsers);
channels.events.on('channelSwitch', updateUsers);
channels.events.on("update", updateUsers);
channels.events.on("channelSwitch", updateUsers);
setInterval(function() {
let typingUsers = [];
let currentTime = (new Date().getTime());
for (let pubkey in usersTyping) {
let lastTyped = usersTyping[pubkey];
setInterval(() => {
const typingUsers = [];
const currentTime = (new Date().getTime());
for (const pubkey of Object.keys(usersTyping)) {
const lastTyped = usersTyping[pubkey];
if (currentTime - lastTyped > 3 * 1000 || currentTime < lastTyped) {
delete usersTyping[pubkey];
} else {
@ -82,7 +84,7 @@ setInterval(function() {
return;
}
ui.consoleState.setContent(typingUsers.join(', ') + " are typing");
ui.consoleState.setContent(typingUsers.join(", ") + " are typing");
}, 0.5 * 1000);
ui.logEntry(`
@ -109,14 +111,14 @@ ui.logEntry(`Rejoining Channels....`);
ui.logEntry(`PK: ${userPubKey}`);
ui.logEntry(`-----------------------------------------------------------`);
const fs = require('fs');
fs.writeFile("/tmp/test", await status.getPublicKey(), function(err) {
const fs = require("fs");
fs.writeFile("/tmp/test", await status.getPublicKey(), (err: any) => {
if (err) {
return console.log(err);
}
});
setInterval(function() {
setInterval(() => {
const channel = channels.getCurrentChannel();
if (!channel.pubKey) {
// TODO: JSON message is being displayed in the chat box of status
@ -125,16 +127,15 @@ ui.logEntry(`Rejoining Channels....`);
}
}, 5 * 1000);
status.joinChat(DEFAULT_CHANNEL, () => {
ui.logEntry(("Joined #" + DEFAULT_CHANNEL).green.underline);
channels.addChannel(DEFAULT_CHANNEL, 'channel');
channels.addChannel(DEFAULT_CHANNEL, "channel");
status.onMessage(DEFAULT_CHANNEL, (err, data) => {
let msg = JSON.parse(data.payload)[1][0];
status.onMessage(DEFAULT_CHANNEL, (err: any, data: any) => {
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);
} else {
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, data) => {
channels.addChannel(data.username, 'contact', {pubKey: data.data.sig});
let msg = JSON.parse(data.payload)[1][0];
if (JSON.parse(data.payload)[1][1] === 'content/json') {
status.onMessage((err: any, data: any) => {
channels.addChannel(data.username, "contact", {pubKey: data.data.sig});
const msg = JSON.parse(data.payload)[1][0];
if (JSON.parse(data.payload)[1][1] === "content/json") {
handleProtocolMessages(data.username, data);
} else {
channels.addMessage(data.username, msg, data.data.sig, data.username);
}
});
ui.events.on('cmd', (cmd) => {
if (cmd.split(' ')[0] === '/join') {
let channelName = cmd.split(' ')[1].replace('#','');
ui.events.on("cmd", (cmd: string) => {
if (cmd.split(" ")[0] === "/join") {
const channelName = cmd.split(" ")[1].replace("#", "");
ui.logEntry("joining " + channelName);
status.joinChat(channelName).then(() => {
ui.logEntry("joined #" + channelName);
channels.addChannel(channelName, 'channel');
channels.addChannel(channelName, "channel");
status.onMessage(channelName, (err, data) => {
let msg = JSON.parse(data.payload)[1][0];
status.onMessage(channelName, (err: any, data: any) => {
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);
} else {
channels.addMessage(channelName, msg, data.data.sig, data.username);
@ -176,14 +176,14 @@ ui.logEntry(`Rejoining Channels....`);
});
return;
}
if (cmd.split(' ')[0] === '/s') {
let channelNumber = cmd.split(' ')[1];
if (cmd.split(" ")[0] === "/s") {
const channelNumber = cmd.split(" ")[1];
channels.switchChannelIndex(parseInt(channelNumber, 10));
return;
}
if(cmd.split(' ')[0] === '/msg') {
let destination = cmd.substr(5);
if (cmd.split(" ")[0] === "/msg") {
const destination = cmd.substr(5);
if (!(CONTACT_CODE_REGEXP.test(destination) || (/^[a-z0-9A-Z\s]{4,}$/).test(destination))) {
ui.logEntry(`Invalid account`.red);
@ -191,13 +191,13 @@ ui.logEntry(`Rejoining Channels....`);
}
// 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) {
channels.addChannel(user.username, 'contact', {pubKey: user.pubkey});
channels.addChannel(user.username, "contact", {pubKey: user.pubkey});
channels.switchChannelIndex(channels.channels.length - 1);
} else {
status.getUserName(destination).then(username => {
channels.addChannel(username, 'contact', {pubKey: destination});
status.getUserName(destination).then((username: string) => {
channels.addChannel(username, "contact", {pubKey: destination});
channels.switchChannelIndex(channels.channels.length - 1);
});
}
@ -215,22 +215,23 @@ ui.logEntry(`Rejoining Channels....`);
});
// 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
if (currentText[0] === '/') return;
if (currentText[0] === "/") {
return;
}
const channel = channels.getCurrentChannel();
if (!channel.pubKey) {
let channelName = channels.getCurrentChannel().name;
const channelName = channels.getCurrentChannel().name;
if (!typingNotificationsTimestamp[channelName]) {
typingNotificationsTimestamp[channelName] = {
lastEvent: 0,
timeout: 0,
lastEvent: 0
};
}
let now = (new Date().getTime());
const now = (new Date().getTime());
clearTimeout(typingNotificationsTimestamp[channelName].timeout);
if (typingNotificationsTimestamp[channelName].lastEvent === 0 || now - typingNotificationsTimestamp[channelName].lastEvent > 3 * 1000) {
@ -238,7 +239,7 @@ ui.logEntry(`Rejoining Channels....`);
status.sendJsonMessage(channelName, {type: "typing"});
}
typingNotificationsTimestamp[channelName].timeout = setTimeout(function() {
typingNotificationsTimestamp[channelName].timeout = setTimeout(() => {
typingNotificationsTimestamp[channelName].lastEvent = (new Date().getTime());
status.sendJsonMessage(channelName, {type: "typing"});
}, 3 * 1000);
@ -246,4 +247,3 @@ ui.logEntry(`Rejoining Channels....`);
});
})();

1
src/types.d.ts vendored Normal file
View File

@ -0,0 +1 @@
declare module "status-js-api";

View File

@ -3,14 +3,14 @@ const blessed = require("neo-blessed");
const Events = require("events");
class UI {
private events: any;
public events: any;
private color: string;
private minimal: boolean;
private screen: any;
private input: any;
private consoleBox: any;
private consoleStateContainer: any;
private consoleState: any;
public consoleState: any;
private wrapper: any;
private users: any;
private channels: any;