From 32036e3f90fc1c41df430d9d811fa3d04844b349 Mon Sep 17 00:00:00 2001
From: Bohdan Horbeshko <bodqhrohro@gmail.com>
Date: Mon, 7 Feb 2022 11:21:43 -0500
Subject: Fix status update for users who blacklisted you or were online long
 time ago

---
 telegram/handlers.go |  8 ++++----
 telegram/utils.go    | 36 ++++++++++++++++++++++++------------
 2 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/telegram/handlers.go b/telegram/handlers.go
index face5dd..0eedc66 100644
--- a/telegram/handlers.go
+++ b/telegram/handlers.go
@@ -133,14 +133,14 @@ func (c *Client) updateHandler() {
 // new user discovered
 func (c *Client) updateUser(update *client.UpdateUser) {
 	c.cache.SetUser(update.User.Id, update.User)
-	show, status := c.userStatusToText(update.User.Status, update.User.Id)
-	go c.ProcessStatusUpdate(update.User.Id, status, show)
+	show, status, presenceType := c.userStatusToText(update.User.Status, update.User.Id)
+	go c.ProcessStatusUpdate(update.User.Id, status, show, gateway.SPType(presenceType))
 }
 
 // user status changed
 func (c *Client) updateUserStatus(update *client.UpdateUserStatus) {
-	show, status := c.userStatusToText(update.Status, update.UserId)
-	go c.ProcessStatusUpdate(update.UserId, status, show, gateway.SPImmed(false))
+	show, status, presenceType := c.userStatusToText(update.Status, update.UserId)
+	go c.ProcessStatusUpdate(update.UserId, status, show, gateway.SPImmed(false), gateway.SPType(presenceType))
 }
 
 // new chat discovered
diff --git a/telegram/utils.go b/telegram/utils.go
index d6b112d..e2fbe3b 100644
--- a/telegram/utils.go
+++ b/telegram/utils.go
@@ -106,8 +106,8 @@ func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *cli
 	return chat, user, nil
 }
 
-func (c *Client) userStatusToText(status client.UserStatus, chatID int64) (string, string) {
-	var show, textStatus string
+func (c *Client) userStatusToText(status client.UserStatus, chatID int64) (string, string, string) {
+	var show, textStatus, presenceType string
 
 	switch status.UserStatusType() {
 	case client.TypeUserStatusOnline:
@@ -128,11 +128,11 @@ func (c *Client) userStatusToText(status client.UserStatus, chatID int64) (strin
 		delete(c.DelayedStatuses, chatID)
 		c.DelayedStatusesLock.Unlock()
 	case client.TypeUserStatusLastWeek:
-		show, textStatus = "unavailable", "Last seen last week"
+		show, textStatus = "xa", "Last seen last week"
 	case client.TypeUserStatusLastMonth:
-		show, textStatus = "unavailable", "Last seen last month"
+		show, textStatus = "xa", "Last seen last month"
 	case client.TypeUserStatusEmpty:
-		show, textStatus = "unavailable", "Last seen a long time ago"
+		presenceType, textStatus = "unavailable", "Last seen a long time ago"
 	case client.TypeUserStatusOffline:
 		offlineStatus, _ := status.(*client.UserStatusOffline)
 		// this will stop working in 2038 O\
@@ -150,7 +150,7 @@ func (c *Client) userStatusToText(status client.UserStatus, chatID int64) (strin
 		c.DelayedStatusesLock.Unlock()
 	}
 
-	return show, textStatus
+	return show, textStatus, presenceType
 }
 
 // LastSeenStatus formats a timestamp to a "Last seen at" string
@@ -161,7 +161,7 @@ func (c *Client) LastSeenStatus(timestamp int64) string {
 }
 
 // ProcessStatusUpdate sets contact status
-func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, args ...args.V) error {
+func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, oldArgs ...args.V) error {
 	if !c.Online() {
 		return nil
 	}
@@ -194,12 +194,17 @@ func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, a
 		}
 	}
 
+	var presenceType string
+	if gateway.SPType.IsSet(oldArgs) {
+		presenceType = gateway.SPType.Get(oldArgs)
+	}
+
 	cachedStatus, ok := c.cache.GetStatus(chatID)
 	if status == "" {
 		if ok {
 			show, status = cachedStatus.XMPP, cachedStatus.Description
 		} else if user != nil && user.Status != nil {
-			show, status = c.userStatusToText(user.Status, chatID)
+			show, status, presenceType = c.userStatusToText(user.Status, chatID)
 		} else {
 			show, status = "chat", chat.Title
 		}
@@ -207,14 +212,21 @@ func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, a
 
 	c.cache.SetStatus(chatID, show, status)
 
-	return gateway.SendPresence(
-		c.xmpp,
-		c.jid,
+	newArgs := []args.V {
 		gateway.SPFrom(strconv.FormatInt(chatID, 10)),
 		gateway.SPShow(show),
 		gateway.SPStatus(status),
 		gateway.SPPhoto(photo),
-		gateway.SPImmed(gateway.SPImmed.Get(args)),
+		gateway.SPImmed(gateway.SPImmed.Get(oldArgs)),
+	}
+	if presenceType != "" {
+		newArgs = append(newArgs, gateway.SPType(presenceType))
+	}
+
+	return gateway.SendPresence(
+		c.xmpp,
+		c.jid,
+		newArgs...,
 	)
 }
 
-- 
cgit v1.2.3