Fix status update for users who blacklisted you or were online long time ago

This commit is contained in:
Bohdan Horbeshko 2022-02-07 11:21:43 -05:00
parent 5a48600b7a
commit 32036e3f90
2 changed files with 28 additions and 16 deletions

View file

@ -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

View file

@ -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...,
)
}