summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Grulich <[email protected]>2017-03-16 11:54:09 +0100
committerJan Grulich <[email protected]>2017-03-16 11:54:09 +0100
commitf1cbe63b6e6c9836cac12dccf45782db6f53740b (patch)
tree9fc59e718bc18eaaa584730bab2a518136de75a1
parent09ec287ac69df65c57b12e777f71cae6a6c8e7ba (diff)
ODRS: Support to post reviews
-rw-r--r--libdiscover/ReviewsBackend/OdrsReviewsBackend.cpp55
-rw-r--r--libdiscover/ReviewsBackend/OdrsReviewsBackend.h3
-rw-r--r--libdiscover/ReviewsBackend/ReviewsModel.cpp2
-rw-r--r--libdiscover/backends/FlatpakBackend/FlatpakResource.cpp10
-rw-r--r--libdiscover/backends/FlatpakBackend/FlatpakResource.h3
-rw-r--r--libdiscover/backends/FlatpakBackend/FlatpakSourcesBackend.cpp4
-rw-r--r--libdiscover/resources/AbstractResource.cpp10
-rw-r--r--libdiscover/resources/AbstractResource.h4
8 files changed, 70 insertions, 21 deletions
diff --git a/libdiscover/ReviewsBackend/OdrsReviewsBackend.cpp b/libdiscover/ReviewsBackend/OdrsReviewsBackend.cpp
index cc08f85..a90a6b4 100644
--- a/libdiscover/ReviewsBackend/OdrsReviewsBackend.cpp
+++ b/libdiscover/ReviewsBackend/OdrsReviewsBackend.cpp
@@ -127,6 +127,7 @@ static QString userHash()
void OdrsReviewsBackend::fetchReviews(AbstractResource *app, int page)
{
+ Q_UNUSED(page)
m_isFetching = true;
// Check cached reviews
@@ -204,12 +205,55 @@ Rating * OdrsReviewsBackend::ratingForApplication(AbstractResource *app) const
void OdrsReviewsBackend::submitUsefulness(Review *r, bool useful)
{
- // TODO
+ Q_UNUSED(r)
+ Q_UNUSED(useful)
}
-void OdrsReviewsBackend::submitReview(AbstractResource *res, const QString &a, const QString &b, const QString &c)
+void OdrsReviewsBackend::submitReview(AbstractResource *res, const QString &summary, const QString &description, const QString &rating)
{
- // TODO
+ QVariantMap map {{QStringLiteral("app_id"), res->appstreamId()},
+ {QStringLiteral("user_skey"), res->getMetadata(QStringLiteral("ODRS::user_skey")).toString()},
+ {QStringLiteral("user_hash"), userHash()},
+ {QStringLiteral("version"), res->isInstalled() ? res->installedVersion() : res->availableVersion()},
+ {QStringLiteral("locale"), QLocale::system().name()},
+ {QStringLiteral("distro"), osName()},
+ {QStringLiteral("user_display"), KUser().property(KUser::FullName)},
+ {QStringLiteral("summary"), summary},
+ {QStringLiteral("description"), description},
+ {QStringLiteral("rating"), rating.toInt() * 10}};
+
+ QJsonDocument document(QJsonObject::fromVariantMap(map));
+ qWarning() << document.toJson();
+
+ QNetworkAccessManager *accessManager = new QNetworkAccessManager(this);
+ QNetworkRequest request(QUrl(QStringLiteral("https://odrs.gnome.org/1.0/reviews/api/submit")));
+ request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json; charset=utf-8"));
+ request.setHeader(QNetworkRequest::ContentLengthHeader, document.toJson().size());
+
+ // Store what we need so we can immediately show our review once it is submitted
+ // Use review_id 0 for now as odrs starts numbering from 1 and once reviews are re-downloaded we get correct id
+ map.insert(QStringLiteral("review_id"), 0);
+ res->addMetadata(QStringLiteral("ODRS::review_map"), map);
+ request.setOriginatingObject(res);
+
+ accessManager->post(request, document.toJson());
+ connect(accessManager, &QNetworkAccessManager::finished, this, &OdrsReviewsBackend::reviewSubmitted);
+}
+
+void OdrsReviewsBackend::reviewSubmitted(QNetworkReply *reply)
+{
+ if (reply->error() == QNetworkReply::NoError) {
+ qWarning() << "Review submitted";
+ AbstractResource *resource = qobject_cast<AbstractResource*>(reply->request().originatingObject());
+ QJsonArray array = QJsonArray::fromVariantList({resource->getMetadata(QStringLiteral("ODRS::review_map")).toMap()});
+ QJsonDocument document = QJsonDocument(array);
+ // Remove local file with reviews so we can re-download it next time to get our review
+ QFile file(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QStringLiteral("/reviews/%1.json").arg(array.first().toObject().value(QStringLiteral("app_id")).toString()));
+ file.remove();
+ parseReviews(document, resource);
+ } else {
+ qWarning() << "Failed to submit review: " << reply->errorString();
+ }
}
void OdrsReviewsBackend::parseRatings()
@@ -259,9 +303,12 @@ void OdrsReviewsBackend::parseReviews(const QJsonDocument &document, AbstractRes
review.value(QStringLiteral("rating")).toInt() / 10, usefulTotal, usefulFavorable,
review.value(QStringLiteral("version")).toString());
// We can also receive just a json with app name and user info so filter these out as there is no review
- if (r->id() && !r->summary().isEmpty() && !r->reviewText().isEmpty()) {
+ if (!r->summary().isEmpty() && !r->reviewText().isEmpty()) {
reviewList << r;
}
+
+ // We should get at least user_skey needed for posting reviews
+ resource->addMetadata(QStringLiteral("ODRS::user_skey"), review.value(QStringLiteral("user_skey")).toString());
}
}
diff --git a/libdiscover/ReviewsBackend/OdrsReviewsBackend.h b/libdiscover/ReviewsBackend/OdrsReviewsBackend.h
index 1cd89ad..500f59b 100644
--- a/libdiscover/ReviewsBackend/OdrsReviewsBackend.h
+++ b/libdiscover/ReviewsBackend/OdrsReviewsBackend.h
@@ -47,13 +47,14 @@ public:
void deleteReview(Review *) override {}
void fetchReviews(AbstractResource *app, int page = 1) override;
bool isFetching() const override { return m_isFetching; }
- void submitReview(AbstractResource *, const QString &, const QString &, const QString &) override;
+ void submitReview(AbstractResource *, const QString &summary, const QString &description, const QString &rating) override;
void flagReview(Review *, const QString &, const QString &) override {}
void submitUsefulness(Review *, bool) override;
private Q_SLOTS:
void ratingsFetched(KJob *job);
void reviewsFetched(QNetworkReply *reply);
+ void reviewSubmitted(QNetworkReply *reply);
Q_SIGNALS:
void ratingsReady();
diff --git a/libdiscover/ReviewsBackend/ReviewsModel.cpp b/libdiscover/ReviewsBackend/ReviewsModel.cpp
index a3c68a4..5b66d78 100644
--- a/libdiscover/ReviewsBackend/ReviewsModel.cpp
+++ b/libdiscover/ReviewsBackend/ReviewsModel.cpp
@@ -134,7 +134,7 @@ void ReviewsModel::restartFetching()
void ReviewsModel::fetchMore(const QModelIndex& parent)
{
- if(!m_backend || !m_app || parent.isValid() || !m_canFetchMore)
+ if(!m_backend || !m_app || parent.isValid() || m_backend->isFetching() || !m_canFetchMore)
return;
m_lastPage++;
diff --git a/libdiscover/backends/FlatpakBackend/FlatpakResource.cpp b/libdiscover/backends/FlatpakBackend/FlatpakResource.cpp
index 69bd936..6c8c737 100644
--- a/libdiscover/backends/FlatpakBackend/FlatpakResource.cpp
+++ b/libdiscover/backends/FlatpakBackend/FlatpakResource.cpp
@@ -272,11 +272,6 @@ QString FlatpakResource::name()
return name;
}
-QVariant FlatpakResource::metadata(const QString &key)
-{
- return m_metadata.value(key);
-}
-
QString FlatpakResource::origin() const
{
return m_origin;
@@ -443,11 +438,6 @@ void FlatpakResource::fetchScreenshots()
Q_EMIT screenshotsFetched(thumbnails, screenshots);
}
-void FlatpakResource::addMetadata(const QString &key, const QVariant &value)
-{
- m_metadata.insert(key, value);
-}
-
void FlatpakResource::setArch(const QString &arch)
{
m_arch = arch;
diff --git a/libdiscover/backends/FlatpakBackend/FlatpakResource.h b/libdiscover/backends/FlatpakBackend/FlatpakResource.h
index 28df0d7..b2be749 100644
--- a/libdiscover/backends/FlatpakBackend/FlatpakResource.h
+++ b/libdiscover/backends/FlatpakBackend/FlatpakResource.h
@@ -89,7 +89,6 @@ public:
QString license() override;
QString longDescription() override;
QString name() override;
- QVariant metadata(const QString &key);
QString origin() const override;
QString packageName() const override;
QUrl resourceFile() const;
@@ -110,7 +109,6 @@ public:
void fetchChangelog() override;
void fetchScreenshots() override;
- void addMetadata(const QString &key, const QVariant &value);
void setArch(const QString &arch);
void setBranch(const QString &branch);
void setBundledIcon(const QPixmap &pixmap);
@@ -144,7 +142,6 @@ public:
QString m_flatpakName;
QString m_iconPath;
int m_installedSize;
- QVariantMap m_metadata;
QString m_origin;
QUrl m_resourceFile;
QString m_runtime;
diff --git a/libdiscover/backends/FlatpakBackend/FlatpakSourcesBackend.cpp b/libdiscover/backends/FlatpakBackend/FlatpakSourcesBackend.cpp
index 51e66a0..9b6308d 100644
--- a/libdiscover/backends/FlatpakBackend/FlatpakSourcesBackend.cpp
+++ b/libdiscover/backends/FlatpakBackend/FlatpakSourcesBackend.cpp
@@ -138,11 +138,11 @@ FlatpakRemote * FlatpakSourcesBackend::installSource(FlatpakResource *resource)
}
remote = flatpak_remote_new(resource->flatpakName().toStdString().c_str());
- flatpak_remote_set_url(remote, resource->metadata(QStringLiteral("repo-url")).toString().toStdString().c_str());
+ flatpak_remote_set_url(remote, resource->getMetadata(QStringLiteral("repo-url")).toString().toStdString().c_str());
flatpak_remote_set_noenumerate(remote, false);
flatpak_remote_set_title(remote, resource->comment().toStdString().c_str());
- const QString gpgKey = resource->metadata(QStringLiteral("gpg-key")).toString();
+ const QString gpgKey = resource->getMetadata(QStringLiteral("gpg-key")).toString();
if (!gpgKey.isEmpty()) {
gsize dataLen = 0;
g_autofree guchar *data = nullptr;
diff --git a/libdiscover/resources/AbstractResource.cpp b/libdiscover/resources/AbstractResource.cpp
index 3d03d9e..58e1930 100644
--- a/libdiscover/resources/AbstractResource.cpp
+++ b/libdiscover/resources/AbstractResource.cpp
@@ -56,6 +56,16 @@ bool AbstractResource::isTechnical() const
return false;
}
+void AbstractResource::addMetadata(const QString &key, const QVariant &value)
+{
+ m_metadata.insert(key, value);
+}
+
+QVariant AbstractResource::getMetadata(const QString &key)
+{
+ return m_metadata.value(key);
+}
+
bool AbstractResource::canUpgrade()
{
return state() == Upgradeable;
diff --git a/libdiscover/resources/AbstractResource.h b/libdiscover/resources/AbstractResource.h
index 274fabe..95a6b46 100644
--- a/libdiscover/resources/AbstractResource.h
+++ b/libdiscover/resources/AbstractResource.h
@@ -154,6 +154,9 @@ class DISCOVERCOMMON_EXPORT AbstractResource : public QObject
virtual QString appstreamId() const;
+ void addMetadata(const QString &key, const QVariant &value);
+ QVariant getMetadata(const QString &key);
+
bool canUpgrade();
bool isInstalled();
@@ -204,6 +207,7 @@ class DISCOVERCOMMON_EXPORT AbstractResource : public QObject
// TODO: make it std::optional or make QCollatorSortKey()
QScopedPointer<QCollatorSortKey> m_collatorKey;
+ QVariantMap m_metadata;
};
Q_DECLARE_METATYPE(QVector<AbstractResource*>)