From 5a196c27e2b324b8535817fcd712cf16b4de9b8d Mon Sep 17 00:00:00 2001
From: blavenie <benoit.lavenier@e-is.pro>
Date: Tue, 27 Nov 2018 15:18:05 +0100
Subject: [PATCH] [fix] Settings: Disable HTML5 notification (by default) - fix
 #771

---
 www/js/services/wallet-services.js            |  2 +-
 www/plugins/es/i18n/locale-en-GB.json         |  4 +-
 www/plugins/es/i18n/locale-en.json            |  4 +-
 www/plugins/es/i18n/locale-fr-FR.json         |  4 +-
 .../controllers/notification-controllers.js   | 12 ++++++
 .../es/js/controllers/settings-controllers.js | 11 ++++++
 .../es/js/controllers/wallet-controllers.js   | 10 +++++
 .../es/js/services/notification-services.js   | 13 ++++---
 .../es/js/services/settings-services.js       | 10 ++++-
 www/plugins/es/js/services/tx-services.js     | 37 +++++++++++++++++--
 .../templates/settings/plugin_settings.html   | 17 ++++++++-
 .../templates/wallet/item_wallet_extend.html  | 14 +++++++
 www/templates/wallet/list/item_wallet.html    | 21 ++---------
 13 files changed, 126 insertions(+), 33 deletions(-)
 create mode 100644 www/plugins/es/templates/wallet/item_wallet_extend.html

diff --git a/www/js/services/wallet-services.js b/www/js/services/wallet-services.js
index 6a140651..7380e15b 100644
--- a/www/js/services/wallet-services.js
+++ b/www/js/services/wallet-services.js
@@ -508,7 +508,7 @@ angular.module('cesium.wallet.services', ['ngApi', 'ngFileSaver', 'cesium.bma.se
       }
 
       // Encryption is enable, but user not auth: use the session storage
-      // (and keep the localstorage value)
+      // (and keep the local storage value)
       if (!isAuth()) return sessionStorage.put(storageKey, contentStr||null);
 
       return $q.all([
diff --git a/www/plugins/es/i18n/locale-en-GB.json b/www/plugins/es/i18n/locale-en-GB.json
index 77ba1fac..d6f5fc10 100644
--- a/www/plugins/es/i18n/locale-en-GB.json
+++ b/www/plugins/es/i18n/locale-en-GB.json
@@ -456,7 +456,9 @@
       "ENABLE_TX_SENT": "Notify the validation of <b>sent payments</b>?",
       "ENABLE_TX_RECEIVED": "Notify the validation of <b>received payments</b>?",
       "ENABLE_CERT_SENT": "Notify the validation of <b>sent certifications</b>?",
-      "ENABLE_CERT_RECEIVED": "Notify the validation of <b>received certifications</b>?"
+      "ENABLE_CERT_RECEIVED": "Notify the validation of <b>received certifications</b>?",
+      "ENABLE_HTML5_NOTIFICATION": "Warn with each new notification?",
+      "ENABLE_HTML5_NOTIFICATION_HELP": "Opens a small popup window with each new notification."
     },
     "CONFIRM": {
       "ASK_ENABLE_TITLE": "Optional features",
diff --git a/www/plugins/es/i18n/locale-en.json b/www/plugins/es/i18n/locale-en.json
index 77ba1fac..d6f5fc10 100644
--- a/www/plugins/es/i18n/locale-en.json
+++ b/www/plugins/es/i18n/locale-en.json
@@ -456,7 +456,9 @@
       "ENABLE_TX_SENT": "Notify the validation of <b>sent payments</b>?",
       "ENABLE_TX_RECEIVED": "Notify the validation of <b>received payments</b>?",
       "ENABLE_CERT_SENT": "Notify the validation of <b>sent certifications</b>?",
-      "ENABLE_CERT_RECEIVED": "Notify the validation of <b>received certifications</b>?"
+      "ENABLE_CERT_RECEIVED": "Notify the validation of <b>received certifications</b>?",
+      "ENABLE_HTML5_NOTIFICATION": "Warn with each new notification?",
+      "ENABLE_HTML5_NOTIFICATION_HELP": "Opens a small popup window with each new notification."
     },
     "CONFIRM": {
       "ASK_ENABLE_TITLE": "Optional features",
diff --git a/www/plugins/es/i18n/locale-fr-FR.json b/www/plugins/es/i18n/locale-fr-FR.json
index c7b150f5..ad238519 100644
--- a/www/plugins/es/i18n/locale-fr-FR.json
+++ b/www/plugins/es/i18n/locale-fr-FR.json
@@ -507,7 +507,9 @@
       "ENABLE_TX_SENT": "Notifier les <b>paiement émis</b> ?",
       "ENABLE_TX_RECEIVED": "Notifier les <b>paiements reçus</b> ?",
       "ENABLE_CERT_SENT": "Notifier les <b>certifications émises</b> ?",
-      "ENABLE_CERT_RECEIVED": "Notifier les <b>certifications reçues</b> ?"
+      "ENABLE_CERT_RECEIVED": "Notifier les <b>certifications reçues</b> ?",
+      "ENABLE_HTML5_NOTIFICATION": "Avertir à chaque nouvelle notification ?",
+      "ENABLE_HTML5_NOTIFICATION_HELP": "Ouvre une petite fenetre à chaque nouvelle notification."
     },
     "CONFIRM": {
       "ASK_ENABLE_TITLE": "Fonctionnalités optionnelles",
diff --git a/www/plugins/es/js/controllers/notification-controllers.js b/www/plugins/es/js/controllers/notification-controllers.js
index a225e44b..424b3b99 100644
--- a/www/plugins/es/js/controllers/notification-controllers.js
+++ b/www/plugins/es/js/controllers/notification-controllers.js
@@ -19,6 +19,18 @@ angular.module('cesium.es.notification.controllers', ['cesium.es.services'])
           login: true
         }
       })
+      .state('app.view_notifications_by_id', {
+        url: "/wallets/:id/notifications",
+        views: {
+          'menuContent': {
+            templateUrl: "plugins/es/templates/notification/view_notifications.html",
+            controller: 'NotificationsCtrl'
+          }
+        },
+        data: {
+          login: true
+        }
+      })
     ;
   })
 
diff --git a/www/plugins/es/js/controllers/settings-controllers.js b/www/plugins/es/js/controllers/settings-controllers.js
index 1e268700..6723dd30 100644
--- a/www/plugins/es/js/controllers/settings-controllers.js
+++ b/www/plugins/es/js/controllers/settings-controllers.js
@@ -227,6 +227,15 @@ function ESPluginSettingsController ($scope, $q,  $translate, $ionicPopup, UIUti
   $scope.onFormChanged = function() {
     if ($scope.loading) return;
 
+    if ($scope.formData.notifications.emitHtml5 !== (!!("Notification" in window) && Notification.permission === "granted")){
+      Notification.requestPermission(function (permission) {
+        // If the user accepts, let's create a notification
+        $scope.formData.notifications.emitHtml5 = (permission === "granted"); // revert to false if permission not granted
+        $scope.onFormChanged(); // Loop
+      });
+      return;
+    }
+
     $scope.loading = true;
     csSettings.data.plugins = csSettings.data.plugins || {};
     csSettings.data.plugins.es = csSettings.data.plugins.es ?
@@ -236,6 +245,8 @@ function ESPluginSettingsController ($scope, $q,  $translate, $ionicPopup, UIUti
     // Fix old settings (unused)
     delete csSettings.data.plugins.es.newNode;
 
+
+
     csSettings.store()
       .then(function() {
         $scope.loading = false;
diff --git a/www/plugins/es/js/controllers/wallet-controllers.js b/www/plugins/es/js/controllers/wallet-controllers.js
index 395f1c1c..ad1ba4bf 100644
--- a/www/plugins/es/js/controllers/wallet-controllers.js
+++ b/www/plugins/es/js/controllers/wallet-controllers.js
@@ -14,6 +14,16 @@ angular.module('cesium.es.wallet.controllers', ['cesium.es.services'])
             }
           }
         })
+
+        .extendState('app.view_wallets', {
+          points: {
+            'item-wallet': {
+              templateUrl: "plugins/es/templates/wallet/item_wallet_extend.html",
+              controller: 'ESExtensionCtrl'
+            }
+          }
+        })
+
       ;
     }
 
diff --git a/www/plugins/es/js/services/notification-services.js b/www/plugins/es/js/services/notification-services.js
index ae9b422e..f3b1634a 100644
--- a/www/plugins/es/js/services/notification-services.js
+++ b/www/plugins/es/js/services/notification-services.js
@@ -11,7 +11,7 @@ angular.module('cesium.es.notification.services', ['cesium.platform', 'cesium.es
   })
 
 .factory('esNotification', function($rootScope, $q, $timeout, $translate, $state, csHttp,
-                                    esHttp, csConfig, csSettings, csWallet, csWot, UIUtils, filterTranslations,
+                                    csConfig, csSettings, esHttp, esSettings, csWallet, csWot, UIUtils, filterTranslations,
                                     BMA, CryptoUtils, csPlatform, Api) {
   'ngInject';
 
@@ -197,7 +197,7 @@ angular.module('cesium.es.notification.services', ['cesium.platform', 'cesium.es
         }
       })
       .then(function() {
-        return emitEsNotification(notification);
+        if (esSettings.notifications.isEmitHtml5Enable()) return emitEsNotification(notification);
       });
   }
 
@@ -326,7 +326,8 @@ angular.module('cesium.es.notification.services', ['cesium.platform', 'cesium.es
       return deferred.promise;
     }
 
-    console.debug('[ES] [notification] Loading count...');
+    var isDefaultWallet =  csWallet.isUserPubkey(data.pubkey);
+    console.debug('[ES] [notification] Loading count...' + data.pubkey.substr(0,8));
 
     // Load unread notifications count
     loadUnreadNotificationsCount(
@@ -339,14 +340,14 @@ angular.module('cesium.es.notification.services', ['cesium.platform', 'cesium.es
         data.notifications.unreadCount = unreadCount;
         data.notifications.warnCount = countWarnEvents(data);
 
-        // Emit HTML5 notification
-        if (unreadCount > 0) {
+        // Emit HTML5 notification (only on main wallet)
+        if (unreadCount > 0 && esSettings.notifications.isEmitHtml5Enable() && isDefaultWallet) {
           $timeout(function() {
             emitEsNotification({
               message: 'COMMON.NOTIFICATION.HAS_UNREAD',
               count: unreadCount,
               state: 'app.view_notifications'
-            }, 'COMMON.APP_NAME');
+            }, data.ui || data.name || data.pubkey.substr(0,8));
           }, 500);
         }
 
diff --git a/www/plugins/es/js/services/settings-services.js b/www/plugins/es/js/services/settings-services.js
index 5b8756a7..b12ed753 100644
--- a/www/plugins/es/js/services/settings-services.js
+++ b/www/plugins/es/js/services/settings-services.js
@@ -45,7 +45,8 @@ angular.module('cesium.es.settings.services', ['cesium.services', 'cesium.es.htt
               txSent: true,
               txReceived: true,
               certSent: true,
-              certReceived: true
+              certReceived: true,
+              emitHtml5: false
             },
             invitations: {
               readTime: true
@@ -86,6 +87,13 @@ angular.module('cesium.es.settings.services', ['cesium.services', 'cesium.es.htt
       !!csSettings.data.plugins.es.host;
   };
 
+  that.notifications = {};
+  that.notifications.isEmitHtml5Enable = function() {
+    return that.isEnable() &&
+      csSettings.data.plugins.es.notifications &&
+      angular.isDefined(csSettings.data.plugins.es.notifications.emitHtml5) ? csSettings.data.plugins.es.notifications.emitHtml5 : false;
+  };
+
   that.wot = {};
   that.wot.isMixedSearchEnable = function() {
     return that.isEnable() &&
diff --git a/www/plugins/es/js/services/tx-services.js b/www/plugins/es/js/services/tx-services.js
index b4aa8e5c..1a1f41e1 100644
--- a/www/plugins/es/js/services/tx-services.js
+++ b/www/plugins/es/js/services/tx-services.js
@@ -11,10 +11,11 @@ angular.module('cesium.es.tx.services', ['ngResource', 'cesium.services', 'cesiu
 
   })
 
-  .factory('esTx', function($q, $rootScope, csCurrency, csTx, esHttp, esWot) {
+  .factory('esTx', function($q, $rootScope, csPlatform, csCurrency, csTx, esHttp, esWot) {
     'ngInject';
 
     var
+      listeners,
       raw = {
         block: {
           search: esHttp.post('/:currency/block/_search')
@@ -115,8 +116,38 @@ angular.module('cesium.es.tx.services', ['ngResource', 'cesium.services', 'cesiu
       return deferred.promise;
     }
 
-    // Register extensions
-    csTx.api.data.on.loadUDs($rootScope, onLoadUDs, this);
+    function addListeners() {
+      // Extend API events
+      listeners = [
+        csTx.api.data.on.loadUDs($rootScope, onLoadUDs, this)
+      ];
+    }
+
+    function removeListeners() {
+      _.forEach(listeners, function(remove){
+        remove();
+      });
+      listeners = [];
+    }
+
+    function refreshState() {
+      var enable = esHttp.alive;
+      if (!enable && listeners && listeners.length > 0) {
+        console.debug("[ES] [tx] Disable");
+        removeListeners();
+      }
+      else if (enable && (!listeners || listeners.length === 0)) {
+        console.debug("[ES] [tx] Enable");
+        addListeners();
+      }
+    }
+
+    // Default action
+    csPlatform.ready().then(function() {
+      esHttp.api.node.on.start($rootScope, refreshState, this);
+      esHttp.api.node.on.stop($rootScope, refreshState, this);
+      return refreshState();
+    });
 
     // Exports
     return {};
diff --git a/www/plugins/es/templates/settings/plugin_settings.html b/www/plugins/es/templates/settings/plugin_settings.html
index 8520e1d3..1bd83aa7 100644
--- a/www/plugins/es/templates/settings/plugin_settings.html
+++ b/www/plugins/es/templates/settings/plugin_settings.html
@@ -15,10 +15,10 @@
     </div>
 
     <div class="item item-toggle dark">
-      <div class="input-label" translate>ES_SETTINGS.ENABLE_REMOTE_STORAGE</div>
+      <div ng-class="{'gray': !formData.enable}" class="input-label" translate>ES_SETTINGS.ENABLE_REMOTE_STORAGE</div>
       <h4 class="gray" ng-bind-html="'ES_SETTINGS.ENABLE_REMOTE_STORAGE_HELP' | translate"></h4>
       <label class="toggle toggle-royal">
-        <input type="checkbox" ng-model="formData.useRemoteStorage" >
+        <input type="checkbox" ng-model="formData.useRemoteStorage" ng-disabled="!formData.enable">
         <div class="track">
           <div class="handle"></div>
         </div>
@@ -46,6 +46,18 @@
 
     <span class="item item-divider" translate>ES_SETTINGS.NOTIFICATIONS.DIVIDER</span>
 
+    <!-- Enable HTML notifications -->
+    <div class="item item-toggle dark hidden-xs hidden-sm">
+      <div ng-class="{'gray': !formData.enable}" class="input-label" translate>ES_SETTINGS.NOTIFICATIONS.ENABLE_HTML5_NOTIFICATION</div>
+      <h4 class="gray" ng-bind-html="'ES_SETTINGS.NOTIFICATIONS.ENABLE_HTML5_NOTIFICATION_HELP' | translate"></h4>
+      <label class="toggle toggle-royal">
+        <input type="checkbox" ng-model="formData.notifications.emitHtml5"  ng-disabled="!formData.enable">
+        <div class="track">
+          <div class="handle"></div>
+        </div>
+      </label>
+    </div>
+
     <span class="item gray item-text-wrap" translate>ES_SETTINGS.NOTIFICATIONS.HELP_TEXT</span>
 
     <label class="item item-toggle dark item-text-wrap">
@@ -85,6 +97,7 @@
       </div>
     </label>
 
+
     <!-- Allow extension here -->
     <cs-extension-point name="common"></cs-extension-point>
 
diff --git a/www/plugins/es/templates/wallet/item_wallet_extend.html b/www/plugins/es/templates/wallet/item_wallet_extend.html
new file mode 100644
index 00000000..19cbb535
--- /dev/null
+++ b/www/plugins/es/templates/wallet/item_wallet_extend.html
@@ -0,0 +1,14 @@
+<h4 class="dark" ng-if=":state:enable">
+  <a ng-if="walletData.invitations.unreadCount">
+    <i class="dark ion-person-stalker"></i>
+    {{walletData.invitations.unreadCount}}
+  </a>
+  <a ng-if="walletData.messages.unreadCount" ui-sref="app.view_messages_by_id({id: wallet.id})">
+    <i class="dark ion-email"></i>
+    {{walletData.messages.unreadCount}}
+  </a>
+  <a ng-if="walletData.notifications.unreadCount" ui-sref="app.view_notifications_by_id({id: wallet.id})">
+    <i class="dark ion-android-notifications"></i>
+    {{walletData.notifications.unreadCount}}
+  </a>
+</h4>
diff --git a/www/templates/wallet/list/item_wallet.html b/www/templates/wallet/list/item_wallet.html
index c5f1c035..1800c5f4 100644
--- a/www/templates/wallet/list/item_wallet.html
+++ b/www/templates/wallet/list/item_wallet.html
@@ -2,9 +2,9 @@
   ng-click="select($event, wallet)"
   ng-init="walletData=wallet.data;">
 
-  <i ng-if="::!walletData.avatar" class="item-image icon"
-     ng-class=":rebind:{'ion-card': !walletData.isMember, 'ion-persion': walletData.isMember}"></i>
-  <i ng-if="::walletData.avatar" class="item-image avatar" style="background-image: url({{::walletData.avatar.src}})"></i>
+  <i ng-if=":rebind:!walletData.avatar" class="item-image icon"
+     ng-class=":rebind:{'ion-card': !walletData.isMember, 'ion-person': walletData.isMember}"></i>
+  <i ng-if=":rebind:walletData.avatar" class="item-image avatar" style="background-image: url({{::walletData.avatar.src}})"></i>
   <div class="badge badge-button badge-avatar badge-assertive ion-locked " ng-hide=":rebind:wallet.isAuth()">
   </div>
 
@@ -24,20 +24,7 @@
     <span ng-if="::!walletData.uid" class="assertive" translate>WOT.NOT_MEMBER_PARENTHESIS</span>
   </h4>
 
-  <h4 class="dark">
-    <span ng-if="walletData.invitations.unreadCount">
-      <i class="dark ion-person-stalker"></i>
-      {{walletData.invitations.unreadCount}}
-    </span>
-      <span ng-if="walletData.messages.unreadCount">
-      <i class="dark ion-email"></i>
-      {{walletData.messages.unreadCount}}
-    </span>
-    <span ng-if="walletData.notifications.unreadCount">
-      <i class="dark ion-android-notifications"></i>
-      {{walletData.notifications.unreadCount}}
-    </span>
-  </h4>
+  <cs-extension-point name="item-wallet"></cs-extension-point>
 
   <span class="badge" ng-if="formData.showBalance"
         ng-class=":rebind:{'badge-assertive': (walletData.balance <= 0), 'badge-balanced': (walletData.balance > 0) }">
-- 
GitLab