Skip to content

Commit

Permalink
fix(dav): add activity logging for favorites in dav
Browse files Browse the repository at this point in the history
Signed-off-by: grnd-alt <[email protected]>
  • Loading branch information
grnd-alt committed Oct 8, 2024
1 parent 16d125c commit 3a2b521
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 7 deletions.
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);
}

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 )
$this->server->addPlugin(
new TagsPlugin(
$this->server->tree, \OC::$server->getTagManager()
$this->server->tree, \OC::$server->getTagManager(), $userSession, $dispatcher, $activityManager
)
);

Expand Down
23 changes: 22 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,12 @@
use OCA\DAV\Connector\Sabre\File;
use OCA\DAV\Connector\Sabre\Node;
use OCA\DAV\Upload\UploadFile;
use OCP\Activity\IManager;
use OCP\Calendar\IManager as OCPIManager;
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 +44,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 +69,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

0 comments on commit 3a2b521

Please sign in to comment.