aboutsummaryrefslogtreecommitdiff
path: root/telegram/handlers.go
blob: 348fd9a7bbfa372a787709e6251f5dcd97378712 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package telegram

import (
	"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)
			default:
				// log only handled types
				continue
			}

			log.Debugf("%#v", update)
		}
	}
}

func (c *Client) updateUser(update *client.UpdateUser) {
	cache.users[update.User.Id] = update.User
	c.processStatusUpdate(update.User.Id, &update.User.Status)
}

func (c *Client) updateUserStatus(update *client.UpdateUserStatus) {
	c.processStatusUpdate(update.UserId, &update.Status, gateway.SPImmed(false))
}