mirror of
https://github.com/nextcloud/ocsms.git
synced 2026-08-22 13:53:00 +00:00
Separate ContactCache from Application
This commit is contained in:
+8
-90
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user