add user join hub

This commit is contained in:
2026-04-27 20:16:36 +02:00
parent 67203c5971
commit 6f7a913e64
4 changed files with 101 additions and 37 deletions
+84 -17
View File
@@ -1,25 +1,61 @@
package httpRequest
import (
"go-socket/packages/Enums/WsEventType"
"go-socket/packages/cache"
"net/http"
"time"
"go-socket/packages/Enums/WsEventType"
"go-socket/packages/cache"
"go-socket/packages/types"
"go-socket/packages/wsServer"
"github.com/google/uuid"
)
func haveUserPermissionsOnChannel(permissions types.CachedUserPermissions, user *types.HubUser, channel *types.HubChannel) bool {
func haveHubUserPermissionsOnChannel(permissions types.CachedUserPermissions, user *types.HubUser, channel *types.HubChannel) bool {
channel.Mu.RLock()
checkAgainst, ok := channel.UsersCachedPermissions[user.OriginalId]
if !ok || (permissions&checkAgainst) == checkAgainst {
channel.Mu.RUnlock()
if !ok || (permissions&checkAgainst) != permissions {
return false
}
return true
}
func addHubUserToPermissionCache(hub *types.Hub, user *types.HubUser) {
user.Mu.RLock()
roles := user.Roles
userId := user.OriginalId
user.Mu.RUnlock()
for _, group := range hub.Groups {
if group == nil {
continue
}
if !roles.HasSameId(group.RolesCanView) {
continue
}
group.UsersCachedPermissions[userId] = types.CachedUserCanView
for _, channel := range group.Channels {
var perms types.CachedUserPermissions
if roles.HasSameId(channel.RolesCanView) {
perms |= types.CachedUserCanView
}
if roles.HasSameId(channel.RolesCanReadHistory) {
perms |= types.CachedUserCanReadHistory
}
if roles.HasSameId(channel.RolesCanMessage) {
perms |= types.CachedUserCanMessage
}
channel.Mu.Lock()
channel.UsersCachedPermissions[userId] = perms
channel.Mu.Unlock()
}
}
}
func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
if !validCheckWithResponseOnFail(&response, request, normal) {
return
@@ -93,7 +129,9 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
channel.RolesCanReadHistory.Add(rootGroup.Id)
channel.RolesCanReadHistory.Add(memberRole.Id)
channel.UsersCachedPermissions[creator.OriginalId] = types.CachedUserPermissionsAll
hub.Channels[channel.Id] = channel
rootGroup.Channels[channel.Id] = channel
cache.SaveHub(hub)
}
func HandleChannelSendMessage(response http.ResponseWriter, request *http.Request) {
@@ -119,25 +157,30 @@ func HandleChannelSendMessage(response http.ResponseWriter, request *http.Reques
return
}
if !haveUserPermissionsOnChannel(types.CachedUserCanMessage, hubUser, channel) {
if !haveHubUserPermissionsOnChannel(types.CachedUserCanMessage, hubUser, channel) {
http.Error(response, "cannot send messages here", http.StatusUnauthorized)
return
}
message := &types.Message{
Id: uuid.New(),
AttachedFile: "",
Content: msgContent,
Sender: user.Id,
Receiver: channel.Id,
CreatedAt: time.Now(),
}
channel.Mu.RLock()
recipients := make([]uuid.UUID, 0, len(channel.UsersCachedPermissions))
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(),
if userCachedPerms.CanReadHistory() && id != user.Id {
recipients = append(recipients, id)
}
}
channel.Mu.RUnlock()
for _, id := range recipients {
targetUser, err := cache.GetUserById(id)
if err != nil {
// todo Add to postgres in future
@@ -149,3 +192,27 @@ func HandleChannelSendMessage(response http.ResponseWriter, request *http.Reques
})
}
}
func HandleHubJoin(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.StatusBadRequest)
return
}
hub, err := getHubByIdStr(ctx, request.Header.Get("hubid"))
if err != nil {
http.Error(response, "invalid hubid", http.StatusBadRequest)
return
}
hubUser := types.NewHubUser()
hubUser.OriginalId = user.Id
hubUser.Roles.Add(hub.JoinRole.Id)
hubUser.CreatedAt = time.Now()
hub.Users[hubUser.OriginalId] = hubUser
addHubUserToPermissionCache(hub, hubUser)
}