idk how to plan this out

This commit is contained in:
gitGnome
2026-04-24 15:34:58 +02:00
parent 9cb05e7155
commit 635139aad2
6 changed files with 140 additions and 359 deletions
+54 -82
View File
@@ -2,6 +2,7 @@ package types
import (
"crypto/sha256"
"go-socket/packages/Enums/permission"
"math/rand/v2"
"sync"
"time"
@@ -24,6 +25,11 @@ func (r Rgba) GetRandom() Rgba {
return r
}
type pairUuidHubChannelGroupRole struct {
first uuid.UUID
second *HubChannelGroupRole
}
type User struct {
Mu sync.RWMutex `json:"-"`
Name string `json:"name"`
@@ -60,7 +66,7 @@ type Connection struct {
State ConnectionState.ConnectionState `json:"state"`
}
func NewConnection() *Connection {
func CreateConn() *Connection {
return &Connection{
MessagesBuff: make([]*Message, config.MaxDirectMsgCache),
}
@@ -124,69 +130,27 @@ type WsAuthMessage struct {
Error string `json:"error"`
}
type RolePermission uint32
const (
// Hub permissions
PermissionSetHubName RolePermission = 1 << iota
PermissionSetHubBg
PermissionSetHubColor
PermissionRemoveHub
PermissionSetUserColorAllowed
// User permissions
PermissionAddUser
PermissionRemoveUser
PermissionRenameUser
PermissionMuteUser
// Role permissions
PermissionAddRole
PermissionRemoveRole
PermissionChangeRoleName
PermissionChangeRoleColor
PermissionChangeRolePermissions
PermissionOnlySelfRoleRemove
// Channel group permissions
PermissionAddChannelGroup
PermissionRemoveChannelGroup
PermissionSetChannelGroupName
PermissionSetChannelGroupColor
PermissionSetChannelGroupPermittedVisibleRoles
// Channel permissions
PermissionAddChannel
PermissionRemoveChannel
PermissionSetChannelName
PermissionSetChannelPermittedVisibleRoles
PermissionSetChannelPermittedSendMessageRoles
PermissionSetChannelPermittedReadHistoryRoles
)
type Hub struct {
Mu sync.RWMutex `json:"-"`
Id uuid.UUID `json:"id"`
CreatedAt time.Time `json:"createdAt"`
MessagesBuff []*Message `json:"-"`
NextBuffIdx uint32 `json:"-"`
HaveOverflowed bool `json:"-"`
Users map[uuid.UUID]*HubUser `json:"-"`
Roles map[uint8]*HubRole `json:"-"`
ChannelGroups map[uuid.UUID]*HubChannelGroup `json:"-"`
Creator uuid.UUID `json:"creator"`
Name string `json:"name"`
Color Rgba `json:"color"`
AllowUserColor bool `json:"allowUserColor"`
Mu sync.RWMutex `json:"-"`
Id uuid.UUID `json:"id"`
CreatedAt time.Time `json:"createdAt"`
Users map[uuid.UUID]*HubUser `json:"-"`
GlobalRoles map[uint8]*HubGlobalRole `json:"-"`
ChannelGroupRoles map[uint8]*HubChannelGroupRole `json:"-"`
ChannelGroups map[uuid.UUID]*HubChannelGroup `json:"-"`
Creator uuid.UUID `json:"creator"`
Name string `json:"name"`
Color Rgba `json:"color"`
AllowUserColor bool `json:"allowUserColor"`
}
func NewHub() *Hub {
func CreateHub() *Hub {
return &Hub{
Id: uuid.New(),
MessagesBuff: make([]*Message, config.MaxHubMsgCache),
Users: make(map[uuid.UUID]*HubUser),
Roles: make(map[uint8]*HubRole),
ChannelGroups: make(map[uuid.UUID]*HubChannelGroup),
Id: uuid.New(),
Users: make(map[uuid.UUID]*HubUser),
GlobalRoles: make(map[uint8]*HubGlobalRole),
ChannelGroupRoles: make(map[uint8]*HubChannelGroupRole),
ChannelGroups: make(map[uuid.UUID]*HubChannelGroup),
}
}
@@ -205,35 +169,43 @@ type HubChannel struct {
}
type HubUser struct {
Id uuid.UUID `json:"id"`
Username string `json:"username"`
Roles []uint8 `json:"roles"`
CreatedAt time.Time `json:"createdAt"`
Id uuid.UUID `json:"id"`
Username string `json:"username"`
GlobalRoles []uint8 `json:"globalRoles"`
ChannelGroupRoles map[uint8]pairUuidHubChannelGroupRole `json:"ChannelGroupRoles"`
CreatedAt time.Time `json:"createdAt"`
}
type HubRole struct {
Name string `json:"role"`
Id uint8 `json:"id"`
RolePermission RolePermission `json:"rolePermission"`
Color Rgba `json:"color"`
type HubGlobalRole struct {
Name string `json:"role"`
Id uint8 `json:"id"`
RolePermission permission.Global `json:"rolePermission"`
Color Rgba `json:"color"`
}
func (h *HubRole) GrantPermission(r RolePermission) {
func (h *HubGlobalRole) GrantPermission(r permission.Global) {
h.RolePermission |= r
}
func (h *HubRole) GrantPermissions(rs []RolePermission) {
for _, r := range rs {
h.RolePermission |= r
}
}
func (h *HubRole) RevokePermission(r RolePermission) {
func (h *HubGlobalRole) RevokePermission(r permission.Global) {
h.RolePermission &^= r
}
func (h *HubRole) RevokePermissions(rs []RolePermission) {
for _, r := range rs {
h.RolePermission &^= r
}
}
func (h *HubRole) HasPermission(r RolePermission) bool {
func (h *HubGlobalRole) HasPermission(r permission.Global) bool {
return h.RolePermission&r != 0
}
type HubChannelGroupRole struct {
Name string `json:"role"`
Id uint8 `json:"id"`
RolePermission permission.ChannelGroup `json:"rolePermission"`
Color Rgba `json:"color"`
}
func (h *HubChannelGroupRole) GrantPermission(r permission.ChannelGroup) {
h.RolePermission |= r
}
func (h *HubChannelGroupRole) RevokePermission(r permission.ChannelGroup) {
h.RolePermission &^= r
}
func (h *HubChannelGroupRole) HasPermission(r permission.ChannelGroup) bool {
return h.RolePermission&r != 0
}