mirror of
https://github.com/fazo96/telecommander.git
synced 2025-01-10 11:34:20 +01:00
many. many improvements especially to history download and ui
This commit is contained in:
parent
50d18b6dce
commit
470a44e092
31
lib/ui.js
31
lib/ui.js
@ -6,7 +6,7 @@ module.exports = function(data){
|
|||||||
data.selectedWindow = data.statusWindow // the currently selected window
|
data.selectedWindow = data.statusWindow // the currently selected window
|
||||||
|
|
||||||
// Get msgBox for given group/user NAME, create if not exists
|
// Get msgBox for given group/user NAME, create if not exists
|
||||||
data.getMsgBox = function(chat,switchto){
|
data.getMsgBox = function(chat){
|
||||||
if(chat === undefined){
|
if(chat === undefined){
|
||||||
log('ERROR: asked for box for "undefined"!!')
|
log('ERROR: asked for box for "undefined"!!')
|
||||||
return data.msgBox[statusWindow]
|
return data.msgBox[statusWindow]
|
||||||
@ -15,11 +15,8 @@ module.exports = function(data){
|
|||||||
//log('Generating window: "'+chat+'"')
|
//log('Generating window: "'+chat+'"')
|
||||||
data.msgBox[chat] = data.mkBox(chat)
|
data.msgBox[chat] = data.mkBox(chat)
|
||||||
data.screen.append(data.msgBox[chat])
|
data.screen.append(data.msgBox[chat])
|
||||||
data.getMessages(chat,data.msgBox[chat])
|
//data.getMessages(chat,data.msgBox[chat])
|
||||||
} // else log('Getting window','"'+chat+'"')
|
} // else log('Getting window','"'+chat+'"')
|
||||||
if(switchto === true){
|
|
||||||
data.switchToBox(chat)
|
|
||||||
}
|
|
||||||
return data.msgBox[chat]
|
return data.msgBox[chat]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,10 +26,13 @@ module.exports = function(data){
|
|||||||
data.selectedWindow = boxname;
|
data.selectedWindow = boxname;
|
||||||
if(data.selectedWindow != data.statusWindow)
|
if(data.selectedWindow != data.statusWindow)
|
||||||
data.cmdline.setLabel('to '+data.selectedWindow)
|
data.cmdline.setLabel('to '+data.selectedWindow)
|
||||||
else
|
else {
|
||||||
|
data.markAsRead(data.selectedWindow)
|
||||||
data.cmdline.setLabel('Command for Telecommander')
|
data.cmdline.setLabel('Command for Telecommander')
|
||||||
|
}
|
||||||
var newb = data.getMsgBox(data.selectedWindow)
|
var newb = data.getMsgBox(data.selectedWindow)
|
||||||
newb.show()
|
newb.show()
|
||||||
|
data.screen.render()
|
||||||
}
|
}
|
||||||
|
|
||||||
data.screen = blessed.screen({
|
data.screen = blessed.screen({
|
||||||
@ -52,7 +52,7 @@ module.exports = function(data){
|
|||||||
bg: 'white'
|
bg: 'white'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
selected: { bold: true },
|
selected: { bold: true, fg: 'white' },
|
||||||
scrollbar: {
|
scrollbar: {
|
||||||
fg: 'white', bg: 'white',
|
fg: 'white', bg: 'white',
|
||||||
track: { fg: 'grey', bg: 'grey' }
|
track: { fg: 'grey', bg: 'grey' }
|
||||||
@ -63,6 +63,7 @@ module.exports = function(data){
|
|||||||
// Contact list window
|
// Contact list window
|
||||||
data.chats = blessed.list({
|
data.chats = blessed.list({
|
||||||
keys: true,
|
keys: true,
|
||||||
|
tags: true,
|
||||||
label: 'Conversations',
|
label: 'Conversations',
|
||||||
left: 0,
|
left: 0,
|
||||||
top:0,
|
top:0,
|
||||||
@ -85,6 +86,7 @@ module.exports = function(data){
|
|||||||
data.mkBox = function(txt){
|
data.mkBox = function(txt){
|
||||||
var b = ChatBox({
|
var b = ChatBox({
|
||||||
keys: true,
|
keys: true,
|
||||||
|
tags: true,
|
||||||
mouse: true,
|
mouse: true,
|
||||||
right: 0,
|
right: 0,
|
||||||
label: { text: txt, side: 'left' },
|
label: { text: txt, side: 'left' },
|
||||||
@ -106,6 +108,9 @@ module.exports = function(data){
|
|||||||
b.key('enter',function(){
|
b.key('enter',function(){
|
||||||
data.cmdline.focus()
|
data.cmdline.focus()
|
||||||
})
|
})
|
||||||
|
b.on('scroll',function(){
|
||||||
|
if(b.getScroll() === 0) data.getMessages(txt,b)
|
||||||
|
})
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,7 +148,9 @@ module.exports = function(data){
|
|||||||
data.screen.key('tab',function(){
|
data.screen.key('tab',function(){
|
||||||
data.screen.focusPush(data.chats)
|
data.screen.focusPush(data.chats)
|
||||||
})
|
})
|
||||||
|
data.screen.key('0',function(){
|
||||||
|
data.switchToBox(data.statusWindow)
|
||||||
|
})
|
||||||
data.command = function(cmd){
|
data.command = function(cmd){
|
||||||
cmdl = cmd.split(' ')
|
cmdl = cmd.split(' ')
|
||||||
cmdname = cmdl[0]
|
cmdname = cmdl[0]
|
||||||
@ -217,9 +224,9 @@ module.exports = function(data){
|
|||||||
data.chats.on('select',function(selected){
|
data.chats.on('select',function(selected){
|
||||||
//data.log('SELECT:',selected.content)
|
//data.log('SELECT:',selected.content)
|
||||||
if(selected === undefined) return
|
if(selected === undefined) return
|
||||||
data.switchToBox(selected.content)
|
var sel = data.escapeFromList(selected)
|
||||||
data.cmdline.focus()
|
data.switchToBox(sel)
|
||||||
data.screen.render()
|
data.msgBox[data.selectedWindow].focus()
|
||||||
})
|
})
|
||||||
/*
|
/*
|
||||||
data.cmdline.on('click',function(){
|
data.cmdline.on('click',function(){
|
||||||
@ -238,7 +245,7 @@ module.exports = function(data){
|
|||||||
|
|
||||||
// What happens when the user submits a command in the prompt
|
// What happens when the user submits a command in the prompt
|
||||||
data.cmdline.on('submit',function(value){
|
data.cmdline.on('submit',function(value){
|
||||||
data.getMsgBox(data.statusWindow).pushLine('< '+value)
|
data.getMsgBox(data.statusWindow).add('< '+value)
|
||||||
if(data.selectedWindow === data.statusWindow || data.nameToObj(data.selectedWindow) === undefined){
|
if(data.selectedWindow === data.statusWindow || data.nameToObj(data.selectedWindow) === undefined){
|
||||||
//log('Window:',selectedWindow,'Eval cmd:',value)
|
//log('Window:',selectedWindow,'Eval cmd:',value)
|
||||||
data.command(value)
|
data.command(value)
|
||||||
|
95
lib/util.js
95
lib/util.js
@ -1,3 +1,5 @@
|
|||||||
|
var blessed = require('blessed')
|
||||||
|
|
||||||
module.exports = function(data){
|
module.exports = function(data){
|
||||||
// Contacts holds all the contacts data
|
// Contacts holds all the contacts data
|
||||||
data.contacts = { }
|
data.contacts = { }
|
||||||
@ -15,7 +17,13 @@ module.exports = function(data){
|
|||||||
data.contacts[u.id] = { user: u, id: u.id}
|
data.contacts[u.id] = { user: u, id: u.id}
|
||||||
var name = data.getName(u.id,'user')
|
var name = data.getName(u.id,'user')
|
||||||
data.unameToUid[name] = u.id
|
data.unameToUid[name] = u.id
|
||||||
if(!data.chats.getItem(name)) data.chats.addItem(name)
|
data.rebuildChatList()
|
||||||
|
/*
|
||||||
|
if(!data.chats.getItem(name)){
|
||||||
|
data.chats.addItem(name)
|
||||||
|
data.screen.render()
|
||||||
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
data.addGroup = function(group){
|
data.addGroup = function(group){
|
||||||
@ -41,8 +49,47 @@ module.exports = function(data){
|
|||||||
} else {
|
} else {
|
||||||
data.groups[group.id] = { id: group.id, title: group.title, group: group }
|
data.groups[group.id] = { id: group.id, title: group.title, group: group }
|
||||||
data.gnameToGid[group.title] = group.id
|
data.gnameToGid[group.title] = group.id
|
||||||
if(!data.chats.getItem(group.title)) data.chats.addItem(group.title)
|
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
if(!data.chats.getItem(group.title)){
|
||||||
|
data.chats.addItem(group.title)
|
||||||
|
data.screen.render()
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
data.rebuildChatList()
|
||||||
|
}
|
||||||
|
|
||||||
|
data.rebuildChatList = function(){
|
||||||
|
var list = []
|
||||||
|
for(c in data.contacts) list.push(data.contacts[c])
|
||||||
|
for(c in data.groups) list.push(data.groups[c])
|
||||||
|
function cmpstr(a,b){
|
||||||
|
return data.getName(a.id,a.title?'group':'user').localeCompare(data.getName(b.id,b.title?'group':'user'))
|
||||||
|
}
|
||||||
|
list.sort(function(a,b){
|
||||||
|
if(a.toread > 0 && b.toread > 0){
|
||||||
|
var diff = a.toread - b.toread
|
||||||
|
if(diff == 0) return cmpstr(a,b)
|
||||||
|
return diff
|
||||||
|
}
|
||||||
|
if(a.toread > 0) return -1
|
||||||
|
if(b.toread > 0) return 1
|
||||||
|
return cmpstr(a,b)
|
||||||
|
})
|
||||||
|
data.chats.setItems(list.map(function(item){
|
||||||
|
return data.getName(item.id,item.title?'group':'user',true)
|
||||||
|
}))
|
||||||
|
if(!data.chats.focused == data.screen.focused) data.chats.setScrollPerc(0)
|
||||||
|
//data.chats.select(data.selectedWindow)
|
||||||
|
data.screen.render()
|
||||||
|
}
|
||||||
|
|
||||||
|
data.markAsRead = function(name){
|
||||||
|
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()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Updates the current state
|
// Updates the current state
|
||||||
@ -54,27 +101,39 @@ module.exports = function(data){
|
|||||||
data.state.unreadCount = newstate.unread_count || data.state.unreadCount || 0
|
data.state.unreadCount = newstate.unread_count || data.state.unreadCount || 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// process an update
|
data.getName = function(id,type,forChatList){
|
||||||
data.onUpdate = function(upd){
|
var name,obj,toread
|
||||||
data.log('Got Update:',upd.toPrintable())
|
if(type === undefined) throw new Error('no type')
|
||||||
|
else if(id === data.user.id){
|
||||||
|
obj = data.user
|
||||||
|
} else if(type === 'group' && data.groups[id]) {
|
||||||
|
obj = data.groups[id]
|
||||||
|
toread = obj.toread
|
||||||
|
} else if(type === 'user' && data.contacts[id]) {
|
||||||
|
obj = data.contacts[id].user
|
||||||
|
toread = data.contacts[id].toread
|
||||||
|
} else data.log('Failed to find name for',type,id)
|
||||||
|
if(obj === undefined){
|
||||||
|
throw new Error("Can't find name for",type,id)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
if(type === 'user'){
|
||||||
data.nameForUser = function(u){
|
// User
|
||||||
if(u.first_name === undefined && u.last_name === undefined && u.username === undefined){
|
if(obj.first_name === undefined && obj.last_name === undefined && obj.username === undefined){
|
||||||
data.log('Zombie User: '+u)
|
data.log('Zombie User: '+obj)
|
||||||
return 'undefined'
|
return 'undefined'
|
||||||
}
|
}
|
||||||
return u.first_name + ' ' + u.last_name + (u.username?' (@'+u.username+')':'')
|
name = obj.first_name + (obj.last_name?' '+obj.last_name:'') + (obj.username?' (@'+obj.username+')':'')
|
||||||
|
} else {
|
||||||
|
// Group
|
||||||
|
name = obj.title
|
||||||
|
}
|
||||||
|
if(forChatList && toread > 0) return '* '+name
|
||||||
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
data.getName = function(id,type){
|
data.escapeFromList = function(txt){
|
||||||
if(id === data.user.id) return data.nameForUser(data.user)
|
return blessed.stripTags(txt.text || txt.content || String(text)).replace('* ','')
|
||||||
else if(type === undefined) throw new Error('no type')
|
|
||||||
else if(type === 'group' && data.groups[id])
|
|
||||||
return data.groups[id].title
|
|
||||||
else if(type === 'user' && data.contacts[id])
|
|
||||||
return data.nameForUser(data.contacts[id].user)
|
|
||||||
else data.log('Failed to find name for',type,id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
data.nameToObj = function(name){
|
data.nameToObj = function(name){
|
||||||
|
@ -172,8 +172,8 @@ data.whenReady = function(){
|
|||||||
data.downloadData = function(){
|
data.downloadData = function(){
|
||||||
data.log('Downloading data...')
|
data.log('Downloading data...')
|
||||||
data.client.contacts.getContacts('',function(cont){
|
data.client.contacts.getContacts('',function(cont){
|
||||||
data.chats.clearItems()
|
//data.chats.clearItems()
|
||||||
data.chats.add(data.statusWindow)
|
//data.chats.add(data.statusWindow)
|
||||||
cont.users.list.forEach(data.addUser)
|
cont.users.list.forEach(data.addUser)
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -195,7 +195,7 @@ data.downloadData = function(){
|
|||||||
data.downloadUpdates = function(){
|
data.downloadUpdates = function(){
|
||||||
data.client.updates.getDifference(data.state.pts,data.state.date,data.state.qts,function(res){
|
data.client.updates.getDifference(data.state.pts,data.state.date,data.state.qts,function(res){
|
||||||
if(!res.instanceOf('api.type.updates.DifferenceEmpty')){
|
if(!res.instanceOf('api.type.updates.DifferenceEmpty')){
|
||||||
//log('Got Diff: ',res.toPrintable())
|
//data.log('Got Diff: ',res.toPrintable())
|
||||||
if(res.state){
|
if(res.state){
|
||||||
data.updateState(res.state)
|
data.updateState(res.state)
|
||||||
}
|
}
|
||||||
@ -205,19 +205,22 @@ data.downloadUpdates = function(){
|
|||||||
for(c in res.users.list) data.addUser(res.users.list[c])
|
for(c in res.users.list) data.addUser(res.users.list[c])
|
||||||
if(res.new_messages){
|
if(res.new_messages){
|
||||||
res.new_messages.list.forEach(function(msg){
|
res.new_messages.list.forEach(function(msg){
|
||||||
data.appendMsg(msg,undefined,false,true)
|
data.appendMsg(msg,undefined,false,false)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
data.rebuildChatList()
|
||||||
}
|
}
|
||||||
setTimeout(data.downloadUpdates,1000)
|
setTimeout(data.downloadUpdates,1000)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get message history with given name in the given box
|
// Get message history with given name in the given box
|
||||||
|
// BROKEN, need to be rethinked
|
||||||
data.getMessages = function(name,box){
|
data.getMessages = function(name,box){
|
||||||
if(!data.connected){
|
if(!data.connected){
|
||||||
return log('Uh cant get messages cuz not connected.....')
|
return // data.log('Uh cant get messages cuz not connected.....')
|
||||||
}
|
}
|
||||||
|
if(data.downloadingMessages == true) return
|
||||||
//log('Name to obj:',name)
|
//log('Name to obj:',name)
|
||||||
var obj = data.nameToObj(name)
|
var obj = data.nameToObj(name)
|
||||||
if(!obj || !obj.id){
|
if(!obj || !obj.id){
|
||||||
@ -225,9 +228,11 @@ data.getMessages = function(name,box){
|
|||||||
}
|
}
|
||||||
var type = obj.title?'group':'user'
|
var type = obj.title?'group':'user'
|
||||||
var peer = data.idToPeer(obj.id,type)
|
var peer = data.idToPeer(obj.id,type)
|
||||||
box.add('Downloading message history for '+name)
|
//box.add('Downloading message history for '+name)
|
||||||
if(!peer) return log('Could not find peer:',name)
|
if(!peer) return log('Could not find peer:',name)
|
||||||
data.client.messages.getHistory(peer,0,-1,100,function(res){
|
data.downloadingMessages = true
|
||||||
|
var oldnlines = box.getLines().length
|
||||||
|
data.client.messages.getHistory(peer,0,obj.oldest_message||0,10,function(res){
|
||||||
//log(res.toPrintable())
|
//log(res.toPrintable())
|
||||||
//log('Got history for: '+getName(peer.user_id||peer.chat_id,peer.chat_id?'group':'user'))
|
//log('Got history for: '+getName(peer.user_id||peer.chat_id,peer.chat_id?'group':'user'))
|
||||||
if(!res.messages){
|
if(!res.messages){
|
||||||
@ -236,13 +241,13 @@ data.getMessages = function(name,box){
|
|||||||
res.messages.list.sort(function(msg1,msg2){
|
res.messages.list.sort(function(msg1,msg2){
|
||||||
return msg1.date - msg2.date
|
return msg1.date - msg2.date
|
||||||
})
|
})
|
||||||
if(res.messages.list.length === 0)
|
res.messages.list.reverse()
|
||||||
return data.appendToUserBox('No messages.',res)
|
|
||||||
res.messages.list.forEach(function(msg){
|
res.messages.list.forEach(function(msg){
|
||||||
//if(!msg.message) return log('Empty message!',msg.toPrintable())
|
data.appendMsg(msg,undefined,false,true)
|
||||||
//log('Scheduling message: '+msg.toPrintable())
|
|
||||||
data.appendMsg(msg)
|
|
||||||
})
|
})
|
||||||
|
if(oldnlines == 0) box.setScrollPerc(100)
|
||||||
|
//box.add(obj.oldest_message)
|
||||||
|
data.downloadingMessages = false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -266,45 +271,53 @@ data.appendToUserBox = function(msg,context){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Writes given telegram.link "message" object to given boxId
|
// Writes given telegram.link "message" object to given boxId
|
||||||
data.appendMsg = function(msg,toBoxId,bare,smartmode){
|
data.appendMsg = function(msg,toBoxId,bare,prepend){
|
||||||
var box,param
|
var box,param,obj
|
||||||
if(toBoxId != undefined){
|
if(toBoxId != undefined){
|
||||||
box = toBoxId
|
box = toBoxId
|
||||||
} else {
|
} else {
|
||||||
if(msg.to_id.chat_id != undefined){
|
if(msg.to_id.chat_id != undefined){
|
||||||
// Is a group
|
// Is a group
|
||||||
param = data.getName(msg.to_id.chat_id,'group')
|
param = data.getName(msg.to_id.chat_id,'group')
|
||||||
|
obj = data.groups[msg.to_id.chat_id]
|
||||||
} else if(msg.from_id === msg.to_id.user_id || msg.from_id != data.user.id){
|
} else if(msg.from_id === msg.to_id.user_id || msg.from_id != data.user.id){
|
||||||
param = data.getName(msg.from_id,'user')
|
param = data.getName(msg.from_id,'user')
|
||||||
|
obj = data.contacts[msg.from_id]
|
||||||
} else if(msg.to_id.user_id != undefined && msg.to_id.user_id != data.user.id) {
|
} else if(msg.to_id.user_id != undefined && msg.to_id.user_id != data.user.id) {
|
||||||
// don't forget dat .user_id! don't need it in from_id...
|
// don't forget dat .user_id! don't need it in from_id...
|
||||||
param = data.getName(msg.to_id.user_id,'user')
|
param = data.getName(msg.to_id.user_id,'user')
|
||||||
|
obj = data.contacts[msg.to_id.user_id]
|
||||||
}
|
}
|
||||||
if(smartmode && !bare){
|
// Increase unread count if necessary
|
||||||
// Smart mode doesn't append the message to the box if it doesn't exist
|
if(data.selectedWindow != param || data.msgBox[param] === undefined){
|
||||||
// because when created, the box will download message history
|
if(!obj.toread) obj.toread = 1
|
||||||
if(data.msgBox[param] === undefined) return;
|
else obj.toread++
|
||||||
}
|
}
|
||||||
|
// Update oldest message reference
|
||||||
|
if(!obj.oldest_message || parseInt(obj.oldest_message) > parseInt(msg.id))
|
||||||
|
obj.oldest_message = parseInt(msg.id)
|
||||||
box = data.getMsgBox(param)
|
box = data.getMsgBox(param)
|
||||||
}
|
}
|
||||||
if(bare)
|
if(bare)
|
||||||
box.add(msg)
|
box.add(msg)
|
||||||
else {
|
else {
|
||||||
var from = msg.from_id
|
var id = msg.from_id
|
||||||
var date = moment.unix(msg.date).fromNow()
|
var date = moment.unix(msg.date).format('DD-MM-YYYY H:mm')
|
||||||
name = data.getName(from,'user')
|
name = data.getName(id,'user')
|
||||||
var txt
|
var txt = (name || id)+' {|} {grey-fg}'+date+'{/grey-fg}\n'
|
||||||
if(msg.media){
|
if(msg.media){
|
||||||
if(msg.media.photo)
|
if(msg.media.photo)
|
||||||
txt = ' <*> (Photo)'
|
txt += '{grey-fg}>>>{/grey-fg} (Photo)'
|
||||||
else if(msg.media.audio)
|
else if(msg.media.audio)
|
||||||
txt = " <*> (Audio Message) "+msg.media.audio.duration+" seconds"
|
txt += "{grey-fg}>>>{/grey-fg} (Audio Message) "+msg.media.audio.duration+" seconds"
|
||||||
else if(msg.message)
|
else if(!msg.message)
|
||||||
txt = ' > '+msg.message
|
txt += "{grey-fg}>>>{/grey-fg} (Unsupported Message)"
|
||||||
else txt = " <*> (Unsupported Message)"
|
|
||||||
}
|
}
|
||||||
box.add(date+' | '+(name || from)+txt)
|
if(msg.message) txt += '{grey-fg}>{/grey-fg} '+msg.message
|
||||||
|
if(prepend) box.prepend(txt)
|
||||||
|
else box.add(txt)
|
||||||
}
|
}
|
||||||
|
return box
|
||||||
}
|
}
|
||||||
|
|
||||||
// - Entry Point -
|
// - Entry Point -
|
||||||
|
Loading…
Reference in New Issue
Block a user