125 lines
3.3 KiB
Go
125 lines
3.3 KiB
Go
package httpRequest
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"go-socket/packages/convertions"
|
|
"go-socket/packages/types"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func canHubUserMessage(channel)
|
|
|
|
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.FormValue("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.OriginalId = user.Id
|
|
creator.CreatedAt = hub.CreatedAt
|
|
hub.Users[creator.OriginalId] = creator
|
|
|
|
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: types.BoundRolesMax,
|
|
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
|
|
rootGroup.RolesCanView.Add(rootRole.Id)
|
|
rootGroup.RolesCanView.Add(memberRole.Id)
|
|
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!"
|
|
channel.CreatedAt = hub.CreatedAt
|
|
channel.RolesCanMessage.Add(rootGroup.Id)
|
|
channel.RolesCanMessage.Add(memberRole.Id)
|
|
channel.RolesCanView.Add(rootGroup.Id)
|
|
channel.RolesCanView.Add(memberRole.Id)
|
|
channel.RolesCanReadHistory.Add(rootGroup.Id)
|
|
channel.RolesCanReadHistory.Add(memberRole.Id)
|
|
hub.Channels[channel.Id] = channel
|
|
}
|
|
|
|
func HandleChannelSendMessage(response http.ResponseWriter, request *http.Request) {
|
|
if !validCheckWithResponseOnFail(&response, request, normal) {
|
|
return
|
|
}
|
|
ctx := request.Context()
|
|
user, hubUser, hub, err := getHubUserIfValidWithResponseOnFail(ctx, response, request.Header.Get("token"), request.FormValue("hubid"))
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
channelId, err := convertions.StringToUuid(request.FormValue("channelid"))
|
|
if err != nil {
|
|
http.Error(response, "invalid channelid", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
msgContent := request.FormValue("msgContent")
|
|
attachedFile := request.FormValue("attachedFile")
|
|
|
|
if msgContent == "" && attachedFile == "" {
|
|
http.Error(response, "empty msgContent", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// TODO add check in future
|
|
// if attachedFile != "" && !strings.HasPrefix(attachedFile, conn.Id.String()+"/") {
|
|
// http.Error(response, "invalid attachedFile", http.StatusBadRequest)
|
|
// return
|
|
// }
|
|
|
|
channel, err := getHubChannelIfValidWithResponseOnFail(ctx, response, hub, hubUser, request.FormValue("hubid"))
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
}
|