Files
go-socket/packages/httpRequest/hubs.go
T
2026-04-27 18:03:10 +02:00

152 lines
4.1 KiB
Go

package httpRequest
import (
"go-socket/packages/Enums/WsEventType"
"go-socket/packages/cache"
"net/http"
"time"
"go-socket/packages/types"
"go-socket/packages/wsServer"
"github.com/google/uuid"
)
func haveUserPermissionsOnChannel(permissions types.CachedUserPermissions, user *types.HubUser, channel *types.HubChannel) bool {
checkAgainst, ok := channel.UsersCachedPermissions[user.OriginalId]
if !ok || (permissions&checkAgainst) == checkAgainst {
return false
}
return true
}
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.HubBoundRolesMax,
Name: "member",
Color: types.Rgba{}.GetRandom(),
CreatedAt: hub.CreatedAt,
}
hub.JoinRole = memberRole
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)
channel.UsersCachedPermissions[creator.OriginalId] = types.CachedUserPermissionsAll
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
}
msgContent := request.FormValue("msgContent")
attachedFile := request.FormValue("attachedFile")
if msgContent == "" && attachedFile == "" {
http.Error(response, "empty msgContent", http.StatusBadRequest)
return
}
channel, err := getHubChannelIfValidWithResponseOnFail(ctx, response, hub, hubUser, request.FormValue("channelid"))
if err != nil {
return
}
if !haveUserPermissionsOnChannel(types.CachedUserCanMessage, hubUser, channel) {
http.Error(response, "cannot send messages here", http.StatusUnauthorized)
return
}
for id, userCachedPerms := range channel.UsersCachedPermissions {
if !userCachedPerms.CanReadHistory() || id == user.Id {
continue
}
message := &types.Message{
Id: uuid.New(),
AttachedFile: "",
Content: msgContent,
Sender: user.Id,
Receiver: channel.Id,
CreatedAt: time.Now(),
}
targetUser, err := cache.GetUserById(id)
if err != nil {
// todo Add to postgres in future
continue
}
wsServer.WsSendMessageCloseIfTimeout(targetUser, types.WsEventMessage{
Type: WsEventType.HubMessage,
Event: message,
})
}
}