Add an automatic version notification for new stable releases, and optionally beta releases

This commit is contained in:
Oliver 2026-05-11 22:23:09 -06:00
parent d3a60fbef5
commit 09ac9624ed
6 changed files with 106 additions and 0 deletions

View file

@ -18,6 +18,7 @@ import { TAFMacro } from "../documents/Macro.mjs";
import { TAFTokenDocument } from "../documents/Token.mjs";
// Settings
import { registerReleaseSettings } from "../utils/ReleaseChannels.mjs";
import { registerWorldSettings } from "../settings/world.mjs";
// Utils
@ -89,6 +90,7 @@ Hooks.on(`init`, () => {
);
// #endregion Sheets
registerReleaseSettings();
registerWorldSettings();
registerSockets();

View file

@ -1,3 +1,4 @@
import { checkForNewReleases } from "../utils/ReleaseChannels.mjs";
import { checkMigrations } from "../migrations/checkMigrations.mjs";
Hooks.on(`ready`, () => {
@ -7,4 +8,5 @@ Hooks.on(`ready`, () => {
};
checkMigrations();
checkForNewReleases();
});

View file

@ -0,0 +1,75 @@
import { __ID__, filePath } from "../consts.mjs";
import { Logger } from "./Logger.mjs";
const { ChatMessage } = foundry.documents;
const { renderTemplate } = foundry.applications.handlebars;
const { fetchJsonWithTimeout, isNewerVersion } = foundry.utils;
let newestVersion = null;
async function fetchLatestVersion() {
const includeBetas = game.settings.get(__ID__, `betaChannel`);
try {
const releases = await fetchJsonWithTimeout(`https://git.varify.ca/api/v1/repos/Foundry/taf/releases?pre-release=${includeBetas}&limit=1`);
return releases.at(0);
} catch {
Logger.error(`Failed to fetch latest version from Forgejo, halting version checks`);
return;
};
};
export async function getLatestVersion() {
if (!newestVersion) {
newestVersion = await fetchLatestVersion();
};
return newestVersion;
};
export async function checkForNewReleases() {
// Only perform the update check if the game is ready and the user is the active GM
if (!game.user.isActiveGM || !game.ready) { return };
// Don't do any version notifications if this is a fresh world
const lastNotifiedVersion = game.settings.get(__ID__, `lastUpdateNotified`);
if (!lastNotifiedVersion) {
game.settings.set(__ID__, `lastUpdateNotified`, game.system.version);
return;
};
let latest = newestVersion = await getLatestVersion();
// Is the latest release newer than the previously notified release
const latestVersion = latest.tag_name.replace(/^v/, ``);
if (!isNewerVersion(latestVersion, lastNotifiedVersion)) { return };
const content = await renderTemplate(
filePath(`templates/new-version-message.hbs`),
{
release: latest,
system: game.system,
},
);
ChatMessage.implementation.create({
whisper: [ game.user.id ],
content,
});
// Mark the new version as the
game.settings.set(__ID__, `lastUpdateNotified`, latestVersion);
};
export function registerReleaseSettings() {
game.settings.register(__ID__, `betaChannel`, {
name: `${__ID__}.settings.betaChannel.name`,
hint: `${__ID__}.settings.betaChannel.hint`,
config: true,
type: Boolean,
default: false,
scope: `world`,
});
game.settings.register(__ID__, `lastUpdateNotified`, {
config: false,
type: String,
scope: `world`,
});
};