From 7ff94fd072dbd2e301175ffa894a85907f878b7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Wed, 3 Sep 2014 23:42:24 +0200 Subject: [PATCH] HaikuDepot: Implemented getting user-ratings for packages. This happens when a package is first clicked, and its details are shown. Currently, ratings can only be entered via the web-app. --- src/apps/haikudepot/Model.cpp | 93 ++++++++++++++++++++----- src/apps/haikudepot/WebAppInterface.cpp | 36 ++++++++-- src/apps/haikudepot/WebAppInterface.h | 10 ++- 3 files changed, 117 insertions(+), 22 deletions(-) diff --git a/src/apps/haikudepot/Model.cpp b/src/apps/haikudepot/Model.cpp index b0259bc075..4738b7991b 100644 --- a/src/apps/haikudepot/Model.cpp +++ b/src/apps/haikudepot/Model.cpp @@ -541,24 +541,85 @@ Model::PopulatePackage(const PackageInfoRef& package, uint32 flags) fPopulatedPackages.Add(package); - if (package->Title() == "wonderbrush") { + if ((flags & POPULATE_USER_RATINGS) != 0) { + // Retrieve info from web-app + WebAppInterface interface; + interface.SetPreferredLanguage(fPreferredLanguage); + BMessage info; + + status_t status = interface.RetrieveUserRatings(package->Title(), + package->Architecture(), 0, 50, info); + if (status == B_OK) { + // Parse message + BMessage result; + BMessage items; + if (info.FindMessage("result", &result) == B_OK + && result.FindMessage("items", &items) == B_OK) { + int index = 0; + while (true) { + BString name; + name << index++; + + BMessage item; + if (items.FindMessage(name, &item) != B_OK) + break; +// item.PrintToStream(); - if ((flags & POPULATE_CATEGORIES) != 0) { - package->AddCategory(CategoryGraphics()); - package->AddCategory(CategoryProductivity()); + BString user; + BMessage userInfo; + if (item.FindMessage("user", &userInfo) != B_OK + || userInfo.FindString("nickname", &user) != B_OK) { + // Ignore, we need the user name + continue; + } + + // Extract basic info + BString languageCode; + BString comment; + double rating; + if (item.FindString("naturalLanguageCode", + &languageCode) != B_OK + || item.FindString("comment", &comment) != B_OK + || item.FindDouble("rating", &rating) != B_OK) { + // Ignore this entry, we need the basics + continue; + } + // For which version of the package was the rating? + BString major = "?"; + BString minor = "?"; + BString micro = ""; + BMessage version; + if (item.FindMessage("pkgVersion", &version) == B_OK) { + version.FindString("major", &major); + version.FindString("minor", &minor); + version.FindString("micro", µ); + } + BString versionString = major; + versionString << "."; + versionString << minor; + if (micro.Length() > 0) { + versionString << "."; + versionString << micro; + } + // Add the rating to the PackageInfo + package->AddUserRating( + UserRating(UserInfo(user), rating, + comment, languageCode, versionString, 0, 0) + ); + } + } else if (info.FindMessage("error", &result) == B_OK) { + result.PrintToStream(); + } } - - if ((flags & POPULATE_USER_RATINGS) != 0) { - package->AddUserRating( - UserRating(UserInfo("binky"), 4.5f, - "Awesome!", "en", "2.1.2", 0, 0) - ); - package->AddUserRating( - UserRating(UserInfo("twinky"), 5.0f, - "The best!", "en", "2.1.2", 3, 1) - ); - } - + +// package->AddUserRating( +// UserRating(UserInfo("binky"), 4.5f, +// "Awesome!", "en", "2.1.2", 0, 0) +// ); +// package->AddUserRating( +// UserRating(UserInfo("twinky"), 5.0f, +// "The best!", "en", "2.1.2", 3, 1) +// ); } } diff --git a/src/apps/haikudepot/WebAppInterface.cpp b/src/apps/haikudepot/WebAppInterface.cpp index 1461ccac94..79092c150d 100644 --- a/src/apps/haikudepot/WebAppInterface.cpp +++ b/src/apps/haikudepot/WebAppInterface.cpp @@ -279,7 +279,7 @@ WebAppInterface::RetrievePackageInfo(const BString& packageName, // printf("Sending JSON:\n%s\n", jsonString.String()); - return _SendJsonRequest(jsonString, message); + return _SendJsonRequest("pkg", jsonString, message); } @@ -313,7 +313,7 @@ WebAppInterface::RetrieveBulkPackageInfo(const StringList& packageNames, // printf("Sending JSON:\n%s\n", jsonString.String()); - return _SendJsonRequest(jsonString, message); + return _SendJsonRequest("pkg", jsonString, message); } @@ -348,9 +348,37 @@ WebAppInterface::RetrievePackageIcon(const BString& packageName, status_t -WebAppInterface::_SendJsonRequest(BString jsonString, BMessage& reply) const +WebAppInterface::RetrieveUserRatings(const BString& packageName, + const BString& architecture, int resultOffset, int maxResults, + BMessage& message) { - BUrl url("https://depot.haiku-os.org/api/v1/pkg"); + BString jsonString = JsonBuilder() + .AddValue("jsonrpc", "2.0") + .AddValue("id", ++fRequestIndex) + .AddValue("method", "searchUserRatings") + .AddArray("params") + .AddObject() + .AddValue("pkgName", packageName) + .AddValue("pkgVersionArchitectureCode", architecture) + .AddValue("offset", resultOffset) + .AddValue("limit", maxResults) + .EndObject() + .EndArray() + .End(); + +// printf("Sending JSON:\n%s\n", jsonString.String()); + + return _SendJsonRequest("userrating", jsonString, message); +} + + +status_t +WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString, + BMessage& reply) const +{ + BString urlString("https://depot.haiku-os.org/api/v1/"); + urlString << domain; + BUrl url(urlString); ProtocolListener listener; BUrlContext context; diff --git a/src/apps/haikudepot/WebAppInterface.h b/src/apps/haikudepot/WebAppInterface.h index 3a034d2d21..f978580957 100644 --- a/src/apps/haikudepot/WebAppInterface.h +++ b/src/apps/haikudepot/WebAppInterface.h @@ -42,9 +42,15 @@ public: const BString& packageName, BDataIO* stream); + status_t RetrieveUserRatings( + const BString& packageName, + const BString& architecture, + int resultOffset, int maxResults, + BMessage& message); + private: - status_t _SendJsonRequest(BString jsonString, - BMessage& reply) const; + status_t _SendJsonRequest(const char* domain, + BString jsonString, BMessage& reply) const; private: BString fUsername;