49 lines
910 B
Go
49 lines
910 B
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
)
|
|
|
|
var (
|
|
mu sync.RWMutex
|
|
clients = make(map[uint32]*Client)
|
|
chatGroups = make(map[uint32]ChatGroup)
|
|
)
|
|
|
|
func AddGroupToCache(chatGroup *ChatGroup) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
chatGroups[chatGroup.Id] = *chatGroup
|
|
}
|
|
|
|
func RemoveGroupFromCache(chatGroup *ChatGroup) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
delete(chatGroups, chatGroup.Id)
|
|
}
|
|
|
|
func AddClientConnectionsToCache(client *Client) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
for _, groupIn := range client.Groups {
|
|
chatGroups[groupIn.Id].Members[client.Id] = client
|
|
}
|
|
}
|
|
|
|
func RemoveClientConnectionsToCache(client *Client) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
for _, groupIn := range client.Groups {
|
|
delete(chatGroups[groupIn.Id].Members, client.Id)
|
|
}
|
|
}
|
|
|
|
func GetClientFromId(id uint32) (*Client, error) {
|
|
client, ok := clients[id]
|
|
if !ok {
|
|
return nil, errors.New("no such user")
|
|
}
|
|
return client, nil
|
|
}
|