2022-02-25 17:52:03 +00:00
|
|
|
package chat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/forPelevin/gomoji"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/protocol"
|
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SendMessageResponse struct {
|
|
|
|
Chat *Chat `json:"chat"`
|
|
|
|
Messages []*common.Message `json:"messages"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) SendSticker(ctx context.Context, communityID types.HexBytes, chatID string, packID int32, hash string, responseTo string) (*SendMessageResponse, error) {
|
|
|
|
ensName, _ := api.s.accountsDB.GetPreferredUsername()
|
|
|
|
|
|
|
|
msg := &common.Message{
|
|
|
|
CommunityID: string(communityID.Bytes()),
|
2023-08-18 11:39:59 +00:00
|
|
|
ChatMessage: &protobuf.ChatMessage{
|
2022-02-25 17:52:03 +00:00
|
|
|
ChatId: chatID,
|
|
|
|
ContentType: protobuf.ChatMessage_STICKER,
|
|
|
|
Text: "Update to latest version to see a nice sticker here!",
|
|
|
|
Payload: &protobuf.ChatMessage_Sticker{
|
|
|
|
Sticker: &protobuf.StickerMessage{
|
|
|
|
Hash: hash,
|
|
|
|
Pack: packID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ResponseTo: responseTo,
|
|
|
|
EnsName: ensName,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := api.s.messenger.SendChatMessage(ctx, msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.toSendMessageResponse(response)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) toSendMessageResponse(response *protocol.MessengerResponse) (*SendMessageResponse, error) {
|
|
|
|
protocolChat := response.Chats()[0]
|
|
|
|
|
|
|
|
community, err := api.s.messenger.GetCommunityByID(types.HexBytes(protocolChat.CommunityID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pubKey := types.EncodeHex(crypto.FromECDSAPub(api.s.messenger.IdentityPublicKey()))
|
2023-03-24 15:19:31 +00:00
|
|
|
chat, err := api.toAPIChat(protocolChat, community, pubKey, false)
|
2022-02-25 17:52:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &SendMessageResponse{
|
|
|
|
Chat: chat,
|
|
|
|
Messages: response.Messages(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isTextOrEmoji(text string) protobuf.ChatMessage_ContentType {
|
|
|
|
contentType := protobuf.ChatMessage_TEXT_PLAIN
|
|
|
|
if gomoji.RemoveEmojis(text) == "" && len(gomoji.FindAll(text)) != 0 {
|
|
|
|
contentType = protobuf.ChatMessage_EMOJI
|
|
|
|
}
|
|
|
|
|
|
|
|
return contentType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) SendMessage(ctx context.Context, communityID types.HexBytes, chatID string, text string, responseTo string) (*SendMessageResponse, error) {
|
|
|
|
ensName, _ := api.s.accountsDB.GetPreferredUsername()
|
|
|
|
|
|
|
|
msg := &common.Message{
|
|
|
|
CommunityID: string(communityID.Bytes()),
|
2023-08-18 11:39:59 +00:00
|
|
|
ChatMessage: &protobuf.ChatMessage{
|
2022-02-25 17:52:03 +00:00
|
|
|
ChatId: chatID,
|
|
|
|
ContentType: isTextOrEmoji(text),
|
|
|
|
Text: text,
|
|
|
|
ResponseTo: responseTo,
|
|
|
|
EnsName: ensName,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := api.s.messenger.SendChatMessage(ctx, msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.toSendMessageResponse(response)
|
|
|
|
}
|
|
|
|
|
2023-03-29 22:18:22 +00:00
|
|
|
func (api *API) SendImages(ctx context.Context, communityID types.HexBytes, chatID string, imagePaths []string, text string, responseTo string) (*SendMessageResponse, error) {
|
2022-02-25 17:52:03 +00:00
|
|
|
ensName, _ := api.s.accountsDB.GetPreferredUsername()
|
|
|
|
|
|
|
|
var messages []*common.Message
|
|
|
|
|
|
|
|
for _, imagePath := range imagePaths {
|
|
|
|
messages = append(messages, &common.Message{
|
|
|
|
CommunityID: string(communityID.Bytes()),
|
2023-08-18 11:39:59 +00:00
|
|
|
ChatMessage: &protobuf.ChatMessage{
|
2022-02-25 17:52:03 +00:00
|
|
|
ChatId: chatID,
|
|
|
|
ContentType: protobuf.ChatMessage_IMAGE,
|
|
|
|
Text: "Update to latest version to see a nice image here!",
|
2023-03-29 22:18:22 +00:00
|
|
|
ResponseTo: responseTo,
|
2022-02-25 17:52:03 +00:00
|
|
|
EnsName: ensName,
|
|
|
|
},
|
|
|
|
ImagePath: imagePath,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if text != "" {
|
|
|
|
messages = append(messages, &common.Message{
|
|
|
|
CommunityID: string(communityID.Bytes()),
|
2023-08-18 11:39:59 +00:00
|
|
|
ChatMessage: &protobuf.ChatMessage{
|
2022-02-25 17:52:03 +00:00
|
|
|
ChatId: chatID,
|
|
|
|
ContentType: isTextOrEmoji(text),
|
|
|
|
Text: text,
|
2023-03-29 22:18:22 +00:00
|
|
|
ResponseTo: responseTo,
|
2022-02-25 17:52:03 +00:00
|
|
|
EnsName: ensName,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := api.s.messenger.SendChatMessages(ctx, messages)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.toSendMessageResponse(response)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) SendAudio(ctx context.Context, communityID types.HexBytes, chatID string, audioPath string, responseTo string) (*SendMessageResponse, error) {
|
|
|
|
ensName, _ := api.s.accountsDB.GetPreferredUsername()
|
|
|
|
|
|
|
|
msg := &common.Message{
|
|
|
|
CommunityID: string(communityID.Bytes()),
|
2023-08-18 11:39:59 +00:00
|
|
|
ChatMessage: &protobuf.ChatMessage{
|
2022-02-25 17:52:03 +00:00
|
|
|
ChatId: chatID,
|
|
|
|
Text: "Update to latest version to listen to an audio message here!",
|
|
|
|
ContentType: protobuf.ChatMessage_AUDIO,
|
|
|
|
ResponseTo: responseTo,
|
|
|
|
EnsName: ensName,
|
|
|
|
},
|
|
|
|
AudioPath: audioPath,
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := api.s.messenger.SendChatMessage(ctx, msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.toSendMessageResponse(response)
|
|
|
|
}
|