Cache changelog information on the server for 15 minutes
This commit is contained in:
parent
9691df67e3
commit
57c24704af
@ -7,12 +7,18 @@ module.exports = {
|
|||||||
fetch,
|
fetch,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const versions = {
|
||||||
|
current: {
|
||||||
|
version: `v${pkg.version}`,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
function fetch(callback) {
|
function fetch(callback) {
|
||||||
const changelog = {
|
// Serving information from cache
|
||||||
current: {
|
if (versions.current.changelog) {
|
||||||
version: `v${pkg.version}`,
|
callback(versions);
|
||||||
},
|
return;
|
||||||
};
|
}
|
||||||
|
|
||||||
request.get({
|
request.get({
|
||||||
uri: "https://api.github.com/repos/thelounge/lounge/releases",
|
uri: "https://api.github.com/repos/thelounge/lounge/releases",
|
||||||
@ -22,8 +28,7 @@ function fetch(callback) {
|
|||||||
},
|
},
|
||||||
}, (error, response, body) => {
|
}, (error, response, body) => {
|
||||||
if (error || response.statusCode !== 200) {
|
if (error || response.statusCode !== 200) {
|
||||||
callback(changelog);
|
callback(versions);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,8 +41,8 @@ function fetch(callback) {
|
|||||||
// Find the current release among releases on GitHub
|
// Find the current release among releases on GitHub
|
||||||
for (i = 0; i < body.length; i++) {
|
for (i = 0; i < body.length; i++) {
|
||||||
release = body[i];
|
release = body[i];
|
||||||
if (release.tag_name === changelog.current.version) {
|
if (release.tag_name === versions.current.version) {
|
||||||
changelog.current.changelog = release.body_html;
|
versions.current.changelog = release.body_html;
|
||||||
prerelease = release.prerelease;
|
prerelease = release.prerelease;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@ -51,7 +56,7 @@ function fetch(callback) {
|
|||||||
|
|
||||||
// Find latest release or pre-release if current version is also a pre-release
|
// Find latest release or pre-release if current version is also a pre-release
|
||||||
if (!release.prerelease || release.prerelease === prerelease) {
|
if (!release.prerelease || release.prerelease === prerelease) {
|
||||||
changelog.latest = {
|
versions.latest = {
|
||||||
prerelease: release.prerelease,
|
prerelease: release.prerelease,
|
||||||
version: release.tag_name,
|
version: release.tag_name,
|
||||||
url: release.html_url,
|
url: release.html_url,
|
||||||
@ -62,6 +67,13 @@ function fetch(callback) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(changelog);
|
// Emptying cached information after 15 minutes
|
||||||
|
setTimeout(() => {
|
||||||
|
delete versions.current.changelog;
|
||||||
|
delete versions.latest;
|
||||||
|
}, 15 * 60 * 1000
|
||||||
|
);
|
||||||
|
|
||||||
|
callback(versions);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user