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:
parent
b2c87b1b72
commit
9a65aeed77
|
@ -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"
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
|
|
|
@ -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":
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
Reference in New Issue