refactor: version check

This commit is contained in:
Curve 2021-03-19 21:22:32 +01:00
parent 6599f6f1ee
commit 2a6a19f12b
No known key found for this signature in database
GPG Key ID: 460F6C466BD35813
4 changed files with 34 additions and 12 deletions

View File

@ -1,5 +1,6 @@
#pragma once
#include "../../core/global/globals.hpp"
#include "../../helper/version/check.hpp"
#include <nlohmann/json.hpp>
namespace nlohmann
@ -147,6 +148,19 @@ namespace nlohmann
j.at("settings").get_to(obj.settings);
}
};
template <> struct adl_serializer<Soundux::Objects::VersionStatus>
{
static void to_json(json &j, const Soundux::Objects::VersionStatus &obj)
{
j = {{"current", obj.current}, {"latest", obj.latest}, {"outdated", obj.outdated}};
}
static void from_json(const json &j, Soundux::Objects::VersionStatus &obj)
{
j.at("latest").get_to(obj.latest);
j.at("current").get_to(obj.current);
j.at("outdated").get_to(obj.outdated);
}
};
#if defined(__linux__)
template <> struct adl_serializer<Soundux::Objects::PulseRecordingStream>
{

View File

@ -1,11 +1,12 @@
#include "check.hpp"
#include <fancy.hpp>
#include <json.hpp>
#include <optional>
#include <regex>
httplib::Client VersionCheck::client("https://api.github.com");
bool VersionCheck::isLatest()
std::optional<Soundux::Objects::VersionStatus> VersionCheck::getStatus()
{
auto githubTags = client.Get("/repos/Soundux/Soundux/tags");
@ -26,14 +27,7 @@ bool VersionCheck::isLatest()
{
if (match[1].matched)
{
if (match[1] != SOUNDUX_VERSION)
{
Fancy::fancy.logTime().warning() << "Current version is " << SOUNDUX_VERSION
<< " latest version is " << match[1] << std::endl;
return false;
}
Fancy::fancy.logTime().success() << "You are using the latest version of soundux" << std::endl;
return true;
return Soundux::Objects::VersionStatus{SOUNDUX_VERSION, match[1], match[1] != SOUNDUX_VERSION};
}
Fancy::fancy.logTime().warning() << "Failed to fetch latest version" << std::endl;
}
@ -56,5 +50,5 @@ bool VersionCheck::isLatest()
{
Fancy::fancy.logTime().warning() << "Request failed!" << std::endl;
}
return true;
return std::nullopt;
}

View File

@ -1,10 +1,24 @@
#pragma once
#include <httplib.h>
#include <optional>
namespace Soundux
{
namespace Objects
{
struct VersionStatus
{
std::string current;
std::string latest;
bool outdated;
};
} // namespace Objects
} // namespace Soundux
class VersionCheck
{
static httplib::Client client;
public:
static bool isLatest();
static std::optional<Soundux::Objects::VersionStatus> getStatus();
};

View File

@ -78,7 +78,7 @@ namespace Soundux::Objects
[](const std::string &url) { return Globals::gYtdl.download(url); });
webview.addCallback("stopYoutubeDLDownload", []() { Globals::gYtdl.killDownload(); });
webview.addCallback("getSystemInfo", []() -> std::string { return SystemInfo::getSummary(); });
webview.addCallback("isLatest", []() { return VersionCheck::isLatest(); });
webview.addCallback("updateCheck", []() { return VersionCheck::getStatus(); });
#if !defined(__linux__)
webview.addCallback("getOutputs", [this]() { return getOutputs(); });