From cec1192ea0f9a4d5be486921e506fadcb9310fb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Mon, 29 Sep 2014 22:02:54 +0200 Subject: [PATCH] HaikuDepot: Use getUserRatingByUserAndPkgVersion... ... instead of searching for a rating from the given user. This API will also return deactivated ratings, so the user could re-activate it. --- src/apps/haikudepot/model/WebAppInterface.cpp | 55 ++++++++++++++----- src/apps/haikudepot/model/WebAppInterface.h | 3 + src/apps/haikudepot/ui/RatePackageWindow.cpp | 23 ++++---- 3 files changed, 53 insertions(+), 28 deletions(-) diff --git a/src/apps/haikudepot/model/WebAppInterface.cpp b/src/apps/haikudepot/model/WebAppInterface.cpp index de648f6689..f591d44046 100644 --- a/src/apps/haikudepot/model/WebAppInterface.cpp +++ b/src/apps/haikudepot/model/WebAppInterface.cpp @@ -76,24 +76,46 @@ public: JsonBuilder& AddItem(const char* item) { - if (fInList) - fString << ",\""; - else + return AddItem(item, false); + } + + JsonBuilder& AddItem(const char* item, bool nullIfEmpty) + { + if (item == NULL || (nullIfEmpty && strlen(item) == 0)) { + if (fInList) + fString << ",null"; + else + fString << "null"; + } else { + if (fInList) + fString << ",\""; + else + fString << '"'; + // TODO: Escape item + fString << item; fString << '"'; - // TODO: Escape item - fString << item; - fString << "\""; + } fInList = true; return *this; } JsonBuilder& AddValue(const char* name, const char* value) + { + return AddValue(name, value, false); + } + + JsonBuilder& AddValue(const char* name, const char* value, + bool nullIfEmpty) { _StartName(name); - fString << '\"'; - // TODO: Escape value - fString << value; - fString << '\"'; + if (value == NULL || (nullIfEmpty && strlen(value) == 0)) { + fString << "null"; + } else { + fString << '"'; + // TODO: Escape value + fString << value; + fString << '"'; + } fInList = true; return *this; } @@ -391,20 +413,23 @@ WebAppInterface::RetrieveUserRatings(const BString& packageName, status_t WebAppInterface::RetrieveUserRating(const BString& packageName, - const BString& architecture, const BString& username, - BMessage& message) + const BPackageVersion& version, const BString& architecture, + const BString& username, BMessage& message) { BString jsonString = JsonBuilder() .AddValue("jsonrpc", "2.0") .AddValue("id", ++fRequestIndex) - .AddValue("method", "searchUserRatings") + .AddValue("method", "getUserRatingByUserAndPkgVersion") .AddArray("params") .AddObject() .AddValue("userNickname", username) .AddValue("pkgName", packageName) .AddValue("pkgVersionArchitectureCode", architecture) - .AddValue("offset", 0) - .AddValue("limit", 1) + .AddValue("pkgVersionMajor", version.Major(), true) + .AddValue("pkgVersionMinor", version.Minor(), true) + .AddValue("pkgVersionMicro", version.Micro(), true) + .AddValue("pkgVersionPreRelease", version.PreRelease(), true) + .AddValue("pkgVersionRevision", (int)version.Revision()) .EndObject() .EndArray() .End(); diff --git a/src/apps/haikudepot/model/WebAppInterface.h b/src/apps/haikudepot/model/WebAppInterface.h index 255dee8816..95f903465d 100644 --- a/src/apps/haikudepot/model/WebAppInterface.h +++ b/src/apps/haikudepot/model/WebAppInterface.h @@ -8,12 +8,14 @@ #include #include +#include #include "List.h" class BDataIO; class BMessage; +using BPackageKit::BPackageVersion; typedef List StringList; @@ -56,6 +58,7 @@ public: status_t RetrieveUserRating( const BString& packageName, + const BPackageVersion& version, const BString& architecture, const BString& username, BMessage& message); diff --git a/src/apps/haikudepot/ui/RatePackageWindow.cpp b/src/apps/haikudepot/ui/RatePackageWindow.cpp index dbd913c4fe..0201502eea 100644 --- a/src/apps/haikudepot/ui/RatePackageWindow.cpp +++ b/src/apps/haikudepot/ui/RatePackageWindow.cpp @@ -391,27 +391,24 @@ RatePackageWindow::_QueryRatingThread() BMessage info; status_t status = interface.RetrieveUserRating( - package->Title(), package->Architecture(), username, info); + package->Title(), package->Version(), package->Architecture(), + username, info); // info.PrintToStream(); BMessage result; - BMessage items; - BMessage rating; if (status == B_OK && info.FindMessage("result", &result) == B_OK - && result.FindMessage("items", &items) == B_OK - && items.FindMessage("0", &rating) == B_OK && Lock()) { - rating.FindString("code", &fRatingID); - rating.FindBool("active", &fRatingActive); + result.FindString("code", &fRatingID); + result.FindBool("active", &fRatingActive); BString comment; - if (rating.FindString("comment", &comment) == B_OK) { + if (result.FindString("comment", &comment) == B_OK) { MarkupParser parser; fRatingText = parser.CreateDocumentFromMarkup(comment); fTextView->SetTextDocument(fRatingText); } - if (rating.FindString("userRatingStabilityCode", + if (result.FindString("userRatingStabilityCode", &fStability) == B_OK) { int32 index = -1; for (int32 i = fStabilityCodes.CountItems() - 1; i >= 0; i--) { @@ -426,16 +423,16 @@ RatePackageWindow::_QueryRatingThread() if (item != NULL) item->SetMarked(true); } - if (rating.FindString("naturalLanguageCode", + if (result.FindString("naturalLanguageCode", &fCommentLanguage) == B_OK) { BMenuItem* item = fCommentLanguageField->Menu()->ItemAt( fModel.SupportedLanguages().IndexOf(fCommentLanguage)); if (item != NULL) item->SetMarked(true); } - double ratingValue; - if (rating.FindDouble("rating", &ratingValue) == B_OK) { - fRating = (float)ratingValue; + double rating; + if (result.FindDouble("rating", &rating) == B_OK) { + fRating = (float)rating; fSetRatingView->SetPermanentRating(fRating); }