Files
go-socket/packages/httpRequest/hubs.go
T

76 lines
1.7 KiB
Go

package httpRequest
import (
"net/http"
"time"
"go-socket/packages/types"
"github.com/google/uuid"
)
func HandleHubCreate(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.StatusUnauthorized)
return
}
hubName := request.Header.Get("hubname")
if hubName == "" {
http.Error(response, "hub name is required", http.StatusBadRequest)
return
}
hub := types.NewHub()
hub.Name = hubName
hub.Color = types.Rgba{}.GetRandom()
hub.Id = uuid.New()
hub.Creator = user.Id
hub.CreatedAt = time.Now()
creator := types.NewHubUser()
creator.Name = user.Name
creator.OriginalId = user.Id
creator.CreatedAt = hub.CreatedAt
rootRole := &types.HubRole{
Id: uint8(0),
Permissions: types.PermissionAll(),
Name: "root",
Color: types.Rgba{}.GetRandom(),
CreatedAt: hub.CreatedAt,
}
hub.Roles[rootRole.Id] = rootRole
creator.Roles.Add(rootRole.Id)
memberRole := &types.HubRole{
Id: uint8(255),
Name: "member",
Color: types.Rgba{}.GetRandom(),
CreatedAt: hub.CreatedAt,
}
hub.Roles[memberRole.Id] = memberRole
creator.Roles.Add(memberRole.Id)
rootGroup := types.NewHubGroup()
rootGroup.Name = "root"
rootGroup.Id = uint8(1)
rootGroup.Color = types.Rgba{}.GetRandom()
rootGroup.CreatedAt = hub.CreatedAt
hub.Groups[rootGroup.Id] = rootGroup
channel := types.NewHubChannel()
channel.Name = "main channel"
channel.Position = uint8(0)
channel.Id = uuid.New()
channel.ParentId = rootGroup.Id
channel.Description = "The fist channel!"
hub.Channels[channel.Id] = channel
}