Add IUser::getAvatarImage() for easy access

This commit is contained in:
Thomas Müller 2015-11-25 21:44:36 +01:00
parent c91192fb73
commit dad6470baa
5 changed files with 51 additions and 7 deletions

View File

@ -70,20 +70,25 @@ class SyncSystemAddressBook extends Command {
$name = $user->getBackendClassName();
$userId = $user->getUID();
$displayName = $user->getDisplayName();
//$emailAddress = $user->getEMailAddress();
$image = $user->getAvatarImage(-1);
$cardId = "$name:$userId.vcf";
$card = $this->backend->getCard($systemAddressBook['id'], $cardId);
if ($card === false) {
$vCard = new VCard();
$vCard->add(new Text($vCard, 'UID', $user->getUID()));
$vCard->add(new Text($vCard, 'UID', $userId));
$vCard->add(new Text($vCard, 'FN', $displayName));
// $vCard->add(new Text($vCard, 'EMAIL', $user->getEMailAddress()));
//$vCard->add(new Binary($vCard, 'PHOTO', $user->getAvatar()));
// $vCard->add(new Text($vCard, 'EMAIL', $emailAddress));
if ($image) {
$vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]);
}
$vCard->validate();
$this->backend->createCard($systemAddressBook['id'], $cardId, $vCard->serialize());
} else {
$updated = false;
$vCard = Reader::read($card['carddata']);
if($vCard->FN !== $user->getDisplayName()) {
if($vCard->FN !== $displayName) {
$vCard->FN = new Text($vCard, 'FN', $displayName);
$updated = true;
}

View File

@ -79,7 +79,9 @@ class Avatar implements \OCP\IAvatar {
/** @var File $node */
$node = $this->folder->get('avatar.' . $ext);
$avatar->loadFromData($node->getContent());
if ($size > 0) {
$avatar->resize($size);
}
$this->folder->newFile('avatar.' . $size . '.' . $ext)->putContent($avatar->data());
}
return $avatar;

View File

@ -30,6 +30,8 @@
namespace OC\User;
use OC\Hooks\Emitter;
use OCP\IAvatarManager;
use OCP\IImage;
use OCP\IUser;
use OCP\IConfig;
@ -74,17 +76,21 @@ class User implements IUser {
*/
private $config;
/** @var IAvatarManager */
private $avatarManager;
/**
* @param string $uid
* @param \OC_User_Interface $backend
* @param \OC\Hooks\Emitter $emitter
* @param \OCP\IConfig $config
*/
public function __construct($uid, $backend, $emitter = null, IConfig $config = null) {
public function __construct($uid, $backend, $emitter = null, IConfig $config = null, $avatarManager = null) {
$this->uid = $uid;
$this->backend = $backend;
$this->emitter = $emitter;
$this->config = $config;
$this->avatarManager = $avatarManager;
if ($this->config) {
$enabled = $this->config->getUserValue($uid, 'core', 'enabled', 'true');
$this->enabled = ($enabled === 'true');
@ -93,6 +99,9 @@ class User implements IUser {
$this->enabled = true;
$this->lastLogin = \OC::$server->getConfig()->getUserValue($uid, 'login', 'lastLogin', 0);
}
if (is_null($this->avatarManager)) {
$this->avatarManager = \OC::$server->getAvatarManager();
}
}
/**
@ -316,4 +325,21 @@ class User implements IUser {
public function getEMailAddress() {
return $this->config->getUserValue($this->uid, 'settings', 'email');
}
/**
* get the avatar image if it exists
*
* @param int $size
* @return IImage|null
* @since 9.0.0
*/
public function getAvatarImage($size) {
$avatar = $this->avatarManager->getAvatar($this->uid);
$image = $avatar->get(-1);
if ($image) {
return $image;
}
return null;
}
}

View File

@ -152,4 +152,13 @@ interface IUser {
* @since 9.0.0
*/
public function getEMailAddress();
/**
* get the avatar image if it exists
*
* @param int $size
* @return IImage|null
* @since 9.0.0
*/
public function getAvatarImage($size);
}

View File

@ -98,6 +98,8 @@ class SimpleUserForTesting implements IUser {
}
public function getEMailAddress() {
// TODO: Implement getEMailAddress() method.
}
public function getAvatarImage($size) {
}
}