Skip to content

Commit

Permalink
[buteo-sync-plugins-social] Ignore Google contact date if it cannot b…
Browse files Browse the repository at this point in the history
…e 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.
  • Loading branch information
blammit committed Mar 11, 2021
1 parent d201eac commit 734477e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 33 deletions.
63 changes: 44 additions & 19 deletions src/google/google-contacts/googlepeoplejson.cpp
Expand Up @@ -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();
Expand All @@ -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;
}

Expand All @@ -80,7 +83,11 @@ QList<T> jsonArrayToList(const QJsonArray &array)
{
QList<T> 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;
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -223,7 +230,7 @@ bool GooglePeople::Address::saveContactDetails(QContact *contact, const QList<Ad
return true;
}

GooglePeople::Address GooglePeople::Address::fromJsonObject(const QJsonObject &obj)
GooglePeople::Address GooglePeople::Address::fromJsonObject(const QJsonObject &obj, bool *)
{
Address ret;
ret.metadata = FieldMetadata::fromJsonObject(obj.value("metadata").toObject());
Expand Down Expand Up @@ -296,7 +303,7 @@ bool GooglePeople::Biography::saveContactDetails(QContact *contact, const QList<
return saveContactDetail(contact, &detail);
}

GooglePeople::Biography GooglePeople::Biography::fromJsonObject(const QJsonObject &object)
GooglePeople::Biography GooglePeople::Biography::fromJsonObject(const QJsonObject &object, bool *)
{
Biography ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
Expand Down Expand Up @@ -332,11 +339,20 @@ bool GooglePeople::Birthday::saveContactDetails(QContact *contact, const QList<B
return saveContactDetail(contact, &detail);
}

GooglePeople::Birthday GooglePeople::Birthday::fromJsonObject(const QJsonObject &object)
GooglePeople::Birthday GooglePeople::Birthday::fromJsonObject(const QJsonObject &object, bool *error)
{
bool dateOk = false;
const QDate date = jsonObjectToDate(object.value("date").toObject(), &dateOk);
if (error) {
*error = !dateOk;
}
if (!dateOk) {
return Birthday();
}

Birthday ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
ret.date = jsonObjectToDate(object.value("date").toObject());
ret.date = date;
return ret;
}

Expand Down Expand Up @@ -388,7 +404,7 @@ bool GooglePeople::EmailAddress::saveContactDetails(QContact *contact, const QLi
return true;
}

GooglePeople::EmailAddress GooglePeople::EmailAddress::fromJsonObject(const QJsonObject &object)
GooglePeople::EmailAddress GooglePeople::EmailAddress::fromJsonObject(const QJsonObject &object, bool *)
{
EmailAddress ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
Expand Down Expand Up @@ -467,11 +483,20 @@ bool GooglePeople::Event::saveContactDetails(QContact *contact, const QList<Even
return true;
}

GooglePeople::Event GooglePeople::Event::fromJsonObject(const QJsonObject &object)
GooglePeople::Event GooglePeople::Event::fromJsonObject(const QJsonObject &object, bool *error)
{
bool dateOk = false;
const QDate date = jsonObjectToDate(object.value("date").toObject(), &dateOk);
if (error) {
*error = !dateOk;
}
if (!dateOk) {
return Event();
}

Event ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
ret.date = jsonObjectToDate(object.value("date").toObject());
ret.date = date;
ret.type = object.value("type").toString();
return ret;
}
Expand Down Expand Up @@ -568,7 +593,7 @@ bool GooglePeople::Membership::saveContactDetails(
return true;
}

GooglePeople::Membership GooglePeople::Membership::fromJsonObject(const QJsonObject &object)
GooglePeople::Membership GooglePeople::Membership::fromJsonObject(const QJsonObject &object, bool *)
{
Membership ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
Expand Down Expand Up @@ -631,7 +656,7 @@ bool GooglePeople::Name::saveContactDetails(QContact *contact, const QList<Name>
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());
Expand Down Expand Up @@ -676,7 +701,7 @@ bool GooglePeople::Nickname::saveContactDetails(QContact *contact, const QList<N
return true;
}

GooglePeople::Nickname GooglePeople::Nickname::fromJsonObject(const QJsonObject &object)
GooglePeople::Nickname GooglePeople::Nickname::fromJsonObject(const QJsonObject &object, bool *)
{
Nickname ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
Expand Down Expand Up @@ -718,7 +743,7 @@ bool GooglePeople::Organization::saveContactDetails(QContact *contact, const QLi
return true;
}

GooglePeople::Organization GooglePeople::Organization::fromJsonObject(const QJsonObject &object)
GooglePeople::Organization GooglePeople::Organization::fromJsonObject(const QJsonObject &object, bool *)
{
Organization ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
Expand Down Expand Up @@ -795,7 +820,7 @@ bool GooglePeople::PhoneNumber::saveContactDetails(QContact *contact, const QLis
return true;
}

GooglePeople::PhoneNumber GooglePeople::PhoneNumber::fromJsonObject(const QJsonObject &object)
GooglePeople::PhoneNumber GooglePeople::PhoneNumber::fromJsonObject(const QJsonObject &object, bool *)
{
PhoneNumber ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
Expand Down Expand Up @@ -882,7 +907,7 @@ QString GooglePeople::PersonMetadata::etag(const QContact &contact)
return sourceInfo.value("etag").toString();
}

GooglePeople::PersonMetadata GooglePeople::PersonMetadata::fromJsonObject(const QJsonObject &object)
GooglePeople::PersonMetadata GooglePeople::PersonMetadata::fromJsonObject(const QJsonObject &object, bool *)
{
PersonMetadata ret;
ret.sources = jsonArrayToList<Source>(object.value("sources").toArray());
Expand Down Expand Up @@ -961,7 +986,7 @@ bool GooglePeople::Photo::saveContactDetails(QContact *contact, const QList<Phot
return true;
}

GooglePeople::Photo GooglePeople::Photo::fromJsonObject(const QJsonObject &object)
GooglePeople::Photo GooglePeople::Photo::fromJsonObject(const QJsonObject &object, bool *)
{
Photo ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
Expand Down Expand Up @@ -1008,7 +1033,7 @@ bool GooglePeople::Url::saveContactDetails(QContact *contact, const QList<Url> &
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());
Expand Down Expand Up @@ -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<Address>(object.value("addresses").toArray());
ret.biographies = jsonArrayToList<Biography>(object.value("biographies").toArray());
ret.birthdays = jsonArrayToList<Birthday>(object.value("birthdays").toArray());
Expand Down
28 changes: 14 additions & 14 deletions src/google/google-contacts/googlepeoplejson.h
Expand Up @@ -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
Expand Down Expand Up @@ -75,7 +75,7 @@ namespace GooglePeople
QString countryCode;

static bool saveContactDetails(QContact *contact, const QList<Address> &values);
static Address fromJsonObject(const QJsonObject &obj);
static Address fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -90,7 +90,7 @@ namespace GooglePeople
*/

static bool saveContactDetails(QContact *contact, const QList<Biography> &values);
static Biography fromJsonObject(const QJsonObject &obj);
static Biography fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -105,7 +105,7 @@ namespace GooglePeople
*/

static bool saveContactDetails(QContact *contact, const QList<Birthday> &values);
static Birthday fromJsonObject(const QJsonObject &obj);
static Birthday fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -119,7 +119,7 @@ namespace GooglePeople
QString displayName;

static bool saveContactDetails(QContact *contact, const QList<EmailAddress> &values);
static EmailAddress fromJsonObject(const QJsonObject &obj);
static EmailAddress fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -135,7 +135,7 @@ namespace GooglePeople
*/

static bool saveContactDetails(QContact *contact, const QList<Event> &values);
static Event fromJsonObject(const QJsonObject &obj);
static Event fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -155,7 +155,7 @@ namespace GooglePeople
const QList<Membership> &values,
int accountId,
const QList<QContactCollection> &candidateCollections);
static Membership fromJsonObject(const QJsonObject &obj);
static Membership fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -182,7 +182,7 @@ namespace GooglePeople
*/

static bool saveContactDetails(QContact *contact, const QList<Name> &values);
static Name fromJsonObject(const QJsonObject &obj);
static Name fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -197,7 +197,7 @@ namespace GooglePeople
*/

static bool saveContactDetails(QContact *contact, const QList<Nickname> &values);
static Nickname fromJsonObject(const QJsonObject &obj);
static Nickname fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -222,7 +222,7 @@ namespace GooglePeople
*/

static bool saveContactDetails(QContact *contact, const QList<Organization> &values);
static Organization fromJsonObject(const QJsonObject &obj);
static Organization fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -239,7 +239,7 @@ namespace GooglePeople
*/

static bool saveContactDetails(QContact *contact, const QList<PhoneNumber> &values);
static PhoneNumber fromJsonObject(const QJsonObject &obj);
static PhoneNumber fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -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);
};

Expand All @@ -270,7 +270,7 @@ namespace GooglePeople
QString *localAvatarFile = nullptr);

static bool saveContactDetails(QContact *contact, const QList<Photo> &values);
static Photo fromJsonObject(const QJsonObject &obj);
static Photo fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -283,7 +283,7 @@ namespace GooglePeople
QString formattedType;

static bool saveContactDetails(QContact *contact, const QList<Url> &values);
static Url fromJsonObject(const QJsonObject &obj);
static Url fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand Down

0 comments on commit 734477e

Please sign in to comment.