thelounge/src/plugins/changelog.js
Jérémie Astori 238e894377
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-25 17:44:53 -05:00

84 lines
1.9 KiB
JavaScript

"use strict";
const pkg = require("../../package.json");
const request = require("request");
const TIME_TO_LIVE = 15 * 60 * 1000; // 15 minutes, in milliseconds
module.exports = {
fetch,
};
const versions = {
current: {
version: `v${pkg.version}`,
},
};
function fetch(callback) {
// Serving information from cache
if (versions.current.changelog) {
callback(versions);
return;
}
request.get({
uri: "https://api.github.com/repos/thelounge/lounge/releases",
headers: {
Accept: "application/vnd.github.v3.html", // Request rendered markdown
"User-Agent": pkg.name + "; +" + pkg.repository.git, // Identify the client
},
}, (error, response, body) => {
if (error || response.statusCode !== 200) {
callback(versions);
return;
}
let i;
let release;
let prerelease = false;
body = JSON.parse(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) {
versions.latest = {
prerelease: release.prerelease,
version: release.tag_name,
url: release.html_url,
};
break;
}
}
}
// Add expiration date to the data to send to the client for later refresh
versions.expiresAt = Date.now() + TIME_TO_LIVE;
// Emptying cached information after reaching said expiration date
setTimeout(() => {
delete versions.current.changelog;
delete versions.latest;
}, TIME_TO_LIVE);
callback(versions);
});
}