From 49b644236214881b3609bc664d4c140188f93f58 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Wed, 8 Apr 2015 13:38:29 +0000 Subject: [PATCH] Separate ContactCache from Application --- appinfo/ocsmsapp.php | 98 +++---------------------------- controller/smscontroller.php | 17 +++--- lib/contactcache.php | 111 +++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 97 deletions(-) create mode 100644 lib/contactcache.php diff --git a/appinfo/ocsmsapp.php b/appinfo/ocsmsapp.php index dd5e5a4..accc2c7 100644 --- a/appinfo/ocsmsapp.php +++ b/appinfo/ocsmsapp.php @@ -27,14 +27,6 @@ use \OCA\OcSms\Lib\PhoneNumberFormatter; class OcSmsApp extends App { - /** - * @var array used to cache the parsed contacts for every request - */ - private static $contacts; - private static $contactPhotos; - - private static $contactsInverted; - private $c; public function __construct (array $urlParams=array()) { @@ -70,6 +62,13 @@ class OcSmsApp extends App { return new SmsMapper($c->query('ServerContainer')->getDb()); }); + /** + * Managers + */ + $container->registerService('ContactsManager', function($c) { + return $c->getServer()->getContactsManager(); + }); + /** * Controllers */ @@ -80,6 +79,7 @@ class OcSmsApp extends App { $c->query('UserId'), $c->query('SmsMapper'), $c->query('ConfigMapper'), + $c->query('ContactsManager'), $c->query('ServerContainer')->getURLGenerator(), $app ); @@ -94,87 +94,5 @@ class OcSmsApp extends App { $app ); }); - - /** - * Managers - */ - $container->registerService('ContactsManager', function($c){ - return $c->getServer()->getContactsManager(); - }); - } - - public function getContacts() { - // Only load contacts if they aren't in the buffer - if(count(self::$contacts) == 0) { - $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; - } - - public function getContactPhotos() { - // Only load contacts if they aren't in the buffer - if(count(self::$contactPhotos) == 0) { - $this->loadContacts(); - } - return self::$contactPhotos; - } - - /** - * 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(); - - // Cache country because of loops - $configuredCountry = $this->c->query('ConfigMapper')->getCountry(); - - $cm = $this->c['ContactsManager']; - if ($cm == null) { - return; - } - - $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++) { - $this->pushPhoneNumberToCache($phoneIds[$i], $r["FN"], $configuredCountry); - } - } - else { - $this->pushPhoneNumberToCache($phoneIds, $r["FN"], $configuredCountry); - } - - if (isset ($r["PHOTO"])) { - // Remove useless prefix - $photoURL = preg_replace("#^VALUE=uri:#","",$r["PHOTO"], 1); - self::$contactPhotos[$r["FN"]] = $photoURL; - } - } - } - } - - private function pushPhoneNumberToCache($rawPhone, $contactName, $country) { - - $phoneNb = PhoneNumberFormatter::format($country, $rawPhone); - self::$contacts[$phoneNb] = $contactName; - // Inverted contacts - if (!isset(self::$contactsInverted[$contactName])) { - self::$contactsInverted[$contactName] = array(); - } - array_push(self::$contactsInverted[$contactName], $phoneNb); } } diff --git a/controller/smscontroller.php b/controller/smscontroller.php index 3ad942e..7051037 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -22,6 +22,7 @@ use \OCA\OcSms\AppInfo\OcSmsApp; use \OCA\OcSms\Db\ConfigMapper; use \OCA\OcSms\Db\SmsMapper; +use \OCA\OcSms\Lib\ContactCache; use \OCA\OcSms\Lib\CountryCodes; use \OCA\OcSms\Lib\PhoneNumberFormatter; @@ -33,14 +34,16 @@ class SmsController extends Controller { private $smsMapper; private $errorMsg; private $urlGenerator; + private $contactCache; - public function __construct ($appName, IRequest $request, $userId, SmsMapper $mapper, ConfigMapper $cfgMapper, $urlGenerator, OcSmsApp $app){ + public function __construct ($appName, IRequest $request, $userId, SmsMapper $mapper, ConfigMapper $cfgMapper, $contactsManager, $urlGenerator, OcSmsApp $app){ parent::__construct($appName, $request); $this->app = $app; $this->userId = $userId; $this->smsMapper = $mapper; $this->configMapper = $cfgMapper; $this->urlGenerator = $urlGenerator; + $this->contactCache = new ContactCache($cfgMapper, $contactsManager); } /** @@ -73,9 +76,9 @@ class SmsController extends Controller { */ public function retrieveAllPeers () { $phoneList = $this->smsMapper->getLastMessageTimestampForAllPhonesNumbers($this->userId); - $contactsSrc = $this->app->getContacts(); + $contactsSrc = $this->contactCache->getContacts(); $contacts = array(); - $photos = $this->app->getContactPhotos(); + $photos = $this->contactCache->getContactPhotos(); // Cache country because of loops $configuredCountry = $this->configMapper->getCountry(); @@ -102,8 +105,8 @@ class SmsController extends Controller { * @NoCSRFRequired */ public function getConversation ($phoneNumber, $lastDate = 0) { - $contacts = $this->app->getContacts(); - $iContacts = $this->app->getInvertedContacts(); + $contacts = $this->contactCache->getContacts(); + $iContacts = $this->contactCache->getInvertedContacts(); $contactName = ""; // Cache country because of loops @@ -159,8 +162,8 @@ class SmsController extends Controller { */ public function checkNewMessages($lastDate) { $phoneList = $this->smsMapper->getNewMessagesCountForAllPhonesNumbers($this->userId, $lastDate); - $contactsSrc = $this->app->getContacts(); - $photosSrc = $this->app->getContactPhotos(); + $contactsSrc = $this->contactCache->getContacts(); + $photosSrc = $this->contactCache->getContactPhotos(); $contacts = array(); $photos = array(); diff --git a/lib/contactcache.php b/lib/contactcache.php new file mode 100644 index 0000000..6cecf92 --- /dev/null +++ b/lib/contactcache.php @@ -0,0 +1,111 @@ + + * @contributor: stagprom + * @copyright Loic Blot 2014-2015 + */ + +namespace OCA\OcSms\Lib; + +class ContactCache { + /** + * @var array used to cache the parsed contacts for every request + */ + private $contacts; + private $contactsInverted; + private $contactPhotos; + + private $cfgMapper; + private $contactsManager; + + public function __construct ($cfgMapper, $contactsManager) { + $contacts = array(); + $contactPhotos = array(); + $contactsInverted = array(); + + $this->cfgMapper = $cfgMapper; + $this->contactsManager = $contactsManager; + } + + public function getContacts() { + // Only load contacts if they aren't in the buffer + if(count($this->contacts) == 0) { + $this->loadContacts(); + } + return $this->contacts; + } + + public function getInvertedContacts() { + // Only load contacts if they aren't in the buffer + if(count($this->contactsInverted) == 0) { + $this->loadContacts(); + } + return $this->contactsInverted; + } + + public function getContactPhotos() { + // Only load contacts if they aren't in the buffer + if(count($this->contactPhotos) == 0) { + $this->loadContacts(); + } + return $this->contactPhotos; + } + + /** + * Partially importe this function from owncloud Chat app + * https://github.com/owncloud/chat/blob/master/app/chat.php + */ + private function loadContacts() { + $this->contacts = array(); + $this->contactsInverted = array(); + + // Cache country because of loops + $configuredCountry = $this->cfgMapper->getCountry(); + + $cm = $this->contactsManager; + if ($cm == null) { + return; + } + + $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++) { + $this->pushPhoneNumberToCache($phoneIds[$i], $r["FN"], $configuredCountry); + } + } + else { + $this->pushPhoneNumberToCache($phoneIds, $r["FN"], $configuredCountry); + } + + if (isset ($r["PHOTO"])) { + // Remove useless prefix + $photoURL = preg_replace("#^VALUE=uri:#","",$r["PHOTO"], 1); + $this->contactPhotos[$r["FN"]] = $photoURL; + } + } + } + } + + private function pushPhoneNumberToCache($rawPhone, $contactName, $country) { + + $phoneNb = PhoneNumberFormatter::format($country, $rawPhone); + $this->contacts[$phoneNb] = $contactName; + // Inverted contacts + if (!isset($this->contactsInverted[$contactName])) { + $this->contactsInverted[$contactName] = array(); + } + array_push($this->contactsInverted[$contactName], $phoneNb); + } +}; + +?>