/
classifier.go
98 lines (85 loc) · 2.6 KB
/
classifier.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package updates
import (
"github.com/go-faster/errors"
"github.com/gotd/td/tg"
)
func isCommonPtsUpdate(u tg.UpdateClass) (pts, ptsCount int, ok bool) {
switch u := u.(type) {
case *tg.UpdateNewMessage:
return u.Pts, u.PtsCount, true
case *tg.UpdateDeleteMessages:
return u.Pts, u.PtsCount, true
case *tg.UpdateReadHistoryInbox:
return u.Pts, u.PtsCount, true
case *tg.UpdateReadHistoryOutbox:
return u.Pts, u.PtsCount, true
case *tg.UpdateWebPage:
return u.Pts, u.PtsCount, true
case *tg.UpdateReadMessagesContents:
return u.Pts, u.PtsCount, true
case *tg.UpdateEditMessage:
return u.Pts, u.PtsCount, true
case *tg.UpdateFolderPeers:
return u.Pts, u.PtsCount, true
case *tg.UpdatePinnedMessages:
return u.Pts, u.PtsCount, true
}
return
}
func isCommonQtsUpdate(u tg.UpdateClass) (qts int, ok bool) {
switch u := u.(type) {
case *tg.UpdateNewEncryptedMessage:
return u.Qts, true
case *tg.UpdateChatParticipant:
return u.Qts, true
case *tg.UpdateBotStopped:
return u.Qts, true
case *tg.UpdateChannelParticipant:
return u.Qts, true
}
return
}
func isChannelPtsUpdate(u tg.UpdateClass) (channelID int64, pts, ptsCount int, ok bool, err error) {
switch u := u.(type) {
case *tg.UpdateNewChannelMessage:
channelID, err = extractChannelID(u.Message)
return channelID, u.Pts, u.PtsCount, true, err
case *tg.UpdateReadChannelInbox:
return u.ChannelID, u.Pts, 0, true, nil
case *tg.UpdateDeleteChannelMessages:
return u.ChannelID, u.Pts, u.PtsCount, true, nil
case *tg.UpdateEditChannelMessage:
channelID, err = extractChannelID(u.Message)
return channelID, u.Pts, u.PtsCount, true, err
case *tg.UpdateChannelWebPage:
return u.ChannelID, u.Pts, u.PtsCount, true, nil
case *tg.UpdatePinnedChannelMessages:
return u.ChannelID, u.Pts, u.PtsCount, true, nil
}
return
}
func extractChannelID(msg tg.MessageClass) (int64, error) {
switch msg := msg.(type) {
case *tg.Message:
if c, ok := msg.PeerID.(*tg.PeerChannel); ok {
return c.ChannelID, nil
}
return 0, errors.Errorf("unexpected tg.Message peer type: %T", msg.PeerID)
case *tg.MessageEmpty:
peer, ok := msg.GetPeerID()
if !ok {
return 0, errors.New("tg.MessageEmpty have no peerID field")
}
if c, ok := peer.(*tg.PeerChannel); ok {
return c.ChannelID, nil
}
return 0, errors.Errorf("unexpected tg.MessageEmpty peer type: %T", peer)
case *tg.MessageService:
if c, ok := msg.PeerID.(*tg.PeerChannel); ok {
return c.ChannelID, nil
}
return 0, errors.Errorf("unexpected tg.MessageService peer type: %T", msg.PeerID)
default:
return 0, errors.Errorf("unexpected tg.MessageClass type: %T", msg)
}
}