before rewrite

This commit is contained in:
2026-03-15 17:59:22 +01:00
parent c97b21a39e
commit 9a677d7e46
9 changed files with 125 additions and 30 deletions
+24 -4
View File
@@ -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
}