Bump version to 2.1

[FIX] correct presence handling when we have several priorities
This commit is contained in:
annelin 2020-03-09 02:55:50 +02:00
parent 82aa27558b
commit 5014133cc0
2 changed files with 23 additions and 7 deletions

View file

@ -53,11 +53,12 @@ class TelegramClient
@jid = jid @jid = jid
@session = session @session = session
@cache = {chats: {nil => []}, users: {}} @cache = {chats: {nil => []}, users: {}}
@resources = []
end end
## connect telegram client ## connect telegram client
def connect() def connect(resource=nil)
return if self.online? # already connected. return self.refresh(resource) if self.online? # already connected
@logger.warn 'Connecting to Telegram network..' @logger.warn 'Connecting to Telegram network..'
@telegram = TD::Client.new(database_directory: 'sessions/' + @jid, files_directory: 'sessions/' + @jid + '/files/') @telegram = TD::Client.new(database_directory: 'sessions/' + @jid, files_directory: 'sessions/' + @jid + '/files/')
@telegram.on(TD::Types::Update::AuthorizationState) do |u| @logger.debug(u); self.update_authorizationstate(u) end @telegram.on(TD::Types::Update::AuthorizationState) do |u| @logger.debug(u); self.update_authorizationstate(u) end
@ -69,22 +70,37 @@ class TelegramClient
@telegram.on(TD::Types::Update::DeleteMessages) do |u| @logger.debug(u); self.update_deletemessages(u) end @telegram.on(TD::Types::Update::DeleteMessages) do |u| @logger.debug(u); self.update_deletemessages(u) end
@telegram.on(TD::Types::Update::File) do |u| @logger.debug(u); self.update_file(u) end @telegram.on(TD::Types::Update::File) do |u| @logger.debug(u); self.update_file(u) end
@telegram.connect() @telegram.connect()
@resources << resource
end end
## disconnect telegram client ## disconnect telegram client
def disconnect() def disconnect(resource=nil)
return unless self.online? # already disconnected @resources.delete resource
return if @resources.count > 0 or not self.online?
@logger.warn 'Disconnecting from Telegram network..' @logger.warn 'Disconnecting from Telegram network..'
@cache[:chats].each_key do |chat| @xmpp.send_presence(@jid, chat, :unavailable) end # we're offline (unsubscribe if logout) @cache[:chats].each_key do |chat| @xmpp.send_presence(@jid, chat, :unavailable) end # we're offline (unsubscribe if logout)
@telegram.dispose() @telegram.dispose()
@telegram = nil @telegram = nil
end end
## refresh roster
def refresh(resource=nil)
return if @resources.include? resource
@logger.warn 'Refreshing roster for resource %s' % resource
@cache[:chats].each_key do |chat| self.process_status_update(chat) if chat; end
@resources << resource
end
## online? ## online?
def online? def online?
@telegram and @telegram.alive? @telegram and @telegram.alive?
end end
def authorized?
@telegram and @telegram.alive and @state == TD::Types::AuthorizationState::Ready
end
######################################################################### #########################################################################
# telegram updates handlers ############################################# # telegram updates handlers #############################################
######################################################################### #########################################################################

View file

@ -52,8 +52,8 @@ class XMPPComponent
@logger.debug presence.to_s @logger.debug presence.to_s
@sessions[presence.from.bare.to_s] = TelegramClient.new(self, presence.from.bare.to_s) unless @sessions.key? presence.from.bare.to_s # create session @sessions[presence.from.bare.to_s] = TelegramClient.new(self, presence.from.bare.to_s) unless @sessions.key? presence.from.bare.to_s # create session
@sessions[presence.from.bare.to_s] = nil if presence.type == :unsubscribed # destroy session @sessions[presence.from.bare.to_s] = nil if presence.type == :unsubscribed # destroy session
@sessions[presence.from.bare.to_s].disconnect() if presence.type == :unavailable or presence.type == :error # go offline @sessions[presence.from.bare.to_s].disconnect(presence.from.resource) if presence.type == :unavailable or presence.type == :error # go offline
@sessions[presence.from.bare.to_s].connect() if presence.type == :subscribe or not presence.type # go online @sessions[presence.from.bare.to_s].connect(presence.from.resource) if presence.type == :subscribe or not presence.type # go online
end end
def handle_message(message) def handle_message(message)