Skip to content

Commit

Permalink
added desktop notifications, close #18
Browse files Browse the repository at this point in the history
  • Loading branch information
kraiz committed Mar 31, 2015
1 parent 6cc3105 commit a6ba347
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<script src="js/directives.js"></script>
<script src="js/services/rpc.js"></script>
<script src="js/services/update.js"></script>
<script src="js/services/notify.js"></script>
<script src="js/controllers/main.js"></script>
<script src="js/controllers/hubs.js"></script>
<script src="js/controllers/browse.js"></script>
Expand Down
3 changes: 2 additions & 1 deletion app/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

var EiskaltApp = angular.module('EiskaltApp', ['ngRoute', 'ngStorage', 'ngSanitize', 'luegg.directives', 'ui.bootstrap',
'angularBootstrapNavTree', 'UpdateCheck', 'EiskaltRPC', 'EiskaltFilters',
'EiskaltDirectives']);
'EiskaltDirectives', 'DesktopNotification']);

EiskaltApp.value('settings', {
version: '0.5.2',
localStorageVersion: 2,
updateUrl: 'https://api.github.com/repos/kraiz/icecult/releases',
chatMessagesKept: 250,
notificationTimeout: 5000,
refresh: {
hashAndRatio: 5000,
chat: 3000,
Expand Down
7 changes: 5 additions & 2 deletions app/js/controllers/hubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ EiskaltApp.controller('HubsCtrl', function ($scope, EiskaltRPC) {
};
});

EiskaltApp.controller('HubCtrl', function ($scope, $interval, $localStorage, settings, EiskaltRPC) {
EiskaltApp.controller('HubCtrl', function ($scope, $interval, $localStorage, settings, EiskaltRPC, DesktopNotification) {
EiskaltRPC.GetHubUserList($scope.hub.huburl).success(function (users) {
$scope.users = [];
angular.forEach(users, function (user) {
Expand All @@ -40,7 +40,10 @@ EiskaltApp.controller('HubCtrl', function ($scope, $interval, $localStorage, set
}
$scope.refreshChat = function () {
EiskaltRPC.GetChatPub($scope.hub.huburl).success(function (messages) {
Array.prototype.push.apply($scope.$storage.chatlog[$scope.hub.huburl], messages);
if (messages.length > 0) {
DesktopNotification.notifyChat($scope.hub, messages);
Array.prototype.push.apply($scope.$storage.chatlog[$scope.hub.huburl], messages);
}
});
};
$scope.refreshChat();
Expand Down
43 changes: 43 additions & 0 deletions app/js/services/notify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

angular.module('DesktopNotification', []).factory('DesktopNotification', function ($window, settings) {

var notify = function (title, text) {
// no support
if (!("Notification" in window)) return;
// request permission if not granted nor denied
if (Notification.permission !== "granted" && Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
notify(title, text);
});
return;
}
// if granted, notify!
if (Notification.permission === "granted") {
var notification = new Notification(title, {body: text});
notification.onclick = function() {
$window.focus();
};
notification.onshow = function() {
$window.setTimeout(function() {
notification.close()
}, settings.notificationTimeout);
};
}
};

var notifyChat = function(hub, messages) {
if (document.hidden) {
notify(
hub.hubname,
messages.map(function(msg) {
return msg.nick + ': ' + msg.text
}).join('\n')
);
}
};

return {
notifyChat: notifyChat
}
});

0 comments on commit a6ba347

Please sign in to comment.