From 9a65aeed7727ec7d78b630ef7a7c22371272f284 Mon Sep 17 00:00:00 2001 From: John Brooks Date: Wed, 20 Sep 2017 13:43:05 -0600 Subject: [PATCH] Improve ContactRequestChannel's API After the RequestOpenChannel changes, it's now possible to specify the name and message of an outbound request as variables of the channel handler, instead implementing an interface method to return them. Also added the SendResponse method, which is necessary to respond to an inbound request that was in the Pending state. --- application/acceptallcontactmanager.go | 4 ---- application/application.go | 4 ---- channels/contactrequestchannel.go | 16 ++++++++++++---- channels/contactrequestchannel_test.go | 12 +++++------- examples/echobot/main.go | 11 ++++++----- 5 files changed, 23 insertions(+), 24 deletions(-) diff --git a/application/acceptallcontactmanager.go b/application/acceptallcontactmanager.go index 3aebec4..8bc34a8 100644 --- a/application/acceptallcontactmanager.go +++ b/application/acceptallcontactmanager.go @@ -14,10 +14,6 @@ func (aacm *AcceptAllContactManager) LookupContact(hostname string, publicKey rs return true, true } -func (aacm *AcceptAllContactManager) GetContactDetails() (string, string) { - return "", "" -} - func (aacm *AcceptAllContactManager) ContactRequest(name string, message string) string { return "Accepted" } diff --git a/application/application.go b/application/application.go index 15f1740..4338899 100644 --- a/application/application.go +++ b/application/application.go @@ -28,10 +28,6 @@ type RicochetApplicationInstance struct { ChatMessageAckHandler func(*RicochetApplicationInstance, uint32) } -func (rai *RicochetApplicationInstance) GetContactDetails() (string, string) { - return "EchoBot", "I LIVE 😈😈!!!!" -} - func (rai *RicochetApplicationInstance) ContactRequest(name string, message string) string { return "Accepted" } diff --git a/channels/contactrequestchannel.go b/channels/contactrequestchannel.go index af33f1d..4ca1b2d 100644 --- a/channels/contactrequestchannel.go +++ b/channels/contactrequestchannel.go @@ -16,11 +16,14 @@ const ( // ContactRequestChannel implements the ChannelHandler interface for a channel of // type "im.ricochet.contact.request". The channel may be inbound or outbound. -// a ContactRequestChannelHandler implementation to handle chat events. type ContactRequestChannel struct { // Methods of Handler are called for chat events on this channel Handler ContactRequestChannelHandler channel *Channel + + // Properties of the request + Name string + Message string } // ContactRequestChannelHandler is implemented by an application type to receive @@ -30,7 +33,6 @@ type ContactRequestChannel struct { // ConnectionHandler; there is no need to use a distinct type as a // ContactRequestChannelHandler. type ContactRequestChannelHandler interface { - GetContactDetails() (string, string) ContactRequest(name string, message string) string ContactRequestRejected() ContactRequestAccepted() @@ -87,6 +89,8 @@ func (crc *ContactRequestChannel) OpenInbound(channel *Channel, oc *Protocol_Dat return nil, InvalidContactMessageError } + crc.Name = contactRequest.GetNickname() + crc.Message = contactRequest.GetMessageText() result := crc.Handler.ContactRequest(contactRequest.GetNickname(), contactRequest.GetMessageText()) messageBuilder := new(utils.MessageBuilder) return messageBuilder.ReplyToContactRequestOnResponse(channel.ID, result), nil @@ -100,9 +104,8 @@ func (crc *ContactRequestChannel) OpenInbound(channel *Channel, oc *Protocol_Dat // returned, it will be sent as the OpenChannel message. func (crc *ContactRequestChannel) OpenOutbound(channel *Channel) ([]byte, error) { crc.channel = channel - name, message := crc.Handler.GetContactDetails() messageBuilder := new(utils.MessageBuilder) - return messageBuilder.OpenContactRequestChannel(channel.ID, name, message), nil + return messageBuilder.OpenContactRequestChannel(channel.ID, crc.Name, crc.Message), nil } // OpenOutboundResult is called when a response is received for an @@ -125,6 +128,11 @@ func (crc *ContactRequestChannel) OpenOutboundResult(err error, crm *Protocol_Da crc.channel.SendMessage([]byte{}) } +func (crc *ContactRequestChannel) SendResponse(status string) { + messageBuilder := new(utils.MessageBuilder) + crc.channel.SendMessage(messageBuilder.ReplyToContactRequest(crc.channel.ID, status)) +} + func (crc *ContactRequestChannel) handleStatus(status string) { switch status { case "Accepted": diff --git a/channels/contactrequestchannel_test.go b/channels/contactrequestchannel_test.go index ad0076c..dd584c1 100644 --- a/channels/contactrequestchannel_test.go +++ b/channels/contactrequestchannel_test.go @@ -12,10 +12,6 @@ type TestContactRequestHandler struct { Received bool } -func (tcrh *TestContactRequestHandler) GetContactDetails() (string, string) { - return "", "" -} - func (tcrh *TestContactRequestHandler) ContactRequest(name string, message string) string { if name == "test_nickname" && message == "test_message" { tcrh.Received = true @@ -71,9 +67,11 @@ func TestContactRequestOpenOutbound(t *testing.T) { } func TestContactRequestOpenOutboundResult(t *testing.T) { - contactRequestChannel := new(ContactRequestChannel) - handler := new(TestContactRequestHandler) - contactRequestChannel.Handler = handler + contactRequestChannel := &ContactRequestChannel{ + Name: "test_nickname", + Message: "test_message", + Handler: &TestContactRequestHandler{}, + } channel := Channel{ID: 1} contactRequestChannel.OpenOutbound(&channel) diff --git a/examples/echobot/main.go b/examples/echobot/main.go index 609e3c3..c20bdc5 100644 --- a/examples/echobot/main.go +++ b/examples/echobot/main.go @@ -16,10 +16,6 @@ type RicochetEchoBot struct { messages chan string } -func (echobot *RicochetEchoBot) GetContactDetails() (string, string) { - return "EchoBot", "I LIVE 😈😈!!!!" -} - func (echobot *RicochetEchoBot) ContactRequest(name string, message string) string { return "Pending" } @@ -65,7 +61,12 @@ func (echobot *RicochetEchoBot) Connect(privateKeyFile string, hostname string) if !known { err := rc.Do(func() error { - _, err := rc.RequestOpenChannel("im.ricochet.contact.request", &channels.ContactRequestChannel{Handler: echobot}) + _,err := rc.RequestOpenChannel("im.ricochet.contact.request", + &channels.ContactRequestChannel{ + Handler: echobot, + Name: "EchoBot", + Message: "I LIVE 😈😈!!!!", + }) return err }) if err != nil {