add per channel/group for user cahcing

This commit is contained in:
2026-04-26 17:44:31 +02:00
parent 9c73a01101
commit 472b4ea703
2 changed files with 55 additions and 32 deletions
+2 -1
View File
@@ -53,7 +53,7 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
creator.Roles.Add(rootRole.Id)
memberRole := &types.HubRole{
Id: types.BoundRolesMax,
Id: types.HubBoundRolesMax,
Name: "member",
Color: types.Rgba{}.GetRandom(),
CreatedAt: hub.CreatedAt,
@@ -83,6 +83,7 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
channel.RolesCanView.Add(memberRole.Id)
channel.RolesCanReadHistory.Add(rootGroup.Id)
channel.RolesCanReadHistory.Add(memberRole.Id)
channel.UsersCanView.Add()
hub.Channels[channel.Id] = channel
}
+33 -11
View File
@@ -187,17 +187,37 @@ func PermissionAll() Permissions {
return math.MaxUint32
}
type BoundRoles [3]uint64
type HubBoundRoles [3]uint64
const BoundRolesMax uint8 = 191
const HubBoundRolesMax uint8 = 191
func (r *BoundRoles) Add(bit uint8) { r[bit>>6] |= 1 << (bit & 63) }
func (r *BoundRoles) Remove(bit uint8) { r[bit>>6] &^= 1 << (bit & 63) }
func (r *BoundRoles) Has(bit uint8) bool { return r[bit>>6]>>(bit&63)&1 == 1 }
func (r *BoundRoles) HasSameId(against BoundRoles) bool {
func (r *HubBoundRoles) Add(bit uint8) { r[bit>>6] |= 1 << (bit & 63) }
func (r *HubBoundRoles) Remove(bit uint8) { r[bit>>6] &^= 1 << (bit & 63) }
func (r *HubBoundRoles) Has(bit uint8) bool { return r[bit>>6]>>(bit&63)&1 == 1 }
func (r *HubBoundRoles) HasSameId(against HubBoundRoles) bool {
return against[0]&r[0] != 0 || against[1]&r[1] != 0 || against[2]&r[2] != 0
}
type CachedUserPermissions uint8
const (
CachedUserCanView CachedUserPermissions = 1 << iota
CachedUserCanReadHistory
CachedUserCanMessage
)
func (p *CachedUserPermissions) SetCanView() { *p |= CachedUserCanView }
func (p *CachedUserPermissions) ClearCanView() { *p &^= CachedUserCanView }
func (p CachedUserPermissions) CanView() bool { return p&CachedUserCanView != 0 }
func (p *CachedUserPermissions) SetCanReadHistory() { *p |= CachedUserCanReadHistory }
func (p *CachedUserPermissions) ClearCanReadHistory() { *p &^= CachedUserCanReadHistory }
func (p CachedUserPermissions) CanReadHistory() bool { return p&CachedUserCanReadHistory != 0 }
func (p *CachedUserPermissions) SetCanMessage() { *p |= CachedUserCanMessage }
func (p *CachedUserPermissions) ClearCanMessage() { *p &^= CachedUserCanMessage }
func (p CachedUserPermissions) CanMessage() bool { return p&CachedUserCanMessage != 0 }
type Hub struct {
Mu sync.RWMutex `json:"-"`
CreatedAt time.Time `json:"createdAt"`
@@ -244,7 +264,7 @@ func (h *HubRole) HasPermission(r Permissions) bool {
type HubUser struct {
Mu sync.RWMutex `json:"mu"`
CreatedAt time.Time `json:"createdAt"`
Roles BoundRoles `json:"-"`
Roles HubBoundRoles `json:"-"`
Name string `json:"name"` // Name empty = original name
OriginalId uuid.UUID `json:"originalId"`
IsMuted bool `json:"isMuted"`
@@ -258,7 +278,8 @@ type HubGroup struct {
Name string `json:"name"`
CreatedAt time.Time `json:"createdAt"`
Color Rgba `json:"color"`
RolesCanView BoundRoles `json:"rolesCanView"`
RolesCanView HubBoundRoles `json:"rolesCanView"`
UsersCachedPermissions map[uuid.UUID]CachedUserPermissions `json:"-"`
Id uint8 `json:"id"` // Id 0 for unused
}
@@ -273,9 +294,10 @@ type HubChannel struct {
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"`
RolesCanView HubBoundRoles `json:"rolesCanView"`
RolesCanMessage HubBoundRoles `json:"rolesCanMessage"`
RolesCanReadHistory HubBoundRoles `json:"rolesCanReadHistory"`
UsersCachedPermissions map[uuid.UUID]CachedUserPermissions `json:"-"`
NextBuffIdx uint32 `json:"-"`
Id uuid.UUID `json:"id"`
ParentId uint8 `json:"parentId"`