feat(images): Add albumId to images album and allow to receive image messages without a text (#3287)

This commit is contained in:
Boris Melnik 2023-03-23 19:06:50 +03:00 committed by GitHub
parent daa926f748
commit 6666110ba4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 163 additions and 1 deletions

View File

@ -698,3 +698,14 @@ func (m *Message) LoadImage() error {
return nil
}
func (m *Message) SetAlbumID(albumID string) error {
imageMessage := m.GetImage()
if imageMessage == nil {
return errors.New("Image is empty")
}
imageMessage.AlbumId = albumID
m.Payload = &protobuf.ChatMessage_Image{Image: imageMessage}
return nil
}

View File

@ -233,7 +233,7 @@ func ValidateReceivedChatMessage(message *protobuf.ChatMessage, whisperTimestamp
return errors.New("timestamp can't be 0")
}
if message.ContentType != protobuf.ChatMessage_DISCORD_MESSAGE {
if message.ContentType != protobuf.ChatMessage_DISCORD_MESSAGE && (message.ContentType != protobuf.ChatMessage_IMAGE || message.Text != "") {
if err := ValidateText(message.Text); err != nil {
return err
}

View File

@ -18,6 +18,7 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/golang/protobuf/proto"
"github.com/google/uuid"
"github.com/pkg/errors"
"go.uber.org/zap"
@ -1968,7 +1969,18 @@ func (m *Messenger) SendChatMessage(ctx context.Context, message *common.Message
func (m *Messenger) SendChatMessages(ctx context.Context, messages []*common.Message) (*MessengerResponse, error) {
var response MessengerResponse
generatedAlbumID, err := uuid.NewRandom()
if err != nil {
return nil, err
}
for _, message := range messages {
if message.ContentType == protobuf.ChatMessage_IMAGE && len(messages) > 1 {
err = message.SetAlbumID(generatedAlbumID.String())
if err != nil {
return nil, err
}
}
messageResponse, err := m.SendChatMessage(ctx, message)
if err != nil {
return nil, err
@ -1996,6 +2008,7 @@ func (m *Messenger) sendChatMessage(ctx context.Context, message *common.Message
if err != nil {
return nil, err
}
} else if len(message.CommunityID) != 0 {
community, err := m.communitiesManager.GetByIDString(message.CommunityID)
if err != nil {

View File

@ -0,0 +1,138 @@
package protocol
import (
"context"
"crypto/ecdsa"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
"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/common"
"github.com/status-im/status-go/protocol/protobuf"
// "github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku"
)
func TestMessengerSendImagesAlbumSuite(t *testing.T) {
suite.Run(t, new(MessengerSendImagesAlbumSuite))
}
type MessengerSendImagesAlbumSuite struct {
suite.Suite
m *Messenger
privateKey *ecdsa.PrivateKey // private key for the main instance of Messenger
// If one wants to send messages between different instances of Messenger,
// a single waku service should be shared.
shh types.Waku
logger *zap.Logger
}
func (s *MessengerSendImagesAlbumSuite) SetupTest() {
s.logger = tt.MustCreateTestLogger()
config := waku.DefaultConfig
config.MinimumAcceptedPoW = 0
shh := waku.New(&config, s.logger)
s.shh = gethbridge.NewGethWakuWrapper(shh)
s.Require().NoError(shh.Start())
s.m = s.newMessenger()
s.privateKey = s.m.identity
_, err := s.m.Start()
s.Require().NoError(err)
}
func (s *MessengerSendImagesAlbumSuite) TearDownTest() {
s.Require().NoError(s.m.Shutdown())
}
func (s *MessengerSendImagesAlbumSuite) newMessenger() *Messenger {
privateKey, err := crypto.GenerateKey()
s.Require().NoError(err)
messenger, err := newMessengerWithKey(s.shh, privateKey, s.logger, nil)
s.Require().NoError(err)
return messenger
}
func buildImageWithoutAlbumIDMessage(s *MessengerSendImagesAlbumSuite, chat Chat) *common.Message {
file, err := os.Open("../_assets/tests/test.jpg")
s.Require().NoError(err)
defer file.Close()
payload, err := ioutil.ReadAll(file)
s.Require().NoError(err)
clock, timestamp := chat.NextClockAndTimestamp(&testTimeSource{})
message := &common.Message{}
message.ChatId = chat.ID
message.Clock = clock
message.Timestamp = timestamp
message.WhisperTimestamp = clock
message.LocalChatID = chat.ID
message.MessageType = protobuf.MessageType_ONE_TO_ONE
message.ContentType = protobuf.ChatMessage_IMAGE
image := protobuf.ImageMessage{
Payload: payload,
Type: protobuf.ImageType_JPEG,
Width: 1200,
Height: 1000,
}
message.Payload = &protobuf.ChatMessage_Image{Image: &image}
return message
}
func (s *MessengerSendImagesAlbumSuite) TestAlbumImageMessagesSend() {
theirMessenger := s.newMessenger()
_, err := theirMessenger.Start()
s.Require().NoError(err)
theirChat := CreateOneToOneChat("Their 1TO1", &s.privateKey.PublicKey, s.m.transport)
err = theirMessenger.SaveChat(theirChat)
s.Require().NoError(err)
ourChat := CreateOneToOneChat("Our 1TO1", &theirMessenger.identity.PublicKey, s.m.transport)
err = s.m.SaveChat(ourChat)
s.Require().NoError(err)
const messageCount = 3
var album []*common.Message
for i := 0; i < messageCount; i++ {
album = append(album, buildImageWithoutAlbumIDMessage(s, *ourChat))
}
err = s.m.SaveChat(ourChat)
s.NoError(err)
response, err := s.m.SendChatMessages(context.Background(), album)
s.NoError(err)
s.Require().Equal(messageCount, len(response.Messages()), "it returns the messages")
s.Require().NoError(err)
s.Require().Len(response.Messages(), messageCount)
response, err = WaitOnMessengerResponse(
theirMessenger,
func(r *MessengerResponse) bool { return len(r.messages) > 0 },
"no messages",
)
s.Require().NoError(err)
s.Require().Len(response.Chats(), 1)
s.Require().Len(response.Messages(), messageCount)
for _, message := range response.Messages() {
image := message.GetImage()
s.Require().NotNil(image)
s.Require().NotEmpty(image.AlbumId)
}
}