Skip to content

Commit

Permalink
[buteo-sync-plugins-google] Further improve Google Contacts avatar sy…
Browse files Browse the repository at this point in the history
…nc. Contributes to JB#51976

If some avatars were not downloaded due to transient error, request
throttle, etc, then we should ensure that we download those during
subsequent sync cycles.

Also, ensure that we only trigger avatar downloads once all other
sync requests are complete, to avoid throttling of sync requests.
  • Loading branch information
chriadam committed Nov 24, 2020
1 parent db60b48 commit e78a06a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/google/google-contacts/googletwowaycontactsyncadaptor.cpp
Expand Up @@ -244,6 +244,9 @@ void GoogleContactSqliteSyncAdaptor::syncFinishedSuccessfully()
} else {
m_collection.setId(savedCollection.id());
}

// and trigger downloading avatars.
q->downloadAvatars(m_accountId);
}

void GoogleContactSqliteSyncAdaptor::syncFinishedWithError()
Expand Down Expand Up @@ -553,6 +556,22 @@ void GoogleTwoWayContactSyncAdaptor::contactsFinishedHandler()
if (newEtag.isEmpty()) {
SOCIALD_LOG_ERROR("No etag found for contact:" << guid);
} else if (newEtag == m_contactEtags[accountId].value(guid)) {
// the etags match, so no remote changes have occurred.
// most likely this is a spurious change, however it
// may be the case that we have not yet downloaded the
// avatar for this contact. Check this.
const QContactAvatar avatar = c.detail<QContactAvatar>();
const QString remoteImageUrl = avatar.imageUrl().toString();
const QString localFileName = remoteImageUrl.isEmpty()
? QString()
: avatar.imageUrl().isLocalFile()
? avatar.imageUrl().toString()
: GoogleContactImageDownloader::staticOutputFile(guid, remoteImageUrl);
if (!localFileName.isEmpty() && !QFile::exists(localFileName)) {
SOCIALD_LOG_DEBUG("Remote modification spurious except for missing avatar");
m_contactAvatars[accountId].insert(guid, remoteImageUrl); // outstanding avatars.
m_avatarEtags[accountId][guid] = avatar.value(QContactAvatar::FieldMetaData).toString();
}
SOCIALD_LOG_DEBUG("Disregarding spurious remote modification for contact:" << guid);
continue;
}
Expand Down Expand Up @@ -782,9 +801,6 @@ void GoogleTwoWayContactSyncAdaptor::upsyncLocalChangesList(int accountId)
}
}

// Attempt to download any outstanding avatars.
queueOutstandingAvatars(accountId, m_accessTokens[accountId]);

// Save the sync timestamp.
GoogleContactSqliteSyncAdaptor *sqliteSync = m_sqliteSync.value(accountId);
if (!sqliteSync->m_syncDateTime.isValid()) {
Expand Down Expand Up @@ -893,6 +909,12 @@ void GoogleTwoWayContactSyncAdaptor::postErrorHandler()
sender()->setProperty("isError", QVariant::fromValue<bool>(true));
}

void GoogleTwoWayContactSyncAdaptor::downloadAvatars(int accountId)
{
// Attempt to download any outstanding avatars.
queueOutstandingAvatars(accountId, m_accessTokens[accountId]);
}

void GoogleTwoWayContactSyncAdaptor::queueOutstandingAvatars(int accountId, const QString &accessToken)
{
int queuedCount = 0;
Expand Down Expand Up @@ -925,7 +947,7 @@ bool GoogleTwoWayContactSyncAdaptor::queueAvatarForDownload(int accountId, const
return false;
}

void GoogleTwoWayContactSyncAdaptor::transformContactAvatars(QList<QContact> &remoteContacts, int accountId, const QString &accessToken)
void GoogleTwoWayContactSyncAdaptor::transformContactAvatars(QList<QContact> &remoteContacts, int accountId, const QString &)
{
// The avatar detail from the remote contact will be of the form:
// https://www.google.com/m8/feeds/photos/media/user@gmail.com/userId
Expand Down Expand Up @@ -981,11 +1003,9 @@ void GoogleTwoWayContactSyncAdaptor::transformContactAvatars(QList<QContact> &re
SOCIALD_LOG_ERROR("Unable to transform avatar detail");
}

// queue outstanding avatar for download once all upsyncs are complete
m_contactAvatars[accountId].insert(contactGuid, remoteImageUrl);
m_avatarEtags[accountId][contactGuid] = newAvatarEtag;

// then trigger the download
queueAvatarForDownload(accountId, accessToken, contactGuid, remoteImageUrl);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/google/google-contacts/googletwowaycontactsyncadaptor.h
Expand Up @@ -113,6 +113,8 @@ class GoogleTwoWayContactSyncAdaptor : public GoogleDataTypeSyncAdaptor
const QList<QContact> &locallyDeleted,
int accountId);

void downloadAvatars(int accountId);

QContactManager *m_contactManager = nullptr;

protected:
Expand Down

0 comments on commit e78a06a

Please sign in to comment.