2022-02-25 17:52:03 +00:00
|
|
|
package chat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/forPelevin/gomoji"
|
|
|
|
|
|
|
|
"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 {
|
2024-05-13 19:06:21 +00:00
|
|
|
Chat *protocol.Chat `json:"chat"`
|
2022-02-25 17:52:03 +00:00
|
|
|
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]
|
|
|
|
|
|
|
|
return &SendMessageResponse{
|
2024-05-13 19:06:21 +00:00
|
|
|
Chat: protocolChat,
|
2022-02-25 17:52:03 +00:00
|
|
|
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)
|
|
|
|
}
|