From 472b4ea70354bc5634d43f66c15ce65000db406f Mon Sep 17 00:00:00 2001 From: Sisi Date: Sun, 26 Apr 2026 17:44:31 +0200 Subject: [PATCH] add per channel/group for user cahcing --- packages/httpRequest/hubs.go | 3 +- packages/types/types.go | 84 +++++++++++++++++++++++------------- 2 files changed, 55 insertions(+), 32 deletions(-) diff --git a/packages/httpRequest/hubs.go b/packages/httpRequest/hubs.go index 21f644a..7ee1754 100644 --- a/packages/httpRequest/hubs.go +++ b/packages/httpRequest/hubs.go @@ -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 } diff --git a/packages/types/types.go b/packages/types/types.go index 2d912c1..3763312 100644 --- a/packages/types/types.go +++ b/packages/types/types.go @@ -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"` @@ -242,12 +262,12 @@ func (h *HubRole) HasPermission(r Permissions) bool { } type HubUser struct { - Mu sync.RWMutex `json:"mu"` - CreatedAt time.Time `json:"createdAt"` - Roles BoundRoles `json:"-"` - Name string `json:"name"` // Name empty = original name - OriginalId uuid.UUID `json:"originalId"` - IsMuted bool `json:"isMuted"` + Mu sync.RWMutex `json:"mu"` + CreatedAt time.Time `json:"createdAt"` + Roles HubBoundRoles `json:"-"` + Name string `json:"name"` // Name empty = original name + OriginalId uuid.UUID `json:"originalId"` + IsMuted bool `json:"isMuted"` } func NewHubUser() *HubUser { @@ -255,11 +275,12 @@ func NewHubUser() *HubUser { } type HubGroup struct { - 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 + Name string `json:"name"` + CreatedAt time.Time `json:"createdAt"` + Color Rgba `json:"color"` + RolesCanView HubBoundRoles `json:"rolesCanView"` + UsersCachedPermissions map[uuid.UUID]CachedUserPermissions `json:"-"` + Id uint8 `json:"id"` // Id 0 for unused } func NewHubGroup() *HubGroup { @@ -267,20 +288,21 @@ func NewHubGroup() *HubGroup { } 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"` - 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:"-"` + 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 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"` + Position uint8 `json:"position"` + HaveOverflowed bool `json:"-"` } func NewHubChannel() *HubChannel {