You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
telegabber/telegram/handlers.go

100 lines
2.3 KiB

package telegram
import (
"strconv"
"dev.narayana.im/narayana/telegabber/xmpp/gateway"
log "github.com/sirupsen/logrus"
"github.com/zelenin/go-tdlib/client"
)
func uhOh() {
log.Fatal("Update type mismatch")
}
func (c *Client) updateHandler() {
listener := c.client.GetListener()
defer listener.Close()
for update := range listener.Updates {
if update.GetClass() == client.ClassUpdate {
switch update.GetType() {
case client.TypeUpdateUser:
typedUpdate, ok := update.(*client.UpdateUser)
if !ok {
uhOh()
}
c.updateUser(typedUpdate)
case client.TypeUpdateUserStatus:
typedUpdate, ok := update.(*client.UpdateUserStatus)
if !ok {
uhOh()
}
c.updateUserStatus(typedUpdate)
case client.TypeUpdateNewChat:
typedUpdate, ok := update.(*client.UpdateNewChat)
if !ok {
uhOh()
}
c.updateNewChat(typedUpdate)
default:
// log only handled types
continue
}
log.Debugf("%#v", update)
}
}
}
func (c *Client) updateUser(update *client.UpdateUser) {
cache.users[update.User.Id] = update.User
show, status := userStatusToText(update.User.Status)
c.processStatusUpdate(update.User.Id, status, show)
}
func (c *Client) updateUserStatus(update *client.UpdateUserStatus) {
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")
}
}