From 8405ae92370d9c3db116c726f1a538f8b8e971ed Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Fri, 4 Sep 2015 17:49:10 +0200 Subject: [PATCH] can now see who's online --- lib/ui.js | 15 +++++++++++++++ lib/util.js | 43 ++++++++++++++++++++++++++++--------------- telecommander.js | 12 +++++++++++- 3 files changed, 54 insertions(+), 16 deletions(-) diff --git a/lib/ui.js b/lib/ui.js index 9b5f83a..f9ea098 100644 --- a/lib/ui.js +++ b/lib/ui.js @@ -194,6 +194,21 @@ module.exports = function(data){ data.chats.height = data.screen.height - data.cmdline.height data.screen.render() }) + data.screen.enableMouse() + data.screen.on('mouse',function(){ + data.updateLastKnownAction() + }) + data.screen.on('keypress',function(){ + data.updateLastKnownAction() + }) + /* // Commented out cause doens't work on most terminals + data.screen.on('focus',function(){ + data.log('Screen Focus') + }) + data.screen.on('blur',function(){ + data.log('Screen blur') + }) + */ data.screen.key('tab',function(){ data.screen.focusPush(data.chats) }) diff --git a/lib/util.js b/lib/util.js index 2cb4085..14c0312 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,4 +1,5 @@ var blessed = require('blessed') +var moment = require('moment') module.exports = function(data){ // Contacts holds all the contacts data @@ -12,18 +13,29 @@ module.exports = function(data){ data.user = { } // holds data about current user data.state = { } // keeps track of the telegram update state + data.updateLastKnownAction = function(){ + data.lastKnownAction = moment() + data.user.offline = false + } + data.addUser = function(u){ if(!data.user || !data.user.id) return log("Can't add invalid user object to contacts",u) - data.contacts[u.id] = { user: u, id: u.id} + var wasOnline,online = false + data.contacts[u.id] = { user: u, id: u.id, online: online } var name = data.getName(u.id,'user') data.unameToUid[name] = u.id + data.updateOnlineStatus(u.id,u.status) data.rebuildChatList() - /* - if(!data.chats.getItem(name)){ - data.chats.addItem(name) - data.screen.render() + //data.log(u.toPrintable()) + } + + data.updateOnlineStatus = function(id,s){ + if(s.instanceOf('api.type.UserStatusOffline')){ + data.contacts[id].online_expires = moment.unix(s.was_online) + } else if(s.instanceOf('api.type.UserStatusOnline')){ + data.contacts[id].online_expires = moment.unix(s.expires) + data.log('Online status for',data.getName(id,'user'),'expires',data.contacts[id].online_expires.fromNow()) } - */ } data.addGroup = function(group){ @@ -38,6 +50,7 @@ module.exports = function(data){ return } } + data.log(group.toPrintable()) if(group.left === true) return; if(group.title === undefined){ if(!isNaN(group)){ // Is ID @@ -50,12 +63,6 @@ module.exports = function(data){ data.groups[group.id] = { id: group.id, title: group.title, group: group } data.gnameToGid[group.title] = group.id } - /* - if(!data.chats.getItem(group.title)){ - data.chats.addItem(group.title) - data.screen.render() - } - */ data.rebuildChatList() } @@ -115,7 +122,7 @@ module.exports = function(data){ } data.getName = function(id,type,forChatList){ - var name,obj,toread + var name,obj,toread,online_expires if(type === undefined) throw new Error('no type') else if(id === data.user.id){ obj = data.user @@ -125,6 +132,7 @@ module.exports = function(data){ } else if(type === 'user' && data.contacts[id]) { obj = data.contacts[id].user toread = data.contacts[id].toread + online_expires = data.contacts[id].online_expires } else data.log('Failed to find name for',type,id) if(obj === undefined){ name = 'Unknown '+type+' '+id @@ -138,8 +146,13 @@ module.exports = function(data){ } else { // Group name = obj.title } - if(forChatList && toread > 0) return '* '+name - return name + if(forChatList){ + if(toread > 0) name = '* '+name + if(online_expires && moment().isBefore(online_expires)) + name = '{green-fg}'+name+'{/green-fg}' + return name + } + return blessed.cleanTags(name) } data.escapeFromList = function(txt){ diff --git a/telecommander.js b/telecommander.js index 22b0ba4..865ae9c 100755 --- a/telecommander.js +++ b/telecommander.js @@ -176,6 +176,7 @@ data.downloadData = function(){ data.client.contacts.getContacts('',function(cont){ //data.chats.clearItems() //data.chats.add(data.statusWindow) + //data.log(cont.toPrintable()) cont.users.list.forEach(data.addUser) data.loader.stop() }) @@ -191,7 +192,7 @@ data.downloadData = function(){ data.log(data.state.unreadCount,'unread messages') //data.log('Started receiving updates') // Can't use registerOnUpdates because it's apparently broken - //client.registerOnUpdates(onUpdate) + //data.client.registerOnUpdates(data.onUpdate) setTimeout(data.downloadUpdates,1000) }) } @@ -212,12 +213,21 @@ data.downloadUpdates = function(){ data.appendMsg(msg,undefined,false,false) }) } + if(res.other_updates){ + for(c in res.other_updates.list) data.onUpdate(res.other_updates.list[c]) + } data.rebuildChatList() } setTimeout(data.downloadUpdates,1000) }) } +data.onUpdate = function(o){ + if(o.instanceOf('api.type.UpdateUserStatus')){ + data.updateOnlineStatus(o.user_id,o.status) + } +} + // Get message history with given name in the given box // BROKEN, need to be rethinked data.getMessages = function(name,box){