129 lines
3.6 KiB
Go
129 lines
3.6 KiB
Go
package httpRequest
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"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}
|
|
err = postgresql.GetWholeUser(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 getConnectionWithResponseOnFail(response *http.ResponseWriter, request *http.Request, user *types.User) (*types.Connection, bool) {
|
|
connectionId, err := convertions.StringToUuid(request.FormValue("connectionid"))
|
|
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 {
|
|
return nil, errors.New("hub not found")
|
|
}
|
|
|
|
return hub, nil
|
|
}
|
|
|
|
func getHubUserIfValidWithResponseOnFail(ctx context.Context, response http.ResponseWriter, token string, hubId string) (
|
|
*types.User, *types.HubUser, *types.Hub, error) {
|
|
hub, err := getHubByIdStr(ctx, hubId)
|
|
if err != nil {
|
|
http.Error(response, "invalid hubid", http.StatusBadRequest)
|
|
return nil, nil, nil, errors.New("no such hub")
|
|
}
|
|
|
|
user, err := getUserByToken(ctx, token)
|
|
if err != nil {
|
|
http.Error(response, "invalid token", http.StatusBadRequest)
|
|
return nil, nil, nil, errors.New("invalid token")
|
|
}
|
|
|
|
hub.Mu.RLock()
|
|
hubUser, ok := hub.Users[user.Id]
|
|
hub.Mu.RUnlock()
|
|
if !ok {
|
|
http.Error(response, "invalid hubid", http.StatusUnauthorized)
|
|
return nil, nil, nil, errors.New("invalid hubid")
|
|
}
|
|
|
|
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 channelid", http.StatusBadRequest)
|
|
return nil, errors.New("invalid channelid")
|
|
}
|
|
channel, ok := hub.Channels[channelUuid]
|
|
if !ok {
|
|
http.Error(response, "invalid channelid", http.StatusUnauthorized)
|
|
return nil, errors.New("invalid channelid")
|
|
}
|
|
|
|
group := hub.Groups[channel.ParentId]
|
|
if group == nil {
|
|
slog.Warn("hub channel has no parent group", "Hub", hub.Id, "Channel", channel.Id)
|
|
http.Error(response, "internal server error", http.StatusInternalServerError)
|
|
return nil, errors.New("internal server error")
|
|
}
|
|
|
|
if !group.RolesCanView.HasSameId(hubUser.Roles) {
|
|
http.Error(response, "invalid channelid", http.StatusUnauthorized)
|
|
return nil, errors.New("invalid channelid")
|
|
}
|
|
|
|
if !channel.RolesCanView.HasSameId(hubUser.Roles) {
|
|
http.Error(response, "invalid channelid", http.StatusUnauthorized)
|
|
return nil, errors.New("invalid channelid")
|
|
}
|
|
|
|
return channel, nil
|
|
}
|
|
|
|
// TODO cache on roles or channels needed for quick lookup
|
|
func getHubChannelReadHistorayAndViewChannel(hub *types.Hub, channel *types.HubChannel) []*types.HubUser
|