|
|
|
@@ -8,6 +8,7 @@ import (
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"go-socket/packages/convertions"
|
|
|
|
|
"go-socket/packages/postgresql"
|
|
|
|
|
|
|
|
|
|
"go-socket/packages/cache"
|
|
|
|
|
"go-socket/packages/minio"
|
|
|
|
@@ -187,6 +188,31 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
hub.Channels[channel.Id] = channel
|
|
|
|
|
|
|
|
|
|
cache.SaveHub(hub)
|
|
|
|
|
err = postgresql.HubSave(ctx, hub)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(response, "failed to save hub", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err = postgresql.HubUserSave(ctx, hub.Id, creator)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(response, "failed to save hub user", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err = postgresql.HubRoleSave(ctx, hub.Id, rootRole)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(response, "failed to save hub role", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err = postgresql.HubRoleSave(ctx, hub.Id, memberRole)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(response, "failed to save hub role", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err = postgresql.HubChannelSave(ctx, hub.Id, channel)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(response, "failed to save hub channel", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response.WriteHeader(http.StatusCreated)
|
|
|
|
|
response.Write([]byte(hub.Id.String()))
|
|
|
|
@@ -224,6 +250,10 @@ func HandleHubJoin(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
hub.Users[hubUser.OriginalId] = hubUser
|
|
|
|
|
updateChannelCacheForSpecUser(hubUser, hub)
|
|
|
|
|
|
|
|
|
|
if err = postgresql.HubUserSave(ctx, hub.Id, hubUser); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusCreated)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -254,7 +284,7 @@ func HandleHubMessage(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if attachedFile != "" && !strings.HasPrefix(attachedFile, channel.Id.String()+"/") {
|
|
|
|
|
if attachedFile != "" && !strings.HasPrefix(attachedFile, string(minio.HubChannelFilePrefix)+channel.Id.String()+"/") {
|
|
|
|
|
http.Error(response, "invalid attachedFile", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -440,7 +470,7 @@ func hubPermissionContext(response http.ResponseWriter, request *http.Request, r
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleHubSetName(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
_, hub, _, _, ok := hubPermissionContext(response, request, normal, types.PermissionSetHubName)
|
|
|
|
|
_, hub, ctx, _, ok := hubPermissionContext(response, request, normal, types.PermissionSetHubName)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -450,11 +480,15 @@ func HandleHubSetName(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
hub.Name = newName
|
|
|
|
|
if err := postgresql.HubUpdate(ctx, hub, &types.HubUpdate{Name: true}); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleHubSetColor(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
_, hub, _, _, ok := hubPermissionContext(response, request, normal, types.PermissionSetHubColor)
|
|
|
|
|
_, hub, ctx, _, ok := hubPermissionContext(response, request, normal, types.PermissionSetHubColor)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -464,181 +498,64 @@ func HandleHubSetColor(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
hub.Color = color
|
|
|
|
|
if err = postgresql.HubUpdate(ctx, hub, &types.HubUpdate{Color: true}); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleHubSetIcon(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
_, hub, ctx, _, ok := hubPermissionContext(response, request, hubIcon, types.PermissionSetHubIcon)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file, header, err := request.FormFile("file")
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(response, "missing file", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
isImg, contentType, err := isImage(file)
|
|
|
|
|
if err != nil || !isImg {
|
|
|
|
|
http.Error(response, "invalid file", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key := minio.GetKey(&minio.GetKeyOptions{
|
|
|
|
|
MimeType: contentType,
|
|
|
|
|
UploadType: minio.HubIcon,
|
|
|
|
|
HubId: hub.Id,
|
|
|
|
|
})
|
|
|
|
|
if err = minio.Upload(ctx, key, file, header.Size, contentType, map[string]string{
|
|
|
|
|
"originalName": header.Filename,
|
|
|
|
|
}); err != nil {
|
|
|
|
|
http.Error(response, "upload failed", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if hub.IconUrl != "" {
|
|
|
|
|
if err = minio.Delete(ctx, hub.IconUrl); err != nil {
|
|
|
|
|
minio.Delete(ctx, key)
|
|
|
|
|
http.Error(response, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
hub.IconUrl = key
|
|
|
|
|
response.WriteHeader(http.StatusCreated)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleHubSetBg(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
_, hub, ctx, _, ok := hubPermissionContext(response, request, hubBackground, types.PermissionSetHubBg)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file, header, err := request.FormFile("file")
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(response, "missing file", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
isImg, contentType, err := isImage(file)
|
|
|
|
|
if err != nil || !isImg {
|
|
|
|
|
http.Error(response, "invalid file", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key := minio.GetKey(&minio.GetKeyOptions{
|
|
|
|
|
MimeType: contentType,
|
|
|
|
|
UploadType: minio.HubBackground,
|
|
|
|
|
HubId: hub.Id,
|
|
|
|
|
})
|
|
|
|
|
if err = minio.Upload(ctx, key, file, header.Size, contentType, map[string]string{
|
|
|
|
|
"originalName": header.Filename,
|
|
|
|
|
}); err != nil {
|
|
|
|
|
http.Error(response, "upload failed", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if hub.BgUrl != "" {
|
|
|
|
|
if err = minio.Delete(ctx, hub.BgUrl); err != nil {
|
|
|
|
|
minio.Delete(ctx, key)
|
|
|
|
|
http.Error(response, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
hub.BgUrl = key
|
|
|
|
|
response.WriteHeader(http.StatusCreated)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleGetHubIcon(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
if !validCheckWithResponseOnFail(response, request, normal) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx := request.Context()
|
|
|
|
|
_, _, hub, err := getHubUserIfValidWithResponseOnFail(ctx, response, request)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if hub.IconUrl == "" {
|
|
|
|
|
http.Error(response, "hub has no icon", http.StatusNoContent)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
url, meta, err := minio.GetDownloadUrlAndMetadata(ctx, hub.IconUrl)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(response, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iconData, err := json.Marshal(map[string]any{
|
|
|
|
|
"url": url.String(),
|
|
|
|
|
"metadata": meta,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(response, "json error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response.WriteHeader(http.StatusOK)
|
|
|
|
|
response.Write(iconData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleGetHubBg(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
if !validCheckWithResponseOnFail(response, request, normal) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx := request.Context()
|
|
|
|
|
_, _, hub, err := getHubUserIfValidWithResponseOnFail(ctx, response, request)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if hub.BgUrl == "" {
|
|
|
|
|
http.Error(response, "hub has no background", http.StatusNoContent)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
url, meta, err := minio.GetDownloadUrlAndMetadata(ctx, hub.BgUrl)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(response, "internal server error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bgData, err := json.Marshal(map[string]any{
|
|
|
|
|
"url": url.String(),
|
|
|
|
|
"metadata": meta,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(response, "json error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response.WriteHeader(http.StatusOK)
|
|
|
|
|
response.Write(bgData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleHubRemove(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
_, hub, _, _, ok := hubPermissionContext(response, request, normal, types.PermissionRemoveHub)
|
|
|
|
|
_, hub, ctx, _, ok := hubPermissionContext(response, request, normal, types.PermissionRemoveHub)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := postgresql.HubDelete(ctx, hub.Id); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
cache.DeleteHub(hub)
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleHubToggleUserColorAllowed(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
_, hub, _, _, ok := hubPermissionContext(response, request, normal, types.PermissionSetUserColorAllowed)
|
|
|
|
|
_, hub, ctx, _, ok := hubPermissionContext(response, request, normal, types.PermissionSetUserColorAllowed)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
hub.UserColorAllowed = !hub.UserColorAllowed
|
|
|
|
|
if err := postgresql.HubUpdate(ctx, hub, &types.HubUpdate{UserColorAllowed: true}); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleSetHubJoinRole(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
_, hub, ctx, _, ok := hubPermissionContext(response, request, normal, types.PermissionSetHubJoinRole)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
newRoleId, err := convertions.StringToUint8(request.FormValue("new_role_id"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(response, "bad role_id", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
newRole := hub.Roles[newRoleId]
|
|
|
|
|
if newRole == nil {
|
|
|
|
|
http.Error(response, "bad role_id", http.StatusNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
hub.JoinRole = newRole
|
|
|
|
|
if err = postgresql.HubUpdate(ctx, hub, &types.HubUpdate{JoinRole: true}); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleHubUserRemove(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
_, hub, _, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionRemoveUser)
|
|
|
|
|
_, hub, ctx, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionRemoveUser)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -669,6 +586,10 @@ func HandleHubUserRemove(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
delete(target.Hubs, hub.Id)
|
|
|
|
|
target.Mu.Unlock()
|
|
|
|
|
|
|
|
|
|
if err = postgresql.HubUserDelete(ctx, hub.Id, targetId); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
hub.Mu.Lock()
|
|
|
|
|
delete(hub.Users, targetId)
|
|
|
|
|
hub.Mu.Unlock()
|
|
|
|
@@ -677,7 +598,7 @@ func HandleHubUserRemove(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleHubRenameUser(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
_, hub, _, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionRenameUser)
|
|
|
|
|
_, hub, ctx, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionRenameUser)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -700,20 +621,28 @@ func HandleHubRenameUser(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
target.Name = newName
|
|
|
|
|
if err = postgresql.HubUserUpdate(ctx, hub.Id, target, &types.HubUserUpdate{Name: true}); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleHubRenameSelf(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
requestor, _, _, _, ok := hubPermissionContext(response, request, normal, types.PermissionSelfRename)
|
|
|
|
|
requestor, hub, ctx, _, ok := hubPermissionContext(response, request, normal, types.PermissionSelfRename)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
requestor.Name = request.FormValue("new_name")
|
|
|
|
|
if err := postgresql.HubUserUpdate(ctx, hub.Id, requestor, &types.HubUserUpdate{Name: true}); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleHubToggleMuteUser(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
_, hub, _, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionMuteUser)
|
|
|
|
|
_, hub, ctx, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionMuteUser)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -735,11 +664,15 @@ func HandleHubToggleMuteUser(response http.ResponseWriter, request *http.Request
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
target.IsMuted = !target.IsMuted
|
|
|
|
|
if err = postgresql.HubUserUpdate(ctx, hub.Id, target, &types.HubUserUpdate{IsMuted: true}); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleHubUserAddRole(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
_, hub, _, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionUserAddRole)
|
|
|
|
|
_, hub, ctx, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionUserAddRole)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -772,11 +705,15 @@ func HandleHubUserAddRole(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
}
|
|
|
|
|
target.Roles.Add(roleId)
|
|
|
|
|
updateChannelCacheForSpecUser(target, hub)
|
|
|
|
|
if err = postgresql.HubUserUpdate(ctx, hub.Id, target, &types.HubUserUpdate{Roles: true}); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleHubUserRemoveRole(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
_, hub, _, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionUserRemoveRole)
|
|
|
|
|
_, hub, ctx, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionUserRemoveRole)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -804,11 +741,15 @@ func HandleHubUserRemoveRole(response http.ResponseWriter, request *http.Request
|
|
|
|
|
}
|
|
|
|
|
target.Roles.Remove(roleId)
|
|
|
|
|
updateChannelCacheForSpecUser(target, hub)
|
|
|
|
|
if err = postgresql.HubUserUpdate(ctx, hub.Id, target, &types.HubUserUpdate{Roles: true}); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleHubCreateRole(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
_, hub, _, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionCreateRole)
|
|
|
|
|
_, hub, ctx, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionCreateRole)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -832,18 +773,23 @@ func HandleHubCreateRole(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
http.Error(response, "no role slots available", http.StatusConflict)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
hub.Roles[freeId] = &types.HubRole{
|
|
|
|
|
newRole := &types.HubRole{
|
|
|
|
|
Id: freeId,
|
|
|
|
|
Name: name,
|
|
|
|
|
Color: types.RandomRgba(),
|
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
|
}
|
|
|
|
|
hub.Roles[freeId] = newRole
|
|
|
|
|
hub.Mu.Unlock()
|
|
|
|
|
if err := postgresql.HubRoleSave(ctx, hub.Id, newRole); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusCreated)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleHubRemoveRole(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
_, hub, _, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionRemoveRole)
|
|
|
|
|
_, hub, ctx, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionRemoveRole)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -869,11 +815,15 @@ func HandleHubRemoveRole(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
}
|
|
|
|
|
hub.Roles[targetRoleId] = nil
|
|
|
|
|
hub.Mu.Unlock()
|
|
|
|
|
if err := postgresql.HubRoleDelete(ctx, hub.Id, targetRoleId); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleRoleSetName(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
_, hub, _, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionSetRoleName)
|
|
|
|
|
_, hub, ctx, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionSetRoleName)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -901,11 +851,15 @@ func HandleRoleSetName(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
}
|
|
|
|
|
role.Name = newName
|
|
|
|
|
hub.Mu.Unlock()
|
|
|
|
|
if err = postgresql.HubRoleUpdate(ctx, hub.Id, role, &types.HubRoleUpdate{Name: true}); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleRoleSetColor(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
_, hub, _, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionSetRoleColor)
|
|
|
|
|
_, hub, ctx, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionSetRoleColor)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -933,11 +887,15 @@ func HandleRoleSetColor(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
}
|
|
|
|
|
role.Color = color
|
|
|
|
|
hub.Mu.Unlock()
|
|
|
|
|
if err = postgresql.HubRoleUpdate(ctx, hub.Id, role, &types.HubRoleUpdate{Color: true}); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleRoleSetPermissions(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
requestor, hub, _, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionSetRolePermissions)
|
|
|
|
|
requestor, hub, ctx, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionSetRolePermissions)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -994,21 +952,29 @@ func HandleRoleSetPermissions(response http.ResponseWriter, request *http.Reques
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateChannelCacheForSpecRole(targetRole, hub)
|
|
|
|
|
if err := postgresql.HubRoleUpdate(ctx, hub.Id, targetRole, &types.HubRoleUpdate{Permissions: true}); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleRoleSelfRemove(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
requestor, hub, _, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionUserSelfRoleRemove)
|
|
|
|
|
requestor, hub, ctx, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionUserSelfRoleRemove)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
requestor.Roles.Remove(usedRoleId)
|
|
|
|
|
updateChannelCacheForSpecUser(requestor, hub)
|
|
|
|
|
if err := postgresql.HubUserUpdate(ctx, hub.Id, requestor, &types.HubUserUpdate{Roles: true}); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleChannelCreate(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
requestor, hub, _, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionCreateChannel)
|
|
|
|
|
requestor, hub, ctx, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionCreateChannel)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -1046,11 +1012,15 @@ func HandleChannelCreate(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
hub.Mu.Unlock()
|
|
|
|
|
|
|
|
|
|
updateChannelCacheForSpecChannel(newHubChannel, hub)
|
|
|
|
|
if err := postgresql.HubChannelSave(ctx, hub.Id, newHubChannel); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleChannelRemove(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
_, hub, _, _, ok := hubPermissionContext(response, request, normal, types.PermissionRemoveChannel)
|
|
|
|
|
_, hub, ctx, _, ok := hubPermissionContext(response, request, normal, types.PermissionRemoveChannel)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -1061,17 +1031,21 @@ func HandleChannelRemove(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hub.Mu.Lock()
|
|
|
|
|
if _, ok := hub.Channels[channelId]; !ok {
|
|
|
|
|
if _, ok = hub.Channels[channelId]; !ok {
|
|
|
|
|
hub.Mu.Unlock()
|
|
|
|
|
http.Error(response, "no such channel", http.StatusNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
delete(hub.Channels, channelId)
|
|
|
|
|
hub.Mu.Unlock()
|
|
|
|
|
if err = postgresql.HubChannelDelete(ctx, channelId); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
func HandleChannelSetName(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
_, hub, _, _, ok := hubPermissionContext(response, request, normal, types.PermissionSetChannelName)
|
|
|
|
|
_, hub, ctx, _, ok := hubPermissionContext(response, request, normal, types.PermissionSetChannelName)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -1095,11 +1069,15 @@ func HandleChannelSetName(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
}
|
|
|
|
|
channel.Name = newName
|
|
|
|
|
hub.Mu.Unlock()
|
|
|
|
|
if err = postgresql.HubChannelUpdate(ctx, channel, &types.HubChannelUpdate{Name: true}); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleChannelSetDescription(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
_, hub, _, _, ok := hubPermissionContext(response, request, normal, types.PermissionSetChannelDescription)
|
|
|
|
|
_, hub, ctx, _, ok := hubPermissionContext(response, request, normal, types.PermissionSetChannelDescription)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -1119,11 +1097,15 @@ func HandleChannelSetDescription(response http.ResponseWriter, request *http.Req
|
|
|
|
|
}
|
|
|
|
|
channel.Description = newDescription
|
|
|
|
|
hub.Mu.Unlock()
|
|
|
|
|
if err = postgresql.HubChannelUpdate(ctx, channel, &types.HubChannelUpdate{Description: true}); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func handleChannelRolePermission(response http.ResponseWriter, request *http.Request, perm types.Permissions, modify func(*types.HubChannel, uint8, bool)) {
|
|
|
|
|
_, hub, _, usedRoleId, ok := hubPermissionContext(response, request, normal, perm)
|
|
|
|
|
func handleChannelRolePermission(response http.ResponseWriter, request *http.Request, perm types.Permissions, updateList *types.HubChannelUpdate, modify func(*types.HubChannel, uint8, bool)) {
|
|
|
|
|
_, hub, ctx, usedRoleId, ok := hubPermissionContext(response, request, normal, perm)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -1154,11 +1136,15 @@ func handleChannelRolePermission(response http.ResponseWriter, request *http.Req
|
|
|
|
|
hub.Mu.Unlock()
|
|
|
|
|
|
|
|
|
|
updateChannelCacheForSpecChannel(channel, hub)
|
|
|
|
|
if err = postgresql.HubChannelUpdate(ctx, channel, updateList); err != nil {
|
|
|
|
|
http.Error(response, "db error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleChannelSetPermittedVisibleRole(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
handleChannelRolePermission(response, request, types.PermissionSetChannelPermittedVisibleRoles, func(c *types.HubChannel, roleId uint8, allow bool) {
|
|
|
|
|
handleChannelRolePermission(response, request, types.PermissionSetChannelPermittedVisibleRoles, &types.HubChannelUpdate{RolesCanView: true}, func(c *types.HubChannel, roleId uint8, allow bool) {
|
|
|
|
|
if allow {
|
|
|
|
|
c.RolesCanView.Add(roleId)
|
|
|
|
|
} else {
|
|
|
|
@@ -1168,7 +1154,7 @@ func HandleChannelSetPermittedVisibleRole(response http.ResponseWriter, request
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleChannelSetPermittedSendRole(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
handleChannelRolePermission(response, request, types.PermissionSetChannelPermittedSendMessageRoles, func(c *types.HubChannel, roleId uint8, allow bool) {
|
|
|
|
|
handleChannelRolePermission(response, request, types.PermissionSetChannelPermittedSendMessageRoles, &types.HubChannelUpdate{RolesCanMessage: true}, func(c *types.HubChannel, roleId uint8, allow bool) {
|
|
|
|
|
if allow {
|
|
|
|
|
c.RolesCanMessage.Add(roleId)
|
|
|
|
|
} else {
|
|
|
|
@@ -1178,7 +1164,7 @@ func HandleChannelSetPermittedSendRole(response http.ResponseWriter, request *ht
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleChannelSetPermittedHistoryRole(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
handleChannelRolePermission(response, request, types.PermissionSetChannelPermittedReadHistoryRoles, func(c *types.HubChannel, roleId uint8, allow bool) {
|
|
|
|
|
handleChannelRolePermission(response, request, types.PermissionSetChannelPermittedReadHistoryRoles, &types.HubChannelUpdate{RolesCanReadHistory: true}, func(c *types.HubChannel, roleId uint8, allow bool) {
|
|
|
|
|
if allow {
|
|
|
|
|
c.RolesCanReadHistory.Add(roleId)
|
|
|
|
|
} else {
|
|
|
|
|