-
Notifications
You must be signed in to change notification settings - Fork 12
/
manager_channel.go
373 lines (354 loc) · 9.71 KB
/
manager_channel.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
package mtproto
import (
"log"
"reflect"
"fmt"
)
const (
CHANNEL_DATA_EMPTY int = 0x00
CHANNEL_DATA_REGULAR int = 0x01
CHANNEL_DATA_FULL int = 0x02
)
type Channel struct {
_State int
Flags ChannelFlags
ID int32
AccessHash int64
Title string
About string
Username string
Photo TL // ChatPhoto
Date int32
Version int32
PinnedMessageID int32
RestrictionReason string
AdminRights ChannelAdminRights // flags_14?ChannelAdminRights
BannedRights ChannelBannedRights // flags_15?ChannelBannedRights
Counters ChannelCounters
}
type ChannelCounters struct {
Admins int32
Kicked int32
Banned int32
Unread int32
Participants int32
}
func (ch *Channel) GetPeer() TL {
return TL_peerChannel{
Channel_id: ch.ID,
}
}
func (ch *Channel) GetInputPeer() TL {
return TL_inputPeerChannel{
Channel_id: ch.ID,
Access_hash: ch.AccessHash,
}
}
type ChannelFlags struct {
Creator bool // flags_0?true
Left bool // flags_2?true
Editor bool // flags_3?true
Broadcast bool // flags_5?true
Verified bool // flags_7?true
Megagroup bool // flags_8?true
Restricted bool // flags_9?true
Democracy bool // flags_10?true
Signatures bool // flags_11?true
Min bool // flags_12?true
AdminRightsSet bool //flags_14
BannedRightsSet bool //flags_15
}
func (f *ChannelFlags) loadFlags(flags int32) {
if flags&1<<0 != 0 {
f.Creator = true
}
if flags&1<<2 != 0 {
f.Left = true
}
if flags&1<<3 != 0 {
f.Editor = true
}
if flags&1<<5 != 0 {
f.Broadcast = true
}
if flags&1<<7 != 0 {
f.Verified = true
}
if flags&1<<8 != 0 {
f.Megagroup = true
}
if flags&1<<9 != 0 {
f.Restricted = true
}
if flags&1<<10 != 0 {
f.Democracy = true
}
if flags&1<<11 != 0 {
f.Signatures = true
}
if flags&1<<12 != 0 {
f.Min = true
}
if flags&1<<14 != 0 {
f.AdminRightsSet = true
}
if flags&1<<15 != 0 {
f.BannedRightsSet = true
}
}
type ChannelAdminRights struct {
ChangeInfo bool // flags_0?true
PostMessages bool // flags_1?true
EditMessages bool // flags_2?true
DeleteMessages bool // flags_3?true
BanUsers bool // flags_4?true
InviteUsers bool // flags_5?true
InviteLink bool // flags_6?true
PinMessages bool // flags_7?true
AddAdmins bool // flags_9?true
}
func (f *ChannelAdminRights) loadFlags(flags int32) {
if flags&1<<0 != 0 {
f.ChangeInfo = true
}
if flags&1<<1 != 0 {
f.PostMessages = true
}
if flags&1<<2 != 0 {
f.EditMessages = true
}
if flags&1<<3 != 0 {
f.DeleteMessages = true
}
if flags&1<<4 != 0 {
f.BanUsers = true
}
if flags&1<<5 != 0 {
f.InviteUsers = true
}
if flags&1<<6 != 0 {
f.InviteLink = true
}
if flags&1<<7 != 0 {
f.PinMessages = true
}
if flags&1<<9 != 0 {
f.AddAdmins = true
}
}
type ChannelBannedRights struct {
UntilDate int32
ViewMessages bool // flags_0?true
SendMessages bool // flags_1?true
SendMedia bool // flags_2?true
SendStickers bool // flags_3?true
SendGifs bool // flags_4?true
SendGames bool // flags_5?true
SendInline bool // flags_6?true
EmbedLinks bool // flags_7?true
}
func (f *ChannelBannedRights) loadFlags(flags int32) {
if flags&1<<0 != 0 {
f.ViewMessages = true
}
if flags&1<<1 != 0 {
f.SendMessages = true
}
if flags&1<<2 != 0 {
f.SendMedia = true
}
if flags&1<<3 != 0 {
f.SendStickers = true
}
if flags&1<<4 != 0 {
f.SendGifs = true
}
if flags&1<<5 != 0 {
f.SendGames = true
}
if flags&1<<6 != 0 {
f.SendInline = true
}
if flags&1<<7 != 0 {
f.EmbedLinks = true
}
}
// input:
// 1. TL_channelFull:
// 2. TL_channelForbidden:
// 3. TL_channel
func NewChannel(input TL) *Channel {
channel := new(Channel)
switch ch := input.(type) {
case TL_channelFull:
channel._State = CHANNEL_DATA_FULL
channel.ID = ch.Id
channel.About = ch.About
channel.PinnedMessageID = ch.Pinned_msg_id
channel.Counters.Admins = ch.Admins_count
channel.Counters.Banned = ch.Banned_count
channel.Counters.Kicked = ch.Kicked_count
channel.Counters.Unread = ch.Unread_count
channel.Counters.Participants = ch.Participants_count
channel.Flags.loadFlags(ch.Flags)
case TL_channelForbidden:
channel._State = CHANNEL_DATA_EMPTY
case TL_channel:
channel._State = CHANNEL_DATA_REGULAR
channel.ID = ch.Id
channel.Title = ch.Title
channel.AccessHash = ch.Access_hash
channel.Username = ch.Username
channel.Date = ch.Date
channel.RestrictionReason = ch.Restriction_reason
channel.Flags.loadFlags(ch.Flags)
if channel.Flags.AdminRightsSet && ch.Admin_rights != nil {
channel.AdminRights.loadFlags(ch.Admin_rights.(TL_channelAdminRights).Flags)
}
if channel.Flags.BannedRightsSet && ch.Banned_rights != nil {
channel.BannedRights.UntilDate = ch.Banned_rights.(TL_channelBannedRights).Until_date
channel.BannedRights.loadFlags(ch.Banned_rights.(TL_channelBannedRights).Flags)
}
default:
log.Println("NewChannel::ERROR::", reflect.TypeOf(ch))
return nil
}
return channel
}
func (m *MTProto) Channels_GetParticipants(channel TL, offset, limit int32) []User {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{
TL_channels_getParticipants{
Channel: channel,
Filter: TL_channelParticipantsRecent{},
Offset: offset,
Limit: limit,
},
resp,
}
x := <-resp
users := make([]User, 0)
switch input := x.(type) {
case TL_channels_channelParticipants:
for _, u := range input.Users {
users = append(users, *NewUser(u))
}
case TL_rpc_error:
fmt.Println(input.error_code, input.error_message)
default:
fmt.Println(reflect.TypeOf(input).String())
}
return users
}
func (m *MTProto) Channels_GetChannels(in []TL) []Channel {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{
TL_channels_getChannels{
Id: in,
},
resp,
}
x := <-resp
channels := make([]Channel, 0, len(in))
switch input := x.(type) {
case TL_messages_chats:
for _, ch := range input.Chats {
channels = append(channels, *NewChannel(ch))
}
return channels
case TL_rpc_error:
fmt.Println("MTProto::Channels_GetChannels::", input.error_message, input.error_code)
return channels
default:
fmt.Println(reflect.TypeOf(input).String())
return channels
}
}
func (m *MTProto) Channels_GetFullChannel(channelID int32, accessHash int64) *Channel {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{
TL_channels_getFullChannel{
Channel: TL_inputChannel{
Channel_id: channelID,
Access_hash: accessHash,
},
},
resp,
}
x := <-resp
channel := new(Channel)
switch input := x.(type) {
case TL_messages_chatFull:
channel = NewChannel(input.Chats[0])
case TL_rpc_error:
fmt.Println("MTProto::Channels_GetFullChannel::", input.error_message, input.error_code)
default:
return nil
}
return channel
}
func (m *MTProto) Channels_JoinChannel(channelID int32, accessHash int64) {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{
TL_channels_joinChannel{
Channel: TL_inputChannel{
Channel_id: channelID,
Access_hash: accessHash,
},
},
resp,
}
x := <-resp
//channel := new(Channel)
switch input := x.(type) {
case TL_rpc_error:
fmt.Println("MTProto::Channels_JoinChannel::", input.error_message, input.error_code)
default:
log.Println(reflect.TypeOf(input))
}
return
}
func (m *MTProto) Channels_GetMessages(channel TL, ids []int32) []Message {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{
TL_channels_getMessages{
Channel: channel,
Id: ids,
},
resp,
}
x := <-resp
messages := make([]Message, 0, len(ids))
switch input := x.(type) {
case TL_messages_messages:
for _, m := range input.Messages {
msg := NewMessage(m)
if msg != nil {
messages = append(messages, *msg)
}
}
return messages
case TL_messages_messagesSlice:
for _, m := range input.Messages {
msg := NewMessage(m)
if msg != nil {
messages = append(messages, *msg)
}
}
return messages
case TL_messages_channelMessages:
for _, m := range input.Messages {
msg := NewMessage(m)
if msg != nil {
messages = append(messages, *msg)
}
}
return messages
case TL_rpc_error:
fmt.Println("MTProto::Channels_GetMessages::", input.error_message, input.error_code)
return messages
default:
fmt.Println(reflect.TypeOf(input).String())
return messages
}
}