cwtch/model/message.go

92 lines
2.0 KiB
Go
Raw Normal View History

2018-03-09 20:44:13 +00:00
package model
import (
"sort"
"sync"
2018-03-09 20:44:13 +00:00
"time"
)
2018-05-16 20:53:09 +00:00
// Timeline encapsulates a collection of ordered messages, and a mechanism to access them
// in a threadsafe manner.
type Timeline struct {
2018-05-28 17:44:47 +00:00
Messages []Message
SignedGroupID []byte
2018-05-28 17:44:47 +00:00
lock sync.Mutex
}
2018-03-15 16:33:26 +00:00
// Message is a local representation of a given message sent over a group chat channel.
2018-03-09 20:44:13 +00:00
type Message struct {
Timestamp time.Time
Received time.Time
PeerID string
Message string
Signature []byte
Verified bool
PreviousMessageSig []byte
}
func compareSignatures(a []byte, b []byte) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
// GetMessages returns a copy of the entire timeline
2018-05-20 19:58:16 +00:00
func (t *Timeline) GetMessages() []Message {
2018-05-20 18:29:46 +00:00
t.lock.Lock()
2018-05-20 19:58:16 +00:00
messages := make([]Message, len(t.Messages))
copy(messages[:], t.Messages[:])
2018-05-20 18:29:46 +00:00
t.lock.Unlock()
2018-05-20 19:58:16 +00:00
return messages
2018-05-20 18:29:46 +00:00
}
2018-05-16 20:53:09 +00:00
// Len gets the length of the timeline
2018-05-20 18:29:46 +00:00
func (t *Timeline) Len() int {
return len(t.Messages)
}
2018-05-16 20:53:09 +00:00
// Swap swaps 2 messages on the timeline.
2018-05-20 18:29:46 +00:00
func (t *Timeline) Swap(i, j int) {
t.Messages[i], t.Messages[j] = t.Messages[j], t.Messages[i]
}
2018-05-16 20:53:09 +00:00
2018-05-28 18:05:06 +00:00
// Less checks 2 messages (i and j) in the timeline and returns true if i occcured before j, else false
2018-05-20 18:29:46 +00:00
func (t *Timeline) Less(i, j int) bool {
2018-05-03 17:59:58 +00:00
if t.Messages[i].Timestamp.Before(t.Messages[j].Timestamp) {
return true
}
if compareSignatures(t.Messages[i].PreviousMessageSig, t.SignedGroupID) {
return true
}
if compareSignatures(t.Messages[i].Signature, t.Messages[j].PreviousMessageSig) {
return true
}
return false
}
2018-05-16 20:53:09 +00:00
// Insert inserts a message into the timeline in a thread safe way.
func (t *Timeline) Insert(mi *Message) {
t.lock.Lock()
defer t.lock.Unlock()
2018-05-03 04:12:45 +00:00
2018-05-03 06:01:15 +00:00
for _, m := range t.Messages {
// If the message already exists, then we don't add it
2018-05-03 04:12:45 +00:00
if compareSignatures(m.Signature, mi.Signature) {
return
}
}
t.Messages = append(t.Messages, *mi)
sort.Sort(t)
2018-03-09 20:44:13 +00:00
}