add hub function
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package httpRequest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"go-socket/packages/convertions"
|
||||
"maps"
|
||||
"net/http"
|
||||
"slices"
|
||||
@@ -43,7 +45,7 @@ func haveHubUserCachedPermissions(needed types.CachedUserPermissions, user *type
|
||||
}
|
||||
|
||||
func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
if !validCheckWithResponseOnFail(response, request, normal) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -62,7 +64,7 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
||||
|
||||
hub := types.NewHub()
|
||||
hub.Name = hubName
|
||||
hub.Color = types.Rgba{}.GetRandom()
|
||||
hub.Color = types.RandomRgba()
|
||||
hub.Id = uuid.New()
|
||||
hub.Creator = user.Id
|
||||
hub.CreatedAt = time.Now()
|
||||
@@ -77,7 +79,7 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
||||
Id: uint8(0),
|
||||
Permissions: types.PermissionAll(),
|
||||
Name: "root",
|
||||
Color: types.Rgba{}.GetRandom(),
|
||||
Color: types.RandomRgba(),
|
||||
CreatedAt: hub.CreatedAt,
|
||||
}
|
||||
hub.Roles[rootRole.Id] = rootRole
|
||||
@@ -86,7 +88,7 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
||||
memberRole := &types.HubRole{
|
||||
Id: types.HubBoundRolesMax,
|
||||
Name: "member",
|
||||
Color: types.Rgba{}.GetRandom(),
|
||||
Color: types.RandomRgba(),
|
||||
CreatedAt: hub.CreatedAt,
|
||||
}
|
||||
hub.JoinRole = memberRole
|
||||
@@ -112,13 +114,13 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
func HandleHubJoin(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
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.StatusBadRequest)
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
hub, err := getHubByIdStr(ctx, request.Header.Get("hubid"))
|
||||
@@ -135,7 +137,7 @@ func HandleHubJoin(response http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
func HandleHubMessage(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
if !validCheckWithResponseOnFail(response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -150,6 +152,7 @@ func HandleHubMessage(response http.ResponseWriter, request *http.Request) {
|
||||
|
||||
if hubUser.IsMuted {
|
||||
http.Error(response, "muted", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
msgContent := request.FormValue("msgContent")
|
||||
@@ -192,7 +195,7 @@ func HandleHubMessage(response http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
func HandleGetHubs(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
if !validCheckWithResponseOnFail(response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -208,6 +211,7 @@ func HandleGetHubs(response http.ResponseWriter, request *http.Request) {
|
||||
if len(hubs) == 0 {
|
||||
response.WriteHeader(http.StatusNoContent)
|
||||
response.Write([]byte("no hubs found"))
|
||||
return
|
||||
}
|
||||
converted, err := json.Marshal(hubs)
|
||||
if err != nil {
|
||||
@@ -218,38 +222,101 @@ func HandleGetHubs(response http.ResponseWriter, request *http.Request) {
|
||||
response.Write(converted)
|
||||
}
|
||||
|
||||
func HandleHubPermissionActionCore(
|
||||
response http.ResponseWriter, request *http.Request,
|
||||
takenVarName string, rt requestType, needed types.Permissions,
|
||||
performedAction func(usr *types.HubUser, hub *types.Hub, takenVar *string)) {
|
||||
if !validCheckWithResponseOnFail(&response, request, rt) {
|
||||
return
|
||||
func hubPermissionContext(response http.ResponseWriter, request *http.Request, rt bodyLimit, needed types.Permissions) (*types.HubUser, *types.Hub, context.Context, bool) {
|
||||
if !validCheckWithResponseOnFail(response, request, rt) {
|
||||
return nil, nil, nil, false
|
||||
}
|
||||
ctx := request.Context()
|
||||
_, hubUser, hub, err := getHubUserIfValidWithResponseOnFail(ctx, response, request)
|
||||
if err != nil {
|
||||
return
|
||||
return nil, nil, nil, false
|
||||
}
|
||||
|
||||
if !haveHubUserPermission(hubUser, hub, needed) {
|
||||
http.Error(response, "", http.StatusForbidden)
|
||||
return nil, nil, nil, false
|
||||
}
|
||||
|
||||
var takenVar *string = nil
|
||||
|
||||
if takenVarName != "" {
|
||||
takenVar = new(request.FormValue(takenVarName))
|
||||
}
|
||||
performedAction(hubUser, hub, takenVar)
|
||||
return hubUser, hub, ctx, true
|
||||
}
|
||||
|
||||
func HandleHubSetName(response http.ResponseWriter, request *http.Request) {
|
||||
HandleHubPermissionActionCore(response, request, "newname", normal, types.PermissionSetHubName, func(usr *types.HubUser, hub *types.Hub, takenVar *string) {
|
||||
newName := *takenVar
|
||||
if newName == "" {
|
||||
http.Error(response, "empty name", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
hub.Name = newName
|
||||
})
|
||||
_, hub, _, ok := hubPermissionContext(response, request, normal, types.PermissionSetHubName)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
newName := request.FormValue("newname")
|
||||
if newName == "" {
|
||||
http.Error(response, "empty name", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
hub.Name = newName
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HandleHubSetColor(response http.ResponseWriter, request *http.Request) {
|
||||
_, hub, _, ok := hubPermissionContext(response, request, normal, types.PermissionSetHubColor)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
color, err := convertions.StringToRgba(request.FormValue("newname"))
|
||||
if err != nil {
|
||||
http.Error(response, "bad color", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
hub.Color = color
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HandleHubRemove(response http.ResponseWriter, request *http.Request) {
|
||||
_, hub, _, ok := hubPermissionContext(response, request, normal, types.PermissionRemoveHub)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
cache.DeleteHub(hub)
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HandleHubToggleUserColorAllowed(response http.ResponseWriter, request *http.Request) {
|
||||
_, hub, _, ok := hubPermissionContext(response, request, normal, types.PermissionSetUserColorAllowed)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
hub.UserColorAllowed = !hub.UserColorAllowed
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HandleHubUserRemove(response http.ResponseWriter, request *http.Request) {
|
||||
_, hub, _, ok := hubPermissionContext(response, request, normal, types.PermissionRemoveUser)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
color, err := convertions.StringToRgba(request.FormValue("newname"))
|
||||
if err != nil {
|
||||
http.Error(response, "bad color", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
hub.Color = color
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HandleHubRenameUser(response http.ResponseWriter, request *http.Request) {
|
||||
_, hub, _, ok := hubPermissionContext(response, request, normal, types.PermissionRenameUser)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
newName := request.FormValue("newname")
|
||||
targetId, err := convertions.StringToUuid(request.FormValue("targetid"))
|
||||
if err != nil {
|
||||
http.Error(response, "bad targetid", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
hub.Mu.RLock()
|
||||
target, ok := hub.Users[targetId]
|
||||
if !ok {
|
||||
http.Error(response, "no users found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
hub.Mu.RUnlock()
|
||||
target.Name = newName
|
||||
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user