before rewrite
This commit is contained in:
@@ -6,8 +6,8 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
var Groups map[uint32]ChatGroup
|
||||
var ConnectedClients map[uint32]map[*Client]struct{}
|
||||
var Groups map[uint64]ChatGroup
|
||||
var ConnectedClients map[uint64]map[*Client]struct{}
|
||||
|
||||
func InitCache() {
|
||||
groups, err := GetAllChatGroups(context.Background())
|
||||
@@ -20,7 +20,7 @@ func InitCache() {
|
||||
}
|
||||
}
|
||||
|
||||
func GetGroupById(groupId uint32) (*ChatGroup, error) {
|
||||
func GetGroupById(groupId uint64) (*ChatGroup, error) {
|
||||
group, ok := Groups[groupId]
|
||||
if !ok {
|
||||
return nil, errors.New("group not found")
|
||||
@@ -31,18 +31,21 @@ func GetGroupById(groupId uint32) (*ChatGroup, error) {
|
||||
func AddOrUpdateGroupToCache(mu *sync.Mutex, group ChatGroup) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
Groups[group.Id] = group
|
||||
}
|
||||
|
||||
func RemoveGroupFromCache(mu *sync.Mutex, groupId uint32) {
|
||||
func RemoveGroupFromCache(mu *sync.Mutex, groupId uint64) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
delete(Groups, groupId)
|
||||
}
|
||||
|
||||
func AddOrUpdateConnectedClientToCache(mu *sync.Mutex, client *Client) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
for _, groupId := range client.User.MemberGroupsId {
|
||||
ConnectedClients[groupId][client] = struct{}{}
|
||||
}
|
||||
@@ -51,7 +54,24 @@ func AddOrUpdateConnectedClientToCache(mu *sync.Mutex, client *Client) {
|
||||
func RemoveConnectedClientFromCache(mu *sync.Mutex, client *Client) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
for _, groupId := range client.User.MemberGroupsId {
|
||||
delete(ConnectedClients[groupId], client)
|
||||
}
|
||||
}
|
||||
|
||||
func IsUserInGivenGroup(mu *sync.Mutex, userId uint64, groupId uint64) bool {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
group, ok := ConnectedClients[groupId]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
for client := range group {
|
||||
if client.User.Id == userId {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user