1
0
mirror of https://github.com/nerzhul/ocsms.git synced 2025-06-07 07:56:23 +00:00

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
This commit is contained in:
Loic Blot 2016-01-30 22:47:08 +01:00
parent a4ae53b19f
commit 7fed10a26c
6 changed files with 60 additions and 43 deletions

View File

@ -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

View File

@ -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"));
}
}

View File

@ -166,3 +166,11 @@
background-repeat: no-repeat;
height: 34px;
}
.label-invalid-input {
color: red;
}
label {
font-weight: bold;
}

View File

@ -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;
}
};
?>

View File

@ -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;

View File

@ -20,6 +20,10 @@ use \OCA\OcSms\Lib\CountryCodes;
<button name="app settings" class="settings-button" data-apps-slide-toggle="#app-settings-content"></button>
</div>
<div id="app-settings-content">
<label for="setting_msg_per_page">Max messages on tab loading:</label>
<input type="number" min="10" max="10000" name="setting_msg_per_page" ng-model="setting_msgLimit" ng-change="setMessageLimit()"></input>
<span class="label-invalid-input" ng-if="setting_msgLimit == null || setting_msgLimit == undefined">Invalid message limit</span>
<select name="intl_phone" id="sel_intl_phone">
<?php foreach (CountryCodes::$codes as $code => $cval) { ?>
<option><?php p($code); ?></option>
@ -33,7 +37,7 @@ use \OCA\OcSms\Lib\CountryCodes;
<div id="app-content-header" style="display: none;">
<div id="ocsms-phone-label"></div><div id="ocsms-conversation-removal" class="icon-delete svn delete action" ng-click="removeConversation();"></div>
<div id="ocsms-phone-opt-number"></div>
<div id="ocsms-phone-msg-nb"></div>
<div id="ocsms-phone-msg-nb">{{ messages.length }} message(s) shown. {{ totalMessageCount }} message(s) stored in database.</div>
</div>
<div id="app-content-wrapper">
@ -42,7 +46,7 @@ use \OCA\OcSms\Lib\CountryCodes;
<div class="msg-{{ message.type }}">
<div>{{ message.content }}</div>
<div style="display: block;" id="ocsms-message-removal" class="icon-delete svn delete action" ng-click="removeConversationMessage(message.id);"></div>
<div class="msg-date">{{ message.date }}</div>
<div class="msg-date">{{ message.date | date:'medium' }}</div>
</div>
<div class="msg-spacer"></div>
</div>