diff --git a/appinfo/ocsmsapp.php b/appinfo/ocsmsapp.php index b0933d8..10e1f06 100644 --- a/appinfo/ocsmsapp.php +++ b/appinfo/ocsmsapp.php @@ -27,6 +27,8 @@ class OcSmsApp extends App { */ private static $contacts; + private static $contactsInverted; + private $c; public function __construct (array $urlParams=array()) { @@ -75,33 +77,57 @@ class OcSmsApp extends App { }); } - /** - * Partially importe this function from owncloud Chat app - * https://github.com/owncloud/chat/blob/master/app/chat.php - */ public function getContacts() { // Only load contacts if they aren't in the buffer if(count(self::$contacts) == 0) { - self::$contacts = array(); - $cm = $this->c['ContactsManager']; - $result = $cm->search('',array('FN')); - foreach ($result as $r) { - if (isset ($r["TEL"])) { - $phoneIds = $r["TEL"]; - if (is_array($phoneIds)) { - $countPhone = count($phoneIds); - for ($i=0; $i<$countPhone; $i++) { - $phoneNb = preg_replace("#[ ]#", "", $phoneIds[$i]); - self::$contacts[$phoneNb] = $r["FN"]; - } - } - else { - self::$contacts[$phoneIds] = $r["FN"]; - } - } - } + $this->loadContacts(); } return self::$contacts; } + + public function getInvertedContacts() { + // Only load contacts if they aren't in the buffer + if(count(self::$contactsInverted) == 0) { + $this->loadContacts(); + } + + return self::$contactsInverted; + } + + /** + * Partially importe this function from owncloud Chat app + * https://github.com/owncloud/chat/blob/master/app/chat.php + */ + private function loadContacts() { + self::$contacts = array(); + self::$contactsInverted = array(); + + $cm = $this->c['ContactsManager']; + $result = $cm->search('',array('FN')); + foreach ($result as $r) { + if (isset ($r["TEL"])) { + $phoneIds = $r["TEL"]; + if (is_array($phoneIds)) { + $countPhone = count($phoneIds); + for ($i=0; $i < $countPhone; $i++) { + $phoneNb = preg_replace("#[ ]#", "", $phoneIds[$i]); + + self::$contacts[$phoneNb] = $r["FN"]; + if (!isset(self::$contactsInverted[$r["FN"]])) { + self::$contactsInverted[$r["FN"]] = array(); + } + array_push(self::$contactsInverted[$r["FN"]], $phoneNb); + } + } + else { + self::$contacts[$phoneIds] = $r["FN"]; + if (!isset(self::$contactsInverted[$r["FN"]])) { + self::$contactsInverted[$r["FN"]] = array(); + } + array_push(self::$contactsInverted[$r["FN"]], $phoneIds); + } + } + } + } } diff --git a/controller/smscontroller.php b/controller/smscontroller.php index 9378d6b..a5bc506 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -107,13 +107,37 @@ class SmsController extends Controller { * @NoCSRFRequired */ public function getConversation ($phoneNumber, $lastDate = 0) { - $messages = $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $phoneNumber, $lastDate); $contacts = $this->app->getContacts(); $contactName = ""; if (isset($contacts[$phoneNumber])) { $contactName = $contacts[$phoneNumber]; } + $messages = array(); + + if ($contactName != "") { + $iContacts = $this->app->getInvertedContacts(); + $messages = array(); + if (isset($iContacts[$contactName])) { + $ctPn = count($iContacts[$contactName]); + for ($i=0; $i < $ctPn; $i++) { + $messages = array_merge($messages, + $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $iContacts[$contactName][$i], $lastDate) + ); + } + } + // This case mustn't be reached, but add it. + else { + $messages = $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $phoneNumber, $lastDate); + } + } + else { + $messages = $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $phoneNumber, $lastDate); + } + + // Order by id (date) + ksort($messages); + // @ TODO: filter correctly return new JSONResponse(array("conversation" => $messages, "contactName" => $contactName)); } diff --git a/db/smsmapper.php b/db/smsmapper.php index eb87c0e..0ca5397 100644 --- a/db/smsmapper.php +++ b/db/smsmapper.php @@ -97,7 +97,6 @@ class SmsMapper extends Mapper { "type" => $row["sms_type"] ); } - ksort($messageList); return $messageList; } diff --git a/js/script.js b/js/script.js index f2f8077..b955aea 100644 --- a/js/script.js +++ b/js/script.js @@ -143,8 +143,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 = ''; @@ -154,7 +157,10 @@ function fetchInitialPeerList(jsondata) { fn = jsondata['contacts'][val]; peerLabel = fn; } - peerListBuf += '