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.
This commit is contained in:
John Brooks 2017-09-20 13:43:05 -06:00 committed by Sarah Jamie Lewis
parent b2c87b1b72
commit 9a65aeed77
5 changed files with 23 additions and 24 deletions

View File

@ -14,10 +14,6 @@ func (aacm *AcceptAllContactManager) LookupContact(hostname string, publicKey rs
return true, true return true, true
} }
func (aacm *AcceptAllContactManager) GetContactDetails() (string, string) {
return "", ""
}
func (aacm *AcceptAllContactManager) ContactRequest(name string, message string) string { func (aacm *AcceptAllContactManager) ContactRequest(name string, message string) string {
return "Accepted" return "Accepted"
} }

View File

@ -28,10 +28,6 @@ type RicochetApplicationInstance struct {
ChatMessageAckHandler func(*RicochetApplicationInstance, uint32) ChatMessageAckHandler func(*RicochetApplicationInstance, uint32)
} }
func (rai *RicochetApplicationInstance) GetContactDetails() (string, string) {
return "EchoBot", "I LIVE 😈😈!!!!"
}
func (rai *RicochetApplicationInstance) ContactRequest(name string, message string) string { func (rai *RicochetApplicationInstance) ContactRequest(name string, message string) string {
return "Accepted" return "Accepted"
} }

View File

@ -16,11 +16,14 @@ const (
// ContactRequestChannel implements the ChannelHandler interface for a channel of // ContactRequestChannel implements the ChannelHandler interface for a channel of
// type "im.ricochet.contact.request". The channel may be inbound or outbound. // type "im.ricochet.contact.request". The channel may be inbound or outbound.
// a ContactRequestChannelHandler implementation to handle chat events.
type ContactRequestChannel struct { type ContactRequestChannel struct {
// Methods of Handler are called for chat events on this channel // Methods of Handler are called for chat events on this channel
Handler ContactRequestChannelHandler Handler ContactRequestChannelHandler
channel *Channel channel *Channel
// Properties of the request
Name string
Message string
} }
// ContactRequestChannelHandler is implemented by an application type to receive // 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 // ConnectionHandler; there is no need to use a distinct type as a
// ContactRequestChannelHandler. // ContactRequestChannelHandler.
type ContactRequestChannelHandler interface { type ContactRequestChannelHandler interface {
GetContactDetails() (string, string)
ContactRequest(name string, message string) string ContactRequest(name string, message string) string
ContactRequestRejected() ContactRequestRejected()
ContactRequestAccepted() ContactRequestAccepted()
@ -87,6 +89,8 @@ func (crc *ContactRequestChannel) OpenInbound(channel *Channel, oc *Protocol_Dat
return nil, InvalidContactMessageError return nil, InvalidContactMessageError
} }
crc.Name = contactRequest.GetNickname()
crc.Message = contactRequest.GetMessageText()
result := crc.Handler.ContactRequest(contactRequest.GetNickname(), contactRequest.GetMessageText()) result := crc.Handler.ContactRequest(contactRequest.GetNickname(), contactRequest.GetMessageText())
messageBuilder := new(utils.MessageBuilder) messageBuilder := new(utils.MessageBuilder)
return messageBuilder.ReplyToContactRequestOnResponse(channel.ID, result), nil 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. // returned, it will be sent as the OpenChannel message.
func (crc *ContactRequestChannel) OpenOutbound(channel *Channel) ([]byte, error) { func (crc *ContactRequestChannel) OpenOutbound(channel *Channel) ([]byte, error) {
crc.channel = channel crc.channel = channel
name, message := crc.Handler.GetContactDetails()
messageBuilder := new(utils.MessageBuilder) 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 // 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{}) 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) { func (crc *ContactRequestChannel) handleStatus(status string) {
switch status { switch status {
case "Accepted": case "Accepted":

View File

@ -12,10 +12,6 @@ type TestContactRequestHandler struct {
Received bool Received bool
} }
func (tcrh *TestContactRequestHandler) GetContactDetails() (string, string) {
return "", ""
}
func (tcrh *TestContactRequestHandler) ContactRequest(name string, message string) string { func (tcrh *TestContactRequestHandler) ContactRequest(name string, message string) string {
if name == "test_nickname" && message == "test_message" { if name == "test_nickname" && message == "test_message" {
tcrh.Received = true tcrh.Received = true
@ -71,9 +67,11 @@ func TestContactRequestOpenOutbound(t *testing.T) {
} }
func TestContactRequestOpenOutboundResult(t *testing.T) { func TestContactRequestOpenOutboundResult(t *testing.T) {
contactRequestChannel := new(ContactRequestChannel) contactRequestChannel := &ContactRequestChannel{
handler := new(TestContactRequestHandler) Name: "test_nickname",
contactRequestChannel.Handler = handler Message: "test_message",
Handler: &TestContactRequestHandler{},
}
channel := Channel{ID: 1} channel := Channel{ID: 1}
contactRequestChannel.OpenOutbound(&channel) contactRequestChannel.OpenOutbound(&channel)

View File

@ -16,10 +16,6 @@ type RicochetEchoBot struct {
messages chan string messages chan string
} }
func (echobot *RicochetEchoBot) GetContactDetails() (string, string) {
return "EchoBot", "I LIVE 😈😈!!!!"
}
func (echobot *RicochetEchoBot) ContactRequest(name string, message string) string { func (echobot *RicochetEchoBot) ContactRequest(name string, message string) string {
return "Pending" return "Pending"
} }
@ -65,7 +61,12 @@ func (echobot *RicochetEchoBot) Connect(privateKeyFile string, hostname string)
if !known { if !known {
err := rc.Do(func() error { 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 return err
}) })
if err != nil { if err != nil {