diff --git a/discover/qml/ReviewDialog.qml b/discover/qml/ReviewDialog.qml index 517d9728..a122b615 100644 --- a/discover/qml/ReviewDialog.qml +++ b/discover/qml/ReviewDialog.qml @@ -1,56 +1,61 @@ import QtQuick 2.3 import QtQuick.Controls 1.2 import QtQuick.Dialogs 1.2 import QtQuick.Layouts 1.1 import QtQuick.Window 2.0 import org.kde.kirigami 2.0 as Kirigami Kirigami.OverlaySheet { id: reviewDialog property QtObject application readonly property alias rating: ratingInput.rating readonly property alias summary: summaryInput.text readonly property alias review: reviewInput.text + property QtObject backend: null signal accepted() ColumnLayout { Kirigami.Heading { level: 3; text: i18n("Reviewing '%1'", application.name) } Label { text: i18n("Rating:") } Rating { id: ratingInput editable: true } + Label { + visible: reviewDialog.backend.userName.length > 0 + text: visible ? i18n("Submission name: %1", reviewDialog.backend.userName) : "" + } Label { text: i18n("Summary:") } TextField { id: summaryInput Layout.fillWidth: true placeholderText: i18n("Short summary...") validator: RegExpValidator { regExp: /.{3,70}/ } } TextArea { id: reviewInput readonly property bool acceptableInput: inputIssue.length === 0 readonly property string inputIssue: length < 15 ? i18n("Comment too short") : length > 3000 ? i18n("Comment too long") : "" Layout.fillWidth: true Layout.fillHeight: true } Button { id: acceptButton Layout.alignment: Qt.AlignRight enabled: summaryInput.acceptableInput && reviewInput.acceptableInput text: summaryInput.acceptableInput && reviewInput.acceptableInput ? i18n("Submit") : !summaryInput.acceptableInput ? i18n("Improve summary") : reviewInput.inputIssue onClicked: { reviewDialog.accepted() reviewDialog.sheetOpen = false } } } } diff --git a/discover/qml/ReviewsPage.qml b/discover/qml/ReviewsPage.qml index 583f492b..af89d02c 100644 --- a/discover/qml/ReviewsPage.qml +++ b/discover/qml/ReviewsPage.qml @@ -1,80 +1,81 @@ /*************************************************************************** * Copyright © 2012 Aleix Pol Gonzalez * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation; either version 2 of * * the License or (at your option) version 3 or any later version * * accepted by the membership of KDE e.V. (or its successor approved * * by the membership of KDE e.V.), which shall act as a proxy * * defined in Section 14 of version 3 of the license. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see . * ***************************************************************************/ import QtQuick 2.1 import QtQuick.Controls 1.1 import org.kde.discover 2.0 import org.kde.discover.app 1.0 import org.kde.kirigami 2.0 as Kirigami Kirigami.OverlaySheet { id: page property alias model: reviewsView.model readonly property QtObject reviewsBackend: resource.backend.reviewsBackend readonly property var resource: model.resource readonly property var rd: ReviewDialog { id: reviewDialog application: page.resource parent: overlay - onAccepted: page.reviewsBackend.submitReview(resource, summary, review, rating) + backend: page.reviewsBackend + onAccepted: backend.submitReview(resource, summary, review, rating) } function openReviewDialog() { reviewDialog.sheetOpen = true page.sheetOpen = false } ListView { id: reviewsView clip: true spacing: Kirigami.Units.smallSpacing cacheBuffer: Math.max(0, contentHeight) header: Item { width: parent.width height: reviewButton.height + 2 * Kirigami.Units.largeSpacing Button { id: reviewButton anchors { verticalCenter: parent.verticalCenter left: parent.left leftMargin: Kirigami.Units.largeSpacing } visible: page.reviewsBackend != null enabled: page.resource.isInstalled text: i18n("Review...") onClicked: page.openReviewDialog() } } delegate: ReviewDelegate { anchors { left: parent.left right: parent.right } separator: index != ListView.view.count-1 onMarkUseful: page.model.markUseful(index, useful) } } } diff --git a/libdiscover/ReviewsBackend/AbstractReviewsBackend.h b/libdiscover/ReviewsBackend/AbstractReviewsBackend.h index d83b1402..7f9eb4fe 100644 --- a/libdiscover/ReviewsBackend/AbstractReviewsBackend.h +++ b/libdiscover/ReviewsBackend/AbstractReviewsBackend.h @@ -1,65 +1,65 @@ /*************************************************************************** * Copyright © 2012 Aleix Pol Gonzalez . * ***************************************************************************/ #ifndef ABSTRACTREVIEWSBACKEND_H #define ABSTRACTREVIEWSBACKEND_H #include #include "ReviewsModel.h" class Rating; class AbstractResource; class DISCOVERCOMMON_EXPORT AbstractReviewsBackend : public QObject { Q_OBJECT Q_PROPERTY(bool isReviewable READ isReviewable CONSTANT) Q_PROPERTY(bool hasCredentials READ hasCredentials NOTIFY loginStateChanged) - Q_PROPERTY(QString name READ userName NOTIFY loginStateChanged) + Q_PROPERTY(QString userName READ userName NOTIFY loginStateChanged) public: explicit AbstractReviewsBackend(QObject* parent = nullptr); virtual QString userName() const = 0; virtual bool hasCredentials() const = 0; Q_SCRIPTABLE virtual Rating *ratingForApplication(AbstractResource *app) const = 0; Q_INVOKABLE virtual QString errorMessage() const; Q_INVOKABLE virtual bool isResourceSupported(AbstractResource *res) const = 0; public Q_SLOTS: virtual void login() = 0; virtual void registerAndLogin() = 0; virtual void logout() = 0; virtual void submitUsefulness(Review* r, bool useful) = 0; virtual void submitReview(AbstractResource* app, const QString& summary, const QString& review_text, const QString& rating) = 0; virtual void deleteReview(Review* r) = 0; virtual void flagReview(Review* r, const QString& reason, const QString &text) = 0; virtual bool isFetching() const = 0; virtual void fetchReviews(AbstractResource* app, int page=1) = 0; virtual bool isReviewable() const; Q_SIGNALS: void reviewsReady(AbstractResource *app, const QVector &, bool canFetchMore); void loginStateChanged(); }; #endif // ABSTRACTREVIEWSBACKEND_H diff --git a/libdiscover/appstream/OdrsReviewsBackend.cpp b/libdiscover/appstream/OdrsReviewsBackend.cpp index 4b6586b5..b009e711 100644 --- a/libdiscover/appstream/OdrsReviewsBackend.cpp +++ b/libdiscover/appstream/OdrsReviewsBackend.cpp @@ -1,358 +1,364 @@ /*************************************************************************** * Copyright © 2013 Aleix Pol Gonzalez * * Copyright © 2017 Jan Grulich * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation; either version 2 of * * the License or (at your option) version 3 or any later version * * accepted by the membership of KDE e.V. (or its successor approved * * by the membership of KDE e.V.), which shall act as a proxy * * defined in Section 14 of version 3 of the license. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see . * ***************************************************************************/ #include "OdrsReviewsBackend.h" #include #include #include #include #include #include +#include #include #include #include #include #include #include #include #include #include #include #include #include // #define APIURL "http://127.0.0.1:5000/1.0/reviews/api" #define APIURL "https://odrs.gnome.org/1.0/reviews/api" OdrsReviewsBackend::OdrsReviewsBackend(AbstractResourcesBackend *parent) : AbstractReviewsBackend(parent) , m_isFetching(false) , m_nam(new QNetworkAccessManager(this)) { bool fetchRatings = false; const QUrl ratingsUrl(QStringLiteral(APIURL "/ratings")); const QUrl fileUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QStringLiteral("/ratings/ratings")); const QDir cacheDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)); // Create $HOME/.cache/discover/ratings folder cacheDir.mkdir(QStringLiteral("ratings")); if (QFileInfo::exists(fileUrl.toLocalFile())) { QFileInfo file(fileUrl.toLocalFile()); // Refresh the cached ratings if they are older than one day if (file.lastModified().msecsTo(QDateTime::currentDateTime()) > 1000 * 60 * 60 * 24) { fetchRatings = true; } } else { fetchRatings = true; } if (fetchRatings) { m_isFetching = true; KIO::FileCopyJob *getJob = KIO::file_copy(ratingsUrl, fileUrl, -1, KIO::Overwrite | KIO::HideProgressInfo); connect(getJob, &KIO::FileCopyJob::result, this, &OdrsReviewsBackend::ratingsFetched); } else { parseRatings(); } } void OdrsReviewsBackend::ratingsFetched(KJob *job) { m_isFetching = false; if (job->error()) { qWarning() << "Failed to fetch ratings " << job->errorString(); } else { parseRatings(); } } static QString osName() { QString osReleaseFilename; if (QFileInfo::exists(QStringLiteral("/etc/os-release"))) { osReleaseFilename = QStringLiteral("/etc/os-release"); } else if (QFileInfo::exists(QStringLiteral("/usr/lib/os-release"))) { osReleaseFilename = QStringLiteral("/usr/lib/os-release"); } if (osReleaseFilename.isEmpty()) { return QStringLiteral("Unknown"); } QFile osReleaseFile(osReleaseFilename); if (osReleaseFile.open(QIODevice::ReadOnly)) { QString line; QTextStream stream(&osReleaseFile); while (stream.readLineInto(&line)) { if (line.startsWith(QStringLiteral("NAME"))) { QStringRef name = line.midRef(5).trimmed(); if (name.startsWith(QLatin1Char('\"')) && name.endsWith(QLatin1Char('\"'))) name = name.mid(1, name.size()-2); return name.toString(); } } } return QStringLiteral("Unknown"); } static QString userHash() { QString machineId; QFile file(QStringLiteral("/etc/machine-id")); if (file.open(QIODevice::ReadOnly)) { machineId = QString::fromUtf8(file.readAll()); file.close(); } if (machineId.isEmpty()) { return QString(); } QString salted = QStringLiteral("gnome-software[%1:%2]").arg(KUser().loginName()).arg(machineId); return QString::fromUtf8(QCryptographicHash::hash(salted.toUtf8(), QCryptographicHash::Sha1).toHex()); } void OdrsReviewsBackend::fetchReviews(AbstractResource *app, int page) { Q_UNUSED(page) m_isFetching = true; // Check cached reviews const QString fileName = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QStringLiteral("/reviews/%1.json").arg(app->appstreamId()); if (QFileInfo::exists(fileName)) { QFileInfo file(fileName); // Check if the reviews are not older than a week msecs * secs * hours * days if (file.lastModified().msecsTo(QDateTime::currentDateTime()) < 1000 * 60 * 60 * 24 * 7 ) { QFile reviewFile(fileName); if (reviewFile.open(QIODevice::ReadOnly)) { QByteArray reviews = reviewFile.readAll(); QJsonDocument document = QJsonDocument::fromJson(reviews); parseReviews(document, app); return; } } } const QJsonDocument document(QJsonObject{ {QStringLiteral("app_id"), app->appstreamId()}, {QStringLiteral("distro"), osName()}, {QStringLiteral("user_hash"), userHash()}, {QStringLiteral("version"), app->isInstalled() ? app->installedVersion() : app->availableVersion()}, {QStringLiteral("locale"), QLocale::system().name()}, {QStringLiteral("limit"), 0} }); QNetworkRequest request(QUrl(QStringLiteral(APIURL "/fetch"))); request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json; charset=utf-8")); request.setHeader(QNetworkRequest::ContentLengthHeader, document.toJson().size()); // Store reference to the app for which we request reviews request.setOriginatingObject(app); auto reply = m_nam->post(request, document.toJson()); connect(reply, &QNetworkReply::finished, this, &OdrsReviewsBackend::reviewsFetched); } void OdrsReviewsBackend::reviewsFetched() { QNetworkReply* reply = qobject_cast(sender()); if (reply->error() == QNetworkReply::NoError) { QByteArray data = reply->readAll(); const QJsonDocument document = QJsonDocument::fromJson(data); AbstractResource *resource = qobject_cast(reply->request().originatingObject()); Q_ASSERT(resource); parseReviews(document, resource); // Store reviews to cache so we don't need to download them all the time if (document.array().isEmpty()) { return; } QJsonObject jsonObject = document.array().first().toObject(); if (jsonObject.isEmpty()) { return; } QFile file(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QStringLiteral("/reviews/%1.json").arg(jsonObject.value(QStringLiteral("app_id")).toString())); if (file.open(QIODevice::WriteOnly)) { file.write(document.toJson()); file.close(); } } else { m_isFetching = false; } } Rating * OdrsReviewsBackend::ratingForApplication(AbstractResource *app) const { if (app->isTechnical()) { return nullptr; } return m_ratings[app->appstreamId()]; } void OdrsReviewsBackend::submitUsefulness(Review *review, bool useful) { const QJsonDocument document(QJsonObject{ {QStringLiteral("app_id"), review->applicationName()}, {QStringLiteral("user_skey"), review->getMetadata(QStringLiteral("ODRS::user_skey")).toString()}, {QStringLiteral("user_hash"), userHash()}, {QStringLiteral("distro"), osName()}, {QStringLiteral("review_id"), QJsonValue(double(review->id()))} //if we really need uint64 we should get it in QJsonValue }); QNetworkRequest request(QUrl(QStringLiteral(APIURL "/%1").arg(useful ? QStringLiteral("upvote") : QStringLiteral("downvote")))); request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json; charset=utf-8")); request.setHeader(QNetworkRequest::ContentLengthHeader, document.toJson().size()); auto reply = m_nam->post(request, document.toJson()); connect(reply, &QNetworkReply::finished, this, &OdrsReviewsBackend::usefulnessSubmitted); } void OdrsReviewsBackend::usefulnessSubmitted() { QNetworkReply* reply = qobject_cast(sender()); if (reply->error() == QNetworkReply::NoError) { qWarning() << "Usefullness submitted"; } else { qWarning() << "Failed to submit usefulness: " << reply->errorString(); } } +QString OdrsReviewsBackend::userName() const +{ + return i18n("%1 (%2)", KUser().property(KUser::FullName).toString(), KUser().loginName()); +} + void OdrsReviewsBackend::submitReview(AbstractResource *res, const QString &summary, const QString &description, const QString &rating) { QJsonObject 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"), QJsonValue::fromVariant(KUser().property(KUser::FullName))}, {QStringLiteral("summary"), summary}, {QStringLiteral("description"), description}, {QStringLiteral("rating"), rating.toInt() * 10}}; const QJsonDocument document(map); QNetworkAccessManager *accessManager = new QNetworkAccessManager(this); QNetworkRequest request(QUrl(QStringLiteral(APIURL "/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(reply->request().originatingObject()); const QJsonArray array = {resource->getMetadata(QStringLiteral("ODRS::review_map")).toObject()}; 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() { QFile ratingsDocument(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QStringLiteral("/ratings/ratings")); if (ratingsDocument.open(QIODevice::ReadOnly)) { QJsonDocument jsonDocument = QJsonDocument::fromJson(ratingsDocument.readAll()); QJsonObject jsonObject = jsonDocument.object(); for (auto it = jsonObject.begin(); it != jsonObject.end(); it++) { QJsonObject appJsonObject = it.value().toObject(); const int ratingCount = appJsonObject.value(QLatin1String("total")).toInt(); QVariantMap ratingMap = { { QStringLiteral("star0"), appJsonObject.value(QLatin1String("star0")).toInt() }, { QStringLiteral("star1"), appJsonObject.value(QLatin1String("star1")).toInt() }, { QStringLiteral("star2"), appJsonObject.value(QLatin1String("star2")).toInt() }, { QStringLiteral("star3"), appJsonObject.value(QLatin1String("star3")).toInt() }, { QStringLiteral("star4"), appJsonObject.value(QLatin1String("star4")).toInt() }, { QStringLiteral("star5"), appJsonObject.value(QLatin1String("star5")).toInt() } }; Rating *rating = new Rating(it.key(), ratingCount, ratingMap); m_ratings.insert(it.key(), rating); } ratingsDocument.close(); Q_EMIT ratingsReady(); } } void OdrsReviewsBackend::parseReviews(const QJsonDocument &document, AbstractResource *resource) { m_isFetching = false; QJsonArray reviews = document.array(); if (!reviews.isEmpty()) { QVector reviewList; for (auto it = reviews.begin(); it != reviews.end(); it++) { QJsonObject review = it->toObject(); if (!review.isEmpty()) { const int usefulFavorable = review.value(QStringLiteral("karma_up")).toInt(); const int usefulTotal = review.value(QStringLiteral("karma_down")).toInt() + usefulFavorable; QDateTime dateTime; dateTime.setTime_t(review.value(QStringLiteral("date_created")).toInt()); ReviewPtr r(new Review(review.value(QStringLiteral("app_id")).toString(), resource->packageName(), review.value(QStringLiteral("locale")).toString(), review.value(QStringLiteral("summary")).toString(), review.value(QStringLiteral("description")).toString(), review.value(QStringLiteral("user_display")).toString(), dateTime, true, review.value(QStringLiteral("review_id")).toInt(), 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->summary().isEmpty() && !r->reviewText().isEmpty()) { reviewList << r; // Needed for submitting usefulness r->addMetadata(QStringLiteral("ODRS::user_skey"), review.value(QStringLiteral("user_skey")).toString()); } // We should get at least user_skey needed for posting reviews resource->addMetadata(QStringLiteral("ODRS::user_skey"), review.value(QStringLiteral("user_skey")).toString()); } } Q_EMIT reviewsReady(resource, reviewList, false); } } bool OdrsReviewsBackend::isResourceSupported(AbstractResource* res) const { return !res->appstreamId().isEmpty(); } diff --git a/libdiscover/appstream/OdrsReviewsBackend.h b/libdiscover/appstream/OdrsReviewsBackend.h index 726ea773..ba35e7ac 100644 --- a/libdiscover/appstream/OdrsReviewsBackend.h +++ b/libdiscover/appstream/OdrsReviewsBackend.h @@ -1,75 +1,75 @@ /*************************************************************************** * Copyright © 2013 Aleix Pol Gonzalez * * Copyright © 2017 Jan Grulich * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation; either version 2 of * * the License or (at your option) version 3 or any later version * * accepted by the membership of KDE e.V. (or its successor approved * * by the membership of KDE e.V.), which shall act as a proxy * * defined in Section 14 of version 3 of the license. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see . * ***************************************************************************/ #ifndef ODRSREVIEWSBACKEND_H #define ODRSREVIEWSBACKEND_H #include #include #include #include #include class KJob; class AbstractResourcesBackend; class DISCOVERCOMMON_EXPORT OdrsReviewsBackend : public AbstractReviewsBackend { Q_OBJECT public: explicit OdrsReviewsBackend(AbstractResourcesBackend *parent = nullptr); - QString userName() const override { return {}; } + QString userName() const override; void login() override {} void logout() override {} void registerAndLogin() override {} Rating * ratingForApplication(AbstractResource *app) const override; bool hasCredentials() const override { return false; } void deleteReview(Review *) override {} void fetchReviews(AbstractResource *app, int page = 1) override; bool isFetching() const override { return m_isFetching; } void submitReview(AbstractResource *, const QString &summary, const QString &description, const QString &rating) override; void flagReview(Review *, const QString &, const QString &) override {} void submitUsefulness(Review *review, bool useful) override; QStringList appstreamIds() const { return m_ratings.keys(); } bool isResourceSupported(AbstractResource * res) const override; private Q_SLOTS: void ratingsFetched(KJob *job); void reviewsFetched(); void reviewSubmitted(QNetworkReply *reply); void usefulnessSubmitted(); Q_SIGNALS: void ratingsReady(); private: void parseRatings(); void parseReviews(const QJsonDocument &document, AbstractResource *resource); QHash m_ratings; bool m_isFetching; QNetworkAccessManager* const m_nam; }; #endif // ODRSREVIEWSBACKEND_H