c0d4483154
Covers: snake_case param renames, mutex RLock/Unlock mismatches, user.Hubs keyed by hub.Id, hub color using new_name, DELETE params sent as query, delete(target.Hubs, hub.Id), root/member role Id swap, and the three new PATCH /hub/channel/roles/* handlers.
116 lines
3.0 KiB
Go
116 lines
3.0 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.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, 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 {
|
|
return nil, errors.New("hub not found")
|
|
}
|
|
|
|
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
|
|
}
|