Don't pass contacts without custom fields to the client

When receiving a message with save a contact in the database in order to
avoid re-calculating image/profile.
This contact is then passed to the client, which can negatively impact
performance.

This commit changes the behavior so that only those contacts that have
some custom fields (have been explicitly added by the user, have been
blocked by the user, have sent a contact request or have a verified ens
name) are passed to the client.
This commit is contained in:
Andrea Maria Piana 2020-04-17 13:22:38 +02:00
parent 3a84afd0f1
commit 2d1f6c1cfa
5 changed files with 29 additions and 34 deletions

View File

@ -1 +1 @@
0.52.2
0.52.3

View File

@ -116,6 +116,12 @@ func buildContact(publicKey *ecdsa.PublicKey) (*Contact, error) {
return contact, nil
}
// HasCustomFields returns whether the the contact has any field that is valuable
// to the client other than the computed name/image
func (c Contact) HasCustomFields() bool {
return c.IsAdded() || c.HasBeenAdded() || c.IsBlocked() || c.ENSVerified
}
func contactIDFromPublicKey(key *ecdsa.PublicKey) string {
return types.EncodeHex(crypto.FromECDSAPub(key))

View File

@ -1216,7 +1216,9 @@ func (m *Messenger) Contacts() []*Contact {
defer m.mutex.Unlock()
var contacts []*Contact
for _, contact := range m.allContacts {
contacts = append(contacts, contact)
if contact.HasCustomFields() {
contacts = append(contacts, contact)
}
}
return contacts
}
@ -1980,8 +1982,18 @@ func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filte
messageState.Response.Chats = append(messageState.Response.Chats, messageState.AllChats[id])
}
var contactsToSave []*Contact
for id := range messageState.ModifiedContacts {
messageState.Response.Contacts = append(messageState.Response.Contacts, messageState.AllContacts[id])
contact := messageState.AllContacts[id]
if contact != nil {
// We save all contacts so we can pull back name/image,
// but we only send to client those
// that have some custom fields
contactsToSave = append(contactsToSave, contact)
if contact.HasCustomFields() {
messageState.Response.Contacts = append(messageState.Response.Contacts, contact)
}
}
}
for id := range messageState.ModifiedInstallations {
@ -2009,8 +2021,8 @@ func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filte
}
}
if len(messageState.Response.Contacts) > 0 {
err = m.persistence.SaveContacts(messageState.Response.Contacts)
if len(contactsToSave) > 0 {
err = m.persistence.SaveContacts(contactsToSave)
if err != nil {
return nil, err
}

View File

@ -219,7 +219,7 @@ func (s *MessengerInstallationSuite) TestSyncInstallation() {
s.Require().NoError(err)
var allChats []*Chat
var allContacts []*Contact
var actualContact *Contact
// Wait for the message to reach its destination
err = tt.RetryWithBackOff(func() error {
var err error
@ -229,9 +229,9 @@ func (s *MessengerInstallationSuite) TestSyncInstallation() {
}
allChats = append(allChats, response.Chats...)
allContacts = append(allContacts, response.Contacts...)
if len(allChats) >= 2 && len(allContacts) >= 3 {
if len(allChats) >= 2 && len(response.Contacts) == 1 {
actualContact = response.Contacts[0]
return nil
}
@ -250,28 +250,5 @@ func (s *MessengerInstallationSuite) TestSyncInstallation() {
s.Require().NotNil(statusChat)
var actualContact *Contact
for _, c := range allContacts {
if c.ID == contact.ID {
actualContact = c
}
}
s.Require().True(actualContact.IsAdded())
var ourContact *Contact
myID := types.EncodeHex(crypto.FromECDSAPub(&s.m.identity.PublicKey))
for _, c := range allContacts {
if c.ID == myID {
if ourContact == nil || ourContact.LastUpdated < c.LastUpdated {
ourContact = c
}
}
}
s.Require().NotNil(ourContact)
s.Require().Equal("ens-name", ourContact.Name)
s.Require().Equal("profile-image", ourContact.Photo)
}

View File

@ -1191,7 +1191,7 @@ func (s *MessengerSuite) TestBlockContact() {
Name: "contact-name",
Photo: "contact-photo",
LastUpdated: 20,
SystemTags: []string{"1", "2"},
SystemTags: []string{contactAdded, contactRequestReceived},
DeviceInfo: []ContactDeviceInfo{
{
InstallationID: "1",
@ -1379,7 +1379,7 @@ func (s *MessengerSuite) TestContactPersistence() {
Name: "contact-name",
Photo: "contact-photo",
LastUpdated: 20,
SystemTags: []string{"1", "2"},
SystemTags: []string{contactAdded, contactRequestReceived},
DeviceInfo: []ContactDeviceInfo{
{
InstallationID: "1",
@ -1414,7 +1414,7 @@ func (s *MessengerSuite) TestContactPersistenceUpdate() {
Name: "contact-name",
Photo: "contact-photo",
LastUpdated: 20,
SystemTags: []string{"1", "2"},
SystemTags: []string{contactAdded, contactRequestReceived},
DeviceInfo: []ContactDeviceInfo{
{
InstallationID: "1",