add user join hub
This commit is contained in:
@@ -13,7 +13,6 @@ var (
|
|||||||
Port uint32 = 8080
|
Port uint32 = 8080
|
||||||
MaxDirectMsgCache uint32 = 32
|
MaxDirectMsgCache uint32 = 32
|
||||||
MaxHubChannelMsgCache uint32 = 16
|
MaxHubChannelMsgCache uint32 = 16
|
||||||
MaxUserHubRoles uint8 = 64
|
|
||||||
FileStorageBucketName string = "communicator"
|
FileStorageBucketName string = "communicator"
|
||||||
MaxRequestBytes uint32 = 4 << 10
|
MaxRequestBytes uint32 = 4 << 10
|
||||||
MaxRequestWithFileBytes uint32 = 1 << 30
|
MaxRequestWithFileBytes uint32 = 1 << 30
|
||||||
@@ -28,8 +27,6 @@ type configFile struct {
|
|||||||
Port uint32 `toml:"port"`
|
Port uint32 `toml:"port"`
|
||||||
MaxDirectMsgCache uint32 `toml:"max_direct_messages_cache"`
|
MaxDirectMsgCache uint32 `toml:"max_direct_messages_cache"`
|
||||||
MaxHubChannelMsgCache uint32 `toml:"max_hub_channel_msg_cache"`
|
MaxHubChannelMsgCache uint32 `toml:"max_hub_channel_msg_cache"`
|
||||||
MaxUserHubRoles uint8 `toml:"max_hub_roles"`
|
|
||||||
FileStorageBucketName string `toml:"file_storage_bucket_name"`
|
|
||||||
MaxRequestBytes uint32 `toml:"max_request_bytes"`
|
MaxRequestBytes uint32 `toml:"max_request_bytes"`
|
||||||
MaxRequestWithFileBytes uint32 `toml:"max_request_with_file_bytes"`
|
MaxRequestWithFileBytes uint32 `toml:"max_request_with_file_bytes"`
|
||||||
MaxRequestWithAvatarBytes uint32 `toml:"max_request_with_avatar_bytes"`
|
MaxRequestWithAvatarBytes uint32 `toml:"max_request_with_avatar_bytes"`
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ func getHubChannelIfValidWithResponseOnFail(ctx context.Context, response http.R
|
|||||||
return nil, errors.New("invalid channelid")
|
return nil, errors.New("invalid channelid")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !haveUserPermissionsOnChannel(types.CachedUserCanView, hubUser, channel) {
|
if !haveHubUserPermissionsOnChannel(types.CachedUserCanView, hubUser, channel) {
|
||||||
return nil, errors.New("invalid channelid")
|
return nil, errors.New("invalid channelid")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,25 +1,61 @@
|
|||||||
package httpRequest
|
package httpRequest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go-socket/packages/Enums/WsEventType"
|
|
||||||
"go-socket/packages/cache"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"go-socket/packages/Enums/WsEventType"
|
||||||
|
"go-socket/packages/cache"
|
||||||
"go-socket/packages/types"
|
"go-socket/packages/types"
|
||||||
"go-socket/packages/wsServer"
|
"go-socket/packages/wsServer"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"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]
|
checkAgainst, ok := channel.UsersCachedPermissions[user.OriginalId]
|
||||||
if !ok || (permissions&checkAgainst) == checkAgainst {
|
channel.Mu.RUnlock()
|
||||||
|
if !ok || (permissions&checkAgainst) != permissions {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
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) {
|
func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
||||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||||
return
|
return
|
||||||
@@ -93,7 +129,9 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
|||||||
channel.RolesCanReadHistory.Add(rootGroup.Id)
|
channel.RolesCanReadHistory.Add(rootGroup.Id)
|
||||||
channel.RolesCanReadHistory.Add(memberRole.Id)
|
channel.RolesCanReadHistory.Add(memberRole.Id)
|
||||||
channel.UsersCachedPermissions[creator.OriginalId] = types.CachedUserPermissionsAll
|
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) {
|
func HandleChannelSendMessage(response http.ResponseWriter, request *http.Request) {
|
||||||
@@ -119,16 +157,11 @@ func HandleChannelSendMessage(response http.ResponseWriter, request *http.Reques
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !haveUserPermissionsOnChannel(types.CachedUserCanMessage, hubUser, channel) {
|
if !haveHubUserPermissionsOnChannel(types.CachedUserCanMessage, hubUser, channel) {
|
||||||
http.Error(response, "cannot send messages here", http.StatusUnauthorized)
|
http.Error(response, "cannot send messages here", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for id, userCachedPerms := range channel.UsersCachedPermissions {
|
|
||||||
if !userCachedPerms.CanReadHistory() || id == user.Id {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
message := &types.Message{
|
message := &types.Message{
|
||||||
Id: uuid.New(),
|
Id: uuid.New(),
|
||||||
AttachedFile: "",
|
AttachedFile: "",
|
||||||
@@ -138,6 +171,16 @@ func HandleChannelSendMessage(response http.ResponseWriter, request *http.Reques
|
|||||||
CreatedAt: time.Now(),
|
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 {
|
||||||
|
recipients = append(recipients, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
channel.Mu.RUnlock()
|
||||||
|
|
||||||
|
for _, id := range recipients {
|
||||||
targetUser, err := cache.GetUserById(id)
|
targetUser, err := cache.GetUserById(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// todo Add to postgres in future
|
// 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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -226,7 +226,6 @@ type Hub struct {
|
|||||||
Roles [256]*HubRole `json:"-"`
|
Roles [256]*HubRole `json:"-"`
|
||||||
Users map[uuid.UUID]*HubUser `json:"-"`
|
Users map[uuid.UUID]*HubUser `json:"-"`
|
||||||
Groups [256]*HubGroup `json:"-"`
|
Groups [256]*HubGroup `json:"-"`
|
||||||
Channels map[uuid.UUID]*HubChannel `json:"-"`
|
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
IconUrl string `json:"iconUrl"`
|
IconUrl string `json:"iconUrl"`
|
||||||
BgUrl string `json:"backgroundUrl"`
|
BgUrl string `json:"backgroundUrl"`
|
||||||
@@ -240,7 +239,6 @@ type Hub struct {
|
|||||||
func NewHub() *Hub {
|
func NewHub() *Hub {
|
||||||
return &Hub{
|
return &Hub{
|
||||||
Users: make(map[uuid.UUID]*HubUser),
|
Users: make(map[uuid.UUID]*HubUser),
|
||||||
Channels: make(map[uuid.UUID]*HubChannel),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -282,12 +280,14 @@ type HubGroup struct {
|
|||||||
Color Rgba `json:"color"`
|
Color Rgba `json:"color"`
|
||||||
RolesCanView HubBoundRoles `json:"rolesCanView"`
|
RolesCanView HubBoundRoles `json:"rolesCanView"`
|
||||||
UsersCachedPermissions map[uuid.UUID]CachedUserPermissions `json:"-"`
|
UsersCachedPermissions map[uuid.UUID]CachedUserPermissions `json:"-"`
|
||||||
|
Channels map[uuid.UUID]*HubChannel `json:"channels"`
|
||||||
Id uint8 `json:"id"` // Id 0 for unused
|
Id uint8 `json:"id"` // Id 0 for unused
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHubGroup() *HubGroup {
|
func NewHubGroup() *HubGroup {
|
||||||
return &HubGroup{
|
return &HubGroup{
|
||||||
UsersCachedPermissions: make(map[uuid.UUID]CachedUserPermissions),
|
UsersCachedPermissions: make(map[uuid.UUID]CachedUserPermissions),
|
||||||
|
Channels: make(map[uuid.UUID]*HubChannel),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user