diff --git a/src/channelManager.js b/src/channelManager.js index 4b11919..20128a3 100644 --- a/src/channelManager.js +++ b/src/channelManager.js @@ -34,6 +34,17 @@ class Users { } 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 { diff --git a/src/index.js b/src/index.js index b1ffff1..cc6ec7c 100644 --- a/src/index.js +++ b/src/index.js @@ -16,7 +16,7 @@ channels.events.on('channelSwitch', () => { ui.logEntry("-------------------"); ui.logEntry("now viewing #" + channels.getCurrentChannel().name); channels.dumpPendingMessages().forEach((message) => { - let msg = (message.username + ">").green + " " + message.message; + let msg = (message.username + ">").green + " " + message.message; ui.logEntry(msg); }); }); @@ -34,10 +34,22 @@ var updateUsers = function() { ui.availableUsers(users) } +var handleProtocolMessages = function(channelName, data) { + // 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; + + if (msg.type === 'ping') { + let user = channels.allUsers.addOrUpdateUserKey(fromUser, data.username); + let channel = channels.getChannel(channelName); + channel.users.addUserOrUpdate(user); + channels.events.emit("update"); + } +} + channels.events.on('update', updateUsers); channels.events.on('channelSwitch', updateUsers); - ui.logEntry(` Welcome to _________ __ __ ____ ___ @@ -56,6 +68,12 @@ ui.logEntry(`-----------------------------------------------------------`) var status = new StatusJS(); status.connect("ws://localhost:8546"); +setInterval(function() { + status.sendJsonMessage(channels.getCurrentChannel().name, {type: "ping"}); + + channels.allUsers.updateUsersState(); +}, 5 * 1000); + status.joinChat(DEFAULT_CHANNEL, () => { ui.logEntry(("Joined #" + DEFAULT_CHANNEL).green.underline) @@ -64,7 +82,11 @@ status.joinChat(DEFAULT_CHANNEL, () => { status.onMessage(DEFAULT_CHANNEL, (err, data) => { let msg = JSON.parse(data.payload)[1][0]; - channels.addMessage(DEFAULT_CHANNEL, msg, data.data.sig, data.username) + if (JSON.parse(data.payload)[1][1] === 'content/json') { + handleProtocolMessages(DEFAULT_CHANNEL, data); + } else { + channels.addMessage(DEFAULT_CHANNEL, msg, data.data.sig, data.username) + } }); }); @@ -80,7 +102,11 @@ ui.events.on('cmd', (cmd) => { status.onMessage(channelName, (err, data) => { let msg = JSON.parse(data.payload)[1][0]; - channels.addMessage(channelName, msg, data.data.sig, data.username) + if (JSON.parse(data.payload)[1][1] === 'content/json') { + handleProtocolMessages(channelName, data); + } else { + channels.addMessage(channelName, msg, data.data.sig, data.username) + } }); }) diff --git a/src/ui.js b/src/ui.js index a6fff53..1df880b 100644 --- a/src/ui.js +++ b/src/ui.js @@ -255,7 +255,7 @@ class UI { width: '73%', height: '5%', left: '7%', - top: '92%', + top: '90%', border: { type: 'line' },