Make chats/users cache private
This commit is contained in:
parent
fa841bfa8b
commit
8cd6387552
|
@ -23,16 +23,11 @@ var logConstants = map[string]int32{
|
||||||
":all": 1023,
|
":all": 1023,
|
||||||
}
|
}
|
||||||
|
|
||||||
type cacheStruct struct {
|
type cache struct {
|
||||||
chats map[int64]*client.Chat
|
chats map[int64]*client.Chat
|
||||||
users map[int32]*client.User
|
users map[int32]*client.User
|
||||||
}
|
}
|
||||||
|
|
||||||
var cache = cacheStruct{
|
|
||||||
chats: map[int64]*client.Chat{},
|
|
||||||
users: map[int32]*client.User{},
|
|
||||||
}
|
|
||||||
|
|
||||||
func stringToLogConstant(c string) int32 {
|
func stringToLogConstant(c string) int32 {
|
||||||
level, ok := logConstants[c]
|
level, ok := logConstants[c]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -54,6 +49,7 @@ type Client struct {
|
||||||
jid string
|
jid string
|
||||||
Session *persistence.Session
|
Session *persistence.Session
|
||||||
content *config.TelegramContentConfig
|
content *config.TelegramContentConfig
|
||||||
|
cache *cache
|
||||||
|
|
||||||
locks clientLocks
|
locks clientLocks
|
||||||
online bool
|
online bool
|
||||||
|
@ -98,11 +94,15 @@ func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
parameters: ¶meters,
|
parameters: ¶meters,
|
||||||
xmpp: component,
|
xmpp: component,
|
||||||
jid: jid,
|
jid: jid,
|
||||||
Session: session,
|
Session: session,
|
||||||
content: &conf.Content,
|
content: &conf.Content,
|
||||||
|
cache: &cache{
|
||||||
|
chats: map[int64]*client.Chat{},
|
||||||
|
users: map[int32]*client.User{},
|
||||||
|
},
|
||||||
logVerbosity: logVerbosity,
|
logVerbosity: logVerbosity,
|
||||||
locks: clientLocks{},
|
locks: clientLocks{},
|
||||||
}, nil
|
}, nil
|
||||||
|
|
|
@ -102,7 +102,7 @@ func (c *Client) updateHandler() {
|
||||||
|
|
||||||
// new user discovered
|
// new user discovered
|
||||||
func (c *Client) updateUser(update *client.UpdateUser) {
|
func (c *Client) updateUser(update *client.UpdateUser) {
|
||||||
cache.users[update.User.Id] = update.User
|
c.cache.users[update.User.Id] = update.User
|
||||||
show, status := userStatusToText(update.User.Status)
|
show, status := userStatusToText(update.User.Status)
|
||||||
c.processStatusUpdate(int64(update.User.Id), status, show)
|
c.processStatusUpdate(int64(update.User.Id), status, show)
|
||||||
}
|
}
|
||||||
|
@ -127,7 +127,7 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.chats[update.Chat.Id] = update.Chat
|
c.cache.chats[update.Chat.Id] = update.Chat
|
||||||
|
|
||||||
var isChannel = false
|
var isChannel = false
|
||||||
if update.Chat.Type.ChatTypeType() == client.TypeChatTypeSupergroup {
|
if update.Chat.Type.ChatTypeType() == client.TypeChatTypeSupergroup {
|
||||||
|
|
|
@ -58,7 +58,7 @@ func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *cli
|
||||||
|
|
||||||
if id <= math.MaxInt32 && id >= math.MinInt32 {
|
if id <= math.MaxInt32 && id >= math.MinInt32 {
|
||||||
userID := int32(id)
|
userID := int32(id)
|
||||||
user, ok = cache.users[userID]
|
user, ok = c.cache.users[userID]
|
||||||
if !ok && userID > 0 {
|
if !ok && userID > 0 {
|
||||||
user, err = c.client.GetUser(&client.GetUserRequest{
|
user, err = c.client.GetUser(&client.GetUserRequest{
|
||||||
UserId: userID,
|
UserId: userID,
|
||||||
|
@ -67,11 +67,11 @@ func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *cli
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.users[userID] = user
|
c.cache.users[userID] = user
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheChat, ok = cache.chats[id]
|
cacheChat, ok = c.cache.chats[id]
|
||||||
if !ok {
|
if !ok {
|
||||||
if chat == nil {
|
if chat == nil {
|
||||||
cacheChat, err = c.client.GetChat(&client.GetChatRequest{
|
cacheChat, err = c.client.GetChat(&client.GetChatRequest{
|
||||||
|
@ -81,9 +81,9 @@ func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *cli
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.chats[id] = cacheChat
|
c.cache.chats[id] = cacheChat
|
||||||
} else {
|
} else {
|
||||||
cache.chats[id] = chat
|
c.cache.chats[id] = chat
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if chat == nil {
|
if chat == nil {
|
||||||
|
|
Loading…
Reference in a new issue