Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add activity logging for favorites in dav #48612

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/dav/lib/Connector/Sabre/ServerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function createServer(string $baseUri,
));

if ($this->userSession->isLoggedIn()) {
$server->addPlugin(new \OCA\DAV\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager));
$server->addPlugin(new \OCA\DAV\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager, $this->userSession, $this->eventDispatcher, \OCP\Server::get(\OCP\Activity\IManager::class)));
$server->addPlugin(new \OCA\DAV\Connector\Sabre\SharesPlugin(
$objectTree,
$this->userSession,
Expand Down
69 changes: 66 additions & 3 deletions apps/dav/lib/Connector/Sabre/TagsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
*
*/

use OCA\Files\Activity\FavoriteProvider;
use OCP\Activity\IManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Events\NodeAddedToFavorite;
use OCP\Files\Events\NodeRemovedFromFavorite;
use OCP\IUser;
use OCP\IUserSession;
use Sabre\DAV\PropFind;
use Sabre\DAV\PropPatch;

Expand All @@ -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
*/
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Comment on lines 288 to 294
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use those methods instead:

server/lib/private/Tags.php

Lines 481 to 497 in ff9ea47

public function addToFavorites($objid) {
if (!$this->userHasTag(ITags::TAG_FAVORITE, $this->user)) {
$this->add(ITags::TAG_FAVORITE);
}
return $this->tagAs($objid, ITags::TAG_FAVORITE);
}
/**
* Remove an object from favorites
*
* @param int $objid The id of the object
* @return boolean
*/
public function removeFromFavorites($objid) {
return $this->unTag($objid, ITags::TAG_FAVORITE);
}

And then emit the event in those methods?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • addActivity from TagService can be copied to Tags.
  • Then TagService can be deleted.
  • And then
    $this->tagService->updateFileTags($path, $tags);
    } catch (\OCP\Files\NotFoundException $e) {
    can be ported to use the methods mentioned above.


Expand All @@ -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) {
}
}
}
9 changes: 7 additions & 2 deletions apps/dav/lib/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -277,9 +280,11 @@ public function __construct(IRequest $request, string $baseUri) {
$this->server->addPlugin(
new QuotaPlugin($view));
}

// if (!$user instanceOf IUser )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be removed?

$this->server->addPlugin(
new TagsPlugin(
$this->server->tree, \OC::$server->getTagManager()
$this->server->tree, \OC::$server->getTagManager(), $userSession, $dispatcher, $activityManager
)
);

Expand Down
22 changes: 21 additions & 1 deletion apps/dav/tests/unit/Connector/Sabre/TagsPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
use OCA\DAV\Connector\Sabre\File;
use OCA\DAV\Connector\Sabre\Node;
use OCA\DAV\Upload\UploadFile;
use OCP\Activity\IManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ITagManager;
use OCP\ITags;
use OCP\IUserSession;
use Sabre\DAV\Tree;

class TagsPluginTest extends \Test\TestCase {
Expand Down Expand Up @@ -40,6 +43,14 @@ class TagsPluginTest extends \Test\TestCase {
*/
private $tagger;

/** @var IManager */
private $activityManager;

/** @var IUserSession */
private $userSession;

/** @var IEventDispatcher */
private $dispatcher;
/**
* @var \OCA\DAV\Connector\Sabre\TagsPlugin
*/
Expand All @@ -57,11 +68,20 @@ protected function setUp(): void {
$this->tagManager = $this->getMockBuilder(ITagManager::class)
->disableOriginalConstructor()
->getMock();
$this->activityManager = $this->getMockBuilder(IManager::class)
->disableOriginalConstructor()
->getMock();
$this->userSession = $this->getMockBuilder(IUserSession::class)
->disableOriginalConstructor()
->getMock();
$this->dispatcher = $this->getMockBuilder(IEventDispatcher::class)
->disableOriginalConstructor()
->getMock();
$this->tagManager->expects($this->any())
->method('load')
->with('files')
->willReturn($this->tagger);
$this->plugin = new \OCA\DAV\Connector\Sabre\TagsPlugin($this->tree, $this->tagManager);
$this->plugin = new \OCA\DAV\Connector\Sabre\TagsPlugin($this->tree, $this->tagManager, $this->userSession, $this->dispatcher, $this->activityManager);
$this->plugin->initialize($this->server);
}

Expand Down
Loading