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

Separate ContactCache from Application

This commit is contained in:
Loic Blot 2015-04-08 13:38:29 +00:00
parent a9dbc5d44c
commit 49b6442362
3 changed files with 129 additions and 97 deletions

View File

@ -27,14 +27,6 @@ use \OCA\OcSms\Lib\PhoneNumberFormatter;
class OcSmsApp extends App { 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; private $c;
public function __construct (array $urlParams=array()) { public function __construct (array $urlParams=array()) {
@ -70,6 +62,13 @@ class OcSmsApp extends App {
return new SmsMapper($c->query('ServerContainer')->getDb()); return new SmsMapper($c->query('ServerContainer')->getDb());
}); });
/**
* Managers
*/
$container->registerService('ContactsManager', function($c) {
return $c->getServer()->getContactsManager();
});
/** /**
* Controllers * Controllers
*/ */
@ -80,6 +79,7 @@ class OcSmsApp extends App {
$c->query('UserId'), $c->query('UserId'),
$c->query('SmsMapper'), $c->query('SmsMapper'),
$c->query('ConfigMapper'), $c->query('ConfigMapper'),
$c->query('ContactsManager'),
$c->query('ServerContainer')->getURLGenerator(), $c->query('ServerContainer')->getURLGenerator(),
$app $app
); );
@ -94,87 +94,5 @@ class OcSmsApp extends App {
$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);
} }
} }

View File

@ -22,6 +22,7 @@ use \OCA\OcSms\AppInfo\OcSmsApp;
use \OCA\OcSms\Db\ConfigMapper; use \OCA\OcSms\Db\ConfigMapper;
use \OCA\OcSms\Db\SmsMapper; use \OCA\OcSms\Db\SmsMapper;
use \OCA\OcSms\Lib\ContactCache;
use \OCA\OcSms\Lib\CountryCodes; use \OCA\OcSms\Lib\CountryCodes;
use \OCA\OcSms\Lib\PhoneNumberFormatter; use \OCA\OcSms\Lib\PhoneNumberFormatter;
@ -33,14 +34,16 @@ class SmsController extends Controller {
private $smsMapper; private $smsMapper;
private $errorMsg; private $errorMsg;
private $urlGenerator; 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); parent::__construct($appName, $request);
$this->app = $app; $this->app = $app;
$this->userId = $userId; $this->userId = $userId;
$this->smsMapper = $mapper; $this->smsMapper = $mapper;
$this->configMapper = $cfgMapper; $this->configMapper = $cfgMapper;
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->contactCache = new ContactCache($cfgMapper, $contactsManager);
} }
/** /**
@ -73,9 +76,9 @@ class SmsController extends Controller {
*/ */
public function retrieveAllPeers () { public function retrieveAllPeers () {
$phoneList = $this->smsMapper->getLastMessageTimestampForAllPhonesNumbers($this->userId); $phoneList = $this->smsMapper->getLastMessageTimestampForAllPhonesNumbers($this->userId);
$contactsSrc = $this->app->getContacts(); $contactsSrc = $this->contactCache->getContacts();
$contacts = array(); $contacts = array();
$photos = $this->app->getContactPhotos(); $photos = $this->contactCache->getContactPhotos();
// Cache country because of loops // Cache country because of loops
$configuredCountry = $this->configMapper->getCountry(); $configuredCountry = $this->configMapper->getCountry();
@ -102,8 +105,8 @@ class SmsController extends Controller {
* @NoCSRFRequired * @NoCSRFRequired
*/ */
public function getConversation ($phoneNumber, $lastDate = 0) { public function getConversation ($phoneNumber, $lastDate = 0) {
$contacts = $this->app->getContacts(); $contacts = $this->contactCache->getContacts();
$iContacts = $this->app->getInvertedContacts(); $iContacts = $this->contactCache->getInvertedContacts();
$contactName = ""; $contactName = "";
// Cache country because of loops // Cache country because of loops
@ -159,8 +162,8 @@ class SmsController extends Controller {
*/ */
public function checkNewMessages($lastDate) { public function checkNewMessages($lastDate) {
$phoneList = $this->smsMapper->getNewMessagesCountForAllPhonesNumbers($this->userId, $lastDate); $phoneList = $this->smsMapper->getNewMessagesCountForAllPhonesNumbers($this->userId, $lastDate);
$contactsSrc = $this->app->getContacts(); $contactsSrc = $this->contactCache->getContacts();
$photosSrc = $this->app->getContactPhotos(); $photosSrc = $this->contactCache->getContactPhotos();
$contacts = array(); $contacts = array();
$photos = array(); $photos = array();

111
lib/contactcache.php Normal file
View File

@ -0,0 +1,111 @@
<?php
/**
* ownCloud - ocsms
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Loic Blot <loic.blot@unix-experience.fr>
* @contributor: stagprom <https://github.com/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);
}
};
?>