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

Added experimental merging for contacts with multiple numbers

This commit is contained in:
Loic Blot 2014-10-09 18:27:13 +02:00
parent 667118310d
commit 1138512dc8
4 changed files with 80 additions and 25 deletions

View File

@ -27,6 +27,8 @@ class OcSmsApp extends App {
*/ */
private static $contacts; private static $contacts;
private static $contactsInverted;
private $c; private $c;
public function __construct (array $urlParams=array()) { 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() { public function getContacts() {
// Only load contacts if they aren't in the buffer // Only load contacts if they aren't in the buffer
if(count(self::$contacts) == 0) { if(count(self::$contacts) == 0) {
self::$contacts = array(); $this->loadContacts();
$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"];
}
}
}
} }
return self::$contacts; 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);
}
}
}
}
} }

View File

@ -107,13 +107,37 @@ class SmsController extends Controller {
* @NoCSRFRequired * @NoCSRFRequired
*/ */
public function getConversation ($phoneNumber, $lastDate = 0) { public function getConversation ($phoneNumber, $lastDate = 0) {
$messages = $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $phoneNumber, $lastDate);
$contacts = $this->app->getContacts(); $contacts = $this->app->getContacts();
$contactName = ""; $contactName = "";
if (isset($contacts[$phoneNumber])) { if (isset($contacts[$phoneNumber])) {
$contactName = $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 // @ TODO: filter correctly
return new JSONResponse(array("conversation" => $messages, "contactName" => $contactName)); return new JSONResponse(array("conversation" => $messages, "contactName" => $contactName));
} }

View File

@ -97,7 +97,6 @@ class SmsMapper extends Mapper {
"type" => $row["sms_type"] "type" => $row["sms_type"]
); );
} }
ksort($messageList);
return $messageList; return $messageList;
} }

View File

@ -143,8 +143,11 @@ function changeSelectedConversation(item) {
function fetchInitialPeerList(jsondata) { function fetchInitialPeerList(jsondata) {
// Use a buffer for better jQuery performance // Use a buffer for better jQuery performance
var peerListBuf = ""; var peerListBuf = "";
var bufferedContacts = [];
$.each(jsondata['phonelist'], function(id, val) { $.each(jsondata['phonelist'], function(id, val) {
var fn, peerLabel; var fn, peerLabel;
if (typeof jsondata['contacts'][val] == 'undefined') { if (typeof jsondata['contacts'][val] == 'undefined') {
fn = ''; fn = '';
@ -154,7 +157,10 @@ function fetchInitialPeerList(jsondata) {
fn = jsondata['contacts'][val]; fn = jsondata['contacts'][val];
peerLabel = fn; peerLabel = fn;
} }
peerListBuf += '<li><a href="#" mailbox-navigation="' + val + '">' + peerLabel + '</a></li>'; if (!$.inArray(peerLabel, bufferedContacts)) {
peerListBuf += '<li><a href="#" mailbox-navigation="' + val + '">' + peerLabel + '</a></li>';
bufferedContacts.push(peerLabel);
}
}); });
// Only modify peerList if there is peers // Only modify peerList if there is peers