From 9377d7a15538a6c0af97937806ecd55eb112beb3 Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Sun, 6 Aug 2023 20:04:49 -0400 Subject: [PATCH] Save/read unavailable presence type in cache --- Makefile | 2 +- telegabber.go | 2 +- telegram/cache/cache.go | 10 ++++++++++ telegram/utils.go | 40 ++++++++++++++++++++++++++++++++++++---- xmpp/handlers.go | 14 +++++++++++--- 5 files changed, 59 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 7857e17..33bedad 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ COMMIT := $(shell git rev-parse --short HEAD) TD_COMMIT := "8517026415e75a8eec567774072cbbbbb52376c1" -VERSION := "v1.7.4" +VERSION := "v1.7.5" MAKEOPTS := "-j4" all: diff --git a/telegabber.go b/telegabber.go index df13dd6..37d5890 100644 --- a/telegabber.go +++ b/telegabber.go @@ -15,7 +15,7 @@ import ( goxmpp "gosrc.io/xmpp" ) -var version string = "1.7.4" +var version string = "1.7.5" var commit string var sm *goxmpp.StreamManager diff --git a/telegram/cache/cache.go b/telegram/cache/cache.go index 3d9608d..6847d3e 100644 --- a/telegram/cache/cache.go +++ b/telegram/cache/cache.go @@ -133,3 +133,13 @@ func (cache *Cache) SetStatus(id int64, show string, status string) { Description: status, } } + +// Destruct splits a cached status into show, description and type +func (status *Status) Destruct() (show, description, typ string) { + show, description = status.XMPP, status.Description + if show == "unavailable" { + typ = show + show = "" + } + return +} diff --git a/telegram/utils.go b/telegram/utils.go index 4caf88c..2578671 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -243,15 +243,33 @@ func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, o cachedStatus, ok := c.cache.GetStatus(chatID) if status == "" { if ok { - show, status = cachedStatus.XMPP, cachedStatus.Description + var typ string + show, status, typ = cachedStatus.Destruct() + if presenceType == "" { + presenceType = typ + } + log.WithFields(log.Fields{ + "show": show, + "status": status, + "presenceType": presenceType, + }).Debug("Cached status") } else if user != nil && user.Status != nil { show, status, presenceType = c.userStatusToText(user.Status, chatID) + log.WithFields(log.Fields{ + "show": show, + "status": status, + "presenceType": presenceType, + }).Debug("Status to text") } else { show, status = "chat", chat.Title } } - c.cache.SetStatus(chatID, show, status) + cacheShow := show + if presenceType == "unavailable" { + cacheShow = presenceType + } + c.cache.SetStatus(chatID, cacheShow, status) newArgs := []args.V{ gateway.SPFrom(strconv.FormatInt(chatID, 10)), @@ -1366,12 +1384,26 @@ func (c *Client) UpdateChatNicknames() { for _, id := range c.cache.ChatsKeys() { chat, ok := c.cache.GetChat(id) if ok { + newArgs := []args.V{ + gateway.SPFrom(strconv.FormatInt(id, 10)), + gateway.SPNickname(chat.Title), + } + + cachedStatus, ok := c.cache.GetStatus(id) + if ok { + show, status, typ := cachedStatus.Destruct() + newArgs = append(newArgs, gateway.SPShow(show), gateway.SPStatus(status)) + if typ != "" { + newArgs = append(newArgs, gateway.SPType(typ)) + } + } + gateway.SendPresence( c.xmpp, c.jid, - gateway.SPFrom(strconv.FormatInt(id, 10)), - gateway.SPNickname(chat.Title), + newArgs..., ) + gateway.SetNickname(c.jid, strconv.FormatInt(id, 10), chat.Title, c.xmpp) } } diff --git a/xmpp/handlers.go b/xmpp/handlers.go index 6679a72..e85dfc9 100644 --- a/xmpp/handlers.go +++ b/xmpp/handlers.go @@ -15,6 +15,7 @@ import ( "dev.narayana.im/narayana/telegabber/xmpp/gateway" log "github.com/sirupsen/logrus" + "github.com/soheilhy/args" "gosrc.io/xmpp" "gosrc.io/xmpp/stanza" ) @@ -349,11 +350,18 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) { log.Error(errors.Wrap(err, "TDlib connection failure")) } else { for status := range session.StatusesRange() { + show, description, typ := status.Destruct() + newArgs := []args.V{ + gateway.SPImmed(false), + } + if typ != "" { + newArgs = append(newArgs, gateway.SPType(typ)) + } go session.ProcessStatusUpdate( status.ID, - status.Description, - status.XMPP, - gateway.SPImmed(false), + description, + show, + newArgs..., ) } session.UpdateChatNicknames()