Commit a8d3512d authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Revert "Byebye libravatar"

This reverts commit cacc086a.
parent 0bf80658
......@@ -39,6 +39,8 @@ void GravatarResolvUrlJobTest::shouldHaveDefaultValue()
QCOMPARE(job.hasGravatar(), false);
QCOMPARE(job.pixmap().isNull(), true);
QCOMPARE(job.useDefaultPixmap(), false);
QCOMPARE(job.useLibravatar(), false);
QCOMPARE(job.fallbackGravatar(), true);
}
void GravatarResolvUrlJobTest::shouldChangeValue()
......@@ -51,6 +53,21 @@ void GravatarResolvUrlJobTest::shouldChangeValue()
useDefaultPixmap = false;
job.setUseDefaultPixmap(useDefaultPixmap);
QCOMPARE(job.useDefaultPixmap(), useDefaultPixmap);
bool useLibravatar = true;
job.setUseLibravatar(useLibravatar);
QCOMPARE(job.useLibravatar(), useLibravatar);
useLibravatar = false;
job.setUseLibravatar(useLibravatar);
QCOMPARE(job.useLibravatar(), useLibravatar);
bool fallBackGravatar = false;
job.setFallbackGravatar(fallBackGravatar);
QCOMPARE(job.fallbackGravatar(), fallBackGravatar);
fallBackGravatar = true;
job.setFallbackGravatar(fallBackGravatar);
QCOMPARE(job.fallbackGravatar(), fallBackGravatar);
}
void GravatarResolvUrlJobTest::shouldChangeSize()
......@@ -81,8 +98,12 @@ void GravatarResolvUrlJobTest::shouldAddSizeInUrl()
Gravatar::GravatarResolvUrlJob job;
job.setEmail(QStringLiteral("foo@kde.org"));
job.setSize(1024);
QUrl url = job.generateGravatarUrl();
job.setUseLibravatar(false);
QUrl url = job.generateGravatarUrl(job.useLibravatar());
QCOMPARE(url, QUrl(QStringLiteral("https://secure.gravatar.com:443/avatar/89b4e14cf2fc6d426275c019c6dc9de6?d=404&s=1024")));
job.setUseLibravatar(true);
url = job.generateGravatarUrl(job.useLibravatar());
QCOMPARE(url, QUrl(QStringLiteral("https://seccdn.libravatar.org:443/avatar/2726400c3a33ce56c0ff632cbc0474f766d3b36e68819c601fb02954c1681d85?d=404&s=1024")));
}
void GravatarResolvUrlJobTest::shouldUseDefaultPixmap()
......@@ -91,7 +112,7 @@ void GravatarResolvUrlJobTest::shouldUseDefaultPixmap()
job.setEmail(QStringLiteral("foo@kde.org"));
job.setSize(1024);
job.setUseDefaultPixmap(true);
QUrl url = job.generateGravatarUrl();
QUrl url = job.generateGravatarUrl(job.useLibravatar());
QCOMPARE(url, QUrl(QStringLiteral("https://secure.gravatar.com:443/avatar/89b4e14cf2fc6d426275c019c6dc9de6?s=1024")));
}
......@@ -100,8 +121,12 @@ void GravatarResolvUrlJobTest::shouldUseHttps()
Gravatar::GravatarResolvUrlJob job;
job.setEmail(QStringLiteral("foo@kde.org"));
job.setSize(1024);
QUrl url = job.generateGravatarUrl();
job.setUseLibravatar(false);
QUrl url = job.generateGravatarUrl(job.useLibravatar());
QCOMPARE(url, QUrl(QStringLiteral("https://secure.gravatar.com:443/avatar/89b4e14cf2fc6d426275c019c6dc9de6?d=404&s=1024")));
job.setUseLibravatar(true);
url = job.generateGravatarUrl(job.useLibravatar());
QCOMPARE(url, QUrl(QStringLiteral("https://seccdn.libravatar.org:443/avatar/2726400c3a33ce56c0ff632cbc0474f766d3b36e68819c601fb02954c1681d85?d=404&s=1024")));
}
void GravatarResolvUrlJobTest::shouldNotStart()
......@@ -124,10 +149,13 @@ void GravatarResolvUrlJobTest::shouldGenerateGravatarUrl_data()
QTest::addColumn<QString>("input");
QTest::addColumn<QString>("calculedhash");
QTest::addColumn<QUrl>("output");
QTest::newRow("empty") << QString() << QString() << QUrl();
QTest::newRow("no domain") << QStringLiteral("foo") << QString() << QUrl();
QTest::addColumn<bool>("uselibravatar");
QTest::newRow("empty") << QString() << QString() << QUrl() << false;
QTest::newRow("no domain") << QStringLiteral("foo") << QString() << QUrl() << false;
QTest::newRow("validemail") << QStringLiteral("foo@kde.org") << QStringLiteral("89b4e14cf2fc6d426275c019c6dc9de6")
<< QUrl(QStringLiteral("https://secure.gravatar.com:443/avatar/89b4e14cf2fc6d426275c019c6dc9de6?d=404"));
<< QUrl(QStringLiteral("https://secure.gravatar.com:443/avatar/89b4e14cf2fc6d426275c019c6dc9de6?d=404")) << false;
QTest::newRow("validemaillibravatar") << QStringLiteral("foo@kde.org") << QStringLiteral("2726400c3a33ce56c0ff632cbc0474f766d3b36e68819c601fb02954c1681d85")
<< QUrl(QStringLiteral("https://seccdn.libravatar.org:443/avatar/2726400c3a33ce56c0ff632cbc0474f766d3b36e68819c601fb02954c1681d85?d=404")) << true;
}
void GravatarResolvUrlJobTest::shouldGenerateGravatarUrl()
......@@ -135,9 +163,11 @@ void GravatarResolvUrlJobTest::shouldGenerateGravatarUrl()
QFETCH(QString, input);
QFETCH(QString, calculedhash);
QFETCH(QUrl, output);
QFETCH(bool, uselibravatar);
Gravatar::GravatarResolvUrlJob job;
job.setEmail(input);
QUrl url = job.generateGravatarUrl();
job.setUseLibravatar(uselibravatar);
QUrl url = job.generateGravatarUrl(job.useLibravatar());
QCOMPARE(calculedhash, job.calculatedHash().hexString());
QCOMPARE(url, output);
}
......
......@@ -45,6 +45,7 @@ public:
enum Backend {
None = 0x0,
Libravatar = 0x1,
Gravatar = 0x2
};
int mBackends = Gravatar;
......@@ -73,9 +74,9 @@ bool GravatarResolvUrlJob::canStart() const
}
}
QUrl GravatarResolvUrlJob::generateGravatarUrl()
QUrl GravatarResolvUrlJob::generateGravatarUrl(bool useLibravatar)
{
return createUrl();
return createUrl(useLibravatar);
}
bool GravatarResolvUrlJob::hasGravatar() const
......@@ -119,9 +120,12 @@ void GravatarResolvUrlJob::processNextBackend()
}
QUrl url;
if (d->mBackends & GravatarResolvUrlJobPrivate::Gravatar) {
if (d->mBackends & GravatarResolvUrlJobPrivate::Libravatar) {
d->mBackends &= ~GravatarResolvUrlJobPrivate::Libravatar;
url = createUrl(true);
} else if (d->mBackends & GravatarResolvUrlJobPrivate::Gravatar) {
d->mBackends &= ~GravatarResolvUrlJobPrivate::Gravatar;
url = createUrl();
url = createUrl(false);
}
Q_EMIT resolvUrl(url);
......@@ -163,12 +167,40 @@ void GravatarResolvUrlJob::setEmail(const QString &email)
d->mEmail = email;
}
Hash GravatarResolvUrlJob::calculateHash()
Hash GravatarResolvUrlJob::calculateHash(bool useLibravator)
{
const auto email = d->mEmail.toLower().toUtf8();
if (useLibravator)
return Hash(QCryptographicHash::hash(email, QCryptographicHash::Sha256), Hash::Sha256);
return Hash(QCryptographicHash::hash(email, QCryptographicHash::Md5), Hash::Md5);
}
bool GravatarResolvUrlJob::fallbackGravatar() const
{
return d->mBackends & GravatarResolvUrlJobPrivate::Gravatar;
}
void GravatarResolvUrlJob::setFallbackGravatar(bool fallbackGravatar)
{
if (fallbackGravatar)
d->mBackends |= GravatarResolvUrlJobPrivate::Gravatar;
else
d->mBackends &= ~GravatarResolvUrlJobPrivate::Gravatar;
}
bool GravatarResolvUrlJob::useLibravatar() const
{
return d->mBackends & GravatarResolvUrlJobPrivate::Libravatar;
}
void GravatarResolvUrlJob::setUseLibravatar(bool useLibravatar)
{
if (useLibravatar)
d->mBackends |= GravatarResolvUrlJobPrivate::Libravatar;
else
d->mBackends &= ~GravatarResolvUrlJobPrivate::Libravatar;
}
bool GravatarResolvUrlJob::useDefaultPixmap() const
{
return d->mUseDefaultPixmap;
......@@ -204,7 +236,7 @@ Hash GravatarResolvUrlJob::calculatedHash() const
return d->mCalculatedHash;
}
QUrl GravatarResolvUrlJob::createUrl()
QUrl GravatarResolvUrlJob::createUrl(bool useLibravatar)
{
QUrl url;
d->mCalculatedHash = Hash();
......@@ -220,9 +252,13 @@ QUrl GravatarResolvUrlJob::createUrl()
query.addQueryItem(QStringLiteral("s"), QString::number(d->mSize));
}
url.setScheme(QStringLiteral("https"));
url.setHost(QStringLiteral("secure.gravatar.com"));
if (useLibravatar) {
url.setHost(QStringLiteral("seccdn.libravatar.org"));
} else {
url.setHost(QStringLiteral("secure.gravatar.com"));
}
url.setPort(443);
d->mCalculatedHash = calculateHash();
d->mCalculatedHash = calculateHash(useLibravatar);
url.setPath(QLatin1String("/avatar/") + d->mCalculatedHash.hexString());
url.setQuery(query);
return url;
......
......@@ -55,6 +55,12 @@ public:
Q_REQUIRED_RESULT bool useDefaultPixmap() const;
void setUseDefaultPixmap(bool useDefaultPixmap);
bool useLibravatar() const;
void setUseLibravatar(bool useLibravatar);
bool fallbackGravatar() const;
void setFallbackGravatar(bool fallbackGravatar);
Q_SIGNALS:
void finished(Gravatar::GravatarResolvUrlJob *);
void resolvUrl(const QUrl &url);
......@@ -63,12 +69,12 @@ private:
friend class ::GravatarResolvUrlJobTest;
void slotFinishLoadPixmap(QNetworkReply *reply);
QUrl generateGravatarUrl();
QUrl generateGravatarUrl(bool useLibravatar);
Hash calculatedHash() const;
void processNextBackend();
void startNetworkManager(const QUrl &url);
QUrl createUrl();
Hash calculateHash();
QUrl createUrl(bool useLibravatar);
Hash calculateHash(bool useLibravator);
bool cacheLookup(const Hash &hash);
GravatarResolvUrlJobPrivate *const d;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment