1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-06-29 12:57:46 +02:00
jfa-go/ts/modules/update.ts
Harvey Tindall 92332206f0
add basic update functionality
If enabled, jfa-go pings buildrone (hosted at builds.hrfee.pw) every 30
min for new updates. If there is one, it gets information (and if
applicable, a binary) from the appropriate source (buildrone, github, or
dockerhub) and displays it on the admin page. You can switch update
channels between stable and unstable. For binary releases, updates are
downloaded automatically and installed when the user presses update.

Since this obviously introduces some "phone-home" functionality into
jfa-go, I just want to say IPs are not and will not be logged by
buildrone, although I may later introduce functionality to give a rough
idea of the number of users (again, no IPs stored). The whole thing can
also be turned off in settings.
2021-03-07 15:23:44 +00:00

125 lines
4.7 KiB
TypeScript

import { _get, _post, toggleLoader } from "../modules/common.js";
import { Marked, Renderer } from "@ts-stack/markdown";
interface updateDTO {
new: boolean;
update: Update;
}
export class Updater implements updater {
private _update: Update;
private _date: Date;
updateAvailable = false;
checkForUpdates = (run?: (req: XMLHttpRequest) => void) => _get("/config/update", null, (req: XMLHttpRequest) => {
if (req.readyState == 4) {
if (req.status != 200) {
window.notifications.customError("errorCheckUpdate", window.lang.notif("errorCheckUpdate"));
return
}
let resp = req.response as updateDTO;
if (resp.new) {
this.update = resp.update;
if (run) { run(req); }
// } else {
// window.notifications.customPositive("noUpdatesAvailable", "", window.lang.notif("noUpdatesAvailable"));
}
}
});
get date(): number { return Math.floor(this._date.getTime() / 1000); }
set date(unix: number) {
this._date = new Date(unix * 1000);
document.getElementById("update-date").textContent = this._date.toDateString() + " " + this._date.toLocaleTimeString();
}
get description(): string { return this._update.description; }
set description(description: string) {
this._update.description = description;
const el = document.getElementById("update-description") as HTMLParagraphElement;
el.textContent = description;
if (this.version == "git") {
el.classList.add("monospace");
} else {
el.classList.remove("monospace");
}
}
get changelog(): string { return this._update.changelog; }
set changelog(changelog: string) {
this._update.changelog = changelog;
document.getElementById("update-changelog").innerHTML = Marked.parse(changelog);
}
get version(): string { return this._update.version; }
set version(version: string) {
this._update.version = version;
document.getElementById("update-version").textContent = version;
}
get commit(): string { return this._update.commit; }
set commit(commit: string) {
this._update.commit = commit;
document.getElementById("update-commit").textContent = commit.slice(0, 7);
}
get link(): string { return this._update.link; }
set link(link: string) {
this._update.link = link;
(document.getElementById("update-version") as HTMLAnchorElement).href = link;
}
get download_link(): string { return this._update.download_link; }
set download_link(link: string) { this._update.download_link = link; }
get can_update(): boolean { return this._update.can_update; }
set can_update(can: boolean) {
this._update.can_update = can;
const download = document.getElementById("update-download") as HTMLSpanElement;
const update = document.getElementById("update-update") as HTMLSpanElement;
if (can) {
download.classList.add("unfocused");
update.classList.remove("unfocused");
} else {
download.onclick = () => window.open(this._update.download_link || this._update.link);
download.classList.remove("unfocused");
update.classList.add("unfocused");
}
}
get update(): Update { return this._update; }
set update(update: Update) {
this._update = update;
this.version = update.version;
this.commit = update.commit;
this.date = update.date;
this.description = update.description;
this.changelog = update.changelog;
this.link = update.link;
this.download_link = update.download_link;
this.can_update = update.can_update;
}
constructor() {
const update = document.getElementById("update-update") as HTMLSpanElement;
update.onclick = () => {
toggleLoader(update);
_post("/config/update", null, (req: XMLHttpRequest) => {
if (req.readyState == 4) {
toggleLoader(update);
if (req.status != 200) {
window.notifications.customError("applyUpdateError", window.lang.notif("errorApplyUpdate"));
} else {
window.notifications.customSuccess("applyUpdate", window.lang.notif("updateApplied"));
}
window.modals.updateInfo.close();
}
});
};
this.checkForUpdates(() => {
this.updateAvailable = true;
window.notifications.customPositive("updateAvailable", "", window.lang.notif("updateAvailable"));
});
}
}