github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/privatemessaging/message.go (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package privatemessaging
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/kaleido-io/firefly/internal/config"
    23  	"github.com/kaleido-io/firefly/internal/i18n"
    24  	"github.com/kaleido-io/firefly/pkg/fftypes"
    25  )
    26  
    27  func (pm *privateMessaging) SendMessage(ctx context.Context, ns string, in *fftypes.MessageInput) (out *fftypes.Message, err error) {
    28  	in.Header.Namespace = ns
    29  	in.Header.Type = fftypes.MessageTypePrivate
    30  	if in.Header.Author == "" {
    31  		in.Header.Author = config.GetString(config.OrgIdentity)
    32  	}
    33  	if in.Header.TxType == "" {
    34  		in.Header.TxType = fftypes.TransactionTypeBatchPin
    35  	}
    36  
    37  	sender, err := pm.identity.Resolve(ctx, in.Header.Author)
    38  	if err != nil {
    39  		return nil, i18n.WrapError(ctx, err, i18n.MsgAuthorInvalid)
    40  	}
    41  
    42  	// We optimize the DB storage of all the parts of the message using transaction semantics (assuming those are supported by the DB plugin
    43  	err = pm.database.RunAsGroup(ctx, func(ctx context.Context) error {
    44  		return pm.resolveAndSend(ctx, sender, in)
    45  	})
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	// The broadcastMessage function modifies the input message to create all the refs
    50  	return &in.Message, err
    51  }
    52  
    53  func (pm *privateMessaging) resolveAndSend(ctx context.Context, sender *fftypes.Identity, in *fftypes.MessageInput) (err error) {
    54  	// Resolve the member list into a group
    55  	if err = pm.resolveReceipientList(ctx, sender, in); err != nil {
    56  		return err
    57  	}
    58  
    59  	// The data manager is responsible for the heavy lifting of storing/validating all our in-line data elements
    60  	in.Message.Data, err = pm.data.ResolveInputData(ctx, in.Header.Namespace, in.InputData)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	// Seal the message
    66  	if err := in.Message.Seal(ctx); err != nil {
    67  		return err
    68  	}
    69  
    70  	// Store the message - this asynchronously triggers the next step in process
    71  	return pm.database.InsertMessageLocal(ctx, &in.Message)
    72  }