diff --git a/module/hooks/init.mjs b/module/hooks/init.mjs index b06467e..60a12a5 100644 --- a/module/hooks/init.mjs +++ b/module/hooks/init.mjs @@ -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(); diff --git a/module/hooks/ready.mjs b/module/hooks/ready.mjs index 2a8bf39..6a5141d 100644 --- a/module/hooks/ready.mjs +++ b/module/hooks/ready.mjs @@ -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(); }); diff --git a/module/utils/ReleaseChannels.mjs b/module/utils/ReleaseChannels.mjs new file mode 100644 index 0000000..aa6588b --- /dev/null +++ b/module/utils/ReleaseChannels.mjs @@ -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`, + }); +}; diff --git a/styles/main.css b/styles/main.css index 682b25d..e6ae9f9 100644 --- a/styles/main.css +++ b/styles/main.css @@ -23,6 +23,9 @@ @import url("./elements/span.css") layer(elements); @import url("./elements/table.css") layer(elements); +/* Partials */ +@import url("./misc.css") layer(partials); + /* Apps */ @import url("./Apps/common.css") layer(apps); @import url("./Apps/Ask.css") layer(apps); diff --git a/styles/misc.css b/styles/misc.css new file mode 100644 index 0000000..061bcf5 --- /dev/null +++ b/styles/misc.css @@ -0,0 +1,7 @@ +.taf-chat-version-notif { + h2 { + font-family: var(--font-body); + font-size: 1.5rem; + margin: 0; + } +} diff --git a/templates/new-version-message.hbs b/templates/new-version-message.hbs new file mode 100644 index 0000000..ff11bc0 --- /dev/null +++ b/templates/new-version-message.hbs @@ -0,0 +1,17 @@ +
+ A new {{ifThen release.prerelease "beta" ""}} version of {{ system.title }} has been released. +
+ + Release Notes + +