github.com/status-im/status-go@v1.1.0/protocol/emoji_reaction.go (about)

     1  package protocol
     2  
     3  import (
     4  	"crypto/ecdsa"
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	"github.com/golang/protobuf/proto"
     9  
    10  	accountJson "github.com/status-im/status-go/account/json"
    11  	"github.com/status-im/status-go/eth-node/crypto"
    12  	"github.com/status-im/status-go/eth-node/types"
    13  	"github.com/status-im/status-go/protocol/protobuf"
    14  )
    15  
    16  // EmojiReaction represents an emoji reaction from a user in the application layer, used for persistence, querying and
    17  // signaling
    18  type EmojiReaction struct {
    19  	*protobuf.EmojiReaction
    20  
    21  	// From is a public key of the author of the emoji reaction.
    22  	From string `json:"from,omitempty"`
    23  
    24  	// SigPubKey is the ecdsa encoded public key of the emoji reaction author
    25  	SigPubKey *ecdsa.PublicKey `json:"-"`
    26  
    27  	// LocalChatID is the chatID of the local chat (one-to-one are not symmetric)
    28  	LocalChatID string `json:"localChatId"`
    29  }
    30  
    31  func NewEmojiReaction() *EmojiReaction {
    32  	return &EmojiReaction{EmojiReaction: &protobuf.EmojiReaction{}}
    33  }
    34  
    35  // ID is the Keccak256() contatenation of From-MessageID-EmojiType
    36  func (e *EmojiReaction) ID() string {
    37  	return types.EncodeHex(crypto.Keccak256([]byte(fmt.Sprintf("%s%s%d", e.From, e.MessageId, e.Type))))
    38  }
    39  
    40  // GetSigPubKey returns an ecdsa encoded public key
    41  // this function is required to implement the ChatEntity interface
    42  func (e *EmojiReaction) GetSigPubKey() *ecdsa.PublicKey {
    43  	return e.SigPubKey
    44  }
    45  
    46  // GetProtoBuf returns the struct's embedded protobuf struct
    47  // this function is required to implement the ChatEntity interface
    48  func (e *EmojiReaction) GetProtobuf() proto.Message {
    49  	return e.EmojiReaction
    50  }
    51  
    52  // SetMessageType a setter for the MessageType field
    53  // this function is required to implement the ChatEntity interface
    54  func (e *EmojiReaction) SetMessageType(messageType protobuf.MessageType) {
    55  	e.MessageType = messageType
    56  }
    57  
    58  func (e *EmojiReaction) MarshalJSON() ([]byte, error) {
    59  	item := struct {
    60  		ID          string                      `json:"id"`
    61  		Clock       uint64                      `json:"clock,omitempty"`
    62  		ChatID      string                      `json:"chatId,omitempty"`
    63  		LocalChatID string                      `json:"localChatId,omitempty"`
    64  		From        string                      `json:"from"`
    65  		MessageID   string                      `json:"messageId,omitempty"`
    66  		MessageType protobuf.MessageType        `json:"messageType,omitempty"`
    67  		Retracted   bool                        `json:"retracted,omitempty"`
    68  		EmojiID     protobuf.EmojiReaction_Type `json:"emojiId,omitempty"`
    69  	}{
    70  
    71  		ID:          e.ID(),
    72  		Clock:       e.Clock,
    73  		ChatID:      e.ChatId,
    74  		LocalChatID: e.LocalChatID,
    75  		From:        e.From,
    76  		MessageID:   e.MessageId,
    77  		MessageType: e.MessageType,
    78  		Retracted:   e.Retracted,
    79  		EmojiID:     e.Type,
    80  	}
    81  
    82  	ext, err := accountJson.ExtendStructWithPubKeyData(item.From, item)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	return json.Marshal(ext)
    88  }
    89  
    90  // WrapGroupMessage indicates whether we should wrap this in membership information
    91  func (e *EmojiReaction) WrapGroupMessage() bool {
    92  	return false
    93  }