Release 0.6.1

[FIX] fixed messages echo when xmpp session goes offline
[UPD] do not sync all telegram statuses immediately because it leads to stream error
[UPD] now using messages and files database again to reduce thousands of requests
This commit is contained in:
annelin 2019-04-15 08:03:40 +03:00
parent 7de8809b49
commit 1ae35f501d
2 changed files with 16 additions and 17 deletions

View file

@ -18,10 +18,10 @@ class TelegramClient
config.client.application_version = params['version'] || '1.0' # hmm... config.client.application_version = params['version'] || '1.0' # hmm...
config.client.use_test_dc = params['use_test_dc'] || false config.client.use_test_dc = params['use_test_dc'] || false
config.client.system_version = '42' # I think I have permission to hardcode The Ultimate Question of Life, the Universe, and Everything?.. config.client.system_version = '42' # I think I have permission to hardcode The Ultimate Question of Life, the Universe, and Everything?..
config.client.use_file_database = false # wow config.client.use_file_database = true # wow
config.client.use_message_database = false # such library config.client.use_message_database = true # such library
config.client.use_chat_info_database = false # much options config.client.use_chat_info_database = false # much options
config.client.enable_storage_optimizer = false # ... config.client.enable_storage_optimizer = true # ...
end end
TD::Api.set_log_verbosity_level(params['verbosity'] || 1) TD::Api.set_log_verbosity_level(params['verbosity'] || 1)
end end
@ -33,7 +33,7 @@ class TelegramClient
@logger = Logger.new(STDOUT); @logger.level = @@loglevel; @logger.progname = '[TelegramClient: %s/%s]' % [xmpp.user_jid, login] # create logger @logger = Logger.new(STDOUT); @logger.level = @@loglevel; @logger.progname = '[TelegramClient: %s/%s]' % [xmpp.user_jid, login] # create logger
@xmpp = xmpp # our XMPP user session. we will send messages back to Jabber through this instance. @xmpp = xmpp # our XMPP user session. we will send messages back to Jabber through this instance.
@login = login # store tg login @login = login # store tg login
@cache = {chats: {}, users: {}, users_fi: {}, userpics: {}, unread_msg: {} } # we will store our cache here @cache = {chats: {}, users: {}, users_fullinfo: {}, userpics: {}, unread_msg: {} } # we will store our cache here
@files_dir = File.dirname(__FILE__) + '/../sessions/' + @xmpp.user_jid + '/files/' @files_dir = File.dirname(__FILE__) + '/../sessions/' + @xmpp.user_jid + '/files/'
# spawn telegram client and specify callback handlers # spawn telegram client and specify callback handlers
@ -95,7 +95,6 @@ class TelegramClient
@logger.debug 'Got NewMessage update' @logger.debug 'Got NewMessage update'
@logger.debug update.message.to_json @logger.debug update.message.to_json
return if not @cache[:chats].key? update.message.chat_id # we do not know about this chat, ignore it
return if update.message.is_outgoing and update.message.sending_state.instance_of? TD::Types::MessageSendingState::Pending # ignore self outgoing messages return if update.message.is_outgoing and update.message.sending_state.instance_of? TD::Types::MessageSendingState::Pending # ignore self outgoing messages
# media? # # media? #
@ -158,6 +157,7 @@ class TelegramClient
# send and add message id to unreads # send and add message id to unreads
@cache[:unread_msg][update.message.chat_id] = update.message.id @cache[:unread_msg][update.message.chat_id] = update.message.id
@xmpp.incoming_message(update.message.chat_id.to_s, text) @xmpp.incoming_message(update.message.chat_id.to_s, text)
end end
# new chat update -- when tg client discovers new chat # # new chat update -- when tg client discovers new chat #
@ -348,7 +348,7 @@ class TelegramClient
end end
# update users information and save it to cache # # update users information and save it to cache #
def process_chat_info(chat_id, no_subscription = false) def process_chat_info(chat_id, subscription = true)
@logger.debug 'Updating chat id %s..' % chat_id.to_s @logger.debug 'Updating chat id %s..' % chat_id.to_s
# fullfil cache.. pasha durov, privet. # # fullfil cache.. pasha durov, privet. #
@ -356,9 +356,9 @@ class TelegramClient
@cache[:chats][chat_id] = chat # cache chat @cache[:chats][chat_id] = chat # cache chat
@client.download_file(chat.photo.small.id).wait if chat.photo # download userpic @client.download_file(chat.photo.small.id).wait if chat.photo # download userpic
@cache[:userpics][chat_id] = Digest::SHA1.hexdigest(IO.binread(self.format_content_link(chat.photo.small.remote.id, 'image.jpg', true))) if chat.photo and File.exist? self.format_content_link(chat.photo.small.remote.id, 'image.jpg', true) # cache userpic @cache[:userpics][chat_id] = Digest::SHA1.hexdigest(IO.binread(self.format_content_link(chat.photo.small.remote.id, 'image.jpg', true))) if chat.photo and File.exist? self.format_content_link(chat.photo.small.remote.id, 'image.jpg', true) # cache userpic
@xmpp.presence(chat_id.to_s, :subscribe, nil, nil, @cache[:chats][chat_id].title.to_s) if not no_subscription # send subscription request @xmpp.presence(chat_id.to_s, :subscribe, nil, nil, @cache[:chats][chat_id].title.to_s) if subscription # send subscription request
@xmpp.presence(chat_id.to_s, nil, :chat, @cache[:chats][chat_id].title.to_s, nil, @cache[:userpics][chat_id]) if chat.type.instance_of? TD::Types::ChatType::BasicGroup or chat.type.instance_of? TD::Types::ChatType::Supergroup # send :chat status if its group/supergroup @xmpp.presence(chat_id.to_s, nil, :chat, @cache[:chats][chat_id].title.to_s, nil, @cache[:userpics][chat_id]) if chat.type.instance_of? TD::Types::ChatType::BasicGroup or chat.type.instance_of? TD::Types::ChatType::Supergroup # send :chat status if its group/supergroup
self.process_user_info(chat.type.user_id) if chat.type.instance_of? TD::Types::ChatType::Private # process user if its a private chat # self.process_user_info(chat.type.user_id) if chat.type.instance_of? TD::Types::ChatType::Private # process user if its a private chat
}.wait }.wait
end end
@ -370,7 +370,7 @@ class TelegramClient
self.process_status_update(user_id, user.status) # status update self.process_status_update(user_id, user.status) # status update
}.wait }.wait
@client.get_user_full_info(user_id).then{ |user_info| @client.get_user_full_info(user_id).then{ |user_info|
@cache[:users_fi][user_id] = user_info # here is user "bio" @cache[:users_fullinfo][user_id] = user_info # here is user "bio"
}.wait }.wait
end end
@ -412,7 +412,7 @@ class TelegramClient
lastname = @cache[:users][chat_id].last_name # <N/FAMILY> lastname = @cache[:users][chat_id].last_name # <N/FAMILY>
username = @cache[:users][chat_id].username # <NICKNAME> username = @cache[:users][chat_id].username # <NICKNAME>
phone = @cache[:users][chat_id].phone_number # <TEL> phone = @cache[:users][chat_id].phone_number # <TEL>
bio = @cache[:users_fi][chat_id].bio if @cache[:users_fi].key? chat_id # <DESC> bio = @cache[:users_fullinfo][chat_id].bio if @cache[:users_fullinfo].key? chat_id # <DESC>
end end
# userpic # # userpic #
@ -457,7 +457,7 @@ class TelegramClient
# format tg chat name # # format tg chat name #
def format_chatname(chat_id) def format_chatname(chat_id)
if not @cache[:chats].key? chat_id then self.process_chat_info(chat_id, true) end if not @cache[:chats].key? chat_id then self.process_chat_info(chat_id, false) end
if not @cache[:chats].key? chat_id then return chat_id end if not @cache[:chats].key? chat_id then return chat_id end
name = '%s (%s)' % [@cache[:chats][chat_id].title, chat_id] name = '%s (%s)' % [@cache[:chats][chat_id].title, chat_id]
return name return name

View file

@ -87,6 +87,7 @@ class XMPPComponent
# new message to XMPP component # # new message to XMPP component #
def message_handler(msg) def message_handler(msg)
return if msg.type == :error
@logger.info 'New message from [%s] to [%s]' % [msg.from, msg.to] @logger.info 'New message from [%s] to [%s]' % [msg.from, msg.to]
return self.process_internal_command(msg.from, msg.first_element_text('body') ) if msg.to == @@transport.jid # treat message as internal command if received as transport jid return self.process_internal_command(msg.from, msg.first_element_text('body') ) if msg.to == @@transport.jid # treat message as internal command if received as transport jid
return @sessions[msg.from.bare.to_s].tg_outgoing(msg.from, msg.to.to_s, msg.first_element_text('body')) #if @sessions.key? msg.from.bare.to_s and @sessions[msg.from.bare.to_s].online? # queue message for processing session is active for jid from return @sessions[msg.from.bare.to_s].tg_outgoing(msg.from, msg.to.to_s, msg.first_element_text('body')) #if @sessions.key? msg.from.bare.to_s and @sessions[msg.from.bare.to_s].online? # queue message for processing session is active for jid from
@ -112,8 +113,6 @@ class XMPPComponent
reply.type = :result reply.type = :result
reply.elements["vCard"] = vcard reply.elements["vCard"] = vcard
@@transport.send(reply) @@transport.send(reply)
@sessions[iq.from.bare.to_s].tg_sync_roster(iq.to.to_s) # re-sync status
# time response # # time response #
elsif iq.type == :result and iq.elements["time"] and @sessions.key? iq.from.bare.to_s then elsif iq.type == :result and iq.elements["time"] and @sessions.key? iq.from.bare.to_s then
@logger.debug "Got Timezone response" @logger.debug "Got Timezone response"
@ -235,7 +234,7 @@ class XMPPSession < XMPPComponent
end end
# sync roster # # sync roster #
def tg_sync_roster(to = nil) def tg_sync_status(to = nil)
@logger.debug "Sync Telegram contact status with roster.. %s" % to.to_s @logger.debug "Sync Telegram contact status with roster.. %s" % to.to_s
to = (to) ? to.split('@')[0].to_i : nil to = (to) ? to.split('@')[0].to_i : nil
@telegram.sync_status(to) @telegram.sync_status(to)
@ -282,8 +281,8 @@ class XMPPSession < XMPPComponent
def set_tz(timezone) def set_tz(timezone)
@logger.debug "Set TZ to %s" % timezone @logger.debug "Set TZ to %s" % timezone
@timezone = timezone @timezone = timezone
@logger.debug "Resyncing contact list.." # @logger.debug "Resyncing contact list.."
self.tg_sync_roster() # self.tg_sync_status()
end end
########################################### ###########################################