From 8bde6802de8f85387212a5a7ee05ed5d845fbbfc Mon Sep 17 00:00:00 2001 From: ycc Date: Wed, 30 Nov 2022 21:31:01 +0100 Subject: [PATCH] Invitation errors when no servers defined --- client/identity.go | 7 +++++-- client/peer.go | 2 +- endtoend_test.go | 7 +++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/client/identity.go b/client/identity.go index 1d0ba85..cd2cad9 100644 --- a/client/identity.go +++ b/client/identity.go @@ -33,7 +33,7 @@ func CreateIdentity(nickname string) *Identity { return &id } -func (id *Identity) InvitePeer(MyName string, ContactName string, MessageServerIdxs []int) *meowlib.ContactCard { +func (id *Identity) InvitePeer(MyName string, ContactName string, MessageServerIdxs []int) (*meowlib.ContactCard, error) { var peer Peer var myContactCard meowlib.ContactCard peer.MyIdentity = meowlib.NewKeyPair() @@ -41,6 +41,9 @@ func (id *Identity) InvitePeer(MyName string, ContactName string, MessageServerI peer.MyLookupKp = meowlib.NewKeyPair() peer.Name = ContactName peer.InvitationId = uuid.New().String() + if id.MessageServers.Servers == nil { + return nil, errors.New("no servers defined in your identity") + } for _, i := range MessageServerIdxs { srv := id.MessageServers.Servers[i].ServerData myContactCard.PullServers = append(myContactCard.PullServers, &srv) @@ -52,7 +55,7 @@ func (id *Identity) InvitePeer(MyName string, ContactName string, MessageServerI myContactCard.InvitationId = peer.InvitationId id.Peers = append(id.Peers, peer) - return &myContactCard + return &myContactCard, nil } func (id *Identity) CheckInvitation(ReceivedContact *meowlib.ContactCard) (isAnswer bool, proposedNick string, receivedNick string) { diff --git a/client/peer.go b/client/peer.go index 453229a..6a46d69 100644 --- a/client/peer.go +++ b/client/peer.go @@ -59,7 +59,7 @@ func (pl *PeerList) GetFromName(name string) *Peer { return nil } -func (p *Peer) BuildSimpleUserUMessage(message []byte) (*meowlib.UserMessage, error) { +func (p *Peer) BuildSimpleUserMessage(message []byte) (*meowlib.UserMessage, error) { var msg meowlib.UserMessage msg.From = p.MyIdentity.Public msg.Data = message diff --git a/endtoend_test.go b/endtoend_test.go index 43487c5..a23b1b7 100644 --- a/endtoend_test.go +++ b/endtoend_test.go @@ -30,7 +30,10 @@ func TestEndToEnd(t *testing.T) { // Create an invitation for a friend, I want him/her to know me as Bender // //////////////////////////////////////////////////////////////////////////// fmt.Println("Creating an invitation for the first friend...") - invitation := Me.InvitePeer("Bender", "myfirstfriend", []int{1, 2}) + invitation, err := Me.InvitePeer("Bender", "myfirstfriend", []int{1, 2}) + if err != nil { + println(err) + } // print my invitation a, _ := json.Marshal(invitation) fmt.Println(string(a)) @@ -79,7 +82,7 @@ func TestEndToEnd(t *testing.T) { MyFirstFriend := Me.Peers[0] textmessage := "Hello friend!" // Creating User message - usermessage, err := MyFirstFriend.BuildSimpleUserUMessage([]byte(textmessage)) + usermessage, err := MyFirstFriend.BuildSimpleUserMessage([]byte(textmessage)) if err != nil { fmt.Println(err.Error()) }