add bit mask for bounded roles resolving

This commit is contained in:
2026-04-25 20:42:37 +02:00
parent df1e969d49
commit f22bb43346
2 changed files with 108 additions and 133 deletions
+75 -32
View File
@@ -2,6 +2,7 @@ package types
import (
"crypto/sha256"
"math"
"math/rand/v2"
"sync"
"time"
@@ -146,6 +147,8 @@ const (
PermissionChangeRoleName
PermissionChangeRoleColor
PermissionChangeRoleGlobals
PermissionNoSelfRoleRemove
PermissionOnlySelfRoleRemove
// Channel group permissions
@@ -165,26 +168,57 @@ const (
PermissionSetChannelPermittedReadHistoryRoles
)
func (p *Permissions) Grant(permissions Permissions) {
*p |= permissions
}
func (p *Permissions) GrantMultiple(perms []Permissions) {
for _, perm := range perms {
*p |= perm
}
}
func (p *Permissions) Revoke(permissions Permissions) {
*p &^= permissions
}
func (p *Permissions) Has(permissions Permissions) bool {
return *p&permissions != 0
}
func PermissionAll() Permissions {
return math.MaxUint32
}
type BoundRoles uint64
func (b *BoundRoles) Add(id uint8) {
*b |= 1 << id
}
func (b *BoundRoles) Remove(id uint8) {
*b &^= 1 << id
}
func (b *BoundRoles) Has(id uint8) bool {
return *b&(1<<id) != 0
}
type Hub struct {
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"`
Mu sync.RWMutex `json:"-"`
CreatedAt time.Time `json:"createdAt"`
Roles [256]*HubRole `json:"-"`
Users [256]*HubUser `json:"-"`
Groups [256]*HubGroup `json:"-"`
Channels map[uuid.UUID]*HubChannel `json:"-"`
Name string `json:"name"`
IconUrl string `json:"iconUrl"`
BgUrl string `json:"backgroundUrl"`
JoinRole *HubRole `json:"joinRole"`
Id uuid.UUID `json:"id"`
Creator uuid.UUID `json:"creator"`
Color Rgba `json:"color"`
UserColorAllowed bool `json:"userColorAllowed"`
}
func NewHub() *Hub {
return &Hub{
Roles: make([]*HubRole, 0, 255),
Users: make([]*HubUser, 0, 255),
Groups: make([]*HubGroup, 0, 255),
Channels: make([]*HubChannel, 0, 255),
Channels: make(map[uuid.UUID]*HubChannel),
}
}
@@ -208,36 +242,45 @@ func (h *HubRole) HasPermission(r Permissions) bool {
}
type HubGroup struct {
Name string `json:"name"`
CreatedAt time.Time `json:"createdAt"`
Color Rgba `json:"color"`
Id uint8 `json:"id"` // Id 0 for unused
Name string `json:"name"`
CreatedAt time.Time `json:"createdAt"`
Color Rgba `json:"color"`
RolesCanView BoundRoles `json:"rolesCanView"`
Id uint8 `json:"id"` // Id 0 for unused
}
func NewHubGroup() *HubGroup {
return &HubGroup{}
}
type HubUser struct {
Mu sync.RWMutex `json:"mu"`
Roles []*HubRole `json:"roles"`
CreatedAt time.Time `json:"createdAt"`
Roles BoundRoles `json:"-"`
Name string `json:"name"`
OriginalId uuid.UUID `json:"originalId"`
IsMuted bool `json:"isMuted"`
}
func NewHubUser() *HubUser {
return &HubUser{
Roles: make([]*HubRole, 0, config.MaxUserHubRoles),
}
return &HubUser{}
}
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:"-"`
Mu sync.RWMutex `json:"-"`
MessagesBuff []*Message `json:"-"`
Name string `json:"name"`
Description string `json:"description"`
IconUrl string `json:"iconUrl"`
CreatedAt time.Time `json:"createdAt"`
RolesCanView BoundRoles `json:"rolesCanView"`
RolesCanMessage BoundRoles `json:"rolesCanMessage"`
RolesCanReadHistory BoundRoles `json:"rolesCanReadHistory"`
NextBuffIdx uint32 `json:"-"`
Id uuid.UUID `json:"id"`
ParentId uint8 `json:"parentId"`
Position uint8 `json:"position"`
HaveOverflowed bool `json:"-"`
}
func NewHubChannel() *HubChannel {