]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add activities when a tag is (un-)assigned
authorJoas Schilling <nickvergessen@owncloud.com>
Mon, 1 Feb 2016 15:57:51 +0000 (16:57 +0100)
committerJoas Schilling <nickvergessen@owncloud.com>
Tue, 2 Feb 2016 09:11:58 +0000 (10:11 +0100)
apps/systemtags/activity/extension.php
apps/systemtags/activity/listener.php
apps/systemtags/appinfo/app.php
lib/private/appframework/dependencyinjection/dicontainer.php

index b33b9046d37ff94270bb5994c2f4dabe6f17aef8..aa61b0bb94e019ad914b620170fddb1bd5f250e6 100644 (file)
@@ -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;
        }
 
index ce456afaef2708c2978318587f705d7280ed5591..3afbde7a669b7b54109da3684701562bd33ed16f 100644 (file)
 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
index c6fa21fa9bcdaf153e91e2d58968709a246e7c3f..d948f4857f6950af3e8c632e1f5a355cba71a3fb 100644 (file)
@@ -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);
index ff9da88cd81e963356df19e23e0ed73090b64668..5fc45fdd2e8c621dfa0bfb553b83c3cd9ad35d0b 100644 (file)
@@ -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();
                });