1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-01 13:24:10 +01:00

Refresh groups on CardDAV update. Refs #246

This commit is contained in:
Thomas Tanghus 2013-11-04 15:59:07 +01:00
parent b1128b3027
commit 7416a16bef
3 changed files with 55 additions and 49 deletions

View File

@ -30,9 +30,6 @@ use Sabre\VObject\Property;
class Contact extends VObject\VCard implements IPIMObject {
const THUMBNAIL_PREFIX = 'contact-thumbnail-';
const THUMBNAIL_SIZE = 28;
/**
* The name of the object type in this case VCARD.
*
@ -675,49 +672,6 @@ class Contact extends VObject\VCard implements IPIMObject {
return $updated;
}
// TODO: Cleanup these parameters
public function cacheThumbnail(\OCP\Image $image = null, $remove = false, $update = false) {
$key = self::THUMBNAIL_PREFIX . $this->combinedKey();
//\OC_Cache::remove($key);
if(\OC_Cache::hasKey($key) && $image === null && $remove === false && $update === false) {
return \OC_Cache::get($key);
}
if($remove) {
\OC_Cache::remove($key);
if(!$update) {
return false;
}
}
if(is_null($image)) {
$this->retrieve();
$image = new \OCP\Image();
if(!isset($this->PHOTO) && !isset($this->LOGO)) {
return false;
}
if(!$image->loadFromBase64((string)$this->PHOTO)) {
if(!$image->loadFromBase64((string)$this->LOGO)) {
return false;
}
}
}
if(!$image->centerCrop()) {
\OCP\Util::writeLog('contacts',
__METHOD__ .'. Couldn\'t crop thumbnail for ID ' . $key,
\OCP\Util::ERROR);
return false;
}
if(!$image->resize(self::THUMBNAIL_SIZE)) {
\OCP\Util::writeLog('contacts',
__METHOD__ . '. Couldn\'t resize thumbnail for ID ' . $key,
\OCP\Util::ERROR);
return false;
}
// Cache as base64 for around a month
\OC_Cache::set($key, strval($image), 3000000);
\OCP\Util::writeLog('contacts', 'Caching ' . $key, \OCP\Util::DEBUG);
return \OC_Cache::get($key);
}
public function __get($key) {
if(!$this->isRetrieved()) {
$this->retrieve();

View File

@ -119,9 +119,15 @@ class Hooks{
$contact = $parameters['contact'];
Utils\Properties::updateIndex($parameters['id'], $contact);
// If updated via CardDAV we don't know if PHOTO has changed
if(isset($parameters['carddav']) && $parameters['carddav']
&& (isset($contact->PHOTO) || isset($contact->LOGO))) {
$contact->cacheThumbnail(null, false, true);
if(isset($parameters['carddav']) && $parameters['carddav']) {
if(isset($contact->PHOTO) || isset($contact->LOGO)) {
$contact->cacheThumbnail(null, false, true);
}
$tagMgr = \OC::$server->getTagManager()->load('contact');
$tagMgr->purgeObjects(array($parameters['id']));
if(isset($contact->CATEGORIES)) {
$tagMgr->addMultiple($contact->CATEGORIES->getParts(), true, $parameters['id']);
}
}
}

View File

@ -35,6 +35,9 @@ use Sabre\VObject;
*/
class VCard extends VObject\Component\VCard {
const THUMBNAIL_PREFIX = 'contact-thumbnail-';
const THUMBNAIL_SIZE = 28;
/**
* The following constants are used by the validate() method.
*/
@ -305,4 +308,47 @@ class VCard extends VObject\Component\VCard {
return $this->groups;
}
// TODO: Cleanup these parameters and move method to Utils class
public function cacheThumbnail(\OCP\Image $image = null, $remove = false, $update = false) {
$key = self::THUMBNAIL_PREFIX . $this->combinedKey();
//\OC_Cache::remove($key);
if(\OC_Cache::hasKey($key) && $image === null && $remove === false && $update === false) {
return \OC_Cache::get($key);
}
if($remove) {
\OC_Cache::remove($key);
if(!$update) {
return false;
}
}
if(is_null($image)) {
$this->retrieve();
$image = new \OCP\Image();
if(!isset($this->PHOTO) && !isset($this->LOGO)) {
return false;
}
if(!$image->loadFromBase64((string)$this->PHOTO)) {
if(!$image->loadFromBase64((string)$this->LOGO)) {
return false;
}
}
}
if(!$image->centerCrop()) {
\OCP\Util::writeLog('contacts',
__METHOD__ .'. Couldn\'t crop thumbnail for ID ' . $key,
\OCP\Util::ERROR);
return false;
}
if(!$image->resize(self::THUMBNAIL_SIZE)) {
\OCP\Util::writeLog('contacts',
__METHOD__ . '. Couldn\'t resize thumbnail for ID ' . $key,
\OCP\Util::ERROR);
return false;
}
// Cache as base64 for around a month
\OC_Cache::set($key, strval($image), 3000000);
\OCP\Util::writeLog('contacts', 'Caching ' . $key, \OCP\Util::DEBUG);
return \OC_Cache::get($key);
}
}