-
Notifications
You must be signed in to change notification settings - Fork 12
/
manager_dialog.go
160 lines (150 loc) · 4.6 KB
/
manager_dialog.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
package mtproto
type Dialog struct {
Type string
Pts int32
PeerID int32
PeerAccessHash int64
TopMessageID int32
UnreadCount int32
NotifySettings interface{}
}
// NewDialog returns a pointer to Dialog struct
// input : TL_dialog
func NewDialog(input TL) (d *Dialog) {
d = new(Dialog)
if dialog, ok := input.(TL_dialog); ok {
switch pt := dialog.Peer.(type) {
case TL_peerChat:
d.Type = DIALOG_TYPE_CHAT
d.PeerID = pt.Chat_id
case TL_peerUser:
d.Type = DIALOG_TYPE_USER
d.PeerID = pt.User_id
case TL_peerChannel:
d.Type = DIALOG_TYPE_CHANNEL
d.PeerID = pt.Channel_id
default:
return nil
}
d.Pts = dialog.Pts
d.TopMessageID = dialog.Top_message
d.UnreadCount = dialog.Unread_count
return d
}
return nil
}
// GetInputPeer returns either of the struct below:
// 1. TL_inputPeerChat
// 2. TL_inputPeerChannel
// 3. TL_inputPeerUser
func (d *Dialog) GetInputPeer() TL {
switch d.Type {
case DIALOG_TYPE_CHAT:
return TL_inputPeerChat{
Chat_id: d.PeerID,
}
case DIALOG_TYPE_CHANNEL:
return TL_inputPeerChannel{
Channel_id: d.PeerID,
Access_hash: d.PeerAccessHash,
}
case DIALOG_TYPE_USER:
return TL_inputPeerUser{
User_id: d.PeerID,
}
default:
return nil
}
}
func (m *MTProto) Messages_GetDialogs(
offsetID, offsetDate, limit int32, offsetInputPeer TL,
) ([]Dialog, map[int32]User, map[int32]Chat, map[int32]Channel, map[int32]Message, int) {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{
TL_messages_getDialogs{
Offset_id: offsetID,
Offset_date: offsetDate,
Limit: limit,
Offset_peer: offsetInputPeer,
},
resp,
}
x := <-resp
mMessages := make(map[int32]Message)
mChats := make(map[int32]Chat)
mChannels := make(map[int32]Channel)
mUsers := make(map[int32]User)
var dialogs []Dialog
switch input := x.(type) {
case TL_messages_dialogsSlice:
for _, v := range input.Messages {
m := NewMessage(v)
if m != nil {
mMessages[m.ID] = *m
}
}
for _, v := range input.Chats {
switch v.(type) {
case TL_chatEmpty, TL_chat, TL_chatFull, TL_chatForbidden:
c := NewChat(v)
mChats[c.ID] = *c
case TL_channel, TL_channelFull, TL_channelForbidden:
c := NewChannel(v)
mChannels[c.ID] = *c
}
}
for _, v := range input.Users {
u := NewUser(v)
mUsers[u.ID] = *u
}
for _, v := range input.Dialogs {
d := NewDialog(v)
switch d.Type {
case DIALOG_TYPE_USER:
d.PeerAccessHash = mUsers[d.PeerID].AccessHash
case DIALOG_TYPE_CHAT:
d.PeerAccessHash = mChats[d.PeerID].AccessHash
case DIALOG_TYPE_CHANNEL:
d.PeerAccessHash = mChannels[d.PeerID].AccessHash
}
dialogs = append(dialogs, *d)
}
return dialogs, mUsers, mChats, mChannels, mMessages, int(input.Count)
case TL_messages_dialogs:
for _, v := range input.Messages {
m := NewMessage(v)
if m != nil {
mMessages[m.ID] = *m
}
}
for _, v := range input.Chats {
switch v.(type) {
case TL_chatEmpty, TL_chat, TL_chatFull, TL_chatForbidden:
c := NewChat(v)
mChats[c.ID] = *c
case TL_channel, TL_channelFull, TL_channelForbidden:
c := NewChannel(v)
mChannels[c.ID] = *c
}
}
for _, v := range input.Users {
u := NewUser(v)
mUsers[u.ID] = *u
}
for _, v := range input.Dialogs {
d := NewDialog(v)
switch d.Type {
case DIALOG_TYPE_USER:
d.PeerAccessHash = mUsers[d.PeerID].AccessHash
case DIALOG_TYPE_CHAT:
d.PeerAccessHash = mUsers[d.PeerID].AccessHash
case DIALOG_TYPE_CHANNEL:
d.PeerAccessHash = mChannels[d.PeerID].AccessHash
}
dialogs = append(dialogs, *d)
}
return dialogs, mUsers, mChats, mChannels, mMessages, len(input.Chats)
default:
return nil, nil, nil, nil, nil, 0
}
}