From 98df0f26e7999cd7cc1dce47a3f5fa1ecf8b962d Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Fri, 9 Sep 2016 20:12:17 +0200 Subject: [PATCH] add notifications, fix too many seen requests and misc fixes --- common/locales/en/groups.json | 4 ++- website/client/js/controllers/chatCtrl.js | 3 -- website/client/js/controllers/partyCtrl.js | 10 +++--- .../js/services/notificationServices.js | 14 +++++--- website/client/js/services/pusherService.js | 33 +++++++++++++++---- 5 files changed, 46 insertions(+), 18 deletions(-) diff --git a/common/locales/en/groups.json b/common/locales/en/groups.json index 4ca12b3cd6..a8aed20de8 100644 --- a/common/locales/en/groups.json +++ b/common/locales/en/groups.json @@ -194,5 +194,7 @@ "canOnlyInviteMaxInvites": "You can only invite \"<%= maxInvites %>\" at a time", "onlyCreatorOrAdminCanDeleteChat": "Not authorized to delete this message!", "onlyGroupLeaderCanEditTasks": "Not authorized to manage tasks!", - "onlyGroupTasksCanBeAssigned": "Only group tasks can be assigned" + "onlyGroupTasksCanBeAssigned": "Only group tasks can be assigned", + "newChatMessagePlainNotification": "New message in <%= groupName %> by <%= authorName %>. Click here to open the chat page!", + "newChatMessageTitle": "New message in <%= groupName %>" } diff --git a/website/client/js/controllers/chatCtrl.js b/website/client/js/controllers/chatCtrl.js index 976d5d4142..649a06b9a7 100644 --- a/website/client/js/controllers/chatCtrl.js +++ b/website/client/js/controllers/chatCtrl.js @@ -2,9 +2,6 @@ habitrpg.controller('ChatCtrl', ['$scope', 'Groups', 'Chat', 'User', '$http', 'ApiUrl', 'Notification', 'Members', '$rootScope', 'Analytics', function($scope, Groups, Chat, User, $http, ApiUrl, Notification, Members, $rootScope, Analytics){ - if ($scope.group) { - Chat.markChatSeen($scope.group.id); - } $scope.message = {content:''}; $scope._sending = false; diff --git a/website/client/js/controllers/partyCtrl.js b/website/client/js/controllers/partyCtrl.js index ec170ab0b5..615b8c8834 100644 --- a/website/client/js/controllers/partyCtrl.js +++ b/website/client/js/controllers/partyCtrl.js @@ -24,6 +24,12 @@ habitrpg.controller("PartyCtrl", ['$rootScope','$scope','Groups','Chat','User',' $scope.group = $rootScope.party; $scope.group.loadingParty = false; checkForNotifications(); + if ($state.is('options.social.party')) { + if ('Notification' in window && window.Notification.permission === 'default') { + window.Notification.requestPermission(); + } + Chat.markChatSeen($scope.group._id); + } } function handlePartyError (response) { @@ -52,10 +58,6 @@ habitrpg.controller("PartyCtrl", ['$rootScope','$scope','Groups','Chat','User',' } } - if ($scope.group && $scope.group._id) { - Chat.markChatSeen($scope.group._id); - } - $scope.create = function(group) { group.loadingParty = true; diff --git a/website/client/js/services/notificationServices.js b/website/client/js/services/notificationServices.js index b6b854cde1..3730910cef 100644 --- a/website/client/js/services/notificationServices.js +++ b/website/client/js/services/notificationServices.js @@ -93,9 +93,9 @@ angular.module("habitrpg").factory("Notification", _notify(window.env.t('streakName') + ': ' + val, 'streak', 'glyphicon glyphicon-repeat'); } - function text(val){ + function text(val, onClick){ if (val) { - _notify(val, 'info'); + _notify(val, 'info', null, null, onClick); } } @@ -114,7 +114,7 @@ angular.module("habitrpg").factory("Notification", // Used to stack notifications, must be outside of _notify var stack_topright = {"dir1": "down", "dir2": "left", "spacing1": 15, "spacing2": 15, "firstpos1": 60}; - function _notify(html, type, icon, canHide) { + function _notify(html, type, icon, canHide, onClick) { var notice = $.pnotify({ type: type || 'warning', //('info', 'text', 'warning', 'success', 'gp', 'xp', 'hp', 'lvl', 'death', 'mp', 'crit') text: html, @@ -126,7 +126,13 @@ angular.module("habitrpg").factory("Notification", width: "250px", stack: stack_topright, icon: icon || false - }).click(function() { notice.pnotify_remove() }); + }).click(function() { + notice.pnotify_remove(); + + if (onClick) { + onClick(); + } + }); } return { diff --git a/website/client/js/services/pusherService.js b/website/client/js/services/pusherService.js index 491a99263b..ba0b439cc9 100644 --- a/website/client/js/services/pusherService.js +++ b/website/client/js/services/pusherService.js @@ -1,8 +1,8 @@ 'use strict'; angular.module('habitrpg') -.factory('Pusher', ['$rootScope', 'STORAGE_SETTINGS_ID', 'Groups', 'Shared', '$state', 'Chat', - function($rootScope, STORAGE_SETTINGS_ID, Groups, Shared, $state, Chat) { +.factory('Pusher', ['$rootScope', 'STORAGE_SETTINGS_ID', 'Groups', 'Shared', '$state', 'Chat', 'Notification', + function($rootScope, STORAGE_SETTINGS_ID, Groups, Shared, $state, Chat, Notification) { var settings = JSON.parse(localStorage.getItem(STORAGE_SETTINGS_ID)); var IS_PUSHER_ENABLED = window.env['PUSHER:ENABLED'] === 'true'; @@ -128,29 +128,50 @@ angular.module('habitrpg') }); // When a new chat message is posted - partyChannel.bind('new-chat', function (data) { + partyChannel.bind('new-chat', function (chatData) { Groups.party().then(function () { // wait for the party to be fully loaded $rootScope.loadingParty = false; // make sure the party is set as loaded // Update the party data - Groups.data.party.chat.unshift(data); + Groups.data.party.chat.unshift(chatData); Groups.data.party.chat.splice(200); // If a system message comes in, sync the party as quest status may have changed - if (data.uuid === 'system') { + if (chatData.uuid === 'system') { Groups.party(true).then(function (syncedParty) { // Assign and not replace so that all the references get the modifications _.assign($rootScope.party, syncedParty); }); } - if ($state.is('options.social.party')) { // if we're on the party page, mark the chat as read + if ($state.is('options.social.party') && document.hasFocus()) { // if we're on the party page, mark the chat as read Chat.markChatSeen($rootScope.party._id); } else { // show a notification $rootScope.User.user.newMessages[$rootScope.party._id] = { name: $rootScope.party.name, value: true, }; + + if ('Notification' in window && window.Notification.permission === 'granted') { + var notif = new window.Notification(env.t('newChatMessageTitle', { + groupName: $rootScope.party.name, + }), { + body: (chatData.user || chatData.uuid) + ': ' + chatData.text, + icon: '/favicon_192x192-00993687.png?v=4' + }); + + notif.addEventListener('click', function () { + $state.go('options.social.party'); + notif.close(); + }); + } else { + Notification.text(env.t('newChatMessagePlainNotification', { + groupName: $rootScope.party.name, + authorName: chatData.user || chatData.uuid, + }), function() { + $state.go('options.social.party'); + }); + } } }); });