From 5caadd4643025e16110e0b92e99577685e5eaa80 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 8 Nov 2018 11:24:01 -0400 Subject: [PATCH] /msg function to send direct messages --- src/index.js | 24 ++++++++++++++++++++++++ src/ui.js | 12 ++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 5b6b3db..a3bdce5 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,7 @@ var StatusJS = require('status-js-api'); var ChannelManager = require('./channelManager.js'); const DEFAULT_CHANNEL = "mytest"; +const CONTACT_CODE_REGEXP = /^(0x)?[0-9a-f]{130}$/i; var ui = new UI(); @@ -179,6 +180,29 @@ ui.logEntry(`Rejoining Channels....`); return; } + if(cmd.split(' ')[0] === '/msg') { + let destination = cmd.substr(5); + + if (!(CONTACT_CODE_REGEXP.test(destination) || /^[a-z0-9A-Z\s]{4,}$/.test(destination))) { + ui.logEntry(`Invalid account`.red); + return; + } + + // 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}); + channels.switchChannelIndex(channels.channels.length - 1); + } else { + status.getUserName(destination).then(username => { + channels.addChannel(username, 'contact', {pubKey: destination}); + channels.switchChannelIndex(channels.channels.length - 1); + }); + } + + return; + } + const channel = channels.getCurrentChannel(); if(channel.pubKey){ status.sendMessage(channel.pubKey, cmd); diff --git a/src/ui.js b/src/ui.js index f807fc2..1b353d7 100644 --- a/src/ui.js +++ b/src/ui.js @@ -74,9 +74,9 @@ class UI { this.log = blessed.box({ label: "Logs", padding: 1, - width: "73%", + width: "68%", height: "92%", - left: "7%", + left: "12%", top: "0%", border: { type: "line" @@ -152,7 +152,7 @@ class UI { layoutChannels() { this.wrapper = blessed.layout({ - width: "7%", + width: "12%", height: "100%", top: "0%", left: "0%", @@ -253,10 +253,10 @@ class UI { layoutState() { this.consoleStateContainer = blessed.layout({ - width: '73%', + width: '68%', height: '5%', - left: '7%', - top: '90%', + left: '12%', + top: '92%', layout: "grid" });