forked from sunnyque/mtproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_contact.go
147 lines (133 loc) · 3.36 KB
/
manager_contact.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
package mtproto
import (
"fmt"
"log"
)
// Contact
type Contact struct {
ClientID int64
Firstname string
Lastname string
Phone string
UserID int32
Mutual bool
}
func (c *Contact) GetInputContact() TL {
return TL_inputPhoneContact{
Client_id: c.ClientID,
First_name: c.Firstname,
Last_name: c.Lastname,
Phone: c.Phone,
}
}
func NewContact(in TL) (contact *Contact) {
contact = new(Contact)
switch c := in.(type) {
case TL_contact:
contact.UserID = c.User_id
contact.Mutual = toBool(c.Mutual)
case TL_importedContact:
contact.UserID = c.User_id
contact.ClientID = c.Client_id
case TL_inputPhoneContact:
contact.ClientID = c.Client_id
contact.Firstname = c.First_name
contact.Lastname = c.Last_name
contact.Phone = c.Phone
default:
log.Println("GetContact::Error::Invalid Type")
return nil
}
return
}
func (m *MTProto) Contacts_ResolveUserName(name string) ([]Channel, []Chat, []User, error) {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{TL_contacts_resolveUsername{
name},
resp,
}
x := <-resp
peer, ok := x.(TL_contacts_resolvedPeer)
if !ok {
log.Println(fmt.Sprintf("RPC: %#v", x))
return []Channel{}, []Chat{}, []User{}, fmt.Errorf("RPC: %#v", x)
}
TChats := make([]Chat, 0, len(peer.Chats))
TChannel := make([]Channel, 0, len(peer.Chats))
TUsers := make([]User, 0, len(peer.Users))
for _, v := range peer.Chats {
switch v.(type) {
case TL_chatEmpty, TL_chat, TL_chatFull, TL_chatForbidden:
TChats = append(
TChats,
*NewChat(v),
)
case TL_channel, TL_channelFull, TL_channelForbidden:
TChannel = append(
TChannel,
*NewChannel(v),
)
}
}
for _, v := range peer.Users {
switch u := v.(type) {
case TL_user, TL_userEmpty:
TUsers = append(TUsers, *NewUser(u))
case TL_userProfilePhoto:
TUsers[len(TUsers)-1].Photo = NewUserProfilePhoto(u)
case TL_userStatusRecently, TL_userStatusOffline, TL_userStatusOnline, TL_userStatusLastWeek, TL_userStatusLastMonth:
TUsers[len(TUsers)-1].Status = NewUserStatus(u)
}
}
return TChannel, TChats, TUsers, nil
}
func (m *MTProto) Contacts_GetContacts(hash int32) ([]Contact, []User, error) {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{TL_contacts_getContacts{
hash},
resp,
}
x := <-resp
list, ok := x.(TL_contacts_contacts)
if !ok {
log.Println(fmt.Sprintf("RPC: %#v", x))
return []Contact{}, []User{}, fmt.Errorf("RPC: %#v", x)
}
TContacts := make([]Contact, 0, len(list.Contacts))
TUsers := make([]User, 0, len(list.Users))
for _, v := range list.Contacts {
TContacts = append(
TContacts,
*NewContact(v),
)
}
for _, v := range list.Users {
switch u := v.(type) {
case TL_user, TL_userEmpty:
TUsers = append(TUsers, *NewUser(u))
case TL_userProfilePhoto:
TUsers[len(TUsers)-1].Photo = NewUserProfilePhoto(u)
case TL_userStatusRecently, TL_userStatusOffline, TL_userStatusOnline, TL_userStatusLastWeek, TL_userStatusLastMonth:
TUsers[len(TUsers)-1].Status = NewUserStatus(u)
}
}
return TContacts, TUsers, nil
}
func (m *MTProto) Contacts_ImportContacts(contacts []TL) {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{
TL_contacts_importContacts{
contacts,
},
resp,
}
x := <-resp
switch r := x.(type) {
case TL_contacts_importedContacts:
//TODO:: must do something with response
log.Println(r)
default:
log.Println(fmt.Sprintf("RPC: %#v", x))
return
}
}