add more hub endpoints

This commit is contained in:
cos
2026-04-30 13:27:36 +02:00
parent 0afed9a326
commit 88217db278
3 changed files with 177 additions and 5 deletions
+172 -4
View File
@@ -289,12 +289,19 @@ func HandleHubUserRemove(response http.ResponseWriter, request *http.Request) {
if !ok {
return
}
color, err := convertions.StringToRgba(request.FormValue("newname"))
targetId, err := convertions.StringToUuid(request.FormValue("targetid"))
if err != nil {
http.Error(response, "bad color", http.StatusBadRequest)
http.Error(response, "bad targetid", http.StatusBadRequest)
return
}
hub.Color = color
hub.Mu.Lock()
if _, exists := hub.Users[targetId]; !exists {
hub.Mu.Unlock()
http.Error(response, "user not found", http.StatusNotFound)
return
}
delete(hub.Users, targetId)
hub.Mu.Unlock()
response.WriteHeader(http.StatusAccepted)
}
@@ -311,12 +318,173 @@ func HandleHubRenameUser(response http.ResponseWriter, request *http.Request) {
}
hub.Mu.RLock()
target, ok := hub.Users[targetId]
hub.Mu.RUnlock()
if !ok {
http.Error(response, "no users found", http.StatusNotFound)
return
}
hub.Mu.RUnlock()
target.Name = newName
response.WriteHeader(http.StatusAccepted)
}
func HandleHubRenameSelf(response http.ResponseWriter, request *http.Request) {
hubUser, _, _, ok := hubPermissionContext(response, request, normal, types.PermissionSelfRename)
if !ok {
return
}
hubUser.Name = request.FormValue("newname")
response.WriteHeader(http.StatusAccepted)
}
func HandleHubToggleMuteUser(response http.ResponseWriter, request *http.Request) {
_, hub, _, ok := hubPermissionContext(response, request, normal, types.PermissionMuteUser)
if !ok {
return
}
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]
hub.Mu.RUnlock()
if !ok {
http.Error(response, "user not found", http.StatusNotFound)
return
}
target.IsMuted = !target.IsMuted
response.WriteHeader(http.StatusAccepted)
}
func HandleHubUserAddRole(response http.ResponseWriter, request *http.Request) {
_, hub, _, ok := hubPermissionContext(response, request, normal, types.PermissionUserAddRole)
if !ok {
return
}
targetId, err := convertions.StringToUuid(request.FormValue("targetid"))
if err != nil {
http.Error(response, "bad targetid", http.StatusBadRequest)
return
}
roleId, err := convertions.StringToUint8(request.FormValue("roleid"))
if err != nil {
http.Error(response, "bad roleid", http.StatusBadRequest)
return
}
hub.Mu.RLock()
target, ok := hub.Users[targetId]
role := hub.Roles[roleId]
hub.Mu.RUnlock()
if !ok {
http.Error(response, "user not found", http.StatusNotFound)
return
}
if role == nil {
http.Error(response, "role not found", http.StatusNotFound)
return
}
target.Roles.Add(roleId)
response.WriteHeader(http.StatusAccepted)
}
func HandleHubUserRemoveRole(response http.ResponseWriter, request *http.Request) {
_, hub, _, ok := hubPermissionContext(response, request, normal, types.PermissionUserRemoveRole)
if !ok {
return
}
targetId, err := convertions.StringToUuid(request.FormValue("targetid"))
if err != nil {
http.Error(response, "bad targetid", http.StatusBadRequest)
return
}
roleId, err := convertions.StringToUint8(request.FormValue("roleid"))
if err != nil {
http.Error(response, "bad roleid", http.StatusBadRequest)
return
}
hub.Mu.RLock()
target, ok := hub.Users[targetId]
hub.Mu.RUnlock()
if !ok {
http.Error(response, "user not found", http.StatusNotFound)
return
}
target.Roles.Remove(roleId)
response.WriteHeader(http.StatusAccepted)
}
func HandleHubCreateRole(response http.ResponseWriter, request *http.Request) {
_, hub, _, ok := hubPermissionContext(response, request, normal, types.PermissionCreateRole)
if !ok {
return
}
name := request.FormValue("name")
if name == "" {
http.Error(response, "empty name", http.StatusBadRequest)
return
}
hub.Mu.Lock()
var freeId uint8
found := false
for i := uint8(1); i < types.HubBoundRolesMax; i++ {
if hub.Roles[i] == nil {
freeId = i
found = true
break
}
}
if !found {
hub.Mu.Unlock()
http.Error(response, "no role slots available", http.StatusConflict)
return
}
hub.Roles[freeId] = &types.HubRole{
Id: freeId,
Name: name,
Color: types.RandomRgba(),
CreatedAt: time.Now(),
}
hub.Mu.Unlock()
response.WriteHeader(http.StatusCreated)
}
func HandleHubRemoveRole(response http.ResponseWriter, request *http.Request) {
_, hub, _, ok := hubPermissionContext(response, request, normal, types.PermissionRemoveRole)
if !ok {
return
}
roleId, err := convertions.StringToUint8(request.FormValue("roleid"))
if err != nil {
http.Error(response, "bad roleid", http.StatusBadRequest)
return
}
if roleId == 0 || roleId == types.HubBoundRolesMax {
http.Error(response, "cannot remove system role", http.StatusForbidden)
return
}
hub.Mu.Lock()
if hub.Roles[roleId] == nil {
hub.Mu.Unlock()
http.Error(response, "role not found", http.StatusNotFound)
return
}
hub.Roles[roleId] = nil
hub.Mu.Unlock()
response.WriteHeader(http.StatusAccepted)
}
func HandleHubSelfRoleRemove(response http.ResponseWriter, request *http.Request) {
hubUser, _, _, ok := hubPermissionContext(response, request, normal, types.PermissionSelfRoleRemove)
if !ok {
return
}
roleId, err := convertions.StringToUint8(request.FormValue("roleid"))
if err != nil {
http.Error(response, "bad roleid", http.StatusBadRequest)
return
}
hubUser.Roles.Remove(roleId)
response.WriteHeader(http.StatusAccepted)
}