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.
This commit is contained in:
Stephan Aßmus 2014-09-03 23:42:24 +02:00
parent 2b411c028e
commit 7ff94fd072
3 changed files with 117 additions and 22 deletions

View File

@ -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", &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)
// );
}
}

View File

@ -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;

View File

@ -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;