48d3c6f857
- add files_metadata and files_metadata_connections tables with CRUD helpers - add FileMetadata type and Sha256Hash typedef; replace Media struct - add minio upload, presigned download URL, and key generation - fix bucket existence check to use FileStorageBucketName instead of hardcoded "main" - fix files_metadata_connections table name and trailing comma in DDL - fix column name original -> name in files_metadata schema - add canonical MIME-to-extension map with .unk fallback - add FileDownloadLinkTtl constant (24h)
116 lines
3.2 KiB
Go
116 lines
3.2 KiB
Go
package types
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"math/rand/v2"
|
|
"sync"
|
|
"time"
|
|
|
|
"go-socket/packages/Enums/ConnectionState"
|
|
"go-socket/packages/Enums/WsEventType"
|
|
"go-socket/packages/globals"
|
|
|
|
"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 User struct {
|
|
Mu sync.RWMutex
|
|
Name string
|
|
Pronouns string
|
|
PasswordHash string
|
|
CreatedAt time.Time
|
|
WsConn *websocket.Conn
|
|
Id uuid.UUID
|
|
Connections map[uuid.UUID]*Connection
|
|
Color *Rgba
|
|
}
|
|
|
|
type Connection struct {
|
|
Mu sync.RWMutex `json:"-"`
|
|
Id uuid.UUID `json:"id"`
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
MessagesBuff [globals.MaxDirectMsgCache]*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 (conn *Connection) AddMessageToBuff(message *Message) {
|
|
conn.Mu.Lock()
|
|
defer conn.Mu.Unlock()
|
|
|
|
conn.MessagesBuff[conn.NextBuffIdx%globals.MaxDirectMsgCache] = message
|
|
conn.NextBuffIdx++
|
|
if conn.NextBuffIdx >= globals.MaxDirectMsgCache {
|
|
conn.HaveOverflowed = true
|
|
}
|
|
}
|
|
|
|
// GetSortedMessagesBuff returns arr, length
|
|
func (conn *Connection) GetSortedMessagesBuff() (*[globals.MaxDirectMsgCache]*Message, uint32) {
|
|
conn.Mu.RLock()
|
|
defer conn.Mu.RUnlock()
|
|
|
|
if !conn.HaveOverflowed {
|
|
return &conn.MessagesBuff, conn.NextBuffIdx
|
|
}
|
|
|
|
sorted := new([globals.MaxDirectMsgCache]*Message)
|
|
for i := uint32(0); i < globals.MaxDirectMsgCache; i++ {
|
|
sorted[i] = conn.MessagesBuff[(conn.NextBuffIdx+i)%globals.MaxDirectMsgCache]
|
|
}
|
|
return sorted, globals.MaxDirectMsgCache
|
|
}
|
|
|
|
type ConnectionElevationData struct {
|
|
Id uuid.UUID `json:"id"`
|
|
NewState ConnectionState.ConnectionState `json:"newState"`
|
|
}
|
|
|
|
type Message struct {
|
|
Id uuid.UUID `json:"id"`
|
|
AttachedMedia string `json:"attachedMedia"`
|
|
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:"types"`
|
|
Event any `json:"event"`
|
|
}
|
|
|
|
type WsAuthMessage struct {
|
|
Success bool `json:"success"`
|
|
Error string `json:"error"`
|
|
}
|
|
|
|
type FileMetadata struct {
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
Connections []uuid.UUID `json:"connections"`
|
|
Name string `json:"name"`
|
|
Hash Sha256Hash `json:"hash"`
|
|
Id uuid.UUID `json:"id"`
|
|
UploaderId uuid.UUID `json:"uploaderId"`
|
|
}
|