Skip to content

Commit

Permalink
some pushover refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoncalabrese committed Aug 3, 2015
1 parent 4bdb289 commit 702728d
Showing 1 changed file with 39 additions and 36 deletions.
75 changes: 39 additions & 36 deletions lib/plugins/pushover.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,16 @@ var TIME_2_MINS_S = 120
;

function init (env) {
var pushover = { };
var pushoverAPI = null;

var apiToken = env.extendedSettings && env.extendedSettings.pushover && env.extendedSettings.pushover.apiToken;

var userKeys = (env.extendedSettings && env.extendedSettings.pushover &&
env.extendedSettings.pushover.userKey && env.extendedSettings.pushover.userKey.split(' ')) || [];

if (userKeys.length === 0) {
userKeys = (env.extendedSettings && env.extendedSettings.pushover &&
env.extendedSettings.pushover.groupKey && env.extendedSettings.pushover.groupKey.split(' ')) || [];
}

var announcementKeys = (env.extendedSettings && env.extendedSettings.pushover &&
env.extendedSettings.pushover.announcementKey && env.extendedSettings.pushover.announcementKey.split(' ')) || userKeys;

if (apiToken && (userKeys.length > 0 || announcementKeys.length > 0)) {
pushoverAPI = new Pushover({
token: apiToken
});
}

pushover.PRIORITY_NORMAL = 0;
pushover.PRIORITY_EMERGENCY = 2;

pushover.send = function wrapSend (notify, callback) {
var pushover = {
PRIORITY_NORMAL: 0
, PRIORITY_EMERGENCY: 2
};

var selectedKeys = notify.isAnnouncement ? announcementKeys : userKeys;
pushover.send = function wrapSend(notify, callback) {
var selectedKeys = notify.isAnnouncement ? pushoverAPI.announcementKeys : pushoverAPI.userKeys;

var msg = {
expire: TIME_15_MINS_S
, title: notify.title
, message: notify.message
, sound: notify.pushoverSound || 'gamelan'
, timestamp: new Date()
expire: TIME_15_MINS_S, title: notify.title, message: notify.message, sound: notify.pushoverSound || 'gamelan', timestamp: new Date()
//USE PUSHOVER_EMERGENCY for WARN and URGENT so we get the acks
, priority: notify.level >= levels.WARN ? pushover.PRIORITY_EMERGENCY : pushover.PRIORITY_NORMAL
};
Expand All @@ -64,7 +39,7 @@ function init (env) {

};

pushover.sendAPIRequest = function sendAPIRequest (msg, callback) {
pushover.sendAPIRequest = function sendAPIRequest(msg, callback) {
pushoverAPI.send(msg, function (err, result) {
if (err) {
console.error('unable to send pushover notification', err);
Expand All @@ -75,17 +50,18 @@ function init (env) {
});
};

pushover.cancelWithReceipt = function cancelWithReceipt (receipt, callback) {
pushover.cancelWithReceipt = function cancelWithReceipt(receipt, callback) {
request
.get('https://api.pushover.net/1/receipts/' + receipt + '/cancel.json?token=' + apiToken)
.on('response', function(response) {
.on('response', function (response) {
callback(null, response);
})
.on('error', function(err) {
.on('error', function (err) {
callback(err);
});
};

var pushoverAPI = setupPushover(env);
if (pushoverAPI) {
console.info('Pushover is ready to push');
return pushover;
Expand All @@ -95,4 +71,31 @@ function init (env) {
}
}

function setupPushover(env) {
var apiToken = env.extendedSettings && env.extendedSettings.pushover && env.extendedSettings.pushover.apiToken;

var userKeys = (env.extendedSettings && env.extendedSettings.pushover &&
env.extendedSettings.pushover.userKey && env.extendedSettings.pushover.userKey.split(' ')) || [];

if (userKeys.length === 0) {
userKeys = (env.extendedSettings && env.extendedSettings.pushover &&
env.extendedSettings.pushover.groupKey && env.extendedSettings.pushover.groupKey.split(' ')) || [];
}

var announcementKeys = (env.extendedSettings && env.extendedSettings.pushover &&
env.extendedSettings.pushover.announcementKey && env.extendedSettings.pushover.announcementKey.split(' ')) || userKeys;

if (apiToken && (userKeys.length > 0 || announcementKeys.length > 0)) {
var pushoverAPI = new Pushover({
token: apiToken
});

pushoverAPI.userKeys = userKeys;
pushoverAPI.announcementKeys = announcementKeys;

return pushoverAPI;
}
}


module.exports = init;

0 comments on commit 702728d

Please sign in to comment.