add hubs create in http

This commit is contained in:
2026-04-23 18:14:05 +02:00
parent 8a66a905cb
commit c0d7e53884
6 changed files with 299 additions and 55 deletions
+20 -7
View File
@@ -13,6 +13,7 @@ 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) {
@@ -25,7 +26,6 @@ func GetUserById(id uuid.UUID) (*types.User, error) {
}
return user, nil
}
func GetUserByName(name string) (*types.User, error) {
Mu.RLock()
defer Mu.RUnlock()
@@ -37,14 +37,12 @@ func GetUserByName(name string) (*types.User, error) {
}
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()
@@ -64,7 +62,6 @@ func AddConnection(a, b *types.User, conn *types.Connection) {
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() {
@@ -77,7 +74,6 @@ func DeleteConnection(a, b *types.User, id uuid.UUID) {
second.Mu.Unlock()
first.Mu.Unlock()
}
func GetConnection(user *types.User, id uuid.UUID) (*types.Connection, bool) {
user.Mu.RLock()
defer user.Mu.RUnlock()
@@ -98,7 +94,6 @@ func IncrementConnectionsUnreadMessages(userId uuid.UUID, connId uuid.UUID) {
}
connectionsUnreadMessages[connId][userId]++
}
func DeallocateConnectionsUnreadMessages(userId uuid.UUID, connId uuid.UUID) {
Mu.Lock()
defer Mu.Unlock()
@@ -107,7 +102,6 @@ func DeallocateConnectionsUnreadMessages(userId uuid.UUID, connId uuid.UUID) {
delete(connectionsUnreadMessages[connId], userId)
}
}
func GetConnectionsUnreadMessages(userId uuid.UUID, connId uuid.UUID) (uint32, bool) {
Mu.RLock()
defer Mu.RUnlock()
@@ -117,3 +111,22 @@ func GetConnectionsUnreadMessages(userId uuid.UUID, connId uuid.UUID) (uint32, b
}
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
}