github.com/status-im/status-go@v1.1.0/services/chat/api_send_messages.go (about)

     1  package chat
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/forPelevin/gomoji"
     7  
     8  	"github.com/status-im/status-go/eth-node/types"
     9  	"github.com/status-im/status-go/protocol"
    10  	"github.com/status-im/status-go/protocol/common"
    11  	"github.com/status-im/status-go/protocol/protobuf"
    12  )
    13  
    14  type SendMessageResponse struct {
    15  	Chat     *protocol.Chat    `json:"chat"`
    16  	Messages []*common.Message `json:"messages"`
    17  }
    18  
    19  func (api *API) SendSticker(ctx context.Context, communityID types.HexBytes, chatID string, packID int32, hash string, responseTo string) (*SendMessageResponse, error) {
    20  	ensName, _ := api.s.accountsDB.GetPreferredUsername()
    21  
    22  	msg := &common.Message{
    23  		CommunityID: string(communityID.Bytes()),
    24  		ChatMessage: &protobuf.ChatMessage{
    25  			ChatId:      chatID,
    26  			ContentType: protobuf.ChatMessage_STICKER,
    27  			Text:        "Update to latest version to see a nice sticker here!",
    28  			Payload: &protobuf.ChatMessage_Sticker{
    29  				Sticker: &protobuf.StickerMessage{
    30  					Hash: hash,
    31  					Pack: packID,
    32  				},
    33  			},
    34  			ResponseTo: responseTo,
    35  			EnsName:    ensName,
    36  		},
    37  	}
    38  
    39  	response, err := api.s.messenger.SendChatMessage(ctx, msg)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	return api.toSendMessageResponse(response)
    45  
    46  }
    47  
    48  func (api *API) toSendMessageResponse(response *protocol.MessengerResponse) (*SendMessageResponse, error) {
    49  	protocolChat := response.Chats()[0]
    50  
    51  	return &SendMessageResponse{
    52  		Chat:     protocolChat,
    53  		Messages: response.Messages(),
    54  	}, nil
    55  }
    56  
    57  func isTextOrEmoji(text string) protobuf.ChatMessage_ContentType {
    58  	contentType := protobuf.ChatMessage_TEXT_PLAIN
    59  	if gomoji.RemoveEmojis(text) == "" && len(gomoji.FindAll(text)) != 0 {
    60  		contentType = protobuf.ChatMessage_EMOJI
    61  	}
    62  
    63  	return contentType
    64  }
    65  
    66  func (api *API) SendMessage(ctx context.Context, communityID types.HexBytes, chatID string, text string, responseTo string) (*SendMessageResponse, error) {
    67  	ensName, _ := api.s.accountsDB.GetPreferredUsername()
    68  
    69  	msg := &common.Message{
    70  		CommunityID: string(communityID.Bytes()),
    71  		ChatMessage: &protobuf.ChatMessage{
    72  			ChatId:      chatID,
    73  			ContentType: isTextOrEmoji(text),
    74  			Text:        text,
    75  			ResponseTo:  responseTo,
    76  			EnsName:     ensName,
    77  		},
    78  	}
    79  
    80  	response, err := api.s.messenger.SendChatMessage(ctx, msg)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	return api.toSendMessageResponse(response)
    86  }
    87  
    88  func (api *API) SendImages(ctx context.Context, communityID types.HexBytes, chatID string, imagePaths []string, text string, responseTo string) (*SendMessageResponse, error) {
    89  	ensName, _ := api.s.accountsDB.GetPreferredUsername()
    90  
    91  	var messages []*common.Message
    92  
    93  	for _, imagePath := range imagePaths {
    94  		messages = append(messages, &common.Message{
    95  			CommunityID: string(communityID.Bytes()),
    96  			ChatMessage: &protobuf.ChatMessage{
    97  				ChatId:      chatID,
    98  				ContentType: protobuf.ChatMessage_IMAGE,
    99  				Text:        "Update to latest version to see a nice image here!",
   100  				ResponseTo:  responseTo,
   101  				EnsName:     ensName,
   102  			},
   103  			ImagePath: imagePath,
   104  		})
   105  	}
   106  
   107  	if text != "" {
   108  		messages = append(messages, &common.Message{
   109  			CommunityID: string(communityID.Bytes()),
   110  			ChatMessage: &protobuf.ChatMessage{
   111  				ChatId:      chatID,
   112  				ContentType: isTextOrEmoji(text),
   113  				Text:        text,
   114  				ResponseTo:  responseTo,
   115  				EnsName:     ensName,
   116  			},
   117  		})
   118  	}
   119  
   120  	response, err := api.s.messenger.SendChatMessages(ctx, messages)
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  
   125  	return api.toSendMessageResponse(response)
   126  
   127  }
   128  
   129  func (api *API) SendAudio(ctx context.Context, communityID types.HexBytes, chatID string, audioPath string, responseTo string) (*SendMessageResponse, error) {
   130  	ensName, _ := api.s.accountsDB.GetPreferredUsername()
   131  
   132  	msg := &common.Message{
   133  		CommunityID: string(communityID.Bytes()),
   134  		ChatMessage: &protobuf.ChatMessage{
   135  			ChatId:      chatID,
   136  			Text:        "Update to latest version to listen to an audio message here!",
   137  			ContentType: protobuf.ChatMessage_AUDIO,
   138  			ResponseTo:  responseTo,
   139  			EnsName:     ensName,
   140  		},
   141  		AudioPath: audioPath,
   142  	}
   143  
   144  	response, err := api.s.messenger.SendChatMessage(ctx, msg)
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  
   149  	return api.toSendMessageResponse(response)
   150  }