Files
go-socket/packages/httpRequest/get.go
T
2026-05-07 18:19:53 +02:00

162 lines
4.2 KiB
Go

package httpRequest
import (
"context"
"errors"
"net/http"
"go-socket/packages/convertions"
"go-socket/packages/cache"
"go-socket/packages/postgresql"
"go-socket/packages/tokens"
"go-socket/packages/types"
"github.com/google/uuid"
)
func getUserById(ctx context.Context, userId uuid.UUID) (*types.User, error) {
user, err := cache.GetUserById(userId)
if err != nil {
user = &types.User{Id: userId, Hubs: make(map[uuid.UUID]*types.Hub)}
err = postgresql.UserGetWhole(ctx, user)
if err != nil {
return nil, err
}
}
return user, nil
}
func getUserByToken(ctx context.Context, token string) (*types.User, error) {
userId, err := tokens.TokenValidateGetId(token)
if err != nil {
return nil, err
}
return getUserById(ctx, userId)
}
func getConnection(ctx context.Context, connectionIdStr string, user *types.User) (*types.Connection, bool) {
connectionId, err := convertions.StringToUuid(connectionIdStr)
if err != nil {
return nil, false
}
if conn, ok := cache.GetConnection(user, connectionId); ok {
return conn, true
}
conn, err := postgresql.ConnectionGetById(ctx, connectionId)
if err != nil {
return nil, false
}
if conn.RequestorId != user.Id && conn.RecipientId != user.Id {
return nil, false
}
user.Mu.Lock()
user.Connections[conn.Id] = conn
user.Mu.Unlock()
return conn, true
}
func getChannelFromUser(user *types.User, channelIdStr string) (*types.HubChannel, bool) {
channelId, err := convertions.StringToUuid(channelIdStr)
if err != nil {
return nil, false
}
user.Mu.RLock()
defer user.Mu.RUnlock()
for _, hub := range user.Hubs {
hub.Mu.RLock()
ch, ok := hub.Channels[channelId]
hub.Mu.RUnlock()
if ok {
return ch, true
}
}
return nil, false
}
func getConnectionWithResponseOnFail(response http.ResponseWriter, connectionIdStr string, user *types.User) (*types.Connection, bool) {
connectionId, err := convertions.StringToUuid(connectionIdStr)
if err != nil {
http.Error(response, "invalid connectionid", http.StatusBadRequest)
return nil, false
}
conn, ok := cache.GetConnection(user, connectionId)
if !ok {
http.Error(response, "invalid connectionid", http.StatusBadRequest)
return nil, false
}
return conn, true
}
func getHubByIdStr(ctx context.Context, hubId string) (*types.Hub, error) {
hubUuid, err := convertions.StringToUuid(hubId)
if err != nil {
return nil, err
}
hub, ok := cache.GetHubById(hubUuid)
if !ok {
hub = &types.Hub{Id: hubUuid}
err = postgresql.HubGetWhole(ctx, hub)
if err != nil {
return nil, err
}
for _, u := range hub.Users {
updateChannelCacheForSpecUser(u, hub)
}
cache.SaveHub(hub)
}
return hub, nil
}
func getHubUserIfValidWithResponseOnFail(ctx context.Context, response http.ResponseWriter, request *http.Request) (
*types.User, *types.HubUser, *types.Hub, error) {
user, err := getUserByToken(ctx, request.Header.Get("token"))
if err != nil {
http.Error(response, "invalid token", http.StatusBadRequest)
return nil, nil, nil, errors.New("invalid token")
}
hub, err := getHubByIdStr(ctx, request.Header.Get("hub_id"))
if err != nil {
http.Error(response, "invalid hub_id", http.StatusBadRequest)
return nil, nil, nil, errors.New("no such hub")
}
hub.Mu.RLock()
hubUser, ok := hub.Users[user.Id]
hub.Mu.RUnlock()
if !ok {
http.Error(response, "invalid hub_id", http.StatusUnauthorized)
return nil, nil, nil, errors.New("invalid hub_id")
}
return user, hubUser, hub, nil
}
func getHubChannelIfValidWithResponseOnFail(ctx context.Context, response http.ResponseWriter, hub *types.Hub, hubUser *types.HubUser, channelId string) (
*types.HubChannel, error) {
channelUuid, err := convertions.StringToUuid(channelId)
if err != nil {
http.Error(response, "invalid channel_id", http.StatusBadRequest)
return nil, errors.New("invalid channel_id")
}
hub.Mu.RLock()
channel, ok := hub.Channels[channelUuid]
hub.Mu.RUnlock()
if !ok {
http.Error(response, "channel not found", http.StatusNotFound)
return nil, errors.New("channel not found")
}
if !haveHubUserCachedPermissions(types.CachedUserCanView, hubUser, channel) {
http.Error(response, "forbidden", http.StatusForbidden)
return nil, errors.New("forbidden")
}
return channel, nil
}