aboutsummaryrefslogtreecommitdiff
path: root/telegram/cache/cache.go
diff options
context:
space:
mode:
authorbodqhrohro <bodqhrohro@gmail.com>2020-01-05 16:03:10 +0300
committerbodqhrohro <bodqhrohro@gmail.com>2020-01-05 16:03:10 +0300
commitb8fcac6ae24dd5e07f366741f0f282f33b18b503 (patch)
tree61f86331504dcd079f1a964d103ec299aa70f351 /telegram/cache/cache.go
parenta435a0a556cb88fe1fcc7e3bf860b45530d895e7 (diff)
Resend chat statuses on probe presence
Diffstat (limited to 'telegram/cache/cache.go')
-rw-r--r--telegram/cache/cache.go63
1 files changed, 57 insertions, 6 deletions
diff --git a/telegram/cache/cache.go b/telegram/cache/cache.go
index d1d16f9..b799709 100644
--- a/telegram/cache/cache.go
+++ b/telegram/cache/cache.go
@@ -6,20 +6,30 @@ import (
"github.com/zelenin/go-tdlib/client"
)
+// Status stores formatted data for XMPP presence
+type Status struct {
+ ID int64
+ XMPP string
+ Description string
+}
+
// Cache allows operating the chats and users cache in
// a thread-safe manner
type Cache struct {
- chats map[int64]*client.Chat
- users map[int32]*client.User
- chatsLock sync.Mutex
- usersLock sync.Mutex
+ chats map[int64]*client.Chat
+ users map[int32]*client.User
+ statuses map[int64]*Status
+ chatsLock sync.Mutex
+ usersLock sync.Mutex
+ statusesLock sync.Mutex
}
// NewCache initializes a cache
func NewCache() *Cache {
return &Cache{
- chats: map[int64]*client.Chat{},
- users: map[int32]*client.User{},
+ chats: map[int64]*client.Chat{},
+ users: map[int32]*client.User{},
+ statuses: map[int64]*Status{},
}
}
@@ -49,6 +59,26 @@ func (cache *Cache) UsersKeys() []int32 {
return keys
}
+// StatusesRange loops through the map in a thread-safe manner
+func (cache *Cache) StatusesRange() chan *Status {
+ cache.statusesLock.Lock()
+
+ statusChan := make(chan *Status, 1)
+
+ go func() {
+ defer func() {
+ cache.statusesLock.Unlock()
+ close(statusChan)
+ }()
+
+ for _, status := range cache.statuses {
+ statusChan <- status
+ }
+ }()
+
+ return statusChan
+}
+
// GetChat retrieves chat by id if it's present in the cache
func (cache *Cache) GetChat(id int64) (*client.Chat, bool) {
cache.chatsLock.Lock()
@@ -67,6 +97,15 @@ func (cache *Cache) GetUser(id int32) (*client.User, bool) {
return user, ok
}
+// GetStatus retrieves status by id if it's present in the cache
+func (cache *Cache) GetStatus(id int64) (*Status, bool) {
+ cache.statusesLock.Lock()
+ defer cache.statusesLock.Unlock()
+
+ status, ok := cache.statuses[id]
+ return status, ok
+}
+
// SetChat stores a chat in the cache
func (cache *Cache) SetChat(id int64, chat *client.Chat) {
cache.chatsLock.Lock()
@@ -82,3 +121,15 @@ func (cache *Cache) SetUser(id int32, user *client.User) {
cache.users[id] = user
}
+
+// SetStatus stores a status in the cache
+func (cache *Cache) SetStatus(id int64, show string, status string) {
+ cache.statusesLock.Lock()
+ defer cache.statusesLock.Unlock()
+
+ cache.statuses[id] = &Status{
+ ID: id,
+ XMPP: show,
+ Description: status,
+ }
+}