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) (#249)

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 <[email protected]>
  • Loading branch information
digitaldan authored Mar 17, 2019
1 parent 6302628 commit 639fdad
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 @@ -663,66 +663,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 639fdad

Please sign in to comment.