145 lines
3.0 KiB
Go
145 lines
3.0 KiB
Go
package cache
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
|
|
"go-socket/packages/types"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
var (
|
|
Mu sync.RWMutex
|
|
Users = make(map[uuid.UUID]*types.User)
|
|
connectionsUnreadMessages = make(map[uuid.UUID]map[uuid.UUID]uint32)
|
|
hubs = make(map[uuid.UUID]*types.Hub)
|
|
)
|
|
|
|
func GetUserById(id uuid.UUID) (*types.User, error) {
|
|
Mu.RLock()
|
|
defer Mu.RUnlock()
|
|
|
|
user, ok := Users[id]
|
|
if !ok {
|
|
return nil, fmt.Errorf("user %s not found", id)
|
|
}
|
|
return user, nil
|
|
}
|
|
func GetUsersByIds(ids []uuid.UUID) (users []*types.User, err error) {
|
|
Mu.RLock()
|
|
defer Mu.RUnlock()
|
|
for _, id := range ids {
|
|
user, ok := Users[id]
|
|
if !ok {
|
|
return nil, fmt.Errorf("if %s not found", id)
|
|
}
|
|
users = append(users, user)
|
|
}
|
|
return users, nil
|
|
}
|
|
func GetUserByName(name string) (*types.User, error) {
|
|
Mu.RLock()
|
|
defer Mu.RUnlock()
|
|
|
|
for _, user := range Users {
|
|
if user.Name == name {
|
|
return user, nil
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("user %s not found", name)
|
|
}
|
|
func SaveUser(user *types.User) {
|
|
Mu.Lock()
|
|
defer Mu.Unlock()
|
|
|
|
Users[user.Id] = user
|
|
}
|
|
func DeleteUser(id uuid.UUID) {
|
|
Mu.Lock()
|
|
defer Mu.Unlock()
|
|
|
|
delete(Users, id)
|
|
}
|
|
|
|
func AddConnection(a, b *types.User, conn *types.Connection) {
|
|
first, second := a, b
|
|
if a.Id.String() > b.Id.String() {
|
|
first, second = b, a
|
|
}
|
|
first.Mu.Lock()
|
|
second.Mu.Lock()
|
|
a.Connections[conn.Id] = conn
|
|
b.Connections[conn.Id] = conn
|
|
second.Mu.Unlock()
|
|
first.Mu.Unlock()
|
|
}
|
|
func DeleteConnection(a, b *types.User, id uuid.UUID) {
|
|
first, second := a, b
|
|
if a.Id.String() > b.Id.String() {
|
|
first, second = b, a
|
|
}
|
|
first.Mu.Lock()
|
|
second.Mu.Lock()
|
|
delete(a.Connections, id)
|
|
delete(b.Connections, id)
|
|
second.Mu.Unlock()
|
|
first.Mu.Unlock()
|
|
}
|
|
func GetConnection(user *types.User, id uuid.UUID) (*types.Connection, bool) {
|
|
user.Mu.RLock()
|
|
defer user.Mu.RUnlock()
|
|
conn, ok := user.Connections[id]
|
|
return conn, ok
|
|
}
|
|
|
|
func IncrementConnectionsUnreadMessages(userId uuid.UUID, connId uuid.UUID) {
|
|
Mu.Lock()
|
|
defer Mu.Unlock()
|
|
|
|
if _, ok := connectionsUnreadMessages[connId]; !ok {
|
|
connectionsUnreadMessages[connId] = make(map[uuid.UUID]uint32)
|
|
}
|
|
|
|
if _, ok := connectionsUnreadMessages[connId][userId]; !ok {
|
|
connectionsUnreadMessages[connId][userId] = 0
|
|
}
|
|
connectionsUnreadMessages[connId][userId]++
|
|
}
|
|
func DeleteConnectionsUnreadMessages(userId uuid.UUID, connId uuid.UUID) {
|
|
Mu.Lock()
|
|
defer Mu.Unlock()
|
|
|
|
if _, ok := connectionsUnreadMessages[connId]; ok {
|
|
delete(connectionsUnreadMessages[connId], userId)
|
|
}
|
|
}
|
|
func GetConnectionsUnreadMessages(userId uuid.UUID, connId uuid.UUID) (uint32, bool) {
|
|
Mu.RLock()
|
|
defer Mu.RUnlock()
|
|
|
|
if _, ok := connectionsUnreadMessages[connId]; !ok {
|
|
return 0, false
|
|
}
|
|
return connectionsUnreadMessages[connId][userId], true
|
|
}
|
|
|
|
func SaveHub(hub *types.Hub) {
|
|
Mu.Lock()
|
|
defer Mu.Unlock()
|
|
|
|
hubs[hub.Id] = hub
|
|
}
|
|
func DeleteHub(hub *types.Hub) {
|
|
Mu.Lock()
|
|
defer Mu.Unlock()
|
|
|
|
delete(hubs, hub.Id)
|
|
}
|
|
func GetHubById(id uuid.UUID) (*types.Hub, bool) {
|
|
Mu.RLock()
|
|
defer Mu.RUnlock()
|
|
hub, ok := hubs[id]
|
|
return hub, ok
|
|
}
|