Status caching

This commit is contained in:
annelin 2020-04-22 22:50:38 +03:00
parent dc21d076c1
commit db590fd27f

View file

@ -74,7 +74,7 @@ class TelegramClient
@jid = jid @jid = jid
@session = session @session = session
@resources = Set.new @resources = Set.new
@cache = {chats: {nil => []}, users: {}} @cache = {chats: {nil => []}, users: {}, status: {}}
self.connect() if @session[:keeponline] == 'true' self.connect() if @session[:keeponline] == 'true'
end end
@ -99,11 +99,11 @@ class TelegramClient
def disconnect(resource=nil, quit=false) def disconnect(resource=nil, quit=false)
@resources.delete(resource) @resources.delete(resource)
return if ((@resources.count > 0 || @session[:keeponline] == 'true') && !quit) return if ((@resources.count > 0 || @session[:keeponline] == 'true') && !quit)
return if not self.online? return if 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 @cache[:chats].each_key do |chat| @xmpp.send_presence(@jid, chat, :unavailable) end
@telegram.dispose() @telegram.dispose()
@telegram = nil @telegram = nil
end end
## resend statuses to (to another resource for example) ## resend statuses to (to another resource for example)
@ -219,11 +219,13 @@ class TelegramClient
## new user discovered ## new user discovered
def update_user(update) def update_user(update)
@cache[:users][update.user.id] = update.user @cache[:users][update.user.id] = update.user
@cache[:status][update.user.id] = update.user.status
self.process_status_update(update.user.id, update.user.status) self.process_status_update(update.user.id, update.user.status)
end end
## user status changed ## user status changed
def update_userstatus(update) def update_userstatus(update)
@cache[:status][update.user_id] = update.status
self.process_status_update(update.user_id, update.status, nil, false) self.process_status_update(update.user_id, update.status, nil, false)
end end
@ -258,7 +260,7 @@ class TelegramClient
@logger.info "Status update for %s" % id @logger.info "Status update for %s" % id
chat, user = self.get_contact(id) chat, user = self.get_contact(id)
photo = Digest::SHA1.hexdigest(IO.binread(chat.photo.small.local.path)) if chat and chat.photo and File.exist? chat.photo.small.local.path photo = Digest::SHA1.hexdigest(IO.binread(chat.photo.small.local.path)) if chat and chat.photo and File.exist? chat.photo.small.local.path
status ||= user.status if user and user.status status ||= @cache[:status][id] if @cache[:status][id]
case status case status
when nil then show, status = :chat, chat ? chat.title : nil when nil then show, status = :chat, chat ? chat.title : nil
when TD::Types::UserStatus::Online then show, status = nil, "Online" when TD::Types::UserStatus::Online then show, status = nil, "Online"