add fetching message history
This commit is contained in:
+37
-7
@@ -24,12 +24,42 @@ type User struct {
|
||||
}
|
||||
|
||||
type Connection struct {
|
||||
Id uuid.UUID `json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
MessagesBuf [MaxDirectMsgCache]*Message `json:"-"`
|
||||
RequestorId uint32 `json:"requestorId"`
|
||||
RecipientId uint32 `json:"recipientId"`
|
||||
State ConnectionState.ConnectionState `json:"state"`
|
||||
Mu sync.RWMutex `json:"-"`
|
||||
Id uuid.UUID `json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
MessagesBuff [MaxDirectMsgCache]*Message `json:"-"`
|
||||
NextBuffIdx uint32 `json:"-"`
|
||||
HaveOverflowed bool `json:"-"`
|
||||
RequestorId uint32 `json:"requestorId"`
|
||||
RecipientId uint32 `json:"recipientId"`
|
||||
State ConnectionState.ConnectionState `json:"state"`
|
||||
}
|
||||
|
||||
func (conn *Connection) AddMessageToBuff(message *Message) {
|
||||
conn.Mu.Lock()
|
||||
defer conn.Mu.Unlock()
|
||||
|
||||
conn.MessagesBuff[conn.NextBuffIdx%MaxDirectMsgCache] = message
|
||||
conn.NextBuffIdx++
|
||||
if conn.NextBuffIdx >= MaxDirectMsgCache {
|
||||
conn.HaveOverflowed = true
|
||||
}
|
||||
}
|
||||
|
||||
// GetSortedMessagesBuff returns arr, length
|
||||
func (conn *Connection) GetSortedMessagesBuff() (*[MaxDirectMsgCache]*Message, uint32) {
|
||||
conn.Mu.RLock()
|
||||
defer conn.Mu.RUnlock()
|
||||
|
||||
if !conn.HaveOverflowed {
|
||||
return &conn.MessagesBuff, conn.NextBuffIdx
|
||||
}
|
||||
|
||||
sorted := new([MaxDirectMsgCache]*Message)
|
||||
for i := uint32(0); i < MaxDirectMsgCache; i++ {
|
||||
sorted[i] = conn.MessagesBuff[(conn.NextBuffIdx+i)%MaxDirectMsgCache]
|
||||
}
|
||||
return sorted, MaxDirectMsgCache
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
@@ -37,7 +67,7 @@ type Message struct {
|
||||
Content string `json:"content"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
Sender uint32 `json:"sender"`
|
||||
Receiver uint32 `json:"receiver"`
|
||||
Receiver uuid.UUID `json:"receiver"`
|
||||
IsGroupMessage bool `json:"isGroupMessage"`
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user