Add api method to request message for a single contact (#105)
* Add api to request message for a single contact * Fix tests
This commit is contained in:
parent
4a99222990
commit
1a3e62a756
|
@ -20,26 +20,20 @@ var (
|
|||
ErrMessengerNotSet = errors.New("messenger is not set")
|
||||
)
|
||||
|
||||
// ChatParams are chat specific options.
|
||||
type ChatParams struct {
|
||||
RecipientPubKey hexutil.Bytes `json:"recipientPubKey"` // public key hex-encoded
|
||||
PubChatName string `json:"pubChatName"`
|
||||
}
|
||||
|
||||
// MessagesParams is an object with JSON-serializable parameters
|
||||
// for Messages method.
|
||||
type MessagesParams struct {
|
||||
ChatParams
|
||||
Contact
|
||||
}
|
||||
|
||||
// SendParams is an object with JSON-serializable parameters for Send method.
|
||||
type SendParams struct {
|
||||
ChatParams
|
||||
Contact
|
||||
}
|
||||
|
||||
// RequestParams is an object with JSON-serializable parameters for Request method.
|
||||
type RequestParams struct {
|
||||
ChatParams
|
||||
Contact
|
||||
Limit int `json:"limit"`
|
||||
From int64 `json:"from"`
|
||||
To int64 `json:"to"`
|
||||
|
@ -51,6 +45,16 @@ type Contact struct {
|
|||
PublicKey hexutil.Bytes `json:"key"`
|
||||
}
|
||||
|
||||
func parseContact(c Contact) (client.Contact, error) {
|
||||
if len(c.PublicKey) != 0 {
|
||||
c, err := client.CreateContactPrivate(c.Name, c.PublicKey.String(), client.ContactAdded)
|
||||
if err != nil {
|
||||
return c, err
|
||||
}
|
||||
}
|
||||
return client.CreateContactPublicRoom(c.Name, client.ContactAdded), nil
|
||||
}
|
||||
|
||||
// PublicAPI provides an JSON-RPC API to interact with
|
||||
// the Status Messaging Protocol through a geth node.
|
||||
type PublicAPI struct {
|
||||
|
@ -79,11 +83,11 @@ func (api *PublicAPI) Messages(ctx context.Context, params MessagesParams) (*rpc
|
|||
|
||||
adapterOptions := protocol.SubscribeOptions{
|
||||
ChatOptions: protocol.ChatOptions{
|
||||
ChatName: params.PubChatName, // no transformation required
|
||||
ChatName: params.Name, // no transformation required
|
||||
},
|
||||
}
|
||||
|
||||
adapterOptions.Recipient, err = unmarshalPubKey(params.RecipientPubKey)
|
||||
adapterOptions.Recipient, err = unmarshalPubKey(params.PublicKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -136,11 +140,11 @@ func (api *PublicAPI) Send(ctx context.Context, data hexutil.Bytes, params SendP
|
|||
|
||||
adapterOptions := protocol.SendOptions{
|
||||
ChatOptions: protocol.ChatOptions{
|
||||
ChatName: params.PubChatName, // no transformation required
|
||||
ChatName: params.Name, // no transformation required
|
||||
},
|
||||
}
|
||||
|
||||
adapterOptions.Recipient, err = unmarshalPubKey(params.RecipientPubKey)
|
||||
adapterOptions.Recipient, err = unmarshalPubKey(params.PublicKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -153,24 +157,16 @@ func (api *PublicAPI) Request(ctx context.Context, params RequestParams) (err er
|
|||
if api.service.protocol == nil {
|
||||
return ErrProtocolNotSet
|
||||
}
|
||||
|
||||
adapterOptions := protocol.RequestOptions{
|
||||
Chats: []protocol.ChatOptions{
|
||||
protocol.ChatOptions{
|
||||
ChatName: params.PubChatName, // no transformation required
|
||||
},
|
||||
},
|
||||
c, err := parseContact(params.Contact)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
options := protocol.RequestOptions{
|
||||
Limit: params.Limit,
|
||||
From: params.From,
|
||||
To: params.To,
|
||||
}
|
||||
|
||||
adapterOptions.Chats[0].Recipient, err = unmarshalPubKey(params.RecipientPubKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return api.service.protocol.Request(ctx, adapterOptions)
|
||||
return api.service.messenger.Request(ctx, c, options)
|
||||
}
|
||||
|
||||
// Chat is a high-level subscription-based RPC method.
|
||||
|
@ -249,14 +245,9 @@ func (api *PublicAPI) AddContact(ctx context.Context, contact Contact) (err erro
|
|||
if api.service.messenger == nil {
|
||||
return ErrMessengerNotSet
|
||||
}
|
||||
var c client.Contact
|
||||
if len(contact.PublicKey) != 0 {
|
||||
c, err = client.CreateContactPrivate(contact.Name, contact.PublicKey.String(), client.ContactAdded)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
c = client.CreateContactPublicRoom(contact.Name, client.ContactAdded)
|
||||
c, err := parseContact(contact)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = api.service.messenger.AddContact(c)
|
||||
if err != nil {
|
||||
|
@ -271,14 +262,9 @@ func (api *PublicAPI) SendToContact(ctx context.Context, contact Contact, payloa
|
|||
if api.service.messenger == nil {
|
||||
return ErrMessengerNotSet
|
||||
}
|
||||
var c client.Contact
|
||||
if len(contact.PublicKey) != 0 {
|
||||
c, err = client.CreateContactPrivate(contact.Name, contact.PublicKey.String(), client.ContactAdded)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
c = client.CreateContactPublicRoom(contact.Name, client.ContactAdded)
|
||||
c, err := parseContact(contact)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return api.service.messenger.Send(c, []byte(payload))
|
||||
}
|
||||
|
@ -289,14 +275,9 @@ func (api *PublicAPI) ReadContactMessages(ctx context.Context, contact Contact,
|
|||
if api.service.messenger == nil {
|
||||
return nil, ErrMessengerNotSet
|
||||
}
|
||||
var c client.Contact
|
||||
if len(contact.PublicKey) != 0 {
|
||||
c, err = client.CreateContactPrivate(contact.Name, contact.PublicKey.String(), client.ContactAdded)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
c = client.CreateContactPublicRoom(contact.Name, client.ContactAdded)
|
||||
c, err := parseContact(contact)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return api.service.messenger.Messages(c, offset)
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ import (
|
|||
"github.com/status-im/status-go/node"
|
||||
"github.com/status-im/status-go/params"
|
||||
|
||||
"github.com/status-im/status-console-client/protocol/client"
|
||||
"github.com/status-im/status-console-client/protocol/subscription"
|
||||
"github.com/status-im/status-console-client/protocol/v1"
|
||||
protomock "github.com/status-im/status-console-client/protocol/v1/mock"
|
||||
|
@ -34,8 +35,8 @@ func TestPublicAPISend(t *testing.T) {
|
|||
|
||||
data := []byte("some payload")
|
||||
params := SendParams{
|
||||
ChatParams: ChatParams{
|
||||
PubChatName: "test-chat",
|
||||
Contact{
|
||||
Name: "test-chat",
|
||||
},
|
||||
}
|
||||
result := hexutil.Bytes("abc")
|
||||
|
@ -46,7 +47,7 @@ func TestPublicAPISend(t *testing.T) {
|
|||
gomock.Eq(data),
|
||||
gomock.Eq(protocol.SendOptions{
|
||||
ChatOptions: protocol.ChatOptions{
|
||||
ChatName: params.PubChatName,
|
||||
ChatName: params.Name,
|
||||
},
|
||||
}),
|
||||
).
|
||||
|
@ -69,8 +70,8 @@ func TestPublicAPIRequest(t *testing.T) {
|
|||
|
||||
now := time.Now().Unix()
|
||||
params := RequestParams{
|
||||
ChatParams: ChatParams{
|
||||
PubChatName: "test-chat",
|
||||
Contact: Contact{
|
||||
Name: "test-chat",
|
||||
},
|
||||
Limit: 100,
|
||||
From: now,
|
||||
|
@ -83,7 +84,7 @@ func TestPublicAPIRequest(t *testing.T) {
|
|||
gomock.Eq(protocol.RequestOptions{
|
||||
Chats: []protocol.ChatOptions{
|
||||
protocol.ChatOptions{
|
||||
ChatName: params.PubChatName,
|
||||
ChatName: params.Name,
|
||||
},
|
||||
},
|
||||
Limit: 100,
|
||||
|
@ -109,8 +110,8 @@ func TestPublicAPIMessages(t *testing.T) {
|
|||
|
||||
messages := make(chan protocol.Message)
|
||||
params := MessagesParams{
|
||||
ChatParams: ChatParams{
|
||||
PubChatName: "test-chat",
|
||||
Contact{
|
||||
Name: "test-chat",
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -120,7 +121,7 @@ func TestPublicAPIMessages(t *testing.T) {
|
|||
gomock.Any(),
|
||||
gomock.Eq(protocol.SubscribeOptions{
|
||||
ChatOptions: protocol.ChatOptions{
|
||||
ChatName: params.PubChatName,
|
||||
ChatName: params.Name,
|
||||
},
|
||||
}),
|
||||
).
|
||||
|
@ -140,11 +141,11 @@ func createAndStartNode(privateKey *ecdsa.PrivateKey) (*node.StatusNode, *Servic
|
|||
return service, nil
|
||||
},
|
||||
// func(*gethnode.ServiceContext) (gethnode.Service, error) {
|
||||
// config := &whisper.Config{
|
||||
// MinimumAcceptedPOW: 0.001,
|
||||
// MaxMessageSize: whisper.DefaultMaxMessageSize,
|
||||
// }
|
||||
// return whisper.New(config), nil
|
||||
// config := &whisper.Config{
|
||||
// MinimumAcceptedPOW: 0.001,
|
||||
// MaxMessageSize: whisper.DefaultMaxMessageSize,
|
||||
// }
|
||||
// return whisper.New(config), nil
|
||||
// },
|
||||
}
|
||||
|
||||
|
@ -166,6 +167,7 @@ func setupRPCClient(proto protocol.Protocol) (*rpc.Client, *node.StatusNode, err
|
|||
return nil, nil, err
|
||||
}
|
||||
|
||||
service.SetMessenger(client.NewMessenger(nil, proto, nil))
|
||||
service.SetProtocol(proto)
|
||||
|
||||
client, err := n.GethNode().Attach()
|
||||
|
|
Loading…
Reference in New Issue