From 7fed10a26c6e41b3091b87af9cd26c1782b16eb5 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sat, 30 Jan 2016 22:47:08 +0100 Subject: [PATCH] Message limit & other improvements * Implement message limit permitting to limit number of message shown when loading a conversation * Reformat message date properly using angular JS instead of our own date parser * replace /get/country call with /get/settings to retrieve all settings This fixes issue #90 --- appinfo/routes.php | 3 +- controller/smscontroller.php | 19 ++++++++++-- css/style.css | 8 +++++ db/configmapper.php | 8 +++++ js/public/app.js | 57 ++++++++++++------------------------ templates/main.php | 8 +++-- 6 files changed, 60 insertions(+), 43 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index 3a6d7c8..cc516d7 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -21,7 +21,8 @@ $application->registerRoutes($this, array('routes' => array( array('name' => 'sms#check_new_messages', 'url' => '/get/new_messages', 'verb' => 'GET'), array('name' => 'sms#delete_message', 'url' => '/delete/message', 'verb' => 'POST'), array('name' => 'sms#set_country', 'url'=> '/set/country', 'verb' => 'POST'), - array('name' => 'sms#get_country', 'url'=> '/get/country', 'verb' => 'GET'), + array('name' => 'sms#get_settings', 'url'=> '/get/settings', 'verb' => 'GET'), + array('name' => 'sms#set_messagelimit', 'url'=> '/set/msglimit', '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/smscontroller.php b/controller/smscontroller.php index 53374e8..304587c 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -137,6 +137,9 @@ class SmsController extends Controller { } // Order by id (date) ksort($messages); + $msgLimit = $this->configMapper->getMessageLimit(); + // Only load the last 500 messages + $messages = array_slice($messages, -$msgLimit, $msgLimit, true); // Set the last read message for the conversation (all phone numbers) if (count($messages) > 0) { @@ -239,11 +242,23 @@ class SmsController extends Controller { /** * @NoAdminRequired */ - function getCountry() { + 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->getKey("message_limit"); + return new JSONResponse(array("status" => true, + "country" => $country, + "message_limit" => $message_limit)); } + + /** + * @NoAdminRequired + */ + function setMessageLimit($limit) { + $this->configMapper->set("message_limit", $limit); + return new JSONResponse(array("status" => true, "msg" => "OK")); + } + } diff --git a/css/style.css b/css/style.css index 4d72f9d..57f65aa 100644 --- a/css/style.css +++ b/css/style.css @@ -166,3 +166,11 @@ background-repeat: no-repeat; height: 34px; } + +.label-invalid-input { + color: red; +} + +label { + font-weight: bold; +} diff --git a/db/configmapper.php b/db/configmapper.php index 9437932..0a9dea9 100644 --- a/db/configmapper.php +++ b/db/configmapper.php @@ -74,6 +74,14 @@ class ConfigMapper extends Mapper { * Helpers for different config options */ public function getCountry () { return $this->getKey("country"); } + public function getMessageLimit () { + $limit = $this->getKey("message_limit"); + // Default limit is 500 messages + if ($limit === false) { + $limit = 500; + } + return $limit; + } }; ?> diff --git a/js/public/app.js b/js/public/app.js index b552e66..bcdd257 100644 --- a/js/public/app.js +++ b/js/public/app.js @@ -39,11 +39,23 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile' $scope.buttons = [ {text: "Send"} ]; + $scope.setting_msgLimit = 100; $scope.contacts = []; $scope.messages = []; + $scope.totalMessageCount = 0; + // Settings $scope.sendCountry = function () { $.post(OC.generateUrl('/apps/ocsms/set/country'),{'country': $('select[name=intl_phone]').val()}); }; + + $scope.setMessageLimit = function () { + if ($scope.setting_msgLimit === null || $scope.setting_msgLimit === undefined) { + return; + } + $.post(OC.generateUrl('/apps/ocsms/set/msglimit'),{'limit': $scope.setting_msgLimit}); + }; + + // Conversations $scope.loadConversation = function (contact) { OC.Util.History.pushState('phonenumber=' + contact.nav); @@ -78,7 +90,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile' $('#ocsms-phone-opt-number').html(phoneNumberLabel); } - setMessageCountInfo(jsondata); + $scope.totalMessageCount = jsondata['msgCount'] !== undefined ? jsondata['msgCount'] : 0; if ($('#app-content-header').is(':hidden')) { $('#app-content-header').show(); @@ -121,7 +133,8 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile' } - setMessageCountInfo(jsondata); + $scope.totalMessageCount = jsondata['msgCount'] !== undefined ? jsondata['msgCount'] : 0; + if ($('#ocsms-conversation-removal').is(':hidden')) { $('#ocsms-conversation-removal').show(); } @@ -200,7 +213,6 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile' // Reinit main window $('#ocsms-phone-label').html(''); $('#ocsms-phone-opt-number').html(''); - $('#ocsms-phone-msg-nb').html(''); $('#ocsms-conversation-removal').hide(); $('#app-content-header').hide(); $scope.removeContact({'nav': g_curPhoneNumber}); @@ -261,9 +273,11 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile' } $scope.fetchInitialSettings = function () { - $.getJSON(OC.generateUrl('/apps/ocsms/get/country'), function(jsondata, status) { + $.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"]); + $scope.setting_msgLimit = jsondata["message_limit"]; } }); } @@ -312,13 +326,8 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile' var buf = false; // Improve JS performance var msgClass = ''; - var msgCount = 0; var formatedDate = ''; - var formatedHour = '00'; - var formatedMin = '00'; - var months = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug.', 'Sep.', - 'Oct.', 'Nov.', 'Dec.']; $.each(jsondata["conversation"], function(id, vals) { if (vals["type"] == 1) { @@ -338,21 +347,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile' } // Multiplicate ID to permit date to use it properly - msgDate = new Date(id*1); - - formatedHour = msgDate.getHours(); - if (formatedHour < 10) { - formatedHour = '0' + formatedHour; - } - - formatedMin = msgDate.getMinutes(); - if (formatedMin < 10) { - formatedMin = '0' + formatedMin; - } - formatedDate = msgDate.getDate() + " " + months[msgDate.getMonth()] + " " + - formatedHour + ":" + formatedMin; - - $scope.addConversationMessage({'id': id, 'type': msgClass, 'date': formatedDate, 'content': vals['msg']}); + $scope.addConversationMessage({'id': id, 'type': msgClass, 'date': new Date(id * 1), 'content': vals['msg']}); buf = true; msgCount++; @@ -403,20 +398,6 @@ $.urlParam = function(name){ } }; -function setMessageCountInfo(jsondata) { - if (typeof jsondata['msgCount'] != 'undefined') { - if (jsondata['msgCount'] == 1) { - $('#ocsms-phone-msg-nb').html(jsondata['msgCount'] + ' message'); - } - else { - $('#ocsms-phone-msg-nb').html(jsondata['msgCount'] + ' messages'); - } - } - else { - $('#ocsms-phone-msg-nb').html(''); - } -} - function changeSelectedConversation(item) { if (item === 'undefined' || item == null) { return; diff --git a/templates/main.php b/templates/main.php index 9b901ba..ba7b348 100644 --- a/templates/main.php +++ b/templates/main.php @@ -20,6 +20,10 @@ use \OCA\OcSms\Lib\CountryCodes;
+ + + Invalid message limit +