ramirez/src/data/updates.js

119 lines
3.7 KiB
JavaScript
Raw Normal View History

2020-08-12 17:08:37 -04:00
const url = require("url");
const https = require("https");
const moment = require("moment");
const knex = require("../knex");
const config = require("../cfg");
2019-06-09 10:31:17 -04:00
const UPDATE_CHECK_FREQUENCY = 12; // In hours
let updateCheckPromise = null;
async function initUpdatesTable() {
2020-08-12 17:08:37 -04:00
const row = await knex("updates").first();
2019-06-09 10:31:17 -04:00
if (! row) {
2020-08-12 17:08:37 -04:00
await knex("updates").insert({
2019-06-09 10:31:17 -04:00
available_version: null,
last_checked: null,
});
}
}
/**
* Update current and available versions in the database.
* Only works when `repository` in package.json is set to a GitHub repository
* @returns {Promise<void>}
*/
async function refreshVersions() {
await initUpdatesTable();
2020-08-12 17:08:37 -04:00
const { last_checked } = await knex("updates").first();
2019-06-09 10:31:17 -04:00
// Only refresh available version if it's been more than UPDATE_CHECK_FREQUENCY since our last check
2020-08-12 17:08:37 -04:00
if (last_checked != null && last_checked > moment.utc().subtract(UPDATE_CHECK_FREQUENCY, "hours").format("YYYY-MM-DD HH:mm:ss")) return;
2019-06-09 10:31:17 -04:00
2020-08-12 17:08:37 -04:00
const packageJson = require("../../package.json");
2019-06-09 10:31:17 -04:00
const repositoryUrl = packageJson.repository && packageJson.repository.url;
if (! repositoryUrl) return;
const parsedUrl = url.parse(repositoryUrl);
2020-08-12 17:08:37 -04:00
if (parsedUrl.hostname !== "github.com") return;
2019-06-09 10:31:17 -04:00
2020-08-12 17:08:37 -04:00
const [, owner, repo] = parsedUrl.pathname.split("/");
2019-06-09 10:31:17 -04:00
if (! owner || ! repo) return;
https.get(
{
2020-08-12 17:08:37 -04:00
hostname: "api.github.com",
path: `/repos/${owner}/${repo}/releases`,
2019-06-09 10:31:17 -04:00
headers: {
2020-08-12 17:08:37 -04:00
"User-Agent": `Modmail Bot (https://github.com/${owner}/${repo}) (${packageJson.version})`
2019-06-09 10:31:17 -04:00
}
},
async res => {
if (res.statusCode !== 200) {
2020-08-12 17:08:37 -04:00
await knex("updates").update({
last_checked: moment.utc().format("YYYY-MM-DD HH:mm:ss")
2019-06-09 10:31:17 -04:00
});
console.warn(`[WARN] Got status code ${res.statusCode} when checking for available updates`);
return;
}
2020-08-12 17:08:37 -04:00
let data = "";
res.on("data", chunk => data += chunk);
res.on("end", async () => {
2019-06-09 10:31:17 -04:00
const parsed = JSON.parse(data);
if (! Array.isArray(parsed) || parsed.length === 0) return;
const latestStableRelease = parsed.find(r => ! r.prerelease && ! r.draft);
if (! latestStableRelease) return;
const latestVersion = latestStableRelease.name;
2020-08-12 17:08:37 -04:00
await knex("updates").update({
2019-06-09 10:31:17 -04:00
available_version: latestVersion,
2020-08-12 17:08:37 -04:00
last_checked: moment.utc().format("YYYY-MM-DD HH:mm:ss")
2019-06-09 10:31:17 -04:00
});
});
}
);
}
/**
* @param {String} a Version string, e.g. "2.20.0"
* @param {String} b Version string, e.g. "2.20.0"
* @returns {Number} 1 if version a is larger than b, -1 is version a is smaller than b, 0 if they are equal
*/
function compareVersions(a, b) {
2020-08-12 17:08:37 -04:00
const aParts = a.split(".");
const bParts = b.split(".");
2019-06-09 10:31:17 -04:00
for (let i = 0; i < Math.max(aParts.length, bParts.length); i++) {
2020-08-12 17:08:37 -04:00
let aPart = parseInt((aParts[i] || "0").match(/\d+/)[0] || "0", 10);
let bPart = parseInt((bParts[i] || "0").match(/\d+/)[0] || "0", 10);
2019-06-09 10:31:17 -04:00
if (aPart > bPart) return 1;
if (aPart < bPart) return -1;
}
return 0;
}
async function getAvailableUpdate() {
await initUpdatesTable();
2020-08-12 17:08:37 -04:00
const packageJson = require("../../package.json");
2019-06-09 10:31:17 -04:00
const currentVersion = packageJson.version;
2020-08-12 17:08:37 -04:00
const { available_version: availableVersion } = await knex("updates").first();
2019-06-09 10:31:17 -04:00
if (availableVersion == null) return null;
if (currentVersion == null) return availableVersion;
const versionDiff = compareVersions(currentVersion, availableVersion);
if (versionDiff === -1) return availableVersion;
return null;
}
async function refreshVersionsLoop() {
await refreshVersions();
setTimeout(refreshVersionsLoop, UPDATE_CHECK_FREQUENCY * 60 * 60 * 1000);
}
module.exports = {
getAvailableUpdate,
startVersionRefreshLoop: refreshVersionsLoop
};