From 00d157ea2c61228214866f752fa8fb99a926703a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Blot=20=28=40U-Exp=29?= Date: Mon, 13 Oct 2014 19:39:55 +0200 Subject: [PATCH] contrainer for message count (need tests) --- controller/smscontroller.php | 31 +++++++++++++------------------ db/smsmapper.php | 13 ++++++++++++- js/script.js | 30 ++++++++++++++++++++++++------ 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/controller/smscontroller.php b/controller/smscontroller.php index 8727a5d..13accb1 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -115,31 +115,26 @@ class SmsController extends Controller { $messages = array(); $phoneNumbers = array(); + $msgCount = 0; + + $iContacts = $this->app->getInvertedContacts(); // Contact resolved - if ($contactName != "") { - $iContacts = $this->app->getInvertedContacts(); - $messages = array(); + if ($contactName != "" && isset($iContacts[$contactName])) { + $ctPn = count($iContacts[$contactName]); - // If there is iContacts (this must be) - if (isset($iContacts[$contactName])) { - $ctPn = count($iContacts[$contactName]); + // We merge each message list into global messagelist + for ($i=0; $i < $ctPn; $i++) { + $messages = $messages + + $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $iContacts[$contactName][$i], $lastDate); + $phoneNumbers[] = $iContacts[$contactName][$i]; - // We merge each message list into global messagelist - for ($i=0; $i < $ctPn; $i++) { - $messages = $messages + - $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $iContacts[$contactName][$i], $lastDate); - $phoneNumbers[] = $iContacts[$contactName][$i]; - } - } - // This case mustn't be reached, but add it. - else { - $messages = $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $phoneNumber, $lastDate); - $phoneNumbers[] = $phoneNumber; + $msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $iContacts[$contactName][$i]); } } else { $messages = $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $phoneNumber, $lastDate); + $msgCount = $this->smsMapper->countMessagesForPhoneNumber($this->userId, $phoneNumber); $phoneNumbers[] = $phoneNumber; } @@ -147,7 +142,7 @@ class SmsController extends Controller { ksort($messages); // @ TODO: filter correctly - return new JSONResponse(array("conversation" => $messages, "contactName" => $contactName, "phoneNumbers" => $phoneNumbers)); + return new JSONResponse(array("conversation" => $messages, "contactName" => $contactName, "phoneNumbers" => $phoneNumbers, "msgCount" => $msgCount)); } /** diff --git a/db/smsmapper.php b/db/smsmapper.php index 0ca5397..77dd9e1 100644 --- a/db/smsmapper.php +++ b/db/smsmapper.php @@ -91,7 +91,7 @@ class SmsMapper extends Mapper { $result = $query->execute(array($userId, $phoneNumber, 0, 1, $minDate)); $messageList = array(); - while($row = $result->fetchRow()) { + while ($row = $result->fetchRow()) { $messageList[$row["sms_date"]] = array( "msg" => $row["sms_msg"], "type" => $row["sms_type"] @@ -100,6 +100,17 @@ class SmsMapper extends Mapper { return $messageList; } + public function countMessagesForPhoneNumber ($userId, $phoneNumber) { + $query = \OCP\DB::prepare('SELECT count(smsdate) as ct FROM ' . + '*PREFIX*ocsms_smsdatas WHERE user_id = ? AND sms_address = ? ' . + 'AND sms_mailbox IN (?,?)'); + $result = $query->execute(array($userId, $phoneNumber, 0, 1)); + + if ($row = $result->fetchRow()) { + return $row["ct"]; + } + } + public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) { \OCP\DB::beginTransaction(); diff --git a/js/script.js b/js/script.js index ebf733f..4be6d93 100644 --- a/js/script.js +++ b/js/script.js @@ -53,7 +53,9 @@ var refreshConversation = function() { desktopNotify(unreadCount + " unread message(s) in conversation with " + curContactName); } } - + + setMessageCountInfo(jsondata); + if ($('#app-content-header').is(':hidden')) { $('#app-content-header').show(); } @@ -61,6 +63,20 @@ var refreshConversation = function() { ); }; +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 fetchConversation(phoneNumber) { $.getJSON(OC.generateUrl('/apps/ocsms/get/conversation'), { @@ -96,7 +112,9 @@ function fetchConversation(phoneNumber) { curContactName = jsondata['contactName']; $('#ocsms-phone-opt-number').html(phoneNumberLabel); } - + + setMessageCountInfo(jsondata); + if ($('#app-content-header').is(':hidden')) { $('#app-content-header').show(); } @@ -175,11 +193,11 @@ function changeSelectedConversation(item) { function fetchInitialPeerList(jsondata) { // Use a buffer for better jQuery performance var peerListBuf = ""; - + var bufferedContacts = []; $.each(jsondata['phonelist'], function(id, val) { - + var fn, peerLabel; if (typeof jsondata['contacts'][val] == 'undefined') { fn = ''; @@ -194,7 +212,7 @@ function fetchInitialPeerList(jsondata) { bufferedContacts.push(peerLabel); } }); - + // Only modify peerList if there is peers if (peerListBuf != '') { $('#app-mailbox-peers ul').html(peerListBuf); @@ -243,7 +261,7 @@ function desktopNotify(msg) { OC.Util.History.pushState('phonenumber=' + phoneNumber); // Reset it for refreshConversation lastMsgDate = 0; - + // phoneNumber must exist if (phoneNumber != null) { fetchConversation(phoneNumber);