part of group messages history

This commit is contained in:
2026-04-12 14:39:24 +02:00
parent 3be9619cca
commit 09a6255213
4 changed files with 53 additions and 13 deletions
+40 -13
View File
@@ -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"`
}