mirror of
https://github.com/bckelley/cgm-remote-monitor.git
synced 2026-08-24 03:14:12 -05:00
* Add ADMIN_NOTIFIES_ENABLED feature flag * Skip setting language code if language value is not null
58 lines
1.4 KiB
JavaScript
58 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
const _ = require('lodash');
|
|
|
|
function init (ctx) {
|
|
|
|
const adminnotifies = {};
|
|
|
|
adminnotifies.addNotify = function addnotify (notify) {
|
|
if (!ctx.settings.adminNotifiesEnabled) {
|
|
console.log('Admin notifies disabled, skipping notify', notify);
|
|
return;
|
|
}
|
|
|
|
if (!notify) return;
|
|
|
|
notify.title = notify.title || 'No title';
|
|
notify.message = notify.message || 'No message';
|
|
|
|
const existingMessage = _.find(adminnotifies.notifies, function findExisting (obj) {
|
|
return obj.message == notify.message;
|
|
});
|
|
|
|
if (existingMessage) {
|
|
existingMessage.count += 1;
|
|
existingMessage.lastRecorded = Date.now();
|
|
} else {
|
|
notify.count = 1;
|
|
notify.lastRecorded = Date.now();
|
|
adminnotifies.notifies.push(notify);
|
|
}
|
|
}
|
|
|
|
adminnotifies.getNotifies = function getNotifies () {
|
|
return adminnotifies.notifies;
|
|
}
|
|
|
|
ctx.bus.on('admin-notify', adminnotifies.addNotify);
|
|
|
|
adminnotifies.clean = function cleanNotifies () {
|
|
adminnotifies.notifies = _.filter(adminnotifies.notifies, function findExisting (obj) {
|
|
return obj.persistent || ((Date.now() - obj.lastRecorded) < 1000 * 60 * 60 * 12);
|
|
});
|
|
}
|
|
|
|
adminnotifies.cleanAll = function cleanAll() {
|
|
adminnotifies.notifies = [];
|
|
}
|
|
|
|
adminnotifies.cleanAll();
|
|
|
|
ctx.bus.on('tick', adminnotifies.clean);
|
|
|
|
return adminnotifies;
|
|
}
|
|
|
|
module.exports = init;
|