diff --git a/database.go b/database.go index 5e5933a..bab69b9 100644 --- a/database.go +++ b/database.go @@ -83,6 +83,16 @@ func DbInit(ctx context.Context) { panic(err) } + _, err = dbConn.Exec(ctx, ` + CREATE TABLE IF NOT EXISTS chat_messages ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + sender_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + group_id UUID NOT NULL REFERENCES chat_groups(id) ON DELETE CASCADE, + created_at TIMESTAMP NOT NULL DEFAULT NOW(), + content TEXT NOT NULL + ) + `) + _, err = dbConn.Exec(ctx, ` CREATE TABLE IF NOT EXISTS chat_group_members ( group_id UUID NOT NULL REFERENCES chat_groups(id) ON DELETE CASCADE, @@ -380,3 +390,5 @@ func DbGroupSetOwnerId(ctx context.Context, group *Group) error { return err } + +DbGroup \ No newline at end of file diff --git a/getter.go b/get.go similarity index 100% rename from getter.go rename to get.go diff --git a/globals.go b/globals.go index 835b257..5d1946b 100644 --- a/globals.go +++ b/globals.go @@ -4,5 +4,6 @@ const ( MaxGroupsForUser uint32 = 8 MaxUsersInGroup uint32 = 12 MaxDirectMsgCache uint32 = 12 + MaxGroupMsgCache uint32 = 8 MessagesPartitions uint8 = 2 ) diff --git a/structs.go b/structs.go index 34bfd9d..a3705d0 100644 --- a/structs.go +++ b/structs.go @@ -78,22 +78,49 @@ type Message struct { } type Group struct { - Mu sync.RWMutex `json:"-"` - Name string `json:"name"` - CreatedAt time.Time `json:"createdAt"` - MessagesBuff [MaxDirectMsgCache]*Message `json:"-"` - NextBuffIdx uint32 `json:"-"` - Id uuid.UUID `json:"-"` - CreatorId uuid.UUID `json:"creatorId"` - OwnerId uuid.UUID `json:"ownerId"` - Users map[uuid.UUID]struct{} `json:"-"` - Color [3]uint8 `json:"color"` - EnableUserColors bool `json:"enableUserColors"` - HaveMessageBuffOverflowed bool `json:"-"` + Mu sync.RWMutex `json:"-"` + Name string `json:"name"` + CreatedAt time.Time `json:"createdAt"` + MessagesBuff [MaxGroupMsgCache]*Message `json:"-"` + NextBuffIdx uint32 `json:"-"` + Id uuid.UUID `json:"-"` + CreatorId uuid.UUID `json:"creatorId"` + OwnerId uuid.UUID `json:"ownerId"` + Users map[uuid.UUID]struct{} `json:"-"` + Color [3]uint8 `json:"color"` + EnableUserColors bool `json:"enableUserColors"` + HaveMessageBuffOverflowed bool `json:"-"` +} + +func (g *Group) AddMessageToBuff(message *Message) { + g.Mu.Lock() + defer g.Mu.Unlock() + + g.MessagesBuff[g.NextBuffIdx%MaxGroupMsgCache] = message + g.NextBuffIdx++ + if g.NextBuffIdx >= MaxGroupMsgCache { + g.HaveMessageBuffOverflowed = true + } +} + +// GetSortedMessagesBuff returns arr, length +func (g *Group) GetSortedMessagesBuff() (*[MaxGroupMsgCache]*Message, uint32) { + g.Mu.RLock() + defer g.Mu.RUnlock() + + if !g.HaveMessageBuffOverflowed { + return &g.MessagesBuff, g.NextBuffIdx + } + + sorted := new([MaxGroupMsgCache]*Message) + for i := uint32(0); i < MaxGroupMsgCache; i++ { + sorted[i] = g.MessagesBuff[(g.NextBuffIdx+i)%MaxGroupMsgCache] + } + return sorted, MaxGroupMsgCache } type LoginReturn struct { - Token string `json:"token"` + Token string `json:"token"` UserId uuid.UUID `json:"userId"` }