2024-10-09 01:43:44 +02:00
|
|
|
const { setSetting, setting } = require("./util-server");
|
2021-08-21 13:50:22 +02:00
|
|
|
const axios = require("axios");
|
2022-03-07 09:24:24 +01:00
|
|
|
const compareVersions = require("compare-versions");
|
2023-07-03 09:28:03 +02:00
|
|
|
const { log } = require("../src/util");
|
2021-08-21 13:50:22 +02:00
|
|
|
|
|
|
|
exports.version = require("../package.json").version;
|
|
|
|
exports.latestVersion = null;
|
|
|
|
|
2023-07-03 09:28:03 +02:00
|
|
|
// How much time in ms to wait between update checks
|
|
|
|
const UPDATE_CHECKER_INTERVAL_MS = 1000 * 60 * 60 * 48;
|
|
|
|
const UPDATE_CHECKER_LATEST_VERSION_URL = "https://uptime.kuma.pet/version";
|
|
|
|
|
2021-08-21 13:50:22 +02:00
|
|
|
let interval;
|
|
|
|
|
|
|
|
exports.startInterval = () => {
|
|
|
|
let check = async () => {
|
2024-10-09 01:43:44 +02:00
|
|
|
if (await setting("checkUpdate") === false) {
|
2023-07-03 09:28:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.debug("update-checker", "Retrieving latest versions");
|
|
|
|
|
2021-08-21 13:50:22 +02:00
|
|
|
try {
|
2023-07-03 09:28:03 +02:00
|
|
|
const res = await axios.get(UPDATE_CHECKER_LATEST_VERSION_URL);
|
2021-08-21 13:50:22 +02:00
|
|
|
|
|
|
|
// For debug
|
|
|
|
if (process.env.TEST_CHECK_VERSION === "1") {
|
2021-10-30 18:36:46 +02:00
|
|
|
res.data.slow = "1000.0.0";
|
|
|
|
}
|
|
|
|
|
2024-10-09 01:43:44 +02:00
|
|
|
let checkBeta = await setting("checkBeta");
|
2022-03-07 09:24:24 +01:00
|
|
|
|
|
|
|
if (checkBeta && res.data.beta) {
|
2022-12-05 12:17:24 +01:00
|
|
|
if (compareVersions.compare(res.data.beta, res.data.slow, ">")) {
|
2022-03-07 09:24:24 +01:00
|
|
|
exports.latestVersion = res.data.beta;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 18:36:46 +02:00
|
|
|
if (res.data.slow) {
|
|
|
|
exports.latestVersion = res.data.slow;
|
2021-08-21 13:50:22 +02:00
|
|
|
}
|
|
|
|
|
2023-07-03 09:28:03 +02:00
|
|
|
} catch (_) {
|
|
|
|
log.info("update-checker", "Failed to check for new versions");
|
|
|
|
}
|
2021-08-21 13:50:22 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
check();
|
2023-07-03 09:28:03 +02:00
|
|
|
interval = setInterval(check, UPDATE_CHECKER_INTERVAL_MS);
|
2021-08-21 13:50:22 +02:00
|
|
|
};
|
|
|
|
|
2022-04-20 20:56:40 +02:00
|
|
|
/**
|
|
|
|
* Enable the check update feature
|
|
|
|
* @param {boolean} value Should the check update feature be enabled?
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2021-08-21 13:50:22 +02:00
|
|
|
exports.enableCheckUpdate = async (value) => {
|
2024-10-09 01:43:44 +02:00
|
|
|
await setSetting("checkUpdate", value);
|
2021-08-21 13:50:22 +02:00
|
|
|
|
|
|
|
clearInterval(interval);
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
exports.startInterval();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.socket = null;
|