Skip to content

Commit 3a2b521

Browse files
committed
fix(dav): add activity logging for favorites in dav
Signed-off-by: grnd-alt <salimbelakkaf@outlook.de>
1 parent 16d125c commit 3a2b521

File tree

4 files changed

+96
-7
lines changed

4 files changed

+96
-7
lines changed

apps/dav/lib/Connector/Sabre/ServerFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public function createServer(string $baseUri,
151151
));
152152

153153
if ($this->userSession->isLoggedIn()) {
154-
$server->addPlugin(new \OCA\DAV\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager));
154+
$server->addPlugin(new \OCA\DAV\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager, $this->userSession, $this->eventDispatcher, \OCP\Server::get(\OCP\Activity\IManager::class)));
155155
$server->addPlugin(new \OCA\DAV\Connector\Sabre\SharesPlugin(
156156
$objectTree,
157157
$this->userSession,

apps/dav/lib/Connector/Sabre/TagsPlugin.php

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
*
2929
*/
3030

31+
use OCA\Files\Activity\FavoriteProvider;
32+
use OCP\Activity\IManager;
33+
use OCP\EventDispatcher\IEventDispatcher;
34+
use OCP\Files\Events\NodeAddedToFavorite;
35+
use OCP\Files\Events\NodeRemovedFromFavorite;
36+
use OCP\IUser;
37+
use OCP\IUserSession;
3138
use Sabre\DAV\PropFind;
3239
use Sabre\DAV\PropPatch;
3340

@@ -51,6 +58,15 @@ class TagsPlugin extends \Sabre\DAV\ServerPlugin {
5158
*/
5259
private $tagManager;
5360

61+
/** @var \OCP\IUserSession */
62+
private $userSession;
63+
64+
/** @var IEventDispatcher */
65+
private $dispatcher;
66+
67+
/** @var IManager */
68+
private $activityManager;
69+
5470
/**
5571
* @var \OCP\ITags
5672
*/
@@ -73,11 +89,20 @@ class TagsPlugin extends \Sabre\DAV\ServerPlugin {
7389
* @param \Sabre\DAV\Tree $tree tree
7490
* @param \OCP\ITagManager $tagManager tag manager
7591
*/
76-
public function __construct(\Sabre\DAV\Tree $tree, \OCP\ITagManager $tagManager) {
92+
public function __construct(
93+
\Sabre\DAV\Tree $tree,
94+
\OCP\ITagManager $tagManager,
95+
IUserSession $userSession,
96+
IEventDispatcher $dispatcher,
97+
IManager $activityManager,
98+
) {
7799
$this->tree = $tree;
78100
$this->tagManager = $tagManager;
79101
$this->tagger = null;
80102
$this->cachedTags = [];
103+
$this->userSession = $userSession;
104+
$this->dispatcher = $dispatcher;
105+
$this->activityManager = $activityManager;
81106
}
82107

83108
/**
@@ -248,7 +273,7 @@ public function handleGetProperties(
248273
*
249274
* @return void
250275
*/
251-
public function handleUpdateProperties($path, PropPatch $propPatch) {
276+
public function handleUpdateProperties(string $path, PropPatch $propPatch) {
252277
$node = $this->tree->getNodeForPath($path);
253278
if (!($node instanceof \OCA\DAV\Connector\Sabre\Node)) {
254279
return;
@@ -259,10 +284,12 @@ public function handleUpdateProperties($path, PropPatch $propPatch) {
259284
return true;
260285
});
261286

262-
$propPatch->handle(self::FAVORITE_PROPERTYNAME, function ($favState) use ($node) {
287+
$propPatch->handle(self::FAVORITE_PROPERTYNAME, function ($favState) use ($node, $path) {
263288
if ((int)$favState === 1 || $favState === 'true') {
289+
$this->addActivity(true, $node->getId(), $path);
264290
$this->getTagger()->tagAs($node->getId(), self::TAG_FAVORITE);
265291
} else {
292+
$this->addActivity(false, $node->getId(), $path);
266293
$this->getTagger()->unTag($node->getId(), self::TAG_FAVORITE);
267294
}
268295

@@ -274,4 +301,40 @@ public function handleUpdateProperties($path, PropPatch $propPatch) {
274301
return 200;
275302
});
276303
}
304+
305+
/**
306+
* @param bool $addToFavorite
307+
* @param int $fileId
308+
* @param string $path
309+
*/
310+
protected function addActivity($addToFavorite, $fileId, $path) {
311+
$user = $this->userSession->getUser();
312+
if (!$user instanceof IUser) {
313+
return;
314+
}
315+
316+
if ($addToFavorite) {
317+
$event = new NodeAddedToFavorite($user, $fileId, $path);
318+
} else {
319+
$event = new NodeRemovedFromFavorite($user, $fileId, $path);
320+
}
321+
$this->dispatcher->dispatchTyped($event);
322+
323+
$event = $this->activityManager->generateEvent();
324+
try {
325+
$event->setApp('files')
326+
->setObject('files', $fileId, $path)
327+
->setType('favorite')
328+
->setAuthor($user->getUID())
329+
->setAffectedUser($user->getUID())
330+
->setTimestamp(time())
331+
->setSubject(
332+
$addToFavorite ? FavoriteProvider::SUBJECT_ADDED : FavoriteProvider::SUBJECT_REMOVED,
333+
['id' => $fileId, 'path' => $path]
334+
);
335+
$this->activityManager->publish($event);
336+
} catch (\InvalidArgumentException $e) {
337+
} catch (\BadMethodCallException $e) {
338+
}
339+
}
277340
}

apps/dav/lib/Server.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
use OCA\DAV\Upload\ChunkingPlugin;
5353
use OCA\DAV\Upload\ChunkingV2Plugin;
5454
use OCA\Theming\ThemingDefaults;
55+
use OCP\Activity\IManager;
5556
use OCP\AppFramework\Http\Response;
5657
use OCP\Diagnostics\IEventLogger;
5758
use OCP\EventDispatcher\IEventDispatcher;
@@ -61,6 +62,7 @@
6162
use OCP\IConfig;
6263
use OCP\IPreview;
6364
use OCP\IRequest;
65+
use OCP\IUser;
6466
use OCP\IUserSession;
6567
use OCP\Profiler\IProfiler;
6668
use OCP\SabrePluginEvent;
@@ -241,10 +243,11 @@ public function __construct(IRequest $request, string $baseUri) {
241243
$this->server->addPlugin(new ViewOnlyPlugin(
242244
\OC::$server->getUserFolder(),
243245
));
244-
245246
// custom properties plugin must be the last one
246247
$userSession = \OC::$server->getUserSession();
247248
$user = $userSession->getUser();
249+
$dispatcher = \OC::$server->get(IEventDispatcher::class);
250+
$activityManager = \OC::$server->get(IManager::class);
248251
if ($user !== null) {
249252
$view = \OC\Files\Filesystem::getView();
250253
$config = \OCP\Server::get(IConfig::class);
@@ -277,9 +280,11 @@ public function __construct(IRequest $request, string $baseUri) {
277280
$this->server->addPlugin(
278281
new QuotaPlugin($view));
279282
}
283+
284+
// if (!$user instanceOf IUser )
280285
$this->server->addPlugin(
281286
new TagsPlugin(
282-
$this->server->tree, \OC::$server->getTagManager()
287+
$this->server->tree, \OC::$server->getTagManager(), $userSession, $dispatcher, $activityManager
283288
)
284289
);
285290

apps/dav/tests/unit/Connector/Sabre/TagsPluginTest.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
use OCA\DAV\Connector\Sabre\File;
1212
use OCA\DAV\Connector\Sabre\Node;
1313
use OCA\DAV\Upload\UploadFile;
14+
use OCP\Activity\IManager;
15+
use OCP\Calendar\IManager as OCPIManager;
16+
use OCP\EventDispatcher\IEventDispatcher;
1417
use OCP\ITagManager;
1518
use OCP\ITags;
19+
use OCP\IUserSession;
1620
use Sabre\DAV\Tree;
1721

1822
class TagsPluginTest extends \Test\TestCase {
@@ -40,6 +44,14 @@ class TagsPluginTest extends \Test\TestCase {
4044
*/
4145
private $tagger;
4246

47+
/** @var IManager */
48+
private $activityManager;
49+
50+
/** @var IUserSession */
51+
private $userSession;
52+
53+
/** @var IEventDispatcher */
54+
private $dispatcher;
4355
/**
4456
* @var \OCA\DAV\Connector\Sabre\TagsPlugin
4557
*/
@@ -57,11 +69,20 @@ protected function setUp(): void {
5769
$this->tagManager = $this->getMockBuilder(ITagManager::class)
5870
->disableOriginalConstructor()
5971
->getMock();
72+
$this->activityManager = $this->getMockBuilder(IManager::class)
73+
->disableOriginalConstructor()
74+
->getMock();
75+
$this->userSession = $this->getMockBuilder(IUserSession::class)
76+
->disableOriginalConstructor()
77+
->getMock();
78+
$this->dispatcher = $this->getMockBuilder(IEventDispatcher::class)
79+
->disableOriginalConstructor()
80+
->getMock();
6081
$this->tagManager->expects($this->any())
6182
->method('load')
6283
->with('files')
6384
->willReturn($this->tagger);
64-
$this->plugin = new \OCA\DAV\Connector\Sabre\TagsPlugin($this->tree, $this->tagManager);
85+
$this->plugin = new \OCA\DAV\Connector\Sabre\TagsPlugin($this->tree, $this->tagManager,$this->userSession,$this->dispatcher,$this->activityManager);
6586
$this->plugin->initialize($this->server);
6687
}
6788

0 commit comments

Comments
 (0)