diff --git a/apps/dav/lib/Connector/Sabre/TagsPlugin.php b/apps/dav/lib/Connector/Sabre/TagsPlugin.php index a3f2847ee1ac0..97488acc7545b 100644 --- a/apps/dav/lib/Connector/Sabre/TagsPlugin.php +++ b/apps/dav/lib/Connector/Sabre/TagsPlugin.php @@ -30,6 +30,13 @@ use Sabre\DAV\PropFind; use Sabre\DAV\PropPatch; +use OCP\IUserSession; +use OCP\IUser; +use OCP\Files\Events\NodeAddedToFavorite; +use OCP\Files\Events\NodeRemovedFromFavorite; +use OCA\Files\Activity\FavoriteProvider; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\Activity\IManager; class TagsPlugin extends \Sabre\DAV\ServerPlugin { @@ -51,6 +58,15 @@ class TagsPlugin extends \Sabre\DAV\ServerPlugin { */ private $tagManager; + /** @var \OCP\IUserSession */ + private $userSession; + + /** @var IEventDispatcher */ + private $dispatcher; + + /** @var IManager */ + private $activityManager; + /** * @var \OCP\ITags */ @@ -73,11 +89,20 @@ class TagsPlugin extends \Sabre\DAV\ServerPlugin { * @param \Sabre\DAV\Tree $tree tree * @param \OCP\ITagManager $tagManager tag manager */ - public function __construct(\Sabre\DAV\Tree $tree, \OCP\ITagManager $tagManager) { + public function __construct( + \Sabre\DAV\Tree $tree, + \OCP\ITagManager $tagManager, + IUserSession $userSession, + IEventDispatcher $dispatcher, + IManager $activityManager + ) { $this->tree = $tree; $this->tagManager = $tagManager; $this->tagger = null; $this->cachedTags = []; + $this->userSession = $userSession; + $this->dispatcher = $dispatcher; + $this->activityManager = $activityManager; } /** @@ -248,7 +273,7 @@ public function handleGetProperties( * * @return void */ - public function handleUpdateProperties($path, PropPatch $propPatch) { + public function handleUpdateProperties(string $path, PropPatch $propPatch) { $node = $this->tree->getNodeForPath($path); if (!($node instanceof \OCA\DAV\Connector\Sabre\Node)) { return; @@ -259,10 +284,12 @@ public function handleUpdateProperties($path, PropPatch $propPatch) { return true; }); - $propPatch->handle(self::FAVORITE_PROPERTYNAME, function ($favState) use ($node) { + $propPatch->handle(self::FAVORITE_PROPERTYNAME, function ($favState) use ($node,$path) { if ((int)$favState === 1 || $favState === 'true') { + $this->addActivity(true, $node->getId(), $path); $this->getTagger()->tagAs($node->getId(), self::TAG_FAVORITE); } else { + $this->addActivity(false, $node->getId(), $path); $this->getTagger()->unTag($node->getId(), self::TAG_FAVORITE); } @@ -274,4 +301,40 @@ public function handleUpdateProperties($path, PropPatch $propPatch) { return 200; }); } + + /** + * @param bool $addToFavorite + * @param int $fileId + * @param string $path + */ + protected function addActivity($addToFavorite, $fileId, $path) { + $user = $this->userSession->getUser(); + if (!$user instanceof IUser) { + return; + } + + if ($addToFavorite) { + $event = new NodeAddedToFavorite($user, $fileId, $path); + } else { + $event = new NodeRemovedFromFavorite($user, $fileId, $path); + } + $this->dispatcher->dispatchTyped($event); + + $event = $this->activityManager->generateEvent(); + try { + $event->setApp('files') + ->setObject('files', $fileId, $path) + ->setType('favorite') + ->setAuthor($user->getUID()) + ->setAffectedUser($user->getUID()) + ->setTimestamp(time()) + ->setSubject( + $addToFavorite ? FavoriteProvider::SUBJECT_ADDED : FavoriteProvider::SUBJECT_REMOVED, + ['id' => $fileId, 'path' => $path] + ); + $this->activityManager->publish($event); + } catch (\InvalidArgumentException $e) { + } catch (\BadMethodCallException $e) { + } + } } diff --git a/apps/dav/lib/Server.php b/apps/dav/lib/Server.php index 69f05395cb50b..26c26d165f998 100644 --- a/apps/dav/lib/Server.php +++ b/apps/dav/lib/Server.php @@ -52,6 +52,7 @@ use OCA\DAV\Upload\ChunkingPlugin; use OCA\DAV\Upload\ChunkingV2Plugin; use OCA\Theming\ThemingDefaults; +use OCP\Activity\IManager; use OCP\AppFramework\Http\Response; use OCP\Diagnostics\IEventLogger; use OCP\EventDispatcher\IEventDispatcher; @@ -61,6 +62,7 @@ use OCP\IConfig; use OCP\IPreview; use OCP\IRequest; +use OCP\IUser; use OCP\IUserSession; use OCP\Profiler\IProfiler; use OCP\SabrePluginEvent; @@ -241,10 +243,11 @@ public function __construct(IRequest $request, string $baseUri) { $this->server->addPlugin(new ViewOnlyPlugin( \OC::$server->getUserFolder(), )); - // custom properties plugin must be the last one $userSession = \OC::$server->getUserSession(); $user = $userSession->getUser(); + $dispatcher = \OC::$server->get(IEventDispatcher::class); + $activityManager = \OC::$server->get(IManager::class); if ($user !== null) { $view = \OC\Files\Filesystem::getView(); $config = \OCP\Server::get(IConfig::class); @@ -277,9 +280,11 @@ public function __construct(IRequest $request, string $baseUri) { $this->server->addPlugin( new QuotaPlugin($view)); } + + // if (!$user instanceOf IUser ) $this->server->addPlugin( new TagsPlugin( - $this->server->tree, \OC::$server->getTagManager() + $this->server->tree, \OC::$server->getTagManager(), $userSession, $dispatcher,$activityManager ) );