add hub get logic and part of client

This commit is contained in:
2026-04-28 21:22:33 +02:00
parent a49f9f4615
commit 221fb47495
24 changed files with 1788 additions and 22 deletions
+1 -1
View File
@@ -18,7 +18,7 @@ import (
func getUserById(ctx context.Context, userId uuid.UUID) (*types.User, error) {
user, err := cache.GetUserById(userId)
if err != nil {
user = &types.User{Id: userId}
user = &types.User{Id: userId, Hubs: make(map[uuid.UUID]*types.Hub)}
err = postgresql.GetWholeUser(ctx, user)
if err != nil {
return nil, err
+31
View File
@@ -1,7 +1,10 @@
package httpRequest
import (
"encoding/json"
"maps"
"net/http"
"slices"
"strings"
"time"
@@ -80,6 +83,7 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
hub.Id = uuid.New()
hub.Creator = user.Id
hub.CreatedAt = time.Now()
user.Hubs[user.Id] = hub
creator := types.NewHubUser()
creator.OriginalId = user.Id
@@ -211,3 +215,30 @@ func HandleHubMessage(response http.ResponseWriter, request *http.Request) {
wsServer.WsSendMessageCloseIfTimeout(target, msg)
}
}
func HandleGetHubs(response http.ResponseWriter, request *http.Request) {
if !validCheckWithResponseOnFail(&response, request, normal) {
return
}
ctx := request.Context()
user, err := getUserByToken(ctx, request.Header.Get("token"))
if err != nil {
http.Error(response, "invalid token", http.StatusBadRequest)
return
}
user.Mu.RLock()
hubs := slices.Collect(maps.Values(user.Hubs))
user.Mu.RUnlock()
if len(hubs) == 0 {
response.WriteHeader(http.StatusNoContent)
response.Write([]byte("no hubs found"))
}
converted, err := json.Marshal(hubs)
if err != nil {
http.Error(response, "json error", http.StatusInternalServerError)
return
}
response.WriteHeader(http.StatusOK)
response.Write(converted)
}
+3 -1
View File
@@ -17,6 +17,8 @@ import (
"go-socket/packages/wsServer"
"golang.org/x/crypto/bcrypt"
"github.com/google/uuid"
)
func HandleUserNewToken(response http.ResponseWriter, request *http.Request) {
@@ -45,7 +47,7 @@ func HandleUserNewToken(response http.ResponseWriter, request *http.Request) {
user, err = cache.GetUserByName(username)
if err != nil {
user = &types.User{Name: username}
user = &types.User{Name: username, Hubs: make(map[uuid.UUID]*types.Hub)}
if err = postgresql.UserGetStandardInfoByName(ctx, user); err != nil {
http.Error(response, "bad login", http.StatusUnauthorized)
return
+2 -1
View File
@@ -37,6 +37,7 @@ type User struct {
WsConn *websocket.Conn `json:"-"`
Id uuid.UUID `json:"-"`
Connections map[uuid.UUID]*Connection `json:"-"`
Hubs map[uuid.UUID]*Hub `json:"-"`
Color Rgba `json:"color"`
}
@@ -233,7 +234,7 @@ type Hub struct {
Roles [256]*HubRole `json:"-"`
Users map[uuid.UUID]*HubUser `json:"-"`
Groups [256]*HubGroup `json:"-"`
Channels map[uuid.UUID]*HubChannel `json:"channels"`
Channels map[uuid.UUID]*HubChannel `json:"-"`
Name string `json:"name"`
IconUrl string `json:"iconUrl"`
BgUrl string `json:"backgroundUrl"`