Merge pull request #21845 from owncloud/sync-system-addressbook-on-avatar-change

Changing the avatar of the user emits the changeUser event which trig…
This commit is contained in:
Thomas Müller 2016-01-26 11:37:17 +01:00
commit 5285460669
5 changed files with 40 additions and 21 deletions

View File

@ -29,9 +29,12 @@
namespace OC; namespace OC;
use OC\User\User;
use OCP\Files\Folder; use OCP\Files\Folder;
use OCP\Files\File; use OCP\Files\File;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\IAvatar;
use OCP\IImage;
use OCP\IL10N; use OCP\IL10N;
use OC_Image; use OC_Image;
@ -39,22 +42,25 @@ use OC_Image;
* This class gets and sets users avatars. * This class gets and sets users avatars.
*/ */
class Avatar implements \OCP\IAvatar { class Avatar implements IAvatar {
/** @var Folder */ /** @var Folder */
private $folder; private $folder;
/** @var IL10N */ /** @var IL10N */
private $l; private $l;
/** @var User */
private $user;
/** /**
* constructor * constructor
* *
* @param Folder $folder The folder where the avatars are * @param Folder $folder The folder where the avatars are
* @param IL10N $l * @param IL10N $l
* @param User $user
*/ */
public function __construct (Folder $folder, IL10N $l) { public function __construct (Folder $folder, IL10N $l, $user) {
$this->folder = $folder; $this->folder = $folder;
$this->l = $l; $this->l = $l;
$this->user = $user;
} }
/** /**
@ -83,15 +89,15 @@ class Avatar implements \OCP\IAvatar {
/** /**
* sets the users avatar * sets the users avatar
* @param \OCP\IImage|resource|string $data An image object, imagedata or path to set a new avatar * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar
* @throws \Exception if the provided file is not a jpg or png image * @throws \Exception if the provided file is not a jpg or png image
* @throws \Exception if the provided image is not valid * @throws \Exception if the provided image is not valid
* @throws \OC\NotSquareException if the image is not square * @throws NotSquareException if the image is not square
* @return void * @return void
*/ */
public function set ($data) { public function set ($data) {
if($data instanceOf \OCP\IImage) { if($data instanceOf IImage) {
$img = $data; $img = $data;
$data = $img->data(); $data = $img->data();
} else { } else {
@ -110,11 +116,12 @@ class Avatar implements \OCP\IAvatar {
} }
if (!($img->height() === $img->width())) { if (!($img->height() === $img->width())) {
throw new \OC\NotSquareException(); throw new NotSquareException();
} }
$this->remove(); $this->remove();
$this->folder->newFile('avatar.'.$type)->putContent($data); $this->folder->newFile('avatar.'.$type)->putContent($data);
$this->user->triggerChange();
} }
/** /**
@ -130,13 +137,14 @@ class Avatar implements \OCP\IAvatar {
$avatar->delete(); $avatar->delete();
} }
} }
$this->user->triggerChange();
} }
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function getFile($size) { public function getFile($size) {
$ext = $this->getExtention(); $ext = $this->getExtension();
if ($size === -1) { if ($size === -1) {
$path = 'avatar.' . $ext; $path = 'avatar.' . $ext;
@ -166,12 +174,12 @@ class Avatar implements \OCP\IAvatar {
} }
/** /**
* Get the extention of the avatar. If there is no avatar throw Exception * Get the extension of the avatar. If there is no avatar throw Exception
* *
* @return string * @return string
* @throws NotFoundException * @throws NotFoundException
*/ */
private function getExtention() { private function getExtension() {
if ($this->folder->nodeExists('avatar.jpg')) { if ($this->folder->nodeExists('avatar.jpg')) {
return 'jpg'; return 'jpg';
} elseif ($this->folder->nodeExists('avatar.png')) { } elseif ($this->folder->nodeExists('avatar.png')) {

View File

@ -61,10 +61,11 @@ class AvatarManager implements IAvatarManager {
* @return \OCP\IAvatar * @return \OCP\IAvatar
* @throws \Exception In case the username is potentially dangerous * @throws \Exception In case the username is potentially dangerous
*/ */
public function getAvatar($user) { public function getAvatar($userId) {
if (!$this->userManager->userExists($user)) { $user = $this->userManager->get($userId);
if (is_null($user)) {
throw new \Exception('user does not exist'); throw new \Exception('user does not exist');
} }
return new Avatar($this->rootFolder->getUserFolder($user)->getParent(), $this->l); return new Avatar($this->rootFolder->getUserFolder($userId)->getParent(), $this->l, $user);
} }
} }

View File

@ -140,9 +140,7 @@ class User implements IUser {
$result = $this->backend->setDisplayName($this->uid, $displayName); $result = $this->backend->setDisplayName($this->uid, $displayName);
if ($result) { if ($result) {
$this->displayName = $displayName; $this->displayName = $displayName;
if ($this->emitter) { $this->triggerChange();
$this->emitter->emit('\OC\User', 'changeUser', array($this));
}
} }
return $result !== false; return $result !== false;
} else { } else {
@ -163,9 +161,7 @@ class User implements IUser {
} else { } else {
$this->config->setUserValue($this->uid, 'settings', 'email', $mailAddress); $this->config->setUserValue($this->uid, 'settings', 'email', $mailAddress);
} }
if ($this->emitter) { $this->triggerChange();
$this->emitter->emit('\OC\User', 'changeUser', array($this));
}
} }
/** /**
@ -389,4 +385,10 @@ class User implements IUser {
return $url; return $url;
} }
public function triggerChange() {
if ($this->emitter) {
$this->emitter->emit('\OC\User', 'changeUser', array($this));
}
}
} }

View File

@ -55,7 +55,7 @@ class AvatarManagerTest extends \Test\TestCase {
public function testGetAvatarValidUser() { public function testGetAvatarValidUser() {
$this->userManager->expects($this->once()) $this->userManager->expects($this->once())
->method('userExists') ->method('get')
->with('validUser') ->with('validUser')
->willReturn(true); ->willReturn(true);

View File

@ -16,13 +16,18 @@ class AvatarTest extends \Test\TestCase {
/** @var \OC\Avatar */ /** @var \OC\Avatar */
private $avatar; private $avatar;
/** @var \OC\User\User | PHPUnit_Framework_MockObject_MockObject $user */
private $user;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->folder = $this->getMock('\OCP\Files\Folder'); $this->folder = $this->getMock('\OCP\Files\Folder');
/** @var \OCP\IL10N | PHPUnit_Framework_MockObject_MockObject $l */
$l = $this->getMock('\OCP\IL10N'); $l = $this->getMock('\OCP\IL10N');
$l->method('t')->will($this->returnArgument(0)); $l->method('t')->will($this->returnArgument(0));
$this->avatar = new \OC\Avatar($this->folder, $l); $this->user = $this->getMockBuilder('\OC\User\User')->disableOriginalConstructor()->getMock();
$this->avatar = new \OC\Avatar($this->folder, $l, $this->user);
} }
public function testGetNoAvatar() { public function testGetNoAvatar() {
@ -158,6 +163,9 @@ class AvatarTest extends \Test\TestCase {
->method('putContent') ->method('putContent')
->with($image->data()); ->with($image->data());
// One on remove and once on setting the new avatar
$this->user->expects($this->exactly(2))->method('triggerChange');
$this->avatar->set($image->data()); $this->avatar->set($image->data());
} }