-
Notifications
You must be signed in to change notification settings - Fork 12
/
manager_update.go
531 lines (507 loc) · 15.3 KB
/
manager_update.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package mtproto
import (
"reflect"
"log"
"fmt"
"github.com/fatih/structs"
)
const (
UPDATE_TYPE_NEW_MESSAGE = "NewMessage"
UPDATE_TYPE_CHANNEL_NEW_MESSAGE = "ChannelNewMessage"
UPDATE_TYPE_READ_CHANNEL_INBOX = "ReadChannelInbox"
UPDATE_TYPE_READ_CHANNEL_OUTBOX = "ReadChannelOutbox"
UPDATE_TYPE_CHANNEL_TOO_LONG = "ChannelTooLong"
UPDATE_TYPE_READ_HISTORY_INBOX = "ReadHistoryInbox"
UPDATE_TYPE_READ_HISTORY_OUTBOX = "ReadHistoryOutbox"
UPDATE_TYPE_USER_PHOTO = "UserPhoto"
UPDATE_TYPE_EDIT_MESSAGE = "EditMessage"
UPDATE_TYPE_EDIT_CHANNEL_MESSAGE = "EditChannelMessage"
UPDATE_TYPE_CONTACT_LINK = "ContactLink"
UPDATE_TYPE_DRAFT_MESSAGE = "DraftMessage"
UPDATE_TYPE_SAVED_GIFS = "SavedGIFs"
UPDATE_TYPE_MESSAGE_ID = "MessageID"
UPDATE_TYPE_DELETE_MESSAGES = "DeleteMessages"
UPDATE_TYPE_CONTACT_REGISTERED = "ContactRegistered"
UPDATE_TYPE_USER_BLOCKED = "UserBlocked"
UPDATE_TYPE_CHANNEL_READ_MESSAGES_CONTENTS = "ChannelReadMessagesContents"
UPDATE_TYPE_USER_TYPING = "UserTyping"
UPDATE_TYPE_CHAT_PARTICIPANT_ADD = "ChatParticipantAdd"
UPDATE_TYPE_CHAT_PARTICIPANT_ADMIN = "ChatParticipantAdmin"
UPDATE_TYPE_CHAT_PARTICIPANT_DELETE = "ChatParticipantDelete"
UPDATE_TYPE_CHAT_USER_TYPING = "ChatUserTyping"
)
const (
UPDATE_DIFFERENCE_EMPTY = "EMPTY"
UPDATE_DIFFERENCE_COMPLETE = "COMPLETE"
UPDATE_DIFFERENCE_SLICE = "SLICE"
UPDATE_DIFFERENCE_TOO_LONG = "TOO_LONG"
)
type IUpdate interface {
GetType(IUpdate) string
GetInt32(IUpdate, string) (int32, bool)
GetString(IUpdate, string) (string, bool)
GetMap(IUpdate) map[string]interface{}
}
type UpdateCore struct{}
func (u UpdateCore) GetInt32(i IUpdate, keyName string) (int32, bool) {
if f, ok := structs.New(i).FieldOk(keyName); !ok {
return 0, false
} else {
return f.Value().(int32), true
}
}
func (u UpdateCore) GetString(i IUpdate, keyName string) (string, bool) {
if f, ok := structs.New(i).FieldOk(keyName); !ok {
return "", false
} else {
return f.Value().(string), true
}
}
func (u UpdateCore) GetType(i IUpdate) string {
return reflect.TypeOf(i).String()
}
func (u UpdateCore) GetMap(i IUpdate) map[string]interface{} {
return structs.Map(i)
}
type UpdateNewMessage struct {
UpdateCore
Pts int32
PtsCount int32
Message Message
}
type UpdateNewChannelMessage struct {
UpdateCore
Pts int32
PtsCount int32
Message Message
}
type UpdateReadChannelInbox struct {
UpdateCore
ChannelID int32
MaxID int32
}
type UpdateReadChannelOutbox struct {
UpdateCore
ChannelID int32
MaxID int32
}
type UpdateChannelTooLong struct {
UpdateCore
ChannelID int32
Pts int32
Flags int32
}
type UpdateReadHistoryInbox struct {
UpdateCore
Pts int32
PtsCount int32
MaxID int32
Peer Peer
}
type UpdateReadHistoryOutbox struct {
UpdateCore
Pts int32
PtsCount int32
MaxID int32
Peer Peer
}
type UpdateContactLink struct {
UpdateCore
UserID int32
MyLink string
ForeignLink string
}
type UpdateContactRegistered struct {
UpdateCore
UserID int32
Date int32
}
type UpdateUserPhoto struct {
UpdateCore
UserID int32
Date int32
ProfilePhoto UserProfilePhoto
Previous bool
}
type UpdateEditChannelMessage struct {
UpdateCore
Pts int32
PtsCount int32
Message Message
}
type UpdateEditMessage struct {
UpdateCore
Pts int32
PtsCount int32
Message Message
}
type UpdatedSaveGIFs struct {
UpdateCore
}
type UpdateMessageID struct {
UpdateCore
MessageID int32
RandomID int64
}
type UpdateDeleteMessages struct {
UpdateCore
Pts int32
PtsCount int32
MessageIDs []int32
}
type UpdateDraftMessage struct {
UpdateCore
Peer Peer
Draft DraftMessage
}
type UpdateUserBlocked struct {
UpdateCore
UserID int32
Blocked bool
}
type UpdateChannelReadMessagesContents struct {
UpdateCore
MessageIDs []int32
ChannelID int32
}
type UpdateUnknown struct {
UpdateCore
Type string
}
type UpdateState struct {
Qts int32
Pts int32
Date int32
Seq int32
UnreadCounts int32
}
type UpdateDifference struct {
Type string
IsSlice bool
Total int32
NewMessages []Message
OtherUpdates []IUpdate
Chats map[int32]Chat
Channels map[int32]Channel
Users map[int32]User
IntermediateState UpdateState
Seq int32
}
type ChannelUpdateDifference struct {
Empty bool
TooLong bool
Flags int32
Final bool
Pts int32
Timeout int32
NewMessages []Message
OtherUpdates []IUpdate
}
// NewUpdateState
// input :
// 1. TL_updates_state
func NewUpdateState(input TL) *UpdateState {
us := new(UpdateState)
switch in := input.(type) {
case TL_updates_state:
us.Qts = in.Qts
us.Pts = in.Pts
us.Seq = in.Seq
us.Date = in.Date
us.UnreadCounts = in.Unread_count
}
return us
}
// NewUpdate
// input :
// 1. TL_updateNewMessage
// 2. TL_updateNewChannelMessage
func NewUpdate(input TL) IUpdate {
switch u := input.(type) {
case TL_updateNewMessage:
return UpdateNewMessage{
Pts: u.Pts,
PtsCount: u.Pts_count,
Message: *NewMessage(u.Message),
}
case TL_updateNewChannelMessage:
return UpdateNewChannelMessage{
Pts: u.Pts,
PtsCount: u.Pts_count,
Message: *NewMessage(u.Message),
}
case TL_updateReadChannelInbox:
return UpdateReadChannelInbox{
ChannelID: u.Channel_id,
MaxID: u.Max_id,
}
case TL_updateReadChannelOutbox:
return UpdateReadChannelOutbox{
ChannelID: u.Channel_id,
MaxID: u.Max_id,
}
case TL_updateChannelTooLong:
return UpdateChannelTooLong{
Pts: u.Pts,
ChannelID: u.Channel_id,
Flags: u.Flags,
}
case TL_updateReadHistoryInbox:
return UpdateReadHistoryInbox{
Pts: u.Pts,
PtsCount: u.Pts_count,
MaxID: u.Max_id,
Peer: *NewPeer(u.Peer),
}
case TL_updateReadHistoryOutbox:
return UpdateReadHistoryOutbox{
Pts: u.Pts,
PtsCount: u.Pts_count,
MaxID: u.Max_id,
Peer: *NewPeer(u.Peer),
}
case TL_updateUserPhoto:
var previous bool
switch u.Previous.(type) {
case TL_boolFalse:
previous = false
case TL_boolTrue:
previous = true
}
return UpdateUserPhoto{
UserID: u.User_id,
Date: u.Date,
ProfilePhoto: *NewUserProfilePhoto(u.Photo),
Previous: previous,
}
case TL_updateContactLink:
return UpdateContactLink{
UserID: u.User_id,
MyLink: reflect.TypeOf(u.My_link).String(),
ForeignLink: reflect.TypeOf(u.Foreign_link).String(),
}
case TL_updateEditChannelMessage:
return UpdateEditChannelMessage{
Pts: u.Pts,
PtsCount: u.Pts_count,
Message: *NewMessage(u.Message),
}
case TL_updateEditMessage:
return UpdateEditMessage{
Pts: u.Pts,
PtsCount: u.Pts_count,
Message: *NewMessage(u.Message),
}
case TL_updateSavedGifs:
return UpdatedSaveGIFs{}
case TL_updateDraftMessage:
return UpdateDraftMessage{
Peer: *NewPeer(u.Peer),
Draft: *NewDraftMessage(u.Draft),
}
case TL_updateMessageID:
return UpdateMessageID{
MessageID: u.Id,
RandomID: u.Random_id,
}
case TL_updateDeleteMessages:
return UpdateDeleteMessages{
Pts: u.Pts,
PtsCount: u.Pts_count,
MessageIDs: u.Messages,
}
case TL_updateContactRegistered:
return UpdateContactRegistered{
Date: u.Date,
UserID: u.User_id,
}
case TL_updateUserBlocked:
var blocked bool
switch u.Blocked.(type) {
case TL_boolTrue:
blocked = true
}
return UpdateUserBlocked{
UserID: u.User_id,
Blocked: blocked,
}
case TL_updateChannelReadMessagesContents:
return UpdateChannelReadMessagesContents{
ChannelID: u.Channel_id,
MessageIDs: u.Messages,
}
default:
return UpdateUnknown{
Type: reflect.TypeOf(u).String(),
}
}
return nil
}
func (m *MTProto) Updates_GetState() *UpdateState {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{
TL_updates_getState{},
resp,
}
x := <-resp
switch x.(type) {
case TL_updates_state:
return NewUpdateState(x)
default:
log.Println(fmt.Sprintf("RPC: %#v", x))
return nil
}
}
func (m *MTProto) Updates_GetDifference(pts, qts, date int32) *UpdateDifference {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{
TL_updates_getDifference{
Flags: 1,
Pts: pts,
Pts_total_limit: 200,
Qts: qts,
Date: date,
},
resp,
}
x := <-resp
updateDifference := new(UpdateDifference)
updateDifference.Users = make(map[int32]User)
updateDifference.Chats = make(map[int32]Chat)
updateDifference.Channels = make(map[int32]Channel)
switch u := x.(type) {
case TL_updates_differenceEmpty:
updateDifference.Type = UPDATE_DIFFERENCE_EMPTY
updateDifference.IsSlice = false
updateDifference.IntermediateState.Date = u.Date
updateDifference.IntermediateState.Seq = u.Seq
return updateDifference
case TL_updates_difference:
updateDifference.Type = UPDATE_DIFFERENCE_COMPLETE
updateDifference.IsSlice = false
updateDifference.IntermediateState = *NewUpdateState(u.State)
for _, m := range u.New_messages {
msg := NewMessage(m)
if msg != nil {
updateDifference.NewMessages = append(updateDifference.NewMessages, *msg)
}
}
for _, ch := range u.Chats {
switch ch.(type) {
case TL_chatFull, TL_chat, TL_chatForbidden, TL_chatEmpty:
newChat := NewChat(ch)
if newChat != nil {
updateDifference.Chats[newChat.ID] = *newChat
}
case TL_channel, TL_channelForbidden, TL_channelFull:
newChannel := NewChannel(ch)
if newChannel != nil {
updateDifference.Channels[newChannel.ID] = *newChannel
}
}
}
for _, u := range u.Users {
newUser := NewUser(u)
if newUser != nil {
updateDifference.Users[newUser.ID] = *newUser
}
}
for _, update := range u.Other_updates {
updateDifference.OtherUpdates = append(updateDifference.OtherUpdates, NewUpdate(update))
}
return updateDifference
case TL_updates_differenceSlice:
updateDifference.Type = UPDATE_DIFFERENCE_SLICE
updateDifference.IsSlice = true
updateDifference.IntermediateState = *NewUpdateState(u.Intermediate_state)
for _, m := range u.New_messages {
msg := NewMessage(m)
if msg != nil {
updateDifference.NewMessages = append(updateDifference.NewMessages, *msg)
}
}
for _, ch := range u.Chats {
switch ch.(type) {
case TL_chatFull, TL_chat, TL_chatForbidden, TL_chatEmpty:
newChat := NewChat(ch)
if newChat != nil {
updateDifference.Chats[newChat.ID] = *newChat
}
case TL_channel, TL_channelForbidden, TL_channelFull:
newChannel := NewChannel(ch)
if newChannel != nil {
updateDifference.Channels[newChannel.ID] = *newChannel
}
}
}
for _, u := range u.Users {
newUser := NewUser(u)
if newUser != nil {
updateDifference.Users[newUser.ID] = *newUser
}
}
for _, update := range u.Other_updates {
updateDifference.OtherUpdates = append(updateDifference.OtherUpdates, NewUpdate(update))
}
return updateDifference
case TL_updates_differenceTooLong:
updateDifference.Type = UPDATE_DIFFERENCE_TOO_LONG
updateDifference.IntermediateState.Pts = u.Pts
return updateDifference
default:
log.Println(fmt.Sprintf("RPC: %#v", x))
return updateDifference
}
}
func (m *MTProto) Updates_GetChannelDifference(inputChannel TL, pts, limit int32) *ChannelUpdateDifference {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{
TL_updates_getChannelDifference{
Channel: inputChannel,
Filter: TL_channelMessagesFilterEmpty{},
Pts: pts,
Limit: limit,
},
resp,
}
x := <-resp
updateDifference := new(ChannelUpdateDifference)
switch u := x.(type) {
case TL_updates_channelDifferenceEmpty:
updateDifference.Empty = true
updateDifference.Pts = u.Pts
updateDifference.Flags = u.Flags
updateDifference.Timeout = u.Timeout
case TL_updates_channelDifference:
updateDifference.Pts = u.Pts
updateDifference.Flags = u.Flags
updateDifference.Timeout = u.Timeout
updateDifference.NewMessages = []Message{}
updateDifference.OtherUpdates = []IUpdate{}
for _, m := range u.New_messages {
msg := NewMessage(m)
if msg != nil {
updateDifference.NewMessages = append(updateDifference.NewMessages, *msg)
}
}
for _, u := range u.Other_updates {
updateDifference.OtherUpdates = append(updateDifference.OtherUpdates, NewUpdate(u))
}
case TL_updates_channelDifferenceTooLong:
updateDifference.TooLong = true
updateDifference.Pts = u.Pts
updateDifference.Flags = u.Flags
updateDifference.Timeout = u.Timeout
updateDifference.NewMessages = []Message{}
updateDifference.OtherUpdates = []IUpdate{}
for _, m := range u.Messages {
msg := NewMessage(m)
if msg != nil {
updateDifference.NewMessages = append(updateDifference.NewMessages, *msg)
}
}
case TL_rpc_error:
log.Println("Update_GetChannelDiffrence::", u.error_code, u.error_message)
}
return updateDifference
}