change update checking so they can use a better caching system

This commit is contained in:
HJfod 2024-04-22 18:41:49 +03:00
parent 9ca23e01dc
commit 22d5e4e97a
4 changed files with 42 additions and 22 deletions

View file

@ -640,10 +640,32 @@ ServerRequest<std::unordered_set<std::string>> server::getTags(bool useCache) {
);
}
ServerRequest<std::vector<ServerModUpdate>> server::checkUpdates(std::vector<std::string> const& modIDs, bool useCache) {
ServerRequest<std::optional<ServerModUpdate>> server::checkUpdates(Mod* mod) {
return checkAllUpdates().map(
[mod](Result<std::vector<ServerModUpdate>, ServerError>* result) -> Result<std::optional<ServerModUpdate>, ServerError> {
if (result->isOk()) {
for (auto& update : result->unwrap()) {
if (update.id == mod->getID() && update.version > mod->getVersion()) {
return Ok(update);
}
}
return Ok(std::nullopt);
}
return Err(result->unwrapErr());
}
);
}
ServerRequest<std::vector<ServerModUpdate>> server::checkAllUpdates(bool useCache) {
if (useCache) {
return getCache<checkUpdates>().get(modIDs);
return getCache<checkAllUpdates>().get();
}
auto modIDs = ranges::map<std::vector<std::string>>(
Loader::get()->getAllMods(),
[](auto mod) { return mod->getID(); }
);
auto req = web::WebRequest();
req.userAgent(getServerUserAgent());
req.param("platform", GEODE_PLATFORM_SHORT_IDENTIFIER);
@ -681,7 +703,7 @@ void server::clearServerCaches(bool clearGlobalCaches) {
// Only clear global caches if explicitly requested
if (clearGlobalCaches) {
getCache<&getTags>().clear();
getCache<&checkUpdates>().clear();
getCache<&checkAllUpdates>().clear();
}
}
@ -691,6 +713,6 @@ $execute {
getCache<&server::getMod>().limit(size);
getCache<&server::getModLogo>().limit(size);
getCache<&server::getTags>().limit(size);
getCache<&server::checkUpdates>().limit(size);
getCache<&server::checkAllUpdates>().limit(size);
});
}

View file

@ -120,7 +120,8 @@ namespace server {
ServerRequest<ServerModMetadata> getMod(std::string const& id, bool useCache = true);
ServerRequest<ByteVector> getModLogo(std::string const& id, bool useCache = true);
ServerRequest<std::unordered_set<std::string>> getTags(bool useCache = true);
ServerRequest<std::vector<ServerModUpdate>> checkUpdates(std::vector<std::string> const& modIDs, bool useCache = true);
ServerRequest<std::optional<ServerModUpdate>> checkUpdates(Mod* mod);
ServerRequest<std::vector<ServerModUpdate>> checkAllUpdates(bool useCache = true);
void clearServerCaches(bool clearGlobalCaches = false);
}

View file

@ -325,11 +325,7 @@ ModsLayer* ModsLayer::scene() {
}
server::ServerRequest<std::vector<std::string>> ModsLayer::checkInstalledModsForUpdates() {
auto modIDs = ranges::map<std::vector<std::string>>(
Loader::get()->getAllMods(),
[](auto mod) { return mod->getID(); }
);
return server::checkUpdates(modIDs).map([](auto* result) -> Result<std::vector<std::string>, server::ServerError> {
return server::checkAllUpdates().map([](auto* result) -> Result<std::vector<std::string>, server::ServerError> {
if (result->isOk()) {
std::vector<std::string> updatesFound;
for (auto& update : result->unwrap()) {

View file

@ -136,20 +136,21 @@ server::ServerRequest<std::unordered_set<std::string>> ModSource::fetchValidTags
}
server::ServerRequest<std::optional<server::ServerModUpdate>> ModSource::checkUpdates() {
m_availableUpdate = std::nullopt;
return server::checkUpdates({ this->getID() }).map(
[this](auto* result) -> Result<std::optional<server::ServerModUpdate>, server::ServerError> {
if (result->isOk()) {
auto updates = result->unwrap();
if (!updates.empty()) {
auto update = std::move(std::move(updates).at(0));
if (update.version > this->getMetadata().getVersion()) {
m_availableUpdate = update;
return std::visit(makeVisitor {
[this](Mod* mod) {
return server::checkUpdates(mod).map(
[this](auto* result) -> Result<std::optional<server::ServerModUpdate>, server::ServerError> {
if (result->isOk()) {
m_availableUpdate = result->unwrap();
return Ok(m_availableUpdate);
}
return Err(result->unwrapErr());
}
return Ok(std::nullopt);
}
return Err(result->unwrapErr());
);
},
[](server::ServerModMetadata const& metadata) {
// Server mods aren't installed so you can't install updates for them
return server::ServerRequest<std::optional<server::ServerModUpdate>>::immediate(Ok(std::nullopt));
}
);
}, m_value);
}