Merge pull request #7773 from nextcloud/add-events-for-favorites

Add events for favorites
This commit is contained in:
Morris Jobke 2018-01-10 14:42:31 +01:00 committed by GitHub
commit 9978cee1e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 4 deletions

View File

@ -81,13 +81,14 @@ class Application extends App {
$container->registerService('Tagger', function(IContainer $c) {
return $c->query('ServerContainer')->getTagManager()->load('files');
});
$container->registerService('TagService', function(IContainer $c) {
$container->registerService('TagService', function(IContainer $c) use ($server) {
$homeFolder = $c->query('ServerContainer')->getUserFolder();
return new TagService(
$c->query('ServerContainer')->getUserSession(),
$c->query('ServerContainer')->getActivityManager(),
$c->query('Tagger'),
$homeFolder
$homeFolder,
$server->getEventDispatcher()
);
});

View File

@ -31,6 +31,8 @@ use OCP\Files\Folder;
use OCP\ITags;
use OCP\IUser;
use OCP\IUserSession;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
/**
* Service class to manage tags on files.
@ -45,23 +47,28 @@ class TagService {
private $tagger;
/** @var Folder */
private $homeFolder;
/** @var EventDispatcherInterface */
private $dispatcher;
/**
* @param IUserSession $userSession
* @param IManager $activityManager
* @param ITags $tagger
* @param Folder $homeFolder
* @param EventDispatcherInterface $dispatcher
*/
public function __construct(
IUserSession $userSession,
IManager $activityManager,
ITags $tagger,
Folder $homeFolder
Folder $homeFolder,
EventDispatcherInterface $dispatcher
) {
$this->userSession = $userSession;
$this->activityManager = $activityManager;
$this->tagger = $tagger;
$this->homeFolder = $homeFolder;
$this->dispatcher = $dispatcher;
}
/**
@ -114,6 +121,13 @@ class TagService {
return;
}
$eventName = $addToFavorite ? 'addFavorite' : 'removeFavorite';
$this->dispatcher->dispatch(self::class . '::' . $eventName, new GenericEvent(null, [
'userId' => $user->getUID(),
'fileId' => $fileId,
'path' => $path,
]));
$event = $this->activityManager->generateEvent();
try {
$event->setApp('files')

View File

@ -28,6 +28,7 @@ use OC\Tags;
use OCA\Files\Service\TagService;
use OCP\Activity\IManager;
use OCP\IUserSession;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Class TagServiceTest
@ -54,6 +55,9 @@ class TagServiceTest extends \Test\TestCase {
*/
private $root;
/** @var EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject */
private $dispatcher;
/**
* @var \OCA\Files\Service\TagService|\PHPUnit_Framework_MockObject_MockObject
*/
@ -66,7 +70,7 @@ class TagServiceTest extends \Test\TestCase {
protected function setUp() {
parent::setUp();
$this->user = $this->getUniqueID('user');
$this->user = static::getUniqueID('user');
$this->activityManager = $this->createMock(IManager::class);
\OC::$server->getUserManager()->createUser($this->user, 'test');
\OC_User::setUserId($this->user);
@ -83,6 +87,7 @@ class TagServiceTest extends \Test\TestCase {
->will($this->returnValue($user));
$this->root = \OC::$server->getUserFolder();
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$this->tagger = \OC::$server->getTagManager()->load('files');
$this->tagService = $this->getTagService(['addActivity']);
@ -99,6 +104,7 @@ class TagServiceTest extends \Test\TestCase {
$this->activityManager,
$this->tagger,
$this->root,
$this->dispatcher,
])
->setMethods($methods)
->getMock();