revork structures for hub

This commit is contained in:
2026-04-25 16:04:53 +02:00
parent 635139aad2
commit df1e969d49
9 changed files with 165 additions and 157 deletions
+132 -68
View File
@@ -2,7 +2,6 @@ package types
import (
"crypto/sha256"
"go-socket/packages/Enums/permission"
"math/rand/v2"
"sync"
"time"
@@ -25,18 +24,13 @@ 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"`
Pronouns string `json:"pronouns"`
Description string `json:"description"`
Avatar string `json:"avatar"`
ProfileBg string `json:"profileBackground"`
AvatarUrl string `json:"avatarUrl"`
ProfileBgUrl string `json:"profileBackgroundUrl"`
PasswordHash string `json:"-"`
CreatedAt time.Time `json:"createdAt"`
WsConn *websocket.Conn `json:"-"`
@@ -66,12 +60,11 @@ type Connection struct {
State ConnectionState.ConnectionState `json:"state"`
}
func CreateConn() *Connection {
func NewConn() *Connection {
return &Connection{
MessagesBuff: make([]*Message, config.MaxDirectMsgCache),
}
}
func (conn *Connection) AddMessageToBuff(message *Message) {
conn.Mu.Lock()
defer conn.Mu.Unlock()
@@ -130,82 +123,153 @@ type WsAuthMessage struct {
Error string `json:"error"`
}
type Permissions uint32
const (
// Hub permissions
PermissionSetHubName Permissions = 1 << iota
PermissionSetHubIcon
PermissionSetHubBg
PermissionSetHubColor
PermissionRemoveHub
PermissionSetUserColorAllowed
// User permissions
PermissionAddUser
PermissionRemoveUser
PermissionRenameUser
PermissionMuteUser
// Role permissions
PermissionAddRole
PermissionRemoveRole
PermissionChangeRoleName
PermissionChangeRoleColor
PermissionChangeRoleGlobals
PermissionOnlySelfRoleRemove
// Channel group permissions
PermissionAddChannelGroup
PermissionRemoveChannelGroup
PermissionSetChannelGroupName
PermissionSetChannelGroupColor
PermissionSetChannelGroupPermittedVisibleRoles
// Channel permissions
PermissionAddChannel
PermissionRemoveChannel
PermissionSetChannelName
PermissionSetChannelIcon
PermissionSetChannelPermittedVisibleRoles
PermissionSetChannelPermittedSendMessageRoles
PermissionSetChannelPermittedReadHistoryRoles
)
type Hub struct {
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"`
Mu sync.RWMutex `json:"-"`
Roles []*HubRole `json:"-"`
Users []*HubUser `json:"-"`
Groups []*HubGroup `json:"-"`
Channels []*HubChannel `json:"-"`
Name string `json:"name"`
IconUrl string `json:"iconUrl"`
BgUrl string `json:"bgUrl"`
Id uuid.UUID `json:"id"`
Color Rgba `json:"color"`
UserColorAllowed bool `json:"userColorAllowed"`
}
func CreateHub() *Hub {
func NewHub() *Hub {
return &Hub{
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),
Roles: make([]*HubRole, 0, 255),
Users: make([]*HubUser, 0, 255),
Groups: make([]*HubGroup, 0, 255),
Channels: make([]*HubChannel, 0, 255),
}
}
type HubChannelGroup struct {
Id uuid.UUID `json:"id"`
Name string `json:"name"`
Color Rgba `json:"color"`
Position uint8 `json:"position"`
type HubRole struct {
Name string `json:"role"`
CreatedAt time.Time `json:"createdAt"`
Permissions Permissions `json:"permissions"`
Color Rgba `json:"color"`
Id uint8 `json:"id"`
BoundedGroup uint8 `json:"boundedGroup"` // BoundedGroup 0 for global
}
type HubChannel struct {
Id uuid.UUID `json:"id"`
Name uuid.UUID `json:"name"`
ParentGroupId uuid.UUID `json:"parentGroupId"`
Position uint8 `json:"position"`
func (h *HubRole) GrantPermission(r Permissions) {
h.Permissions |= r
}
func (h *HubRole) RevokePermission(r Permissions) {
h.Permissions &^= r
}
func (h *HubRole) HasPermission(r Permissions) bool {
return h.Permissions&r != 0
}
type HubGroup struct {
Name string `json:"name"`
CreatedAt time.Time `json:"createdAt"`
Color Rgba `json:"color"`
Id uint8 `json:"id"` // Id 0 for unused
}
type HubUser struct {
Id uuid.UUID `json:"id"`
Username string `json:"username"`
GlobalRoles []uint8 `json:"globalRoles"`
ChannelGroupRoles map[uint8]pairUuidHubChannelGroupRole `json:"ChannelGroupRoles"`
CreatedAt time.Time `json:"createdAt"`
Mu sync.RWMutex `json:"mu"`
Roles []*HubRole `json:"roles"`
Name string `json:"name"`
OriginalId uuid.UUID `json:"originalId"`
IsMuted bool `json:"isMuted"`
}
type HubGlobalRole struct {
Name string `json:"role"`
Id uint8 `json:"id"`
RolePermission permission.Global `json:"rolePermission"`
Color Rgba `json:"color"`
func NewHubUser() *HubUser {
return &HubUser{
Roles: make([]*HubRole, 0, config.MaxUserHubRoles),
}
}
func (h *HubGlobalRole) GrantPermission(r permission.Global) {
h.RolePermission |= r
}
func (h *HubGlobalRole) RevokePermission(r permission.Global) {
h.RolePermission &^= r
}
func (h *HubGlobalRole) HasPermission(r permission.Global) bool {
return h.RolePermission&r != 0
type HubChannel struct {
Mu sync.RWMutex `json:"-"`
MessagesBuff []*Message `json:"-"`
Name string `json:"name"`
Description string `json:"description"`
IconUrl string `json:"iconUrl"`
CreatedAt time.Time `json:"createdAt"`
NextBuffIdx uint32 `json:"-"`
Id uint8 `json:"id"`
HaveOverflowed bool `json:"-"`
}
type HubChannelGroupRole struct {
Name string `json:"role"`
Id uint8 `json:"id"`
RolePermission permission.ChannelGroup `json:"rolePermission"`
Color Rgba `json:"color"`
func NewHubChannel() *HubChannel {
return &HubChannel{
MessagesBuff: make([]*Message, config.MaxHubChannelMsgCache),
}
}
func (conn *HubChannel) AddMessageToBuff(message *Message) {
conn.Mu.Lock()
defer conn.Mu.Unlock()
size := uint32(len(conn.MessagesBuff))
conn.MessagesBuff[conn.NextBuffIdx%size] = message
conn.NextBuffIdx++
if conn.NextBuffIdx >= size {
conn.HaveOverflowed = true
}
}
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
// GetSortedMessagesBuff returns slice and its valid length.
func (conn *HubChannel) GetSortedMessagesBuff() ([]*Message, uint32) {
conn.Mu.RLock()
defer conn.Mu.RUnlock()
size := uint32(len(conn.MessagesBuff))
if !conn.HaveOverflowed {
return conn.MessagesBuff, conn.NextBuffIdx
}
sorted := make([]*Message, size)
for i := uint32(0); i < size; i++ {
sorted[i] = conn.MessagesBuff[(conn.NextBuffIdx+i)%size]
}
return sorted, size
}