diff --git a/package.json b/package.json index a70db5a..10b5f11 100644 --- a/package.json +++ b/package.json @@ -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:*", diff --git a/src/channelManager.ts b/src/channelManager.ts index 6802acf..6747f65 100644 --- a/src/channelManager.ts +++ b/src/channelManager.ts @@ -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; diff --git a/src/index.js b/src/index.ts similarity index 58% rename from src/index.js rename to src/index.ts index 5cc3ef4..5e7c3bd 100644 --- a/src/index.js +++ b/src/index.ts @@ -1,70 +1,72 @@ -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]; - if (currentTime - lastTyped > 3*1000 || currentTime < lastTyped) { +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 { if (channels.allUsers.users[pubkey]) { @@ -82,8 +84,8 @@ setInterval(function() { return; } - ui.consoleState.setContent(typingUsers.join(', ') + " are typing"); -}, 0.5*1000); + ui.consoleState.setContent(typingUsers.join(", ") + " are typing"); +}, 0.5 * 1000); ui.logEntry(` Welcome to @@ -109,32 +111,31 @@ 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){ + if (!channel.pubKey) { // TODO: JSON message is being displayed in the chat box of status status.sendJsonMessage(channel.name, {type: "ping"}); channels.allUsers.updateUsersState(); } }, 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); - if(user){ - channels.addChannel(user.username, 'contact', {pubKey: user.pubkey}); + 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.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); }); } @@ -206,7 +206,7 @@ ui.logEntry(`Rejoining Channels....`); } const channel = channels.getCurrentChannel(); - if(channel.pubKey){ + if (channel.pubKey) { status.sendMessage(channel.pubKey, cmd); channels.addMessage(channel.name, cmd, channel.pubKey, userName); } else { @@ -215,35 +215,35 @@ 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; + if (!channel.pubKey) { + 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) { + if (typingNotificationsTimestamp[channelName].lastEvent === 0 || now - typingNotificationsTimestamp[channelName].lastEvent > 3 * 1000) { typingNotificationsTimestamp[channelName].lastEvent = (new Date().getTime()); 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); + }, 3 * 1000); } }); })(); - diff --git a/src/types.d.ts b/src/types.d.ts new file mode 100644 index 0000000..0de652e --- /dev/null +++ b/src/types.d.ts @@ -0,0 +1 @@ +declare module "status-js-api"; diff --git a/src/ui.ts b/src/ui.ts index 3595582..3f9e5ea 100644 --- a/src/ui.ts +++ b/src/ui.ts @@ -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;