From d6ff9176880ae8894fb75ba9f3d4f7ac114b5295 Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Tue, 1 Sep 2015 14:12:28 +0200 Subject: [PATCH] many ui improvements, marking messages as read fully works now --- lib/ui.js | 47 +++++++++++++++++++++++++++++++++++------------ lib/util.js | 17 +++++++++++++++-- telecommander.js | 18 +++++++++++++----- 3 files changed, 63 insertions(+), 19 deletions(-) diff --git a/lib/ui.js b/lib/ui.js index b8ecdd2..4ffb2e9 100644 --- a/lib/ui.js +++ b/lib/ui.js @@ -24,14 +24,15 @@ module.exports = function(data){ if(data.selectedWindow && data.msgBox[data.selectedWindow]) data.msgBox[data.selectedWindow].hide() data.selectedWindow = boxname; - if(data.selectedWindow != data.statusWindow) + if(data.selectedWindow != data.statusWindow){ data.cmdline.setLabel('to '+data.selectedWindow) - else { data.markAsRead(data.selectedWindow) + } else { data.cmdline.setLabel('Command for Telecommander') } var newb = data.getMsgBox(data.selectedWindow) newb.show() + newb.emit('scroll') data.screen.render() } @@ -81,6 +82,11 @@ module.exports = function(data){ invertSelected: false, style: data.getDefaultStyle(), }) + data.chats.key('tab',function(){ + if(data.msgBox[data.selectedWindow]) + data.msgBox[data.selectedWindow].focus() + }) + data.screen.append(data.chats) // Function to create a log box data.mkBox = function(txt){ @@ -106,10 +112,15 @@ module.exports = function(data){ style: data.getDefaultStyle() }) b.key('enter',function(){ + this.setScrollPerc(100) data.cmdline.focus() }) b.on('scroll',function(){ - if(b.getScroll() === 0) data.getMessages(txt,b) + // The functions might not yet exist if this is the first window + if(b.getScroll() === 100 && data.markAsRead) + data.markAsRead(data.selectedWindow) + else if(b.getScroll() === 0 && data.getMessages) + data.getMessages(txt,b) }) return b } @@ -126,17 +137,29 @@ module.exports = function(data){ border: { type: 'line' }, style: data.getDefaultStyle() }) - - // mgsBox holds the chat boxes for every list entry - data.msgBox = { } - data.msgBox[data.statusWindow] = data.mkBox(data.statusWindow) - - // Add stuff to the screen - data.screen.append(data.chats); - data.screen.append(data.msgBox[data.statusWindow]); data.screen.append(data.cmdline); - data.chats.addItem(data.msgBox[data.statusWindow]) + + // Widget used to show loading windows + data.loader = blessed.loading({ + width: '30%', + height: 5, + left: 'center', + top: 'center', + align: 'center', + valign: 'center', + border: { type: 'line' }, + style: data.getDefaultStyle() + }) + data.screen.append(data.loader) + + // mgsBox holds the chat window instance for every chat + data.msgBox = { } + + // Add the status window and switch to it + data.msgBox[data.statusWindow] = data.mkBox(data.statusWindow) + data.screen.append(data.msgBox[data.statusWindow]) data.switchToBox(data.statusWindow) + data.screen.on('resize',function(){ for(i in data.msgBox){ item = data.msgBox[i] diff --git a/lib/util.js b/lib/util.js index bcf9306..05cbaf1 100644 --- a/lib/util.js +++ b/lib/util.js @@ -77,7 +77,10 @@ module.exports = function(data){ return cmpstr(a,b) }) data.chats.setItems(list.map(function(item){ - return data.getName(item.id,item.title?'group':'user',true) + var n = data.getName(item.id,item.title?'group':'user',true) + if(n === undefined || n === null || !n || (n.trim && !n.trim())) + data.log('Empty list item:',JSON.stringify(item)) + return n })) if(!data.chats.focused == data.screen.focused) data.chats.setScrollPerc(0) //data.chats.select(data.selectedWindow) @@ -88,8 +91,18 @@ module.exports = function(data){ var obj = data.nameToObj(name) if(obj === undefined) return obj.toread = 0 - data.client.messages.readHistory(data.idToPeer(obj.id,obj.title?'group':'user'),0,0,true) data.rebuildChatList() + if(data.markingAsRead){ + return // we don't want 2339238 requests + } + var maxid = obj.latest_message || 0 + if(obj.latest_read_message > maxid) return // already read them messages + data.latest_read_message = maxid + data.markingAsRead = true + data.client.messages.readHistory(data.idToPeer(obj.id,obj.title?'group':'user'),maxid,0,true,function(res){ + data.log('Done reading history:',res.toPrintable()) + data.markingAsRead = false + }) } // Updates the current state diff --git a/telecommander.js b/telecommander.js index c8eb234..f8fbfa6 100755 --- a/telecommander.js +++ b/telecommander.js @@ -133,10 +133,12 @@ data.sendMsg = function(name,str){ } var obj = data.nameToObj(name) var peer = data.idToPeer(obj.id,obj.title?'group':'user') - var randid = parseInt(Math.random() * 1000000000) - //log('Sending Message to:',peer.toPrintable()) + var randid = parseInt(Math.random() * 999999999) + // Fix bug in telegram.link that doesn't send strings with accented letters + str = str.replace('è',"e'").replace('ù',"u'").replace('à',"a'").replace('ò',"o'").replace('ì',"i'") + //data.log('Sending Message to:',peer.toPrintable()) data.client.messages.sendMessage(peer,str,randid,function(sent){ - //log('Sent message:','"'+str+'"','to:',selectedWindow+':',sent.toPrintable()) + data.log('Sent message:','"'+str+'"','to:',data.selectedWindow+':',sent.toPrintable()) }) } @@ -170,11 +172,13 @@ data.whenReady = function(){ // Downloads stuff data.downloadData = function(){ - data.log('Downloading data...') + data.loader.load('Downloading data') + data.screen.render() data.client.contacts.getContacts('',function(cont){ //data.chats.clearItems() //data.chats.add(data.statusWindow) cont.users.list.forEach(data.addUser) + data.loader.stop() }) data.client.messages.getDialogs(0,0,10,function(dialogs){ @@ -293,9 +297,11 @@ data.appendMsg = function(msg,toBoxId,bare,prepend){ if(!obj.toread) obj.toread = 1 else obj.toread++ } - // Update oldest message reference + // Update oldest and latest message reference if(!obj.oldest_message || parseInt(obj.oldest_message) > parseInt(msg.id)) obj.oldest_message = parseInt(msg.id) + if(!obj.latest_message || parseInt(obj.latest_message) < parseInt(msg.id)) + obj.latest_message = parseInt(msg.id) box = data.getMsgBox(param) } if(bare) @@ -317,6 +323,8 @@ data.appendMsg = function(msg,toBoxId,bare,prepend){ if(prepend) box.prepend(txt) else box.add(txt) } + // Mark messages as read if needed + if(param === data.selectedWindow) data.markAsRead(param) return box }