Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2354 from soluchok/remove_metadata_messenger
Browse files Browse the repository at this point in the history
refactor: Removes metadata from the messenger
  • Loading branch information
fqutishat authored Nov 28, 2020
2 parents a71550e + 694dce2 commit 5fac400
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 230 deletions.
65 changes: 4 additions & 61 deletions pkg/didcomm/messenger/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,18 @@ const (
// MessengerStore is messenger store name.
MessengerStore = "messenger_store"

metadataKey = "metadata_%s"

jsonID = "@id"
jsonThread = "~thread"
jsonThreadID = "thid"
jsonParentThreadID = "pthid"
jsonMetadata = "_internal_metadata"
)

// record is an internal structure and keeps payload about inbound message.
type record struct {
MyDID string `json:"my_did,omitempty"`
TheirDID string `json:"their_did,omitempty"`
ThreadID string `json:"thread_id,omitempty"`
ParentThreadID string `json:"parent_thread_id,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
MyDID string `json:"my_did,omitempty"`
TheirDID string `json:"their_did,omitempty"`
ThreadID string `json:"thread_id,omitempty"`
ParentThreadID string `json:"parent_thread_id,omitempty"`
}

// Provider contains dependencies for the Messenger.
Expand Down Expand Up @@ -83,10 +79,6 @@ func (m *Messenger) HandleInbound(msg service.DIDCommMsgMap, myDID, theirDID str
return fmt.Errorf("threadID: %w", err)
}

if err := m.populateMetadata(thID, msg); err != nil {
return fmt.Errorf("with metadata: %w", err)
}

// saves message payload
return m.saveRecord(msg.ID(), record{
ParentThreadID: msg.ParentThreadID(),
Expand All @@ -96,50 +88,13 @@ func (m *Messenger) HandleInbound(msg service.DIDCommMsgMap, myDID, theirDID str
})
}

func (m *Messenger) saveMetadata(msg service.DIDCommMsgMap) error {
metadata := msg.Metadata()
if len(metadata) == 0 {
return nil
}

thID, err := msg.ThreadID()
if err != nil {
return fmt.Errorf("threadID: %w", err)
}

return m.saveRecord(fmt.Sprintf(metadataKey, thID), record{Metadata: metadata})
}

func (m *Messenger) populateMetadata(thID string, msg service.DIDCommMsgMap) error {
rec, err := m.getRecord(fmt.Sprintf(metadataKey, thID))
if errors.Is(err, storage.ErrDataNotFound) {
return nil
}

if err != nil {
return fmt.Errorf("get record: %w", err)
}

if rec.Metadata == nil {
return nil
}

msg[jsonMetadata] = rec.Metadata

return nil
}

// Send sends the message by starting a new thread.
// Do not provide a message with ~thread decorator. It will be removed.
// Use ReplyTo function instead. It will keep ~thread decorator automatically.
func (m *Messenger) Send(msg service.DIDCommMsgMap, myDID, theirDID string) error {
// fills missing fields
fillIfMissing(msg)

if err := m.saveMetadata(msg); err != nil {
return fmt.Errorf("save metadata: %w", err)
}

msg[jsonThread] = map[string]interface{}{
jsonThreadID: msg.ID(),
}
Expand All @@ -155,10 +110,6 @@ func (m *Messenger) SendToDestination(msg service.DIDCommMsgMap, sender string,
// fills missing fields
fillIfMissing(msg)

if err := m.saveMetadata(msg); err != nil {
return fmt.Errorf("save metadata: %w", err)
}

delete(msg, jsonThread)

return m.dispatcher.Send(msg, sender, destination)
Expand Down Expand Up @@ -188,10 +139,6 @@ func (m *Messenger) ReplyTo(msgID string, msg service.DIDCommMsgMap) error {

msg[jsonThread] = thread

if err := m.saveMetadata(msg); err != nil {
return fmt.Errorf("save metadata: %w", err)
}

return m.dispatcher.SendToDID(msg, rec.MyDID, rec.TheirDID)
}

Expand All @@ -203,10 +150,6 @@ func (m *Messenger) ReplyToNested(msg service.DIDCommMsgMap, opts *service.Neste
// fills missing fields
fillIfMissing(msg)

if err := m.saveMetadata(msg); err != nil {
return fmt.Errorf("save metadata: %w", err)
}

if err := m.fillNestedReplyOption(opts); err != nil {
return fmt.Errorf("failed to prepare nested reply options: %w", err)
}
Expand Down
172 changes: 7 additions & 165 deletions pkg/didcomm/messenger/messenger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
dispatcherMocks "github.com/hyperledger/aries-framework-go/pkg/internal/gomocks/didcomm/dispatcher"
messengerMocks "github.com/hyperledger/aries-framework-go/pkg/internal/gomocks/didcomm/messenger"
storageMocks "github.com/hyperledger/aries-framework-go/pkg/internal/gomocks/storage"
"github.com/hyperledger/aries-framework-go/pkg/storage"
)

const (
Expand Down Expand Up @@ -70,26 +69,6 @@ func TestMessenger_HandleInbound(t *testing.T) {
t.Run("success", func(t *testing.T) {
store := storageMocks.NewMockStore(ctrl)
store.EXPECT().Put(ID, gomock.Any()).Return(nil)
store.EXPECT().Get(gomock.Any()).Return([]byte(`{}`), nil)

storageProvider := storageMocks.NewMockProvider(ctrl)
storageProvider.EXPECT().OpenStore(gomock.Any()).Return(store, nil)

provider := messengerMocks.NewMockProvider(ctrl)
provider.EXPECT().StorageProvider().Return(storageProvider)
provider.EXPECT().OutboundDispatcher().Return(nil)

msgr, err := NewMessenger(provider)
require.NoError(t, err)
require.NotNil(t, msgr)

require.NoError(t, msgr.HandleInbound(service.DIDCommMsgMap{jsonID: ID}, myDID, theirDID))
})

t.Run("success without metadata", func(t *testing.T) {
store := storageMocks.NewMockStore(ctrl)
store.EXPECT().Put(ID, gomock.Any()).Return(nil)
store.EXPECT().Get(gomock.Any()).Return(nil, storage.ErrDataNotFound)

storageProvider := storageMocks.NewMockProvider(ctrl)
storageProvider.EXPECT().OpenStore(gomock.Any()).Return(store, nil)
Expand Down Expand Up @@ -120,71 +99,6 @@ func TestMessenger_HandleInbound(t *testing.T) {
err = msgr.HandleInbound(service.DIDCommMsgMap{}, myDID, theirDID)
require.Contains(t, fmt.Sprintf("%v", err), "message-id is absent")
})

t.Run("metadata with error", func(t *testing.T) {
store := storageMocks.NewMockStore(ctrl)
store.EXPECT().Get(gomock.Any()).Return(nil, errors.New(errMsg))

storageProvider := storageMocks.NewMockProvider(ctrl)
storageProvider.EXPECT().OpenStore(gomock.Any()).Return(store, nil)

provider := messengerMocks.NewMockProvider(ctrl)
provider.EXPECT().StorageProvider().Return(storageProvider)
provider.EXPECT().OutboundDispatcher().Return(nil)

msgr, err := NewMessenger(provider)
require.NoError(t, err)
require.NotNil(t, msgr)

err = msgr.HandleInbound(service.DIDCommMsgMap{jsonID: ID}, myDID, theirDID)
require.Contains(t, fmt.Sprintf("%v", err), errMsg)
})

t.Run("success with metadata", func(t *testing.T) {
store := storageMocks.NewMockStore(ctrl)
payload := []byte(`{"my_did":"myDID","their_did":"theirDID","thread_id":"thID","parent_thread_id":"pthID"}`)
store.EXPECT().Put(ID, payload).Return(nil)
store.EXPECT().Get(gomock.Any()).Return([]byte(`{"metadata":{"key":"val"}}`), nil)

storageProvider := storageMocks.NewMockProvider(ctrl)
storageProvider.EXPECT().OpenStore(gomock.Any()).Return(store, nil)

provider := messengerMocks.NewMockProvider(ctrl)
provider.EXPECT().StorageProvider().Return(storageProvider)
provider.EXPECT().OutboundDispatcher().Return(nil)

msgr, err := NewMessenger(provider)
require.NoError(t, err)
require.NotNil(t, msgr)

msg := service.DIDCommMsgMap{jsonID: ID, jsonThread: map[string]interface{}{
jsonThreadID: "thID",
jsonParentThreadID: "pthID",
}}
require.NoError(t, msgr.HandleInbound(msg, myDID, theirDID))
require.Equal(t, "val", msg.Metadata()["key"])
})

t.Run("success with metadata (thread is nil)", func(t *testing.T) {
store := storageMocks.NewMockStore(ctrl)
store.EXPECT().Put(ID, gomock.Any()).Return(nil)
store.EXPECT().Get(gomock.Any()).Return([]byte(`{"metadata":{"key":"val"}}`), nil)

storageProvider := storageMocks.NewMockProvider(ctrl)
storageProvider.EXPECT().OpenStore(gomock.Any()).Return(store, nil)

provider := messengerMocks.NewMockProvider(ctrl)
provider.EXPECT().StorageProvider().Return(storageProvider)
provider.EXPECT().OutboundDispatcher().Return(nil)

msgr, err := NewMessenger(provider)
require.NoError(t, err)
require.NotNil(t, msgr)

msg := service.DIDCommMsgMap{jsonID: ID}
require.NoError(t, msgr.HandleInbound(msg, myDID, theirDID))
require.Equal(t, "val", msg.Metadata()["key"])
})
}

func sendToDIDCheck(t *testing.T, checks ...string) func(msg service.DIDCommMsgMap, myDID, theirDID string) error {
Expand All @@ -198,9 +112,6 @@ func sendToDIDCheck(t *testing.T, checks ...string) func(msg service.DIDCommMsgM

for _, check := range checks {
switch check {
case jsonMetadata:
// metadata always should be absent
require.Empty(t, msg.Metadata())
case jsonID:
// ID always should be in the message
require.NotEmpty(t, v.ID)
Expand All @@ -225,7 +136,7 @@ func TestMessenger_Send(t *testing.T) {

outbound := dispatcherMocks.NewMockOutbound(ctrl)
outbound.EXPECT().SendToDID(gomock.Any(), myDID, theirDID).
Do(sendToDIDCheck(t, jsonID, jsonMetadata))
Do(sendToDIDCheck(t, jsonID))

provider := messengerMocks.NewMockProvider(ctrl)
provider.EXPECT().StorageProvider().Return(storageProvider)
Expand Down Expand Up @@ -263,7 +174,7 @@ func TestMessenger_Send(t *testing.T) {

outbound := dispatcherMocks.NewMockOutbound(ctrl)
outbound.EXPECT().SendToDID(gomock.Any(), myDID, theirDID).
Do(sendToDIDCheck(t, jsonID, jsonMetadata))
Do(sendToDIDCheck(t, jsonID))

provider := messengerMocks.NewMockProvider(ctrl)
provider.EXPECT().StorageProvider().Return(storageProvider)
Expand All @@ -275,32 +186,6 @@ func TestMessenger_Send(t *testing.T) {

require.NoError(t, msgr.Send(service.DIDCommMsgMap{}, myDID, theirDID))
})

t.Run("save metadata error", func(t *testing.T) {
store := storageMocks.NewMockStore(ctrl)
store.EXPECT().Put(gomock.Any(), gomock.Any()).Return(errors.New(errMsg)).AnyTimes()

storageProvider := storageMocks.NewMockProvider(ctrl)
storageProvider.EXPECT().OpenStore(gomock.Any()).Return(store, nil)

provider := messengerMocks.NewMockProvider(ctrl)
provider.EXPECT().StorageProvider().Return(storageProvider)
provider.EXPECT().OutboundDispatcher().Return(nil)

msgr, err := NewMessenger(provider)
require.NoError(t, err)
require.NotNil(t, msgr)

err = msgr.Send(service.DIDCommMsgMap{
jsonMetadata: map[string]interface{}{"key": "val"},
}, myDID, theirDID)
require.Contains(t, fmt.Sprintf("%v", err), errMsg)

err = msgr.SendToDestination(service.DIDCommMsgMap{
jsonMetadata: map[string]interface{}{"key": "val"},
}, "", nil)
require.Contains(t, fmt.Sprintf("%v", err), errMsg)
})
}

func TestMessenger_ReplyTo(t *testing.T) {
Expand All @@ -316,7 +201,7 @@ func TestMessenger_ReplyTo(t *testing.T) {

outbound := dispatcherMocks.NewMockOutbound(ctrl)
outbound.EXPECT().SendToDID(gomock.Any(), gomock.Any(), gomock.Any()).
Do(sendToDIDCheck(t, jsonID, jsonMetadata, jsonThreadID, jsonParentThreadID))
Do(sendToDIDCheck(t, jsonID, jsonThreadID, jsonParentThreadID))

provider := messengerMocks.NewMockProvider(ctrl)
provider.EXPECT().StorageProvider().Return(storageProvider)
Expand Down Expand Up @@ -356,7 +241,7 @@ func TestMessenger_ReplyTo(t *testing.T) {

outbound := dispatcherMocks.NewMockOutbound(ctrl)
outbound.EXPECT().SendToDID(gomock.Any(), gomock.Any(), gomock.Any()).
Do(sendToDIDCheck(t, jsonID, jsonMetadata, jsonThreadID))
Do(sendToDIDCheck(t, jsonID, jsonThreadID))

provider := messengerMocks.NewMockProvider(ctrl)
provider.EXPECT().StorageProvider().Return(storageProvider)
Expand All @@ -368,28 +253,6 @@ func TestMessenger_ReplyTo(t *testing.T) {

require.NoError(t, msgr.ReplyTo(ID, service.DIDCommMsgMap{}))
})

t.Run("save metadata error", func(t *testing.T) {
store := storageMocks.NewMockStore(ctrl)
store.EXPECT().Get(ID).Return([]byte(`{}`), nil)
store.EXPECT().Put(gomock.Any(), gomock.Any()).Return(errors.New(errMsg))

storageProvider := storageMocks.NewMockProvider(ctrl)
storageProvider.EXPECT().OpenStore(gomock.Any()).Return(store, nil)

provider := messengerMocks.NewMockProvider(ctrl)
provider.EXPECT().StorageProvider().Return(storageProvider)
provider.EXPECT().OutboundDispatcher().Return(nil)

msgr, err := NewMessenger(provider)
require.NoError(t, err)
require.NotNil(t, msgr)

err = msgr.ReplyTo(ID, service.DIDCommMsgMap{
jsonMetadata: map[string]interface{}{"key": "val"},
})
require.Contains(t, fmt.Sprintf("%v", err), errMsg)
})
}

func TestMessenger_ReplyToNested(t *testing.T) {
Expand All @@ -404,7 +267,7 @@ func TestMessenger_ReplyToNested(t *testing.T) {

outbound := dispatcherMocks.NewMockOutbound(ctrl)
outbound.EXPECT().SendToDID(gomock.Any(), gomock.Any(), gomock.Any()).
Do(sendToDIDCheck(t, jsonID, jsonMetadata, jsonParentThreadID))
Do(sendToDIDCheck(t, jsonID, jsonParentThreadID))

provider := messengerMocks.NewMockProvider(ctrl)
provider.EXPECT().StorageProvider().Return(storageProvider)
Expand All @@ -426,7 +289,7 @@ func TestMessenger_ReplyToNested(t *testing.T) {

outbound := dispatcherMocks.NewMockOutbound(ctrl)
outbound.EXPECT().SendToDID(gomock.Any(), gomock.Any(), gomock.Any()).
Do(sendToDIDCheck(t, jsonID, jsonMetadata, jsonParentThreadID))
Do(sendToDIDCheck(t, jsonID, jsonParentThreadID))

provider := messengerMocks.NewMockProvider(ctrl)
provider.EXPECT().StorageProvider().Return(storageProvider)
Expand All @@ -445,7 +308,7 @@ func TestMessenger_ReplyToNested(t *testing.T) {

outbound := dispatcherMocks.NewMockOutbound(ctrl)
outbound.EXPECT().SendToDID(gomock.Any(), gomock.Any(), gomock.Any()).
Do(sendToDIDCheck(t, jsonID, jsonMetadata, jsonParentThreadID))
Do(sendToDIDCheck(t, jsonID, jsonParentThreadID))

provider := messengerMocks.NewMockProvider(ctrl)
provider.EXPECT().StorageProvider().Return(storageProvider)
Expand All @@ -459,27 +322,6 @@ func TestMessenger_ReplyToNested(t *testing.T) {
&service.NestedReplyOpts{ThreadID: thID, TheirDID: theirDID, MyDID: myDID}))
})

t.Run("save metadata error", func(t *testing.T) {
store := storageMocks.NewMockStore(ctrl)
store.EXPECT().Put(gomock.Any(), gomock.Any()).Return(errors.New(errMsg))

storageProvider := storageMocks.NewMockProvider(ctrl)
storageProvider.EXPECT().OpenStore(gomock.Any()).Return(store, nil)

provider := messengerMocks.NewMockProvider(ctrl)
provider.EXPECT().StorageProvider().Return(storageProvider)
provider.EXPECT().OutboundDispatcher().Return(nil)

msgr, err := NewMessenger(provider)
require.NoError(t, err)
require.NotNil(t, msgr)

err = msgr.ReplyToNested(service.DIDCommMsgMap{
jsonMetadata: map[string]interface{}{"key": "val"},
}, &service.NestedReplyOpts{ThreadID: "thID", TheirDID: theirDID, MyDID: myDID})
require.Contains(t, fmt.Sprintf("%v", err), errMsg)
})

t.Run("failure with message ID issues", func(t *testing.T) {
store := storageMocks.NewMockStore(ctrl)
store.EXPECT().Get(msgID).Return(nil, errors.New(errMsg))
Expand Down
Loading

0 comments on commit 5fac400

Please sign in to comment.