part of group messages history
This commit is contained in:
+12
@@ -83,6 +83,16 @@ func DbInit(ctx context.Context) {
|
|||||||
panic(err)
|
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, `
|
_, err = dbConn.Exec(ctx, `
|
||||||
CREATE TABLE IF NOT EXISTS chat_group_members (
|
CREATE TABLE IF NOT EXISTS chat_group_members (
|
||||||
group_id UUID NOT NULL REFERENCES chat_groups(id) ON DELETE CASCADE,
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DbGroup
|
||||||
@@ -4,5 +4,6 @@ const (
|
|||||||
MaxGroupsForUser uint32 = 8
|
MaxGroupsForUser uint32 = 8
|
||||||
MaxUsersInGroup uint32 = 12
|
MaxUsersInGroup uint32 = 12
|
||||||
MaxDirectMsgCache uint32 = 12
|
MaxDirectMsgCache uint32 = 12
|
||||||
|
MaxGroupMsgCache uint32 = 8
|
||||||
MessagesPartitions uint8 = 2
|
MessagesPartitions uint8 = 2
|
||||||
)
|
)
|
||||||
|
|||||||
+40
-13
@@ -78,22 +78,49 @@ type Message struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Group struct {
|
type Group struct {
|
||||||
Mu sync.RWMutex `json:"-"`
|
Mu sync.RWMutex `json:"-"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
MessagesBuff [MaxDirectMsgCache]*Message `json:"-"`
|
MessagesBuff [MaxGroupMsgCache]*Message `json:"-"`
|
||||||
NextBuffIdx uint32 `json:"-"`
|
NextBuffIdx uint32 `json:"-"`
|
||||||
Id uuid.UUID `json:"-"`
|
Id uuid.UUID `json:"-"`
|
||||||
CreatorId uuid.UUID `json:"creatorId"`
|
CreatorId uuid.UUID `json:"creatorId"`
|
||||||
OwnerId uuid.UUID `json:"ownerId"`
|
OwnerId uuid.UUID `json:"ownerId"`
|
||||||
Users map[uuid.UUID]struct{} `json:"-"`
|
Users map[uuid.UUID]struct{} `json:"-"`
|
||||||
Color [3]uint8 `json:"color"`
|
Color [3]uint8 `json:"color"`
|
||||||
EnableUserColors bool `json:"enableUserColors"`
|
EnableUserColors bool `json:"enableUserColors"`
|
||||||
HaveMessageBuffOverflowed bool `json:"-"`
|
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 {
|
type LoginReturn struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
UserId uuid.UUID `json:"userId"`
|
UserId uuid.UUID `json:"userId"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user