needed save users that can view channel instead of checking

This commit is contained in:
2026-04-26 15:09:35 +02:00
parent f22bb43346
commit 9c73a01101
8 changed files with 130 additions and 39 deletions
+52 -3
View File
@@ -4,11 +4,14 @@ 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
@@ -21,7 +24,7 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
return
}
hubName := request.Header.Get("hubname")
hubName := request.FormValue("hubname")
if hubName == "" {
http.Error(response, "hub name is required", http.StatusBadRequest)
return
@@ -35,9 +38,9 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
hub.CreatedAt = time.Now()
creator := types.NewHubUser()
creator.Name = user.Name
creator.OriginalId = user.Id
creator.CreatedAt = hub.CreatedAt
hub.Users[creator.OriginalId] = creator
rootRole := &types.HubRole{
Id: uint8(0),
@@ -50,7 +53,7 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
creator.Roles.Add(rootRole.Id)
memberRole := &types.HubRole{
Id: uint8(255),
Id: types.BoundRolesMax,
Name: "member",
Color: types.Rgba{}.GetRandom(),
CreatedAt: hub.CreatedAt,
@@ -63,6 +66,8 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
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()
@@ -71,5 +76,49 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
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
}
}