Files
go-socket/packages/types/types.go
T
2026-04-24 15:34:58 +02:00

212 lines
6.4 KiB
Go

package types
import (
"crypto/sha256"
"go-socket/packages/Enums/permission"
"math/rand/v2"
"sync"
"time"
"go-socket/packages/Enums/ConnectionState"
"go-socket/packages/Enums/WsEventType"
"go-socket/packages/config"
"github.com/coder/websocket"
"github.com/google/uuid"
)
type Rgba [4]uint8
type Sha256Hash [sha256.Size]byte
func (r Rgba) GetRandom() Rgba {
for i := range r {
r[i] = uint8(rand.IntN(256))
}
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"`
PasswordHash string `json:"-"`
CreatedAt time.Time `json:"createdAt"`
WsConn *websocket.Conn `json:"-"`
Id uuid.UUID `json:"-"`
Connections map[uuid.UUID]*Connection `json:"-"`
Color Rgba `json:"color"`
}
type UserProfileUpdateList struct {
Pronouns bool
Description bool
Color bool
Avatar bool
ProfileBg bool
}
type Connection struct {
Mu sync.RWMutex `json:"-"`
Id uuid.UUID `json:"id"`
CreatedAt time.Time `json:"createdAt"`
MessagesBuff []*Message `json:"-"`
NextBuffIdx uint32 `json:"-"`
RequestorId uuid.UUID `json:"requestorId"`
RecipientId uuid.UUID `json:"recipientId"`
UserWantingToElevate uuid.UUID `json:"userWantingToElevate"` // TODO add to database
HaveOverflowed bool `json:"-"`
State ConnectionState.ConnectionState `json:"state"`
}
func CreateConn() *Connection {
return &Connection{
MessagesBuff: make([]*Message, config.MaxDirectMsgCache),
}
}
func (conn *Connection) 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
}
}
// GetSortedMessagesBuff returns slice and its valid length.
func (conn *Connection) 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
}
type ConnectionStatusSetData struct {
Id uuid.UUID `json:"id"`
NewState ConnectionState.ConnectionState `json:"newState"`
}
type Message struct {
Id uuid.UUID `json:"id"`
AttachedFile string `json:"attachedFile"`
Content string `json:"content"`
CreatedAt time.Time `json:"createdAt"`
Sender uuid.UUID `json:"sender"`
Receiver uuid.UUID `json:"receiver"`
}
type LoginReturn struct {
Token string `json:"token"`
UserId uuid.UUID `json:"userId"`
}
type WsEventMessage struct {
Type WsEventType.WsEventType `json:"type"`
Event any `json:"event"`
}
type WsAuthMessage struct {
Success bool `json:"success"`
Error string `json:"error"`
}
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"`
}
func CreateHub() *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),
}
}
type HubChannelGroup struct {
Id uuid.UUID `json:"id"`
Name string `json:"name"`
Color Rgba `json:"color"`
Position uint8 `json:"position"`
}
type HubChannel struct {
Id uuid.UUID `json:"id"`
Name uuid.UUID `json:"name"`
ParentGroupId uuid.UUID `json:"parentGroupId"`
Position uint8 `json:"position"`
}
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"`
}
type HubGlobalRole struct {
Name string `json:"role"`
Id uint8 `json:"id"`
RolePermission permission.Global `json:"rolePermission"`
Color Rgba `json:"color"`
}
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 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
}