From 4d3c04e41c7984f09dd95d28006436647f27020c Mon Sep 17 00:00:00 2001 From: Brian Sztamfater Date: Thu, 7 Oct 2021 01:42:27 -0300 Subject: [PATCH] Modify CanonicalImage function to return large or thumb image if available (#2300) --- multiaccounts/accounts/database.go | 9 +++++++++ protocol/contact.go | 31 +++++++++++++++++++++++++++++- protocol/local_notifications.go | 17 ++++++++-------- protocol/messenger.go | 20 +++++++++++++++---- protocol/messenger_handler.go | 7 ++++++- 5 files changed, 70 insertions(+), 14 deletions(-) diff --git a/multiaccounts/accounts/database.go b/multiaccounts/accounts/database.go index 6b2fe401b..9a6a77839 100644 --- a/multiaccounts/accounts/database.go +++ b/multiaccounts/accounts/database.go @@ -613,6 +613,15 @@ func (db *Database) GetNotificationsEnabled() (bool, error) { return result, err } +func (db *Database) GetProfilePicturesVisibility() (int, error) { + var result int + err := db.db.QueryRow("SELECT profile_pictures_visibility FROM settings WHERE synthetic_id = 'id'").Scan(&result) + if err == sql.ErrNoRows { + return result, nil + } + return result, err +} + func (db *Database) CanUseMailservers() (bool, error) { var result bool err := db.db.QueryRow("SELECT use_mailservers FROM settings WHERE synthetic_id = 'id'").Scan(&result) diff --git a/protocol/contact.go b/protocol/contact.go index df1b289ef..d1ff2cac7 100644 --- a/protocol/contact.go +++ b/protocol/contact.go @@ -6,10 +6,21 @@ import ( "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/accounts" "github.com/status-im/status-go/protocol/identity/alias" "github.com/status-im/status-go/protocol/identity/identicon" ) +// ContactDeviceInfo is a struct containing information about a particular device owned by a contact +type ContactDeviceInfo struct { + // The installation id of the device + InstallationID string `json:"id"` + // Timestamp represents the last time we received this info + Timestamp int64 `json:"timestamp"` + // FCMToken is to be used for push notifications + FCMToken string `json:"fcmToken"` +} + func (c *Contact) CanonicalName() string { if c.LocalNickname != "" { return c.LocalNickname @@ -22,7 +33,25 @@ func (c *Contact) CanonicalName() string { return c.Alias } -func (c *Contact) CanonicalImage() string { +func (c *Contact) CanonicalImage(profilePicturesVisibility accounts.ProfilePicturesVisibilityType) string { + if profilePicturesVisibility == accounts.ProfilePicturesVisibilityNone || (profilePicturesVisibility == accounts.ProfilePicturesVisibilityContactsOnly && !c.Added) { + return c.Identicon + } + + if largeImage, ok := c.Images[images.LargeDimName]; ok { + imageBase64, err := largeImage.GetDataURI() + if err == nil { + return imageBase64 + } + } + + if thumbImage, ok := c.Images[images.SmallDimName]; ok { + imageBase64, err := thumbImage.GetDataURI() + if err == nil { + return imageBase64 + } + } + return c.Identicon } diff --git a/protocol/local_notifications.go b/protocol/local_notifications.go index e9fae6da5..88a8c5393 100644 --- a/protocol/local_notifications.go +++ b/protocol/local_notifications.go @@ -5,6 +5,7 @@ import ( "encoding/json" gethcommon "github.com/ethereum/go-ethereum/common" + "github.com/status-im/status-go/multiaccounts/accounts" "github.com/status-im/status-go/protocol/common" "github.com/status-im/status-go/protocol/communities" localnotifications "github.com/status-im/status-go/services/local-notifications" @@ -44,14 +45,14 @@ func (n NotificationBody) MarshalJSON() ([]byte, error) { return json.Marshal(item) } -func NewMessageNotification(id string, message *common.Message, chat *Chat, contact *Contact, contacts *contactMap) (*localnotifications.Notification, error) { +func NewMessageNotification(id string, message *common.Message, chat *Chat, contact *Contact, contacts *contactMap, profilePicturesVisibility int) (*localnotifications.Notification, error) { body := &NotificationBody{ Message: message, Chat: chat, Contact: contact, } - return body.toMessageNotification(id, contacts) + return body.toMessageNotification(id, contacts, profilePicturesVisibility) } func DeletedMessageNotification(id string, chat *Chat) *localnotifications.Notification { @@ -74,16 +75,16 @@ func NewCommunityRequestToJoinNotification(id string, community *communities.Com return body.toCommunityRequestToJoinNotification(id) } -func NewPrivateGroupInviteNotification(id string, chat *Chat, contact *Contact) *localnotifications.Notification { +func NewPrivateGroupInviteNotification(id string, chat *Chat, contact *Contact, profilePicturesVisibility int) *localnotifications.Notification { body := &NotificationBody{ Chat: chat, Contact: contact, } - return body.toPrivateGroupInviteNotification(id) + return body.toPrivateGroupInviteNotification(id, profilePicturesVisibility) } -func (n NotificationBody) toMessageNotification(id string, contacts *contactMap) (*localnotifications.Notification, error) { +func (n NotificationBody) toMessageNotification(id string, contacts *contactMap, profilePicturesVisibility int) (*localnotifications.Notification, error) { var title string if n.Chat.PrivateGroupChat() || n.Chat.Public() || n.Chat.CommunityChat() { title = n.Chat.Name @@ -123,7 +124,7 @@ func (n NotificationBody) toMessageNotification(id string, contacts *contactMap) IsGroupConversation: true, Author: localnotifications.NotificationAuthor{ Name: n.Contact.CanonicalName(), - Icon: n.Contact.CanonicalImage(), + Icon: n.Contact.CanonicalImage(accounts.ProfilePicturesVisibilityType(profilePicturesVisibility)), ID: n.Contact.ID, }, Timestamp: n.Message.WhisperTimestamp, @@ -132,7 +133,7 @@ func (n NotificationBody) toMessageNotification(id string, contacts *contactMap) }, nil } -func (n NotificationBody) toPrivateGroupInviteNotification(id string) *localnotifications.Notification { +func (n NotificationBody) toPrivateGroupInviteNotification(id string, profilePicturesVisibility int) *localnotifications.Notification { return &localnotifications.Notification{ ID: gethcommon.HexToHash(id), Body: n, @@ -143,7 +144,7 @@ func (n NotificationBody) toPrivateGroupInviteNotification(id string) *localnoti Deeplink: n.Chat.DeepLink(), Author: localnotifications.NotificationAuthor{ Name: n.Contact.CanonicalName(), - Icon: n.Contact.CanonicalImage(), + Icon: n.Contact.CanonicalImage(accounts.ProfilePicturesVisibilityType(profilePicturesVisibility)), ID: n.Contact.ID, }, Image: "", diff --git a/protocol/messenger.go b/protocol/messenger.go index 576454e3d..087960f41 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -2564,7 +2564,7 @@ func (m *Messenger) markDeliveredMessages(acks [][]byte) { // addNewMessageNotification takes a common.Message and generates a new NotificationBody and appends it to the // []Response.Notifications if the message is m.New -func (r *ReceivedMessageState) addNewMessageNotification(publicKey ecdsa.PublicKey, m *common.Message, responseTo *common.Message) error { +func (r *ReceivedMessageState) addNewMessageNotification(publicKey ecdsa.PublicKey, m *common.Message, responseTo *common.Message, profilePicturesVisibility int) error { if !m.New { return nil } @@ -2586,7 +2586,7 @@ func (r *ReceivedMessageState) addNewMessageNotification(publicKey ecdsa.PublicK } if showMessageNotification(publicKey, m, chat, responseTo) { - notification, err := NewMessageNotification(m.ID, m, chat, contact, r.AllContacts) + notification, err := NewMessageNotification(m.ID, m, chat, contact, r.AllContacts, profilePicturesVisibility) if err != nil { return err } @@ -3275,13 +3275,19 @@ func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filte if err != nil { return nil, err } + + profilePicturesVisibility, err := m.settings.GetProfilePicturesVisibility() + if err != nil { + return nil, err + } + for _, message := range messageState.Response.messages { if _, ok := newMessagesIds[message.ID]; ok { message.New = true if notificationsEnabled { // Create notification body to be eventually passed to `localnotifications.SendMessageNotifications()` - if err = messageState.addNewMessageNotification(m.identity.PublicKey, message, messagesByID[message.ResponseTo]); err != nil { + if err = messageState.addNewMessageNotification(m.identity.PublicKey, message, messagesByID[message.ResponseTo], profilePicturesVisibility); err != nil { return nil, err } } @@ -4202,8 +4208,14 @@ func (m *Messenger) ValidateTransactions(ctx context.Context, addresses []types. if err != nil { return nil, err } + + profilePicturesVisibility, err := m.settings.GetProfilePicturesVisibility() + if err != nil { + return nil, err + } + if notificationsEnabled { - notification, err := NewMessageNotification(message.ID, message, chat, contact, m.allContacts) + notification, err := NewMessageNotification(message.ID, message, chat, contact, m.allContacts, profilePicturesVisibility) if err != nil { return nil, err } diff --git a/protocol/messenger_handler.go b/protocol/messenger_handler.go index 8331eed07..0e25b1aa6 100644 --- a/protocol/messenger_handler.go +++ b/protocol/messenger_handler.go @@ -115,8 +115,13 @@ func (m *Messenger) HandleMembershipUpdate(messageState *ReceivedMessageState, c return errors.Wrap(err, "failed to get group creator") } + profilePicturesVisibility, err := m.settings.GetProfilePicturesVisibility() + if err != nil { + return errors.Wrap(err, "failed to get profilePicturesVisibility setting") + } + if chat.Active && messageState.CurrentMessageState.Contact.ID != ourKey { - messageState.Response.AddNotification(NewPrivateGroupInviteNotification(chat.ID, chat, messageState.CurrentMessageState.Contact)) + messageState.Response.AddNotification(NewPrivateGroupInviteNotification(chat.ID, chat, messageState.CurrentMessageState.Contact, profilePicturesVisibility)) } } else { existingGroup, err := newProtocolGroupFromChat(chat)