From 74928c8e642bbf186dd0a82f6c1a32c2d64a7e38 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sat, 30 Jan 2016 23:23:27 +0100 Subject: [PATCH] Permit to disable notifications from app settings directly This fixes issue #89 --- appinfo/routes.php | 1 + controller/settingscontroller.php | 40 +++++++++++++------- db/configmapper.php | 9 +++++ js/public/app.js | 61 ++++++++++++++++++++----------- templates/main.php | 13 ++++++- 5 files changed, 88 insertions(+), 36 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index e5eaae1..e45319f 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -24,6 +24,7 @@ $application->registerRoutes($this, array('routes' => array( array('name' => 'settings#set_country', 'url'=> '/set/country', 'verb' => 'POST'), array('name' => 'settings#get_settings', 'url'=> '/get/settings', 'verb' => 'GET'), array('name' => 'settings#set_messagelimit', 'url'=> '/set/msglimit', 'verb' => 'POST'), + array('name' => 'settings#set_notification_state', 'url'=> '/set/notification_state', 'verb' => 'POST'), array('name' => 'api#get_api_version', 'url' => '/get/apiversion', 'verb' => 'GET'), // Android APIv1 array('name' => 'api#push', 'url' => '/push', 'verb' => 'POST'), // Android API diff --git a/controller/settingscontroller.php b/controller/settingscontroller.php index c861765..accfbb0 100644 --- a/controller/settingscontroller.php +++ b/controller/settingscontroller.php @@ -34,6 +34,22 @@ class SettingsController extends Controller { $this->configMapper = $cfgMapper; } + /** + * @NoAdminRequired + */ + function getSettings() { + $country = $this->configMapper->getKey("country"); + if ($country === false) { + return new JSONResponse(array("status" => false)); + } + + return new JSONResponse(array("status" => true, + "country" => $country, + "message_limit" => $this->configMapper->getMessageLimit(), + "notification_state" => $this->configMapper->getNotificationState() + )); + } + /** * @NoAdminRequired */ @@ -45,20 +61,6 @@ class SettingsController extends Controller { return new JSONResponse(array("status" => true, "msg" => "OK")); } - /** - * @NoAdminRequired - */ - function getSettings() { - $country = $this->configMapper->getKey("country"); - if ($country === false) { - return new JSONResponse(array("status" => false)); - } - $message_limit = $this->configMapper->getKey("message_limit"); - return new JSONResponse(array("status" => true, - "country" => $country, - "message_limit" => $message_limit)); - } - /** * @NoAdminRequired */ @@ -67,4 +69,14 @@ class SettingsController extends Controller { return new JSONResponse(array("status" => true, "msg" => "OK")); } + /** + * @NoAdminRequired + */ + function setNotificationState($notification) { + if (!is_numeric($notification) || $notification < 0 || $notification > 2) { + return new JSONResponse(array("status" => false, "msg" => "Invalid notification state")); + } + $this->configMapper->set("notification_state", $notification); + return new JSONResponse(array("status" => true, "msg" => "OK")); + } } diff --git a/db/configmapper.php b/db/configmapper.php index f37e290..81f81b2 100644 --- a/db/configmapper.php +++ b/db/configmapper.php @@ -82,6 +82,15 @@ class ConfigMapper extends Mapper { } return $limit; } + + public function getNotificationState () { + $st = $this->getKey("notification_state"); + // Default state is 1/enabled + if ($st === false) { + $st = 1; + } + return $st; + } }; ?> diff --git a/js/public/app.js b/js/public/app.js index 46e2cab..688c986 100644 --- a/js/public/app.js +++ b/js/public/app.js @@ -39,7 +39,10 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile' $scope.buttons = [ {text: "Send"} ]; + $scope.setting_msgLimit = 100; + $scope.setting_enableNotifications = 1; + $scope.contacts = []; $scope.messages = []; $scope.totalMessageCount = 0; @@ -55,6 +58,14 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile' $.post(OC.generateUrl('/apps/ocsms/set/msglimit'),{'limit': $scope.setting_msgLimit}); }; + $scope.setNotificationSetting = function () { + if ($scope.setting_enableNotifications < 0 || $scope.setting_enableNotifications > 2) { + $scope.setting_enableNotifications = 0; + return; + } + $.post(OC.generateUrl('/apps/ocsms/set/notification_state'),{'notification': $scope.setting_enableNotifications}); + }; + // Conversations $scope.loadConversation = function (contact) { OC.Util.History.pushState('phonenumber=' + contact.nav); @@ -128,7 +139,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile' if (document.hasFocus() == false) { g_unreadCountCurrentConv += fmt[0]; document.title = g_originalTitle + " (" + g_unreadCountCurrentConv + ")"; - desktopNotify(g_unreadCountCurrentConv + " unread message(s) in conversation with " + g_curContactName); + $scope.desktopNotify(g_unreadCountCurrentConv + " unread message(s) in conversation with " + g_curContactName); } } @@ -199,7 +210,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile' * or if unreadCount changes */ if (g_unreadCountNotifStep == 0 || g_lastUnreadCountAllConv != g_unreadCountAllConv) { - desktopNotify(g_unreadCountAllConv + " unread message(s) for all conversations"); + $scope.desktopNotify(g_unreadCountAllConv + " unread message(s) for all conversations"); g_unreadCountNotifStep = 12; g_lastUnreadCountAllConv = g_unreadCountAllConv; } @@ -276,8 +287,12 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile' $.getJSON(OC.generateUrl('/apps/ocsms/get/settings'), function(jsondata, status) { if (jsondata['status'] == true) { $('#sel_intl_phone').val(jsondata["country"]); + $('input[name=setting_msg_per_page]').val(jsondata["message_limit"]); + $('select[name=setting_notif]').val(jsondata["notification_state"]); + $scope.setting_msgLimit = jsondata["message_limit"]; + $scope.setting_enableNotifications = jsondata["notification_state"]; } }); } @@ -355,6 +370,29 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile' return [msgCount,buf]; } + $scope.desktopNotify = function (msg) { + if ($scope.setting_enableNotifications == 0) { + return; + } + + if (!("Notification" in window)) { + return; + } + else if (Notification.permission === "granted") { + new Notification("ownCloud SMS - " + msg); + } + else if (Notification.permission !== 'denied') { + Notification.requestPermission(function (permission) { + if(!('permission' in Notification)) { + Notification.permission = permission; + } + if (permission === "granted") { + new Notification("ownCloud SMS - " + msg); + } + }); + } + } + $interval($scope.refreshConversation, 10000); $interval($scope.checkNewMessages, 10000); @@ -412,25 +450,6 @@ function changeSelectedConversation(item) { g_selectedConversation.html(g_selectedConversation.attr("mailbox-label")); } -function desktopNotify(msg) { - if (!("Notification" in window)) { - return; - } - else if (Notification.permission === "granted") { - new Notification("ownCloud SMS - " + msg); - } - else if (Notification.permission !== 'denied') { - Notification.requestPermission(function (permission) { - if(!('permission' in Notification)) { - Notification.permission = permission; - } - if (permission === "granted") { - new Notification("ownCloud SMS - " + msg); - } - }); - } -} - (function ($, OC) { // reset count and title window.onfocus = function () { diff --git a/templates/main.php b/templates/main.php index ba7b348..195782c 100644 --- a/templates/main.php +++ b/templates/main.php @@ -20,16 +20,27 @@ use \OCA\OcSms\Lib\CountryCodes;
- +
Invalid message limit +
+
+
+ +
+ + +