Skip to content

Commit

Permalink
Fix GladysAssistant#301 : Gladys now send a notification when a modul…
Browse files Browse the repository at this point in the history
…e is updated
  • Loading branch information
Pierre-Gilles committed Oct 12, 2018
1 parent 999c30d commit 43e6d12
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 2 deletions.
3 changes: 2 additions & 1 deletion api/core/module/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ module.exports.install = require('./module.install.js');
module.exports.getById = require('./module.getById.js');
module.exports.uninstall = require('./module.uninstall.js');
module.exports.update = require('./module.update.js');
module.exports.upgrade = require('./module.upgrade.js');
module.exports.upgrade = require('./module.upgrade.js');
module.exports.checkUpdate = require('./module.checkUpdate.js');
52 changes: 52 additions & 0 deletions api/core/module/module.checkUpdate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const semver = require('semver')
const Promise = require('bluebird');

module.exports = function checkUpdate() {

// get the list of modules installed
return gladys.module.get()
.then((modulesInstalled) => {
return Promise.map(modulesInstalled, (moduleInstalled) => {

return gladys.utils.request({
method: 'GET',
url: sails.config.update.getModuleBySlugUrl + moduleInstalled.slug
})
.then((moduleOnStore) => {
if (semver.valid(moduleOnStore.version) && semver.valid(moduleInstalled.version)) {
if (semver.gt(moduleOnStore.version, moduleInstalled.version) === true) {
return sendNotificationAdmins(moduleOnStore, moduleInstalled);
}
}
})
}, { concurrency: 1 });
});
};

function sendNotificationAdmins(moduleOnStore, moduleInstalled) {
return gladys.user.getAdmin()
.then((admins) => {
return Promise.map(admins, (admin) => {

// we create a new notification
var notification = {
title: sails.__({
phrase: 'notification-update-available-title',
locale: admin.language.substr(0, 2)
}) + ` ${moduleOnStore.version}`,
text: `${moduleInstalled.name} ` + sails.__({
phrase: 'notification-update-module-available-text',
locale: admin.language.substr(0, 2)
}) + ` ${moduleOnStore.version}`,
icon: 'fa fa-cloud-download',
iconColor: 'blue',
link: '/dashboard/module',
priority: 0,
user: admin.id,
};

// and send it only if we hadn't done it before
return gladys.notification.createIfNotExist(notification);
});
});
}
4 changes: 4 additions & 0 deletions api/core/task/task.init.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ module.exports = function(cb){
// fully installed
gladys.module.init();

// checking for update on module now
gladys.module.checkUpdate();

// Check for update interval
setInterval(function(){
gladys.update.checkUpdate();
gladys.module.checkUpdate();
}, sails.config.update.checkUpdateInterval);

};
1 change: 1 addition & 0 deletions config/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"device-create-button": "Create",
"notification-update-available-title": "Update available !",
"notification-update-available-text": "New version of Gladys :",
"notification-update-module-available-text": "can be updated on version",
"installation-step": "Step",
"installation-create-account": "Create account",
"installation-configuration": "Configuration",
Expand Down
1 change: 1 addition & 0 deletions config/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
"device-create-button": "Créer",
"notification-update-available-title": "Mise à jour disponible !",
"notification-update-available-text": "Nouvelle version de Gladys :",
"notification-update-module-available-text": "peut être mis à jour en version",
"installation-step": "Etape",
"installation-create-account": "Création de votre compte",
"installation-configuration": "Configuration",
Expand Down
1 change: 1 addition & 0 deletions config/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports.update = {
categoryBaseUrl: 'https://raw.githubusercontent.com/GladysProject/gladys-data/master/categories/',
stateBaseUrl: 'https://raw.githubusercontent.com/GladysProject/gladys-data/master/states/',
getLastVersionUrl'https://developer.gladysproject.com/api/gladys/version',
getModuleBySlugUrl: 'https://developer.gladysproject.com/api/v1/modules/',
icon: 'fa fa-refresh',
iconColor: 'bg-light-blue',
link: '/dashboard/parameters',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"sails-util-mvcsloader": "^0.4.0",
"serialization": "^0.2.0",
"suncalc": "^1.8.0",
"uuid": "^3.1.0"
"uuid": "^3.1.0",
"semver": "^5.6.0"
},
"devDependencies": {
"documentation-flat-theme": "^0.2.1",
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6952,6 +6952,10 @@ semver@^5.3.0:
version "5.5.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.1.tgz#7dfdd8814bdb7cabc7be0fb1d734cfb66c940477"

semver@^5.6.0:
version "5.6.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.6.0.tgz#7e74256fbaa49c75aa7c7a205cc22799cac80004"

semver@~5.3.0:
version "5.3.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f"
Expand Down

0 comments on commit 43e6d12

Please sign in to comment.