From 25cbc17becc4a47d67ad24ce12118a0d6b0aacea Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 1 Feb 2016 16:57:51 +0100 Subject: [PATCH] Add activities when a tag is (un-)assigned --- apps/systemtags/activity/extension.php | 47 +++++-- apps/systemtags/activity/listener.php | 121 +++++++++++++++++- apps/systemtags/appinfo/app.php | 23 ++-- .../dependencyinjection/dicontainer.php | 4 + 4 files changed, 175 insertions(+), 20 deletions(-) diff --git a/apps/systemtags/activity/extension.php b/apps/systemtags/activity/extension.php index b33b9046d37..aa61b0bb94e 100644 --- a/apps/systemtags/activity/extension.php +++ b/apps/systemtags/activity/extension.php @@ -38,6 +38,9 @@ class Extension implements IExtension { const UPDATE_TAG = 'update_tag'; const DELETE_TAG = 'delete_tag'; + const ASSIGN_TAG = 'assign_tag'; + const UNASSIGN_TAG = 'unassign_tag'; + /** @var IFactory */ protected $languageFactory; @@ -118,18 +121,36 @@ class Extension implements IExtension { $l = $this->getL10N($languageCode); - /* if ($this->activityManager->isFormattingFilteredObject()) { $translation = $this->translateShort($text, $l, $params); if ($translation !== false) { return $translation; } } - */ return $this->translateLong($text, $l, $params); } + /** + * @param string $text + * @param IL10N $l + * @param array $params + * @return bool|string + */ + protected function translateShort($text, IL10N $l, array $params) { + + switch ($text) { + case self::ASSIGN_TAG: + $params[2] = $this->convertParameterToTag($params[2], $l); + return (string) $l->t('%1$s assigned system tag %3$s', $params); + case self::UNASSIGN_TAG: + $params[2] = $this->convertParameterToTag($params[2], $l); + return (string) $l->t('%1$s unassigned system tag %3$s', $params); + } + + return false; + } + /** * @param string $text * @param IL10N $l @@ -149,6 +170,12 @@ class Extension implements IExtension { $params[1] = $this->convertParameterToTag($params[1], $l); $params[2] = $this->convertParameterToTag($params[2], $l); return (string) $l->t('%1$s updated system tag %3$s to %2$s', $params); + case self::ASSIGN_TAG: + $params[2] = $this->convertParameterToTag($params[2], $l); + return (string) $l->t('%1$s assigned system tag %3$s to %2$s', $params); + case self::UNASSIGN_TAG: + $params[2] = $this->convertParameterToTag($params[2], $l); + return (string) $l->t('%1$s unassigned system tag %3$s from %2$s', $params); } return false; @@ -180,6 +207,14 @@ class Extension implements IExtension { //1 => 'systemtag description', //2 => 'systemtag description', ); + + case self::ASSIGN_TAG: + case self::UNASSIGN_TAG: + return array( + 0 => 'username', + 1 => 'file', + //2 => 'systemtag description', + ); } } @@ -194,14 +229,6 @@ class Extension implements IExtension { * @return integer|false */ public function getGroupParameter($activity) { - if ($activity['app'] === self::APP_NAME) { - switch ($activity['subject']) { - case self::CREATE_TAG: - // Group by system tag - // return 1; - } - } - return false; } diff --git a/apps/systemtags/activity/listener.php b/apps/systemtags/activity/listener.php index ce456afaef2..3afbde7a669 100644 --- a/apps/systemtags/activity/listener.php +++ b/apps/systemtags/activity/listener.php @@ -22,17 +22,36 @@ namespace OCA\SystemTags\Activity; use OCP\Activity\IManager; +use OCP\App\IAppManager; +use OCP\Files\Config\IMountProviderCollection; +use OCP\Files\IRootFolder; +use OCP\Files\Node; use OCP\IGroup; use OCP\IGroupManager; use OCP\IUser; use OCP\IUserSession; +use OCP\Share; use OCP\SystemTag\ISystemTag; +use OCP\SystemTag\ISystemTagManager; use OCP\SystemTag\ManagerEvent; +use OCP\SystemTag\MapperEvent; +use OCP\SystemTag\TagNotFoundException; class Listener { + /** @var IGroupManager */ protected $groupManager; + /** @var IManager */ protected $activityManager; + /** @var IUserSession */ protected $session; + /** @var \OCP\SystemTag\ISystemTagManager */ + protected $tagManager; + /** @var \OCP\App\IAppManager */ + protected $appManager; + /** @var \OCP\Files\Config\IMountProviderCollection */ + protected $mountCollection; + /** @var \OCP\Files\IRootFolder */ + protected $rootFolder; /** * Listener constructor. @@ -40,13 +59,30 @@ class Listener { * @param IGroupManager $groupManager * @param IManager $activityManager * @param IUserSession $session + * @param ISystemTagManager $tagManager + * @param IAppManager $appManager + * @param IMountProviderCollection $mountCollection + * @param IRootFolder $rootFolder */ - public function __construct(IGroupManager $groupManager, IManager $activityManager, IUserSession $session) { + public function __construct(IGroupManager $groupManager, + IManager $activityManager, + IUserSession $session, + ISystemTagManager $tagManager, + IAppManager $appManager, + IMountProviderCollection $mountCollection, + IRootFolder $rootFolder) { $this->groupManager = $groupManager; $this->activityManager = $activityManager; $this->session = $session; + $this->tagManager = $tagManager; + $this->appManager = $appManager; + $this->mountCollection = $mountCollection; + $this->rootFolder = $rootFolder; } + /** + * @param ManagerEvent $event + */ public function event(ManagerEvent $event) { $actor = $this->session->getUser(); if ($actor instanceof IUser) { @@ -88,6 +124,89 @@ class Listener { } } + /** + * @param MapperEvent $event + */ + public function mapperEvent(MapperEvent $event) { + $tagIds = $event->getTags(); + if ($event->getObjectType() !== 'files' ||empty($tagIds) + || !in_array($event->getEvent(), [MapperEvent::EVENT_ASSIGN, MapperEvent::EVENT_UNASSIGN]) + || !$this->appManager->isInstalled('activity')) { + // System tags not for files, no tags, not (un-)assigning or no activity-app enabled (save the energy) + return; + } + + try { + $tags = $this->tagManager->getTagsByIds($tagIds); + } catch (TagNotFoundException $e) { + // User assigned/unassigned a non-existing tag, ignore... + return; + } + + if (empty($tags)) { + return; + } + + // Get all mount point owners + $cache = $this->mountCollection->getMountCache(); + $mounts = $cache->getMountsForFileId($event->getObjectId()); + if (empty($mounts)) { + return; + } + + $users = []; + foreach ($mounts as $mount) { + $owner = $mount->getUser()->getUID(); + $ownerFolder = $this->rootFolder->getUserFolder($owner); + $nodes = $ownerFolder->getById($event->getObjectId()); + if (!empty($nodes)) { + /** @var Node $node */ + $node = array_shift($nodes); + $path = $node->getPath(); + if (strpos($path, '/' . $owner . '/files') === 0) { + $path = substr($path, strlen('/' . $owner . '/files')); + } + // Get all users that have access to the mount point + $users = array_merge($users, Share::getUsersSharingFile($path, $owner, true, true)); + } + } + + $actor = $this->session->getUser(); + if ($actor instanceof IUser) { + $actor = $actor->getUID(); + } else { + $actor = ''; + } + + $activity = $this->activityManager->generateEvent(); + $activity->setApp(Extension::APP_NAME) + ->setType(Extension::APP_NAME) + ->setAuthor($actor) + ->setObject($event->getObjectType(), $event->getObjectId()); + + foreach ($users as $user => $path) { + $activity->setAffectedUser($user); + + foreach ($tags as $tag) { + if ($event->getEvent() === MapperEvent::EVENT_ASSIGN) { + $activity->setSubject(Extension::ASSIGN_TAG, [ + $actor, + $path, + $this->prepareTagAsParameter($tag), + ]); + } else if ($event->getEvent() === MapperEvent::EVENT_UNASSIGN) { + $activity->setSubject(Extension::UNASSIGN_TAG, [ + $actor, + $path, + $this->prepareTagAsParameter($tag), + ]); + } + + $this->activityManager->publish($activity); + } + } + } + /** * @param ISystemTag $tag * @return string diff --git a/apps/systemtags/appinfo/app.php b/apps/systemtags/appinfo/app.php index c6fa21fa9bc..d948f4857f6 100644 --- a/apps/systemtags/appinfo/app.php +++ b/apps/systemtags/appinfo/app.php @@ -22,6 +22,7 @@ use OCA\SystemTags\Activity\Extension; use OCA\SystemTags\Activity\Listener; use OCP\SystemTag\ManagerEvent; +use OCP\SystemTag\MapperEvent; $eventDispatcher = \OC::$server->getEventDispatcher(); $eventDispatcher->addListener( @@ -46,21 +47,25 @@ $eventDispatcher->addListener( $activityManager = \OC::$server->getActivityManager(); $activityManager->registerExtension(function() { - return new Extension( - \OC::$server->getL10NFactory(), - \OC::$server->getActivityManager() - ); + $application = new \OCP\AppFramework\App('systemtags'); + return $application->getContainer()->query('OCA\SystemTags\Activity\Extension'); }); $managerListener = function(ManagerEvent $event) use ($activityManager) { - $listener = new Listener( - \OC::$server->getGroupManager(), - $activityManager, - \OC::$server->getUserSession() - ); + $application = new \OCP\AppFramework\App('systemtags'); + $listener = $application->getContainer()->query('OCA\SystemTags\Activity\Listener'); $listener->event($event); }; $eventDispatcher->addListener(ManagerEvent::EVENT_CREATE, $managerListener); $eventDispatcher->addListener(ManagerEvent::EVENT_DELETE, $managerListener); $eventDispatcher->addListener(ManagerEvent::EVENT_UPDATE, $managerListener); + +$mapperListener = function(MapperEvent $event) use ($activityManager) { + $application = new \OCP\AppFramework\App('systemtags'); + $listener = $application->getContainer()->query('OCA\SystemTags\Activity\Listener'); + $listener->mapperEvent($event); +}; + +$eventDispatcher->addListener(MapperEvent::EVENT_ASSIGN, $mapperListener); +$eventDispatcher->addListener(MapperEvent::EVENT_UNASSIGN, $mapperListener); diff --git a/lib/private/appframework/dependencyinjection/dicontainer.php b/lib/private/appframework/dependencyinjection/dicontainer.php index ff9da88cd81..5fc45fdd2e8 100644 --- a/lib/private/appframework/dependencyinjection/dicontainer.php +++ b/lib/private/appframework/dependencyinjection/dicontainer.php @@ -152,6 +152,10 @@ class DIContainer extends SimpleContainer implements IAppContainer { return $this->getServer()->getL10N($c->query('AppName')); }); + $this->registerService('OCP\\L10N\\IFactory', function($c) { + return $this->getServer()->getL10NFactory(); + }); + $this->registerService('OCP\\ILogger', function($c) { return $this->getServer()->getLogger(); }); -- 2.39.5