Message Sorting

This commit is contained in:
Sarah Jamie Lewis 2018-12-03 11:12:34 -08:00
parent 4a4c153103
commit 01a28ef3b0
2 changed files with 17 additions and 6 deletions

View File

@ -122,13 +122,11 @@ func (g *Group) AddMessage(message *protocol.DecryptedGroupMessage, sig []byte)
return timelineMessage
}
// GetTimeline provides a safe copy of the timeline-=
func (g *Group) GetTimeline() (t []Message) {
// GetTimeline provides a safe copy of the timeline
func (g *Group) GetTimeline() (timeline []Message) {
g.lock.Lock()
t = g.Timeline.GetMessages()
g.lock.Unlock()
return
defer g.lock.Unlock()
return g.Timeline.GetMessages()
}
//EncryptMessage takes a message and encrypts the message under the group key.

View File

@ -58,6 +58,11 @@ func (t *Timeline) Swap(i, j int) {
// Less checks 2 messages (i and j) in the timeline and returns true if i occurred before j, else false
func (t *Timeline) Less(i, j int) bool {
if t.Messages[i].Timestamp.Before(t.Messages[j].Timestamp) {
return true
}
// Short circuit false if j is before i, signature checks will give a wrong order in this case.
if t.Messages[j].Timestamp.Before(t.Messages[i].Timestamp) {
return false
}
@ -73,6 +78,14 @@ func (t *Timeline) Less(i, j int) bool {
return false
}
// Sort sorts the timeline in a canonical order.
// TODO: There is almost definitely a more efficient way of doing things that involve not calling this method on every timeline load.
func (t *Timeline) Sort() {
t.lock.Lock()
defer t.lock.Unlock()
sort.Sort(t)
}
// Insert inserts a message into the timeline in a thread safe way.
func (t *Timeline) Insert(mi *Message) bool {
t.lock.Lock()