update naming of functions, add option to get count of unread messages of user

This commit is contained in:
2026-04-21 19:50:14 +02:00
parent a554c870ef
commit 422c4eb419
13 changed files with 178 additions and 484 deletions
+43 -9
View File
@@ -10,11 +10,12 @@ import (
)
var (
Mu sync.RWMutex
Users = make(map[uuid.UUID]*types.User)
Mu sync.RWMutex
Users = make(map[uuid.UUID]*types.User)
connectionsUnreadMessages = make(map[uuid.UUID]map[uuid.UUID]uint32)
)
func CacheGetUserById(id uuid.UUID) (*types.User, error) {
func GetUserById(id uuid.UUID) (*types.User, error) {
Mu.RLock()
defer Mu.RUnlock()
@@ -25,7 +26,7 @@ func CacheGetUserById(id uuid.UUID) (*types.User, error) {
return user, nil
}
func CacheGetUserByName(name string) (*types.User, error) {
func GetUserByName(name string) (*types.User, error) {
Mu.RLock()
defer Mu.RUnlock()
@@ -37,21 +38,21 @@ func CacheGetUserByName(name string) (*types.User, error) {
return nil, fmt.Errorf("user %s not found", name)
}
func CacheSaveUser(user *types.User) {
func SaveUser(user *types.User) {
Mu.Lock()
defer Mu.Unlock()
Users[user.Id] = user
}
func CacheDeleteUser(id uuid.UUID) {
func DeleteUser(id uuid.UUID) {
Mu.Lock()
defer Mu.Unlock()
delete(Users, id)
}
func CacheAddConnection(a, b *types.User, conn *types.Connection) {
func AddConnection(a, b *types.User, conn *types.Connection) {
first, second := a, b
if a.Id.String() > b.Id.String() {
first, second = b, a
@@ -64,7 +65,7 @@ func CacheAddConnection(a, b *types.User, conn *types.Connection) {
first.Mu.Unlock()
}
func CacheDeleteConnection(a, b *types.User, id uuid.UUID) {
func DeleteConnection(a, b *types.User, id uuid.UUID) {
first, second := a, b
if a.Id.String() > b.Id.String() {
first, second = b, a
@@ -77,9 +78,42 @@ func CacheDeleteConnection(a, b *types.User, id uuid.UUID) {
first.Mu.Unlock()
}
func CacheGetConnection(user *types.User, id uuid.UUID) (*types.Connection, bool) {
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 DeallocateConnectionsUnreadMessages(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
}