forked from cjongseok/mtproto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccess.go
More file actions
142 lines (134 loc) · 3.48 KB
/
Copy pathaccess.go
File metadata and controls
142 lines (134 loc) · 3.48 KB
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
package mtproto
import (
"io/ioutil"
"os"
"fmt"
"github.com/golang/protobuf/proto"
)
// AccessManager keeps channel and user accesses
// Fetching the accesses from the Telegram on every app launching can cause over rate limits,
// if the account has lots of channels and users. AccessManager helps to manage theses accesses
// as json files. Use tools/access to export them.
type AccessManager struct {
//accesses map[int32]Access
channels map[int32]Access
users map[int32]Access
}
type Access struct {
ID int32
//Required bool
Hash int64
}
// NewAccessManager instantiate a AccessManager
// If chats or contacts are given, it returns the instance fulfilled
// with the given id and hashes. Empty, otherwise.
func NewAccessManager(chats *TypeMessagesChats, contacts *TypeContactsContacts) (hm *AccessManager) {
hm = &AccessManager{make(map[int32]Access), make(map[int32]Access)}
if chats != nil {
hm.importTypeMessagesChats(chats)
}
if contacts != nil {
hm.importTypeContactsContacts(contacts)
}
return
}
func (am *AccessManager) importTypeContactsContacts(t *TypeContactsContacts) {
p := t.GetContactsContacts()
if p != nil {
for _, u := range p.Users {
predUser := u.GetUser()
if predUser != nil {
am.users[predUser.Id] = Access{
predUser.Id,
predUser.AccessHash,
}
}
}
}
}
func typeChatToAccess(t *TypeChat) *Access {
if t == nil {
return nil
}
if channel := t.GetChannel(); channel != nil {
return &Access{channel.Id, channel.AccessHash}
} else if chanForbidden := t.GetChannelForbidden(); chanForbidden != nil {
return &Access{chanForbidden.Id, chanForbidden.AccessHash}
}
return nil
}
func (am *AccessManager) importTypeMessagesChats(c *TypeMessagesChats) {
if c == nil {
return
}
//fmt.Println(slog.StringifyIndent(c, " "))
if chats := c.GetMessagesChats(); chats != nil {
for _, chat := range chats.Chats {
access := typeChatToAccess(chat)
if access != nil {
am.channels[access.ID] = *access
}
}
} else if chatsSlice := c.GetMessagesChatsSlice(); chatsSlice != nil {
for _, slice := range chatsSlice.Chats {
access := typeChatToAccess(slice)
if access != nil {
am.channels[access.ID] = *access
}
}
}
}
func (am *AccessManager) ImportUserAccessesFromFile(filepath string) error {
f, err := os.OpenFile(filepath, os.O_RDONLY, 644)
if err != nil {
return err
}
b, err := ioutil.ReadAll(f)
if err != nil {
return err
}
unmarshaled := &TypeContactsContacts{}
err = proto.Unmarshal(b, unmarshaled)
if err != nil {
return err
}
am.importTypeContactsContacts(unmarshaled)
return nil
}
func (am *AccessManager) ImportChanAccessesFromFile(filepath string) error {
f, err := os.OpenFile(filepath, os.O_RDONLY, 644)
if err != nil {
return fmt.Errorf("open file failure; %v", err)
}
b, err := ioutil.ReadAll(f)
if err != nil {
return fmt.Errorf("read file failure; %v", err)
}
unmarshaled := &TypeMessagesChats{}
err = proto.Unmarshal(b, unmarshaled)
if err != nil {
return fmt.Errorf("unmarshal failure; %v", err)
}
am.importTypeMessagesChats(unmarshaled)
return nil
}
func (am *AccessManager) ChannelAccess(id int32) Access {
return am.channels[id]
}
func (am *AccessManager) UserAccess(id int32) Access {
return am.users[id]
}
func (am *AccessManager) Users() []int32 {
var ids []int32
for id, _ := range am.users {
ids = append(ids, id)
}
return ids
}
func (am *AccessManager) Channels() []int32 {
var ids []int32
for id, _ := range am.channels {
ids = append(ids, id)
}
return ids
}