From 734477e2ba95e3434704b5ffafe9cd432971a63b Mon Sep 17 00:00:00 2001 From: Bea Lam Date: Wed, 10 Mar 2021 17:04:29 +1000 Subject: [PATCH] [buteo-sync-plugins-social] Ignore Google contact date if it cannot be read. JB#53422 The date of a Google birthday or event may be missing a 'year' value even if it is otherwise valid. Also, a birthday may be just some freeform text without a date. In these cases, ignore the value, otherwise we will upload an invalid date on the next upsync and overwrite the value on the server. --- .../google-contacts/googlepeoplejson.cpp | 63 +++++++++++++------ src/google/google-contacts/googlepeoplejson.h | 28 ++++----- 2 files changed, 58 insertions(+), 33 deletions(-) diff --git a/src/google/google-contacts/googlepeoplejson.cpp b/src/google/google-contacts/googlepeoplejson.cpp index f781cab..ca855d1 100644 --- a/src/google/google-contacts/googlepeoplejson.cpp +++ b/src/google/google-contacts/googlepeoplejson.cpp @@ -51,7 +51,7 @@ namespace { static const QString StarredContactGroupName = QStringLiteral("contactGroups/starred"); -QDate jsonObjectToDate(const QJsonObject &object) +QDate jsonObjectToDate(const QJsonObject &object, bool *ok) { const int year = object.value("year").toInt(); const int month = object.value("month").toInt(); @@ -61,6 +61,9 @@ QDate jsonObjectToDate(const QJsonObject &object) if (!date.isValid()) { SOCIALD_LOG_ERROR("Cannot read date from JSON:" << object); } + if (ok) { + *ok = date.isValid(); + } return date; } @@ -80,7 +83,11 @@ QList jsonArrayToList(const QJsonArray &array) { QList values; for (auto it = array.constBegin(); it != array.constEnd(); ++it) { - values.append(T::fromJsonObject(it->toObject())); + bool error = false; + const T &value = T::fromJsonObject(it->toObject(), &error); + if (!error) { + values.append(value); + } } return values; } @@ -169,7 +176,7 @@ bool shouldAddDetailChanges(const QContactDetail &detail, bool *hasChanges) } -GooglePeople::Source GooglePeople::Source::fromJsonObject(const QJsonObject &object) +GooglePeople::Source GooglePeople::Source::fromJsonObject(const QJsonObject &object, bool *) { Source ret; ret.type = object.value("type").toString(); @@ -223,7 +230,7 @@ bool GooglePeople::Address::saveContactDetails(QContact *contact, const QList return saveContactDetail(contact, &detail); } -GooglePeople::Name GooglePeople::Name::fromJsonObject(const QJsonObject &object) +GooglePeople::Name GooglePeople::Name::fromJsonObject(const QJsonObject &object, bool *) { Name ret; ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject()); @@ -676,7 +701,7 @@ bool GooglePeople::Nickname::saveContactDetails(QContact *contact, const QList(object.value("sources").toArray()); @@ -961,7 +986,7 @@ bool GooglePeople::Photo::saveContactDetails(QContact *contact, const QList & return true; } -GooglePeople::Url GooglePeople::Url::fromJsonObject(const QJsonObject &object) +GooglePeople::Url GooglePeople::Url::fromJsonObject(const QJsonObject &object, bool *) { Url ret; ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject()); @@ -1091,7 +1116,7 @@ GooglePeople::Person GooglePeople::Person::fromJsonObject(const QJsonObject &obj { Person ret; ret.resourceName = object.value("resourceName").toString(); - ret.metadata = PersonMetadata::fromJsonObject(object.value("metadata").toObject()); + ret.metadata = PersonMetadata::fromJsonObject(object.value("metadata").toObject(), nullptr); ret.addresses = jsonArrayToList
(object.value("addresses").toArray()); ret.biographies = jsonArrayToList(object.value("biographies").toArray()); ret.birthdays = jsonArrayToList(object.value("birthdays").toArray()); diff --git a/src/google/google-contacts/googlepeoplejson.h b/src/google/google-contacts/googlepeoplejson.h index e4c7a37..8dd773e 100644 --- a/src/google/google-contacts/googlepeoplejson.h +++ b/src/google/google-contacts/googlepeoplejson.h @@ -45,7 +45,7 @@ namespace GooglePeople ProfileMetadata profileMetadata; */ - static Source fromJsonObject(const QJsonObject &obj); + static Source fromJsonObject(const QJsonObject &obj, bool *error = nullptr); }; class FieldMetadata @@ -75,7 +75,7 @@ namespace GooglePeople QString countryCode; static bool saveContactDetails(QContact *contact, const QList
&values); - static Address fromJsonObject(const QJsonObject &obj); + static Address fromJsonObject(const QJsonObject &obj, bool *error = nullptr); static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges); }; @@ -90,7 +90,7 @@ namespace GooglePeople */ static bool saveContactDetails(QContact *contact, const QList &values); - static Biography fromJsonObject(const QJsonObject &obj); + static Biography fromJsonObject(const QJsonObject &obj, bool *error = nullptr); static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges); }; @@ -105,7 +105,7 @@ namespace GooglePeople */ static bool saveContactDetails(QContact *contact, const QList &values); - static Birthday fromJsonObject(const QJsonObject &obj); + static Birthday fromJsonObject(const QJsonObject &obj, bool *error = nullptr); static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges); }; @@ -119,7 +119,7 @@ namespace GooglePeople QString displayName; static bool saveContactDetails(QContact *contact, const QList &values); - static EmailAddress fromJsonObject(const QJsonObject &obj); + static EmailAddress fromJsonObject(const QJsonObject &obj, bool *error = nullptr); static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges); }; @@ -135,7 +135,7 @@ namespace GooglePeople */ static bool saveContactDetails(QContact *contact, const QList &values); - static Event fromJsonObject(const QJsonObject &obj); + static Event fromJsonObject(const QJsonObject &obj, bool *error = nullptr); static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges); }; @@ -155,7 +155,7 @@ namespace GooglePeople const QList &values, int accountId, const QList &candidateCollections); - static Membership fromJsonObject(const QJsonObject &obj); + static Membership fromJsonObject(const QJsonObject &obj, bool *error = nullptr); static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges); }; @@ -182,7 +182,7 @@ namespace GooglePeople */ static bool saveContactDetails(QContact *contact, const QList &values); - static Name fromJsonObject(const QJsonObject &obj); + static Name fromJsonObject(const QJsonObject &obj, bool *error = nullptr); static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges); }; @@ -197,7 +197,7 @@ namespace GooglePeople */ static bool saveContactDetails(QContact *contact, const QList &values); - static Nickname fromJsonObject(const QJsonObject &obj); + static Nickname fromJsonObject(const QJsonObject &obj, bool *error = nullptr); static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges); }; @@ -222,7 +222,7 @@ namespace GooglePeople */ static bool saveContactDetails(QContact *contact, const QList &values); - static Organization fromJsonObject(const QJsonObject &obj); + static Organization fromJsonObject(const QJsonObject &obj, bool *error = nullptr); static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges); }; @@ -239,7 +239,7 @@ namespace GooglePeople */ static bool saveContactDetails(QContact *contact, const QList &values); - static PhoneNumber fromJsonObject(const QJsonObject &obj); + static PhoneNumber fromJsonObject(const QJsonObject &obj, bool *error = nullptr); static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges); }; @@ -254,7 +254,7 @@ namespace GooglePeople static QString etag(const QContact &contact); static bool saveContactDetails(QContact *contact, const PersonMetadata &value); - static PersonMetadata fromJsonObject(const QJsonObject &obj); + static PersonMetadata fromJsonObject(const QJsonObject &obj, bool *error = nullptr); static QJsonObject toJsonObject(const QContact &contact); }; @@ -270,7 +270,7 @@ namespace GooglePeople QString *localAvatarFile = nullptr); static bool saveContactDetails(QContact *contact, const QList &values); - static Photo fromJsonObject(const QJsonObject &obj); + static Photo fromJsonObject(const QJsonObject &obj, bool *error = nullptr); static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges); }; @@ -283,7 +283,7 @@ namespace GooglePeople QString formattedType; static bool saveContactDetails(QContact *contact, const QList &values); - static Url fromJsonObject(const QJsonObject &obj); + static Url fromJsonObject(const QJsonObject &obj, bool *error = nullptr); static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges); };