diff options
Diffstat (limited to 'telegram/utils.go')
-rw-r--r-- | telegram/utils.go | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/telegram/utils.go b/telegram/utils.go index 4de55c6..fad1bc0 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -94,14 +94,27 @@ func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *cli return chat, user, nil } -func (c *Client) userStatusToText(status client.UserStatus) (string, string) { +func (c *Client) userStatusToText(status client.UserStatus, chatID int64) (string, string) { var show, textStatus string switch status.UserStatusType() { case client.TypeUserStatusOnline: + onlineStatus, _ := status.(*client.UserStatusOnline) + + c.DelayedStatusesLock.Lock() + c.DelayedStatuses[chatID] = &DelayedStatus{ + TimestampOnline: time.Now().Unix(), + TimestampExpired: int64(onlineStatus.Expires), + } + c.DelayedStatusesLock.Unlock() + textStatus = "Online" case client.TypeUserStatusRecently: show, textStatus = "dnd", "Last seen recently" + + c.DelayedStatusesLock.Lock() + delete(c.DelayedStatuses, chatID) + c.DelayedStatusesLock.Unlock() case client.TypeUserStatusLastWeek: show, textStatus = "unavailable", "Last seen last week" case client.TypeUserStatusLastMonth: @@ -111,20 +124,30 @@ func (c *Client) userStatusToText(status client.UserStatus) (string, string) { case client.TypeUserStatusOffline: offlineStatus, _ := status.(*client.UserStatusOffline) // this will stop working in 2038 O\ - elapsed := time.Now().Unix() - int64(offlineStatus.WasOnline) + wasOnline := int64(offlineStatus.WasOnline) + elapsed := time.Now().Unix() - wasOnline if elapsed < 3600 { show = "away" } else { show = "xa" } - textStatus = time.Unix(int64(offlineStatus.WasOnline), 0). - In(c.Session.TimezoneToLocation()). - Format("Last seen at 15:04 02/01/2006") + textStatus = c.LastSeenStatus(wasOnline) + + c.DelayedStatusesLock.Lock() + delete(c.DelayedStatuses, chatID) + c.DelayedStatusesLock.Unlock() } return show, textStatus } +// LastSeenStatus formats a timestamp to a "Last seen at" string +func (c *Client) LastSeenStatus(timestamp int64) string { + return time.Unix(int64(timestamp), 0). + In(c.Session.TimezoneToLocation()). + Format("Last seen at 15:04 02/01/2006") +} + // ProcessStatusUpdate sets contact status func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, args ...args.V) error { if !c.Online() { @@ -161,7 +184,7 @@ func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, a if status == "" { if user != nil { - show, status = c.userStatusToText(user.Status) + show, status = c.userStatusToText(user.Status, chatID) } else { show, status = "chat", chat.Title } |