hardlounge/server/plugins/changelog.ts

138 lines
3.2 KiB
TypeScript
Raw Normal View History

import got, {Response} from "got";
import colors from "chalk";
import log from "../log";
import pkg from "../../package.json";
import ClientManager from "../clientManager";
const TIME_TO_LIVE = 15 * 60 * 1000; // 15 minutes, in milliseconds
export default {
2020-01-02 10:54:47 +00:00
isUpdateAvailable: false,
Improve UI of the About section and changelog viewer - Keep consistent width between the Help page and Changelog (which is already different from other windows 😠) - Add icons to the About links - Make sure `li` elements (i.e. all the lists in changelogs) are consistent in size with rest of the client - Display version and release notes link on the "About The Lounge" header line, smaller, pushed to the right - Check new releases when opening the Help window in order to display it without having to open the release notes. Release notes are being fed to the Changelog page at that moment to avoid fetching twice. - Re-check version/fetch release notes after 24h. Since The Lounge can now run 24/7, reconnect when losing the network, we have to assume an "always-on" usage. - Change icon, animate background color when getting response from GitHub to avoid flashing. - Combine click handlers with our wonderful window management. These were the same handler, even with similar checks (`target` exists, etc.), just in 2 different places. This is necessary for the next item. - Combine "Open release notes" and "Go back to Help" button behaviors with window management handlers. The window management code is gross as ever, and is in desperate need of a refactor, but at least there is no duplicated code for the same behavior + history management. This fixes the "Next" history behavior (however reloading the app while viewing the notes does not load on the notes, but this is a bug for a different PR!). - Added a rule in the history management thingy: if a link we want to add history handling to has an `id`, store that in the state - Added a button to go back to the Help window - Fixed links to releases - Send user to the GitHub issues *list* instead of *new issue form* because if they do not have a GitHub account, they will be redirected to the login page, which is a rather unpleasant experience when you are already confused... - Fixed a bug that would return data about a new release in `latest` even though it is already the `current`. It was showing the current version as "The Lounge v... is now available". - Added https://user-images.githubusercontent.com to the CSP rule when prefetch storage is enabled, because that is where we have stored screenshots in the changelog so far. Meh (we can improve that later if we decide to have a dedicated place for screenshots). - Fetch changelog info even in public mode because users in public mode can access the release notes. They do not see the result of the version checker however.
2017-12-23 03:40:41 +00:00
fetch,
2020-01-17 10:09:42 +00:00
checkForUpdates,
};
export type ChangelogData = {
current: {
version: string;
changelog?: string;
};
expiresAt: number;
latest?: {
prerelease: boolean;
version: string;
url: string;
};
packages?: boolean;
};
const versions = {
current: {
version: `v${pkg.version}`,
changelog: undefined,
},
expiresAt: -1,
latest: undefined,
packages: undefined,
} as ChangelogData;
2019-04-15 16:19:50 +00:00
async function fetch() {
2020-01-02 10:54:47 +00:00
const time = Date.now();
// Serving information from cache
2020-01-02 10:54:47 +00:00
if (versions.expiresAt > time) {
2019-04-15 16:19:50 +00:00
return versions;
}
2019-04-15 16:19:50 +00:00
try {
const response = await got("https://api.github.com/repos/thelounge/thelounge/releases", {
headers: {
2019-07-17 09:33:59 +00:00
Accept: "application/vnd.github.v3.html", // Request rendered markdown
"User-Agent": pkg.name + "; +" + pkg.repository.url, // Identify the client
2019-04-15 16:19:50 +00:00
},
});
if (response.statusCode !== 200) {
return versions;
}
updateVersions(response);
2020-01-02 10:54:47 +00:00
// Add expiration date to the data to send to the client for later refresh
versions.expiresAt = time + TIME_TO_LIVE;
2019-04-15 16:19:50 +00:00
} catch (error) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
2019-04-15 16:19:50 +00:00
log.error(`Failed to fetch changelog: ${error}`);
}
2019-04-15 16:19:50 +00:00
return versions;
}
function updateVersions(response: Response<string>) {
let i: number;
let release: {tag_name: string; body_html: any; prerelease: boolean; html_url: any};
let prerelease = false;
const body = JSON.parse(response.body);
// Find the current release among releases on GitHub
for (i = 0; i < body.length; i++) {
release = body[i];
if (release.tag_name === versions.current.version) {
versions.current.changelog = release.body_html;
prerelease = release.prerelease;
break;
}
}
// Find the latest release made after the current one if there is one
if (i > 0) {
for (let j = 0; j < i; j++) {
release = body[j];
// Find latest release or pre-release if current version is also a pre-release
if (!release.prerelease || release.prerelease === prerelease) {
2020-01-02 10:54:47 +00:00
module.exports.isUpdateAvailable = true;
versions.latest = {
prerelease: release.prerelease,
version: release.tag_name,
url: release.html_url,
};
break;
}
}
}
}
2020-01-17 10:09:42 +00:00
function checkForUpdates(manager: ClientManager) {
fetch()
.then((versionData) => {
if (!module.exports.isUpdateAvailable) {
// Check for updates every 24 hours + random jitter of <3 hours
setTimeout(
() => checkForUpdates(manager),
24 * 3600 * 1000 + Math.floor(Math.random() * 10000000)
);
}
2020-01-17 10:09:42 +00:00
if (!versionData.latest) {
return;
}
2020-01-17 10:09:42 +00:00
log.info(
`The Lounge ${colors.green(
versionData.latest.version
)} is available. Read more on GitHub: ${versionData.latest.url}`
);
// Notify all connected clients about the new version
manager.clients.forEach((client) => client.emit("changelog:newversion"));
})
.catch((error: Error) => {
log.error(`Failed to check for updates: ${error.message}`);
});
2020-01-17 10:09:42 +00:00
}