Skip to content

Commit

Permalink
Add missing return statements to guard statement in notification send (
Browse files Browse the repository at this point in the history
…#188)

Also replace nested if/else with better readable guard statements in
notification socket handlers.

This may be related to #184

Signed-off-by: Florian Schmidt <florian.schmidt.welzow@t-online.de>
  • Loading branch information
FlorianSW authored and digitaldan committed Apr 22, 2018
1 parent fe52a5a commit b58a564
Showing 1 changed file with 39 additions and 34 deletions.
73 changes: 39 additions & 34 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,66 +682,71 @@ io.sockets.on('connection', function (socket) {
});

socket.on('broadcastnotification', function (data) {
var self = this;
Openhab.findById(self.openhabId, function (error, openhab) {
Openhab.findById(this.openhabId, function (error, openhab) {
if (error) {
logger.error('openHAB-cloud: openHAB lookup error: ' + error);
return;
}
if (!openhab) {
logger.debug('openHAB-cloud: openHAB not found');
return;
}

User.find({
account: openhab.account
}, function (error, users) {
if (!error && users) {
for (var i = 0; i < users.length; i++) {
sendNotificationToUser(users[i], data.message, data.icon, data.severity);
}
} else {
if (error) {
logger.error('openHAB-cloud: Error getting users list: ' + error);
} else {
logger.debug('openHAB-cloud: No users found for openHAB');
}
if (error) {
logger.error('openHAB-cloud: Error getting users list: ' + error);
return;
}

if (!users) {
logger.debug('openHAB-cloud: No users found for openHAB');
return;
}

for (var i = 0; i < users.length; i++) {
sendNotificationToUser(users[i], data.message, data.icon, data.severity);
}
});
});
});

socket.on('lognotification', function (data) {
var self = this;
Openhab.findById(self.openhabId, function (error, openhab) {
Openhab.findById(this.openhabId, function (error, openhab) {
if (error) {
logger.error('openHAB lookup error: ' + error);
return;
}
if (!openhab) {
logger.debug('openHAB not found');
return;
}
User.find({
account: openhab.account
}, function (error, users) {
if (!error && users) {
for (var i = 0; i < users.length; i++) {
newNotification = new Notification({
user: users[i].id,
message: data.message,
icon: data.icon,
severity: data.severity
});
newNotification.save(function (error) {
if (error) {
logger.error('Error saving notification: ' + error);
}
});
}
} else {
if (error) {
logger.error('Error getting users list: ' + error);
} else {
logger.debug('No users found for openhab');
}
if (error) {
logger.error('openHAB-cloud: Error getting users list: ' + error);
return;
}

if (!users) {
logger.debug('openHAB-cloud: No users found for openHAB');
return;
}

for (var i = 0; i < users.length; i++) {
newNotification = new Notification({
user: users[i].id,
message: data.message,
icon: data.icon,
severity: data.severity
});
newNotification.save(function (error) {
if (error) {
logger.error('Error saving notification: ' + error);
}
});
}
});
});
Expand Down

0 comments on commit b58a564

Please sign in to comment.