Skip to content

Commit

Permalink
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 cde368c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
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 @@ -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 {

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

0 comments on commit cde368c

Please sign in to comment.