Modify CanonicalImage function to return large or thumb image if available (#2300)

This commit is contained in:
Brian Sztamfater 2021-10-07 01:42:27 -03:00 committed by GitHub
parent 62b9d8062e
commit 4d3c04e41c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 70 additions and 14 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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: "",

View File

@ -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
}

View File

@ -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)