-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
upconv.go
115 lines (104 loc) · 2.46 KB
/
upconv.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package upconv
import "github.com/gotd/td/tg"
func convertOptional(msg *tg.Message, i tg.UpdatesClass) {
if u, ok := i.(interface {
GetFwdFrom() (tg.MessageFwdHeader, bool)
}); ok {
if v, ok := u.GetFwdFrom(); ok {
msg.SetFwdFrom(v)
}
}
if u, ok := i.(interface{ GetViaBotID() (int64, bool) }); ok {
if v, ok := u.GetViaBotID(); ok {
msg.SetViaBotID(v)
}
}
if u, ok := i.(interface {
GetReplyTo() (tg.MessageReplyHeader, bool)
}); ok {
if v, ok := u.GetReplyTo(); ok {
msg.SetReplyTo(&v)
}
}
if u, ok := i.(interface {
GetEntities() ([]tg.MessageEntityClass, bool)
}); ok {
if v, ok := u.GetEntities(); ok {
msg.SetEntities(v)
}
}
if u, ok := i.(interface {
GetMedia() (tg.MessageMediaClass, bool)
}); ok {
if v, ok := u.GetMedia(); ok {
msg.SetMedia(v)
}
}
}
// ShortMessage converts UpdateShortMessage to UpdateShort.
func ShortMessage(u *tg.UpdateShortMessage) *tg.UpdateShort {
msg := &tg.Message{
ID: u.ID,
PeerID: &tg.PeerUser{UserID: u.UserID},
Message: u.Message,
Date: u.Date,
}
// Optional fields should set by SetXXX(), so GetXXX and Flags.Has()
// can return the right values even we hav't call .Encode()
msg.SetOut(u.Out)
msg.SetMentioned(u.Mentioned)
msg.SetMediaUnread(u.MediaUnread)
msg.SetSilent(u.Silent)
if !u.Out {
msg.SetFromID(&tg.PeerUser{UserID: u.UserID})
}
convertOptional(msg, u)
return &tg.UpdateShort{
Update: &tg.UpdateNewMessage{
Message: msg,
Pts: u.Pts,
PtsCount: u.PtsCount,
},
Date: u.Date,
}
}
// ShortChatMessage converts UpdateShortChatMessage to UpdateShort.
func ShortChatMessage(u *tg.UpdateShortChatMessage) *tg.UpdateShort {
msg := &tg.Message{
ID: u.ID,
PeerID: &tg.PeerChat{ChatID: u.ChatID},
Message: u.Message,
Date: u.Date,
}
msg.SetFromID(&tg.PeerUser{UserID: u.FromID})
msg.SetOut(u.Out)
msg.SetMentioned(u.Mentioned)
msg.SetMediaUnread(u.MediaUnread)
msg.SetSilent(u.Silent)
convertOptional(msg, u)
return &tg.UpdateShort{
Update: &tg.UpdateNewMessage{
Message: msg,
Pts: u.Pts,
PtsCount: u.PtsCount,
},
Date: u.Date,
}
}
// ShortSentMessage converts UpdateShortSentMessage to UpdateShort.
func ShortSentMessage(u *tg.UpdateShortSentMessage) *tg.UpdateShort {
msg := &tg.Message{
ID: u.ID,
Date: u.Date,
}
msg.SetOut(u.Out)
convertOptional(msg, u)
return &tg.UpdateShort{
Update: &tg.UpdateNewMessage{
Message: msg,
Pts: u.Pts,
PtsCount: u.PtsCount,
},
Date: u.Date,
}
}