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:
@@ -541,24 +541,85 @@ Model::PopulatePackage(const PackageInfoRef& package, uint32 flags)
|
|||||||
|
|
||||||
fPopulatedPackages.Add(package);
|
fPopulatedPackages.Add(package);
|
||||||
|
|
||||||
if (package->Title() == "wonderbrush") {
|
|
||||||
|
|
||||||
if ((flags & POPULATE_CATEGORIES) != 0) {
|
|
||||||
package->AddCategory(CategoryGraphics());
|
|
||||||
package->AddCategory(CategoryProductivity());
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((flags & POPULATE_USER_RATINGS) != 0) {
|
if ((flags & POPULATE_USER_RATINGS) != 0) {
|
||||||
package->AddUserRating(
|
// Retrieve info from web-app
|
||||||
UserRating(UserInfo("binky"), 4.5f,
|
WebAppInterface interface;
|
||||||
"Awesome!", "en", "2.1.2", 0, 0)
|
interface.SetPreferredLanguage(fPreferredLanguage);
|
||||||
);
|
BMessage info;
|
||||||
package->AddUserRating(
|
|
||||||
UserRating(UserInfo("twinky"), 5.0f,
|
status_t status = interface.RetrieveUserRatings(package->Title(),
|
||||||
"The best!", "en", "2.1.2", 3, 1)
|
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();
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
// );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -279,7 +279,7 @@ WebAppInterface::RetrievePackageInfo(const BString& packageName,
|
|||||||
|
|
||||||
// printf("Sending JSON:\n%s\n", jsonString.String());
|
// 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());
|
// 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
|
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;
|
ProtocolListener listener;
|
||||||
BUrlContext context;
|
BUrlContext context;
|
||||||
|
|||||||
@@ -42,9 +42,15 @@ public:
|
|||||||
const BString& packageName,
|
const BString& packageName,
|
||||||
BDataIO* stream);
|
BDataIO* stream);
|
||||||
|
|
||||||
|
status_t RetrieveUserRatings(
|
||||||
|
const BString& packageName,
|
||||||
|
const BString& architecture,
|
||||||
|
int resultOffset, int maxResults,
|
||||||
|
BMessage& message);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
status_t _SendJsonRequest(BString jsonString,
|
status_t _SendJsonRequest(const char* domain,
|
||||||
BMessage& reply) const;
|
BString jsonString, BMessage& reply) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BString fUsername;
|
BString fUsername;
|
||||||
|
|||||||
Reference in New Issue
Block a user