/
messages.go
212 lines (178 loc) · 4.96 KB
/
messages.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package telegramapi
import (
"github.com/andreyvit/telegramapi/mtproto"
"log"
"time"
)
func (c *Conn) LoadChats(contacts *ContactList) error {
log.Printf("Loading list of chats...")
r, err := c.Send(&mtproto.TLMessagesGetDialogs{
Flags: 0,
Limit: 1000,
OffsetPeer: &mtproto.TLInputPeerEmpty{},
})
if err != nil {
return err
}
switch r := r.(type) {
case *mtproto.TLMessagesDialogs:
c.updateChatsLocked(contacts, r.Dialogs, r.Messages, r.Chats, r.Users)
return nil
case *mtproto.TLMessagesDialogsSlice:
c.updateChatsLocked(contacts, r.Dialogs, r.Messages, r.Chats, r.Users)
return nil
default:
return c.HandleUnknownReply(r)
}
}
func (c *Conn) updateChatsLocked(contacts *ContactList, dialogs []*mtproto.TLDialog, apimessages []mtproto.TLMessageType, chats []mtproto.TLChatType, users []mtproto.TLUserType) {
c.stateMut.Lock()
defer c.stateMut.Unlock()
accessHashByUserID := make(map[int]uint64)
c.updateUsers(contacts, users, accessHashByUserID)
// for _, apimsg := range apimessages {
// msg := c.updateMessage(contacts, messages, apimsg)
// if msg != nil {
// msgs = append(msgs, msg)
// }
// }
contacts.Chats = contacts.Chats[:0]
for _, dialog := range dialogs {
var chat *Chat
if upeer, ok := dialog.Peer.(*mtproto.TLPeerUser); ok {
if user := contacts.Users[upeer.UserID]; user != nil {
chat = contacts.UserChats[user.ID]
if chat == nil {
chat = &Chat{
Type: UserChat,
ID: user.ID,
User: user,
Messages: newMessageList(),
}
contacts.UserChats[user.ID] = chat
}
chat.AccessHash = accessHashByUserID[user.ID]
chat.Username = user.Username
if user == contacts.Self {
contacts.SelfChat = chat
}
// TODO: dialog.TopMessage
}
}
if chat != nil {
contacts.Chats = append(contacts.Chats, chat)
}
}
}
func (c *Conn) LoadHistory(contacts *ContactList, chat *Chat, limit int) error {
more := true
var count int
log.Printf("Loading history of “%s”...", chat.TitleOrName())
for more && (limit == 0 || count < limit) {
r, err := c.Send(&mtproto.TLMessagesGetHistory{
Peer: chat.inputPeer(),
Limit: 10000,
OffsetID: chat.Messages.MinKnownID,
})
if err != nil {
return err
}
switch r := r.(type) {
case *mtproto.TLMessagesMessages:
c.updateHistoryLocked(contacts, chat, r.Messages, r.Chats, r.Users)
more = false
count += len(r.Messages)
case *mtproto.TLMessagesMessagesSlice:
c.updateHistoryLocked(contacts, chat, r.Messages, r.Chats, r.Users)
more = len(r.Messages) > 0
count += len(r.Messages)
default:
return c.HandleUnknownReply(r)
}
if more {
log.Printf("Loaded %d messages...", count)
}
time.Sleep(1 * time.Second)
}
log.Printf("Done. Loaded %d messages.", count)
return nil
}
func (c *Conn) updateHistoryLocked(contacts *ContactList, chat *Chat, apimessages []mtproto.TLMessageType, chats []mtproto.TLChatType, users []mtproto.TLUserType) {
c.stateMut.Lock()
defer c.stateMut.Unlock()
c.updateUsers(contacts, users, nil)
messages := chat.Messages
var msgs []*Message
for _, apimsg := range apimessages {
msg := c.updateMessage(contacts, messages, apimsg)
if msg != nil {
msgs = append(msgs, msg)
}
}
for i, j := 0, len(msgs)-1; i < j; {
msgs[i], msgs[j] = msgs[j], msgs[i]
i++
j--
}
messages.Messages = append(msgs, messages.Messages...)
}
func (c *Conn) updateMessage(contacts *ContactList, messages *MessageList, apimsg mtproto.TLMessageType) *Message {
switch apimsg := apimsg.(type) {
case *mtproto.TLMessage:
messages.foundID(apimsg.ID)
if apimsg.Message == "" {
return nil
}
fromu := contacts.Users[apimsg.FromID]
if fromu == nil {
return nil
}
msg := messages.MessagesByID[apimsg.ID]
if msg == nil {
msg = &Message{
ID: apimsg.ID,
}
messages.MessagesByID[apimsg.ID] = msg
}
msg.Type = NormalMessage
msg.Date = makeDate(apimsg.Date)
msg.EditDate = makeDate(apimsg.EditDate)
msg.From = fromu
msg.ReplyToID = apimsg.ReplyToMsgID
msg.Text = apimsg.Message
if apimsg.FwdFrom != nil {
msg.FwdFrom = contacts.Users[apimsg.FwdFrom.FromID]
msg.FwdDate = makeDate(apimsg.FwdFrom.Date)
}
return msg
case *mtproto.TLMessageService:
messages.foundID(apimsg.ID)
return nil
case *mtproto.TLMessageEmpty:
messages.foundID(apimsg.ID)
return nil
default:
return nil
}
}
func (c *Conn) updateUsers(contacts *ContactList, users []mtproto.TLUserType, accessHashByUserID map[int]uint64) {
selfUserID := c.state.UserID
for _, apiuser := range users {
if apiuser, ok := apiuser.(*mtproto.TLUser); ok {
user := contacts.Users[apiuser.ID]
if user == nil {
user = &User{ID: apiuser.ID}
contacts.Users[apiuser.ID] = user
}
user.Username = apiuser.Username
user.FirstName = apiuser.FirstName
user.LastName = apiuser.LastName
if accessHashByUserID != nil {
accessHashByUserID[apiuser.ID] = apiuser.AccessHash
}
if user.ID == selfUserID {
contacts.Self = user
}
}
}
}