forked from cwtch.im/cwtch
Adding Method to Save Messages in p2p Transcript
This commit is contained in:
parent
8ca6589de0
commit
8083e6fa5c
|
@ -55,11 +55,11 @@ func (t *Timeline) Swap(i, j int) {
|
|||
t.Messages[i], t.Messages[j] = t.Messages[j], t.Messages[i]
|
||||
}
|
||||
|
||||
// Less checks 2 messages (i and j) in the timeline and returns true if i occcured before j, else false
|
||||
// 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
|
||||
if t.Messages[j].Timestamp.Before(t.Messages[i].Timestamp) {
|
||||
return false
|
||||
}
|
||||
|
||||
if compareSignatures(t.Messages[i].PreviousMessageSig, t.SignedGroupID) {
|
||||
|
|
|
@ -22,6 +22,7 @@ type PublicProfile struct {
|
|||
Blocked bool
|
||||
Onion string
|
||||
Attributes map[string]string
|
||||
Timeline Timeline
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
|
@ -102,6 +103,25 @@ func (p *Profile) RejectInvite(groupID string) {
|
|||
p.lock.Unlock()
|
||||
}
|
||||
|
||||
// AddMessageToContactTimeline allows the saving of a message sent via a direct connection chat to the profile.
|
||||
func (p *Profile) AddMessageToContactTimeline(onion string, fromMe bool, message string, sent time.Time) {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
contact, ok := p.Contacts[onion]
|
||||
|
||||
// We don't really need a Signature here, but we use it to maintain order
|
||||
now := time.Now()
|
||||
sig := p.SignMessage(onion + message + sent.String() + now.String())
|
||||
if ok {
|
||||
if fromMe {
|
||||
contact.Timeline.Insert(&Message{PeerID: p.Onion, Message: message, Timestamp: sent, Received: now, Signature: sig})
|
||||
} else {
|
||||
contact.Timeline.Insert(&Message{PeerID: onion, Message: message, Timestamp: sent, Received: now, Signature: sig})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// AcceptInvite accepts a group invite
|
||||
func (p *Profile) AcceptInvite(groupID string) (err error) {
|
||||
p.lock.Lock()
|
||||
|
|
|
@ -4,8 +4,33 @@ import (
|
|||
"cwtch.im/cwtch/protocol"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
func TestP2P(t *testing.T) {
|
||||
sarah := GenerateNewProfile("Sarah")
|
||||
alice := GenerateNewProfile("Alice")
|
||||
|
||||
sarah.AddContact(alice.Onion, &alice.PublicProfile)
|
||||
|
||||
sarah.AddMessageToContactTimeline(alice.Onion, false, "hello",time.Now())
|
||||
sarah.AddMessageToContactTimeline(alice.Onion, true, "world",time.Now())
|
||||
|
||||
contact,_ := sarah.GetContact(alice.Onion)
|
||||
for i,m := range contact.Timeline.GetMessages() {
|
||||
if i == 0 && (m.Message != "hello" || m.PeerID != alice.Onion) {
|
||||
t.Fatalf("Timeline is invalid: %v", m)
|
||||
}
|
||||
|
||||
if i == 1 && (m.Message != "world" || m.PeerID != sarah.Onion) {
|
||||
t.Fatalf("Timeline is invalid: %v", m)
|
||||
}
|
||||
t.Logf("Message: %v", m)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestProfileIdentity(t *testing.T) {
|
||||
sarah := GenerateNewProfile("Sarah")
|
||||
alice := GenerateNewProfile("Alice")
|
||||
|
@ -66,6 +91,8 @@ func TestBlockPeer(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func TestAcceptNonExistentGroup(t *testing.T) {
|
||||
sarah := GenerateNewProfile("Sarah")
|
||||
sarah.AcceptInvite("doesnotexist")
|
||||
|
|
Loading…
Reference in New Issue