diff options
author | bodqhrohro <bodqhrohro@gmail.com> | 2019-11-30 03:41:22 +0300 |
---|---|---|
committer | bodqhrohro <bodqhrohro@gmail.com> | 2019-11-30 03:41:22 +0300 |
commit | 7030ec0f1ba8eb97da2b17a19594dd2e4c568e1c (patch) | |
tree | a3cf11ec4cdc52844688db6ab5e3e443004d5b55 /telegram/utils.go | |
parent | 6332ea6d28ff850b46a2f7eff592c92ff2c38232 (diff) |
Handle updates of newchat
Diffstat (limited to 'telegram/utils.go')
-rw-r--r-- | telegram/utils.go | 78 |
1 files changed, 40 insertions, 38 deletions
diff --git a/telegram/utils.go b/telegram/utils.go index 7fb78d9..172bc38 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -80,7 +80,40 @@ func (c *Client) GetContactByID(id int32, chat *client.Chat) (*client.Chat, *cli return chat, user, nil } -func (c *Client) processStatusUpdate(chatID int32, status *client.UserStatus, args ...args.V) error { +func userStatusToText(status client.UserStatus) (string, string) { + var show, textStatus string + + switch status.UserStatusType() { + case client.TypeUserStatusOnline: + textStatus = "Online" + case client.TypeUserStatusRecently: + show, textStatus = "dnd", "Last seen recently" + case client.TypeUserStatusLastWeek: + show, textStatus = "unavailable", "Last seen last week" + case client.TypeUserStatusLastMonth: + show, textStatus = "unavailable", "Last seen last month" + case client.TypeUserStatusEmpty: + show, textStatus = "unavailable", "Last seen a long time ago" + case client.TypeUserStatusOffline: + offlineStatus, ok := status.(*client.UserStatusOffline) + if !ok { + log.Fatal("Status type changed before conversion!") + } + // this will stop working in 2038 O\ + elapsed := time.Now().Unix() - int64(offlineStatus.WasOnline) + if elapsed < 3600 { + show = "away" + } else { + show = "xa" + } + // TODO: timezone + textStatus = time.Unix(int64(offlineStatus.WasOnline), 0).Format("Last seen at 15:03 02/01/2006") + } + + return show, textStatus +} + +func (c *Client) processStatusUpdate(chatID int32, status string, show string, args ...args.V) error { if !c.online { return nil } @@ -113,42 +146,11 @@ func (c *Client) processStatusUpdate(chatID int32, status *client.UserStatus, ar } } - if status == nil && user != nil { - status = &user.Status - } - - var show, textStatus string - if status == nil { - show = "chat" - if chat.Title != "" { - textStatus = chat.Title - } - } else { - switch (*status).UserStatusType() { - case client.TypeUserStatusOnline: - textStatus = "Online" - case client.TypeUserStatusRecently: - show, textStatus = "dnd", "Last seen recently" - case client.TypeUserStatusLastWeek: - show, textStatus = "unavailable", "Last seen last week" - case client.TypeUserStatusLastMonth: - show, textStatus = "unavailable", "Last seen last month" - case client.TypeUserStatusEmpty: - show, textStatus = "unavailable", "Last seen a long time ago" - case client.TypeUserStatusOffline: - offlineStatus, ok := (*status).(*client.UserStatusOffline) - if !ok { - log.Fatal("Status type changed before conversion!") - } - // this will stop working in 2038 O\ - elapsed := time.Now().Unix() - int64(offlineStatus.WasOnline) - if elapsed < 3600 { - show = "away" - } else { - show = "xa" - } - // TODO: timezone - textStatus = time.Unix(int64(offlineStatus.WasOnline), 0).Format("Last seen at 15:03 02/01/2006") + if status == "" { + if user != nil { + show, status = userStatusToText(user.Status) + } else { + show, status = "chat", chat.Title } } @@ -157,7 +159,7 @@ func (c *Client) processStatusUpdate(chatID int32, status *client.UserStatus, ar c.jid, gateway.SPFrom(strconv.Itoa(int(chatID))), gateway.SPShow(show), - gateway.SPStatus(textStatus), + gateway.SPStatus(status), gateway.SPPhoto(photo), gateway.SPImmed(gateway.SPImmed.Get(args)), ) |