diff --git a/controller/smscontroller.php b/controller/smscontroller.php index 58730d9..742edc1 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -88,18 +88,18 @@ class SmsController extends Controller { * @NoCSRFRequired */ public function retrieveAllPeers () { - $phoneList = $this->smsMapper->getAllPeersPhoneNumbers($this->userId); + $phoneList = $this->smsMapper->getLastMessageTimestampForAllPhonesNumbers($this->userId); $contactsSrc = $this->app->getContacts(); $contacts = array(); $countPhone = count($phoneList); - for ($i=0; $i < $countPhone; $i++) { - $fmtPN = preg_replace("#[ ]#","/", $phoneList[$i]); + foreach ($phoneList as $number => $ts) { + $fmtPN = preg_replace("#[ ]#","/", $number); if (isset($contactsSrc[$fmtPN])) { $contacts[$fmtPN] = $contactsSrc[$fmtPN]; } } - // @ TODO: filter correctly + return new JSONResponse(array("phonelist" => $phoneList, "contacts" => $contacts)); } @@ -129,22 +129,24 @@ class SmsController extends Controller { // 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); - // Remove slashes $fmtPN = preg_replace("#[/]#"," ", $iContacts[$contactName][$i]); + + $messages = $messages + + $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $fmtPN, $lastDate); + $phoneNumbers[] = $fmtPN; - $msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $iContacts[$contactName][$i]); + $msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $fmtPN); } } else { - $messages = $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $phoneNumber, $lastDate); - $msgCount = $this->smsMapper->countMessagesForPhoneNumber($this->userId, $phoneNumber); - // remove slashes $fmtPN = preg_replace("#[/]#"," ", $phoneNumber); + + $messages = $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $fmtPN, $lastDate); + $msgCount = $this->smsMapper->countMessagesForPhoneNumber($this->userId, $fmtPN); + $phoneNumbers[] = $fmtPN; } diff --git a/db/smsmapper.php b/db/smsmapper.php index 4cd8c01..a61e746 100644 --- a/db/smsmapper.php +++ b/db/smsmapper.php @@ -111,6 +111,22 @@ class SmsMapper extends Mapper { } } + public function getLastMessageTimestampForAllPhonesNumbers ($userId) { + $query = \OCP\DB::prepare('SELECT sms_address,MAX(sms_date) as mx FROM ' . + '*PREFIX*ocsms_smsdatas WHERE user_id = ? AND sms_mailbox IN (?,?) ' . + 'GROUP BY sms_address'); + $result = $query->execute(array($userId, 0, 1)); + + $phoneList = array(); + while ($row = $result->fetchRow()) { + $phoneNumber = preg_replace("#[ ]#", "/", $row["sms_address"]); + if (!in_array($phoneNumber, $phoneList)) { + $phoneList[$phoneNumber] = $row["mx"]; + } + } + return $phoneList; + } + public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) { \OCP\DB::beginTransaction(); diff --git a/js/script.js b/js/script.js index 25c769b..f8ee1a3 100644 --- a/js/script.js +++ b/js/script.js @@ -198,17 +198,17 @@ function fetchInitialPeerList(jsondata) { $.each(jsondata['phonelist'], function(id, val) { var fn, peerLabel, idxVal; - idxVal = val.replace(/ /g,'/'); - if (typeof jsondata['contacts'][idxVal] == 'undefined') { + idxVal = id.replace(/\//g,' '); + if (typeof jsondata['contacts'][id] == 'undefined') { fn = ''; - peerLabel = val; + peerLabel = id; } else { - fn = jsondata['contacts'][idxVal]; + fn = jsondata['contacts'][id]; peerLabel = fn; } if ($.inArray(peerLabel, bufferedContacts) == -1) { - peerListBuf += '
  • ' + peerLabel + '
  • '; + peerListBuf += '
  • ' + peerLabel + '
  • '; bufferedContacts.push(peerLabel); } });