refactor(RD): clear up sorting methods
This refactors the sorting methods to join every bit of it into a single list, easing maintanance. It also removes the weird index contraint on the list of methods by adding an index field to the DS that holds the method. Lastly, it puts the available methods on their respective API, so other resources on the same API can re-use them later on. Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
@ -54,12 +54,23 @@ class ResourceAPI {
|
||||
enum ModLoaderType { Forge = 1 << 0, Cauldron = 1 << 1, LiteLoader = 1 << 2, Fabric = 1 << 3, Quilt = 1 << 4 };
|
||||
Q_DECLARE_FLAGS(ModLoaderTypes, ModLoaderType)
|
||||
|
||||
struct SortingMethod {
|
||||
// The index of the sorting method. Used to allow for arbitrary ordering in the list of methods.
|
||||
// Used by Flame in the API request.
|
||||
unsigned int index;
|
||||
// The real name of the sorting, as used in the respective API specification.
|
||||
// Used by Modrinth in the API request.
|
||||
QString name;
|
||||
// The human-readable name of the sorting, used for display in the UI.
|
||||
QString readable_name;
|
||||
};
|
||||
|
||||
struct SearchArgs {
|
||||
ModPlatform::ResourceType type{};
|
||||
int offset = 0;
|
||||
|
||||
std::optional<QString> search;
|
||||
std::optional<QString> sorting;
|
||||
std::optional<SortingMethod> sorting;
|
||||
std::optional<ModLoaderTypes> loaders;
|
||||
std::optional<std::list<Version> > versions;
|
||||
};
|
||||
@ -95,6 +106,10 @@ class ResourceAPI {
|
||||
std::function<void(QJsonDocument&, ModPlatform::IndexedPack&)> on_succeed;
|
||||
};
|
||||
|
||||
public:
|
||||
/** Gets a list of available sorting methods for this API. */
|
||||
[[nodiscard]] virtual auto getSortingMethods() const -> QList<SortingMethod> = 0;
|
||||
|
||||
public slots:
|
||||
[[nodiscard]] virtual NetJob::Ptr searchProjects(SearchArgs&&, SearchCallbacks&&) const
|
||||
{
|
||||
|
@ -212,3 +212,18 @@ NetJob::Ptr FlameAPI::getFiles(const QStringList& fileIds, QByteArray* response)
|
||||
|
||||
return netJob;
|
||||
}
|
||||
|
||||
// https://docs.curseforge.com/?python#tocS_ModsSearchSortField
|
||||
static QList<ResourceAPI::SortingMethod> s_sorts = { { 1, "Featured", QObject::tr("Sort by Featured") },
|
||||
{ 2, "Popularity", QObject::tr("Sort by Popularity") },
|
||||
{ 3, "LastUpdated", QObject::tr("Sort by Last Updated") },
|
||||
{ 4, "Name", QObject::tr("Sort by Name") },
|
||||
{ 5, "Author", QObject::tr("Sort by Author") },
|
||||
{ 6, "TotalDownloads", QObject::tr("Sort by Downloads") },
|
||||
{ 7, "Category", QObject::tr("Sort by Category") },
|
||||
{ 8, "GameVersion", QObject::tr("Sort by Game Version") } };
|
||||
|
||||
QList<ResourceAPI::SortingMethod> FlameAPI::getSortingMethods() const
|
||||
{
|
||||
return s_sorts;
|
||||
}
|
||||
|
@ -14,20 +14,9 @@ class FlameAPI : public NetworkResourceAPI {
|
||||
NetJob::Ptr matchFingerprints(const QList<uint>& fingerprints, QByteArray* response);
|
||||
NetJob::Ptr getFiles(const QStringList& fileIds, QByteArray* response) const;
|
||||
|
||||
private:
|
||||
static int getSortFieldInt(QString const& sortString)
|
||||
{
|
||||
return sortString == "Featured" ? 1
|
||||
: sortString == "Popularity" ? 2
|
||||
: sortString == "LastUpdated" ? 3
|
||||
: sortString == "Name" ? 4
|
||||
: sortString == "Author" ? 5
|
||||
: sortString == "TotalDownloads" ? 6
|
||||
: sortString == "Category" ? 7
|
||||
: sortString == "GameVersion" ? 8
|
||||
: 1;
|
||||
}
|
||||
[[nodiscard]] auto getSortingMethods() const -> QList<ResourceAPI::SortingMethod> override;
|
||||
|
||||
private:
|
||||
static int getClassId(ModPlatform::ResourceType type)
|
||||
{
|
||||
switch (type) {
|
||||
@ -62,7 +51,7 @@ class FlameAPI : public NetworkResourceAPI {
|
||||
if (args.search.has_value())
|
||||
get_arguments.append(QString("searchFilter=%1").arg(args.search.value()));
|
||||
if (args.sorting.has_value())
|
||||
get_arguments.append(QString("sortField=%1").arg(getSortFieldInt(args.sorting.value())));
|
||||
get_arguments.append(QString("sortField=%1").arg(args.sorting.value().index));
|
||||
get_arguments.append("sortOrder=desc");
|
||||
if (args.loaders.has_value())
|
||||
get_arguments.append(QString("modLoaderType=%1").arg(getMappedModLoader(args.loaders.value())));
|
||||
|
@ -112,3 +112,15 @@ NetJob::Ptr ModrinthAPI::getProjects(QStringList addonIds, QByteArray* response)
|
||||
|
||||
return netJob;
|
||||
}
|
||||
|
||||
// https://docs.modrinth.com/api-spec/#tag/projects/operation/searchProjects
|
||||
static QList<ResourceAPI::SortingMethod> s_sorts = { { 1, "relevance", QObject::tr("Sort by Relevance") },
|
||||
{ 2, "downloads", QObject::tr("Sort by Downloads") },
|
||||
{ 3, "follows", QObject::tr("Sort by Follows") },
|
||||
{ 4, "newest", QObject::tr("Sort by Last Updated") },
|
||||
{ 5, "updated", QObject::tr("Sort by Newest") } };
|
||||
|
||||
QList<ResourceAPI::SortingMethod> ModrinthAPI::getSortingMethods() const
|
||||
{
|
||||
return s_sorts;
|
||||
}
|
||||
|
@ -49,6 +49,8 @@ class ModrinthAPI : public NetworkResourceAPI {
|
||||
NetJob::Ptr getProjects(QStringList addonIds, QByteArray* response) const override;
|
||||
|
||||
public:
|
||||
[[nodiscard]] auto getSortingMethods() const -> QList<ResourceAPI::SortingMethod> override;
|
||||
|
||||
inline auto getAuthorURL(const QString& name) const -> QString { return "https://modrinth.com/user/" + name; };
|
||||
|
||||
static auto getModLoaderStrings(const ModLoaderTypes types) -> const QStringList
|
||||
@ -116,7 +118,7 @@ class ModrinthAPI : public NetworkResourceAPI {
|
||||
if (args.search.has_value())
|
||||
get_arguments.append(QString("query=%1").arg(args.search.value()));
|
||||
if (args.sorting.has_value())
|
||||
get_arguments.append(QString("index=%1").arg(args.sorting.value()));
|
||||
get_arguments.append(QString("index=%1").arg(args.sorting.value().name));
|
||||
get_arguments.append(QString("facets=%1").arg(createFacets(args)));
|
||||
|
||||
return BuildConfig.MODRINTH_PROD_URL + "/search?" + get_arguments.join('&');
|
||||
|
Reference in New Issue
Block a user