Skip to content

Commit

Permalink
Fix contact details large image is always empty (#4864)
Browse files Browse the repository at this point in the history
fixes #13563 contact details largeImage is always empty
  • Loading branch information
kounkou authored Mar 6, 2024
1 parent 67fd2ce commit 0e37ec2
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 9 deletions.
11 changes: 11 additions & 0 deletions protocol/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ var chatColors = []string{

type ChatType int

type ChatContext string

const (
ChatTypeOneToOne ChatType = iota + 1
ChatTypePublic
Expand Down Expand Up @@ -636,3 +638,12 @@ func stringSliceContains(slice []string, item string) bool {
}
return false
}

func GetChatContextFromChatType(chatType ChatType) ChatContext {
switch chatType {
case ChatTypeOneToOne, ChatTypePrivateGroupChat:
return privateChat
default:
return publicChat
}
}
26 changes: 26 additions & 0 deletions protocol/chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,29 @@ func (s *ChatTestSuite) TestDeepLink() {

s.Require().Equal(chat.DeepLink(), "status-app://cc/c432709e-fc73-440d-bb67-cb3a0929dfda#zQ3shZL6dXiFCbDyxnXxwQa9v8QFC2q19subFtyxd7kVszMVo")
}

func (s *ChatTestSuite) TestGetChatContextFromChatType() {
chat := &Chat{
CommunityID: "0x02b1",
ID: "0x02b1",
ChatType: ChatTypeCommunityChat,
}

s.Require().Equal(GetChatContextFromChatType(chat.ChatType), publicChat)

chat = &Chat{
CommunityID: "0x02b1",
ID: "0x02b1",
ChatType: ChatTypeOneToOne,
}

s.Require().Equal(GetChatContextFromChatType(chat.ChatType), privateChat)

chat = &Chat{
CommunityID: "0x02b1",
ID: "0x02b1",
ChatType: ChatTypePrivateGroupChat,
}

s.Require().Equal(GetChatContextFromChatType(chat.ChatType), privateChat)
}
15 changes: 7 additions & 8 deletions protocol/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,13 @@ import (
"github.com/status-im/status-go/telemetry"
)

// todo: kozieiev: get rid of wakutransp word
type chatContext string

const (
PubKeyStringLength = 132

transactionSentTxt = "Transaction sent"

publicChat chatContext = "public-chat"
privateChat chatContext = "private-chat"
publicChat ChatContext = "public-chat"
privateChat ChatContext = "private-chat"
)

var communityAdvertiseIntervalSecond int64 = 60 * 60
Expand Down Expand Up @@ -1127,7 +1124,9 @@ func (m *Messenger) handleStandaloneChatIdentity(chat *Chat) error {
return nil
}

ci, err := m.createChatIdentity(publicChat)
chatContext := GetChatContextFromChatType(chat.ChatType)

ci, err := m.createChatIdentity(chatContext)
if err != nil {
return err
}
Expand Down Expand Up @@ -1274,7 +1273,7 @@ func (m *Messenger) shouldPublishChatIdentity(chatID string) (bool, error) {
// createChatIdentity creates a context based protobuf.ChatIdentity.
// context 'public-chat' will attach only the 'thumbnail' IdentityImage
// context 'private-chat' will attach all IdentityImage
func (m *Messenger) createChatIdentity(context chatContext) (*protobuf.ChatIdentity, error) {
func (m *Messenger) createChatIdentity(context ChatContext) (*protobuf.ChatIdentity, error) {
m.logger.Info(fmt.Sprintf("account keyUID '%s'", m.account.KeyUID))
m.logger.Info(fmt.Sprintf("context '%s'", context))

Expand Down Expand Up @@ -1324,7 +1323,7 @@ func (m *Messenger) adaptIdentityImageToProtobuf(img *images.IdentityImage) *pro
}
}

func (m *Messenger) attachIdentityImagesToChatIdentity(context chatContext, ci *protobuf.ChatIdentity) error {
func (m *Messenger) attachIdentityImagesToChatIdentity(context ChatContext, ci *protobuf.ChatIdentity) error {
s, err := m.getSettings()
if err != nil {
return err
Expand Down
78 changes: 78 additions & 0 deletions protocol/messenger_chat_context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package protocol

import (
_ "github.com/mutecomm/go-sqlcipher/v4" // require go-sqlcipher that overrides default implementation

"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/images"
"github.com/status-im/status-go/multiaccounts/settings"
"github.com/status-im/status-go/protocol/protobuf"
)

func isImageWithNamePresent(imgs map[string]*protobuf.IdentityImage, name string) bool {
for k, v := range imgs {
if k == name && len(v.Payload) > 0 {
return true
}
}

return false
}

func (s *MessengerSuite) retrieveIdentityImages(alice, bob *Messenger, chat *Chat) map[string]*protobuf.IdentityImage {
s.Require().NoError(alice.settings.SaveSettingField(settings.DisplayName, "alice"))

identityImages := images.SampleIdentityImages()
identityImagesMap := make(map[string]images.IdentityImage)
for _, img := range identityImages {
img.KeyUID = s.m.account.KeyUID
identityImagesMap[img.Name] = img
}

err := s.m.multiAccounts.StoreIdentityImages(s.m.account.KeyUID, identityImages, true)
s.Require().NoError(err)
s.Require().NoError(alice.SaveChat(chat))
s.Require().NoError(bob.settings.SaveSettingField(settings.DisplayName, "bob"))
s.Require().NoError(bob.SaveChat(chat))

chatContext := GetChatContextFromChatType(chat.ChatType)

chatIdentity, err := alice.createChatIdentity(chatContext)
s.Require().NoError(err)

imgs := chatIdentity.Images
s.Require().NoError(err)

return imgs
}

func (s *MessengerSuite) TestTwoImagesAreAddedToChatIdentityForPrivateChat() {
alice := s.m
bob := s.newMessenger()
defer TearDownMessenger(&s.Suite, bob)

bobPkString := types.EncodeHex(crypto.FromECDSAPub(&bob.identity.PublicKey))

chat := CreateOneToOneChat(bobPkString, &bob.identity.PublicKey, alice.transport)
s.Require().Equal(privateChat, GetChatContextFromChatType(chat.ChatType))

imgs := s.retrieveIdentityImages(alice, bob, chat)
s.Require().Len(imgs, 2)
s.Require().Equal(true, isImageWithNamePresent(imgs, "thumbnail"))
s.Require().Equal(true, isImageWithNamePresent(imgs, "large"))
}

func (s *MessengerSuite) TestOneImageIsAddedToChatIdentityForPublicChat() {
alice := s.m
bob := s.newMessenger()
defer TearDownMessenger(&s.Suite, bob)

chat := CreatePublicChat("alic-and-bob-chat", &testTimeSource{})
s.Require().Equal(publicChat, GetChatContextFromChatType(chat.ChatType))

imgs := s.retrieveIdentityImages(alice, bob, chat)
s.Require().Len(imgs, 1)
s.Require().Equal(true, isImageWithNamePresent(imgs, "thumbnail"))
s.Require().Equal(false, isImageWithNamePresent(imgs, "large"))
}
2 changes: 1 addition & 1 deletion protocol/messenger_identity_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (s *MessengerProfilePictureHandlerSuite) TestE2eSendingReceivingProfilePict
"bob": {true, false},
}

chatContexts := []chatContext{
chatContexts := []ChatContext{
publicChat,
privateChat,
}
Expand Down

0 comments on commit 0e37ec2

Please sign in to comment.