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/handlers.go | |
parent | 6332ea6d28ff850b46a2f7eff592c92ff2c38232 (diff) |
Handle updates of newchat
Diffstat (limited to 'telegram/handlers.go')
-rw-r--r-- | telegram/handlers.go | 53 |
1 files changed, 51 insertions, 2 deletions
diff --git a/telegram/handlers.go b/telegram/handlers.go index 348fd9a..5ff5dad 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -1,6 +1,8 @@ package telegram import ( + "strconv" + "dev.narayana.im/narayana/telegabber/xmpp/gateway" log "github.com/sirupsen/logrus" @@ -30,6 +32,12 @@ func (c *Client) updateHandler() { uhOh() } c.updateUserStatus(typedUpdate) + case client.TypeUpdateNewChat: + typedUpdate, ok := update.(*client.UpdateNewChat) + if !ok { + uhOh() + } + c.updateNewChat(typedUpdate) default: // log only handled types continue @@ -42,9 +50,50 @@ func (c *Client) updateHandler() { func (c *Client) updateUser(update *client.UpdateUser) { cache.users[update.User.Id] = update.User - c.processStatusUpdate(update.User.Id, &update.User.Status) + show, status := userStatusToText(update.User.Status) + c.processStatusUpdate(update.User.Id, status, show) } func (c *Client) updateUserStatus(update *client.UpdateUserStatus) { - c.processStatusUpdate(update.UserId, &update.Status, gateway.SPImmed(false)) + show, status := userStatusToText(update.Status) + c.processStatusUpdate(update.UserId, status, show, gateway.SPImmed(false)) +} + +func (c *Client) updateNewChat(update *client.UpdateNewChat) { + if update.Chat != nil && update.Chat.Photo != nil && update.Chat.Photo.Small != nil { + _, err := c.client.DownloadFile(&client.DownloadFileRequest{ + FileId: update.Chat.Photo.Small.Id, + Priority: 32, + Synchronous: true, + }) + + if err != nil { + log.Error("Failed to download the chat photo") + } + } + + cache.chats[update.Chat.Id] = update.Chat + + var isChannel = false + if update.Chat.Type.ChatTypeType() == client.TypeChatTypeSupergroup { + typeSupergroup, ok := update.Chat.Type.(*client.ChatTypeSupergroup) + if !ok { + uhOh() + } + isChannel = typeSupergroup.IsChannel + } + + if !(isChannel && update.Chat.LastReadInboxMessageId == 0) { + gateway.SendPresence( + c.xmpp, + c.jid, + gateway.SPFrom(strconv.Itoa(int(update.Chat.Id))), + gateway.SPType("subscribe"), + gateway.SPNickname(update.Chat.Title), + ) + } + + if update.Chat.Id < 0 { + c.processStatusUpdate(int32(update.Chat.Id), update.Chat.Title, "chat") + } } |