2017-07-13 18:32:18 +00:00
|
|
|
"use strict";
|
|
|
|
|
2019-04-15 16:19:50 +00:00
|
|
|
const got = require("got");
|
2020-01-17 10:09:42 +00:00
|
|
|
const colors = require("chalk");
|
2019-04-15 16:19:50 +00:00
|
|
|
const log = require("../log");
|
2017-07-13 18:32:18 +00:00
|
|
|
const pkg = require("../../package.json");
|
|
|
|
|
Improve the version checking part of the changelog feature
- There is no client caching of the changelog/version anymore. Instead, server returns the expiration date of its cache, and that is used by the client as well.
- There is now a "Check now" button on the client that appears when data is stale. This means that info is fetched only once and never refreshed (it was refreshed every hour before) unless the user explicitly wants to check latest version, which in turn is as stale as server info is, i.e. 15 minutes max.
- Button style is shared with the "Join a channel" feature, `.btn-small` (not `.btn-sm` to be explicit that this is not a Bootstrap thing).
- Version checker content is now centralized in the `version_checker` template, instead of being partially in the checker template, partially in the Help template, and partially in the code.
- A "Try again" button lets user attempt to fetch info instead of forcing them to reload the page.
- Use Flexbox to display a nicer version checker: icon is slightly bigger, and button is always aligned on the right.
- Changelog logic has been removed from `lounge.js` and moved into the component file.
- Changelog template is only passed what it needs instead of everything the server gives us.
- Public version now displays version checker, since server is caching things.
- Cleaner code overall.
2017-12-24 23:18:24 +00:00
|
|
|
const TIME_TO_LIVE = 15 * 60 * 1000; // 15 minutes, in milliseconds
|
|
|
|
|
2017-07-13 18:32:18 +00:00
|
|
|
module.exports = {
|
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,
|
2017-07-13 18:32:18 +00:00
|
|
|
};
|
|
|
|
|
2017-12-23 18:37:33 +00:00
|
|
|
const versions = {
|
|
|
|
current: {
|
|
|
|
version: `v${pkg.version}`,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-04-15 16:19:50 +00:00
|
|
|
async function fetch() {
|
2020-01-02 10:54:47 +00:00
|
|
|
const time = Date.now();
|
|
|
|
|
2017-12-23 18:37:33 +00:00
|
|
|
// 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;
|
2017-12-23 18:37:33 +00:00
|
|
|
}
|
2017-07-13 18:32:18 +00:00
|
|
|
|
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
|
2019-12-31 23:15:45 +00:00
|
|
|
"User-Agent": pkg.name + "; +" + pkg.repository.url, // Identify the client
|
2019-04-15 16:19:50 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.statusCode !== 200) {
|
|
|
|
return versions;
|
2017-07-13 18:32:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 08:51:47 +00:00
|
|
|
updateVersions(response);
|
Improve the version checking part of the changelog feature
- There is no client caching of the changelog/version anymore. Instead, server returns the expiration date of its cache, and that is used by the client as well.
- There is now a "Check now" button on the client that appears when data is stale. This means that info is fetched only once and never refreshed (it was refreshed every hour before) unless the user explicitly wants to check latest version, which in turn is as stale as server info is, i.e. 15 minutes max.
- Button style is shared with the "Join a channel" feature, `.btn-small` (not `.btn-sm` to be explicit that this is not a Bootstrap thing).
- Version checker content is now centralized in the `version_checker` template, instead of being partially in the checker template, partially in the Help template, and partially in the code.
- A "Try again" button lets user attempt to fetch info instead of forcing them to reload the page.
- Use Flexbox to display a nicer version checker: icon is slightly bigger, and button is always aligned on the right.
- Changelog logic has been removed from `lounge.js` and moved into the component file.
- Changelog template is only passed what it needs instead of everything the server gives us.
- Public version now displays version checker, since server is caching things.
- Cleaner code overall.
2017-12-24 23:18:24 +00:00
|
|
|
|
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) {
|
|
|
|
log.error(`Failed to fetch changelog: ${error}`);
|
|
|
|
}
|
2017-12-23 18:37:33 +00:00
|
|
|
|
2019-04-15 16:19:50 +00:00
|
|
|
return versions;
|
2017-07-13 18:32:18 +00:00
|
|
|
}
|
2019-06-25 08:51:47 +00:00
|
|
|
|
|
|
|
function updateVersions(response) {
|
|
|
|
let i;
|
|
|
|
let release;
|
|
|
|
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;
|
|
|
|
|
2019-06-25 08:51:47 +00:00
|
|
|
versions.latest = {
|
|
|
|
prerelease: release.prerelease,
|
|
|
|
version: release.tag_name,
|
|
|
|
url: release.html_url,
|
|
|
|
};
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-17 10:09:42 +00:00
|
|
|
|
2020-01-17 10:17:37 +00:00
|
|
|
function checkForUpdates(manager) {
|
2020-01-17 10:09:42 +00:00
|
|
|
fetch().then((versionData) => {
|
|
|
|
if (!module.exports.isUpdateAvailable) {
|
|
|
|
// Check for updates every 24 hours + random jitter of <3 hours
|
2020-03-09 15:58:40 +00:00
|
|
|
setTimeout(
|
|
|
|
() => checkForUpdates(manager),
|
|
|
|
24 * 3600 * 1000 + Math.floor(Math.random() * 10000000)
|
|
|
|
);
|
2020-01-17 10:09:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!versionData.latest) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
`The Lounge ${colors.green(
|
|
|
|
versionData.latest.version
|
|
|
|
)} is available. Read more on GitHub: ${versionData.latest.url}`
|
|
|
|
);
|
2020-01-17 10:17:37 +00:00
|
|
|
|
|
|
|
// Notify all connected clients about the new version
|
|
|
|
manager.clients.forEach((client) => client.emit("changelog:newversion"));
|
2020-01-17 10:09:42 +00:00
|
|
|
});
|
|
|
|
}
|