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 <salimbelakkaf@outlook.de>
  • Loading branch information
grnd-alt committed Oct 23, 2024
1 parent 74cd6e2 commit f6dd0c2
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 54 deletions.
6 changes: 3 additions & 3 deletions apps/dav/lib/Connector/Sabre/TagsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ 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->getTagger()->tagAs($node->getId(), self::TAG_FAVORITE);
$this->getTagger()->addToFavorites($node->getId(), $path);
} else {
$this->getTagger()->unTag($node->getId(), self::TAG_FAVORITE);
$this->getTagger()->removeFromFavorites($node->getId(), $path);
}

if (is_null($favState)) {
Expand Down
50 changes: 6 additions & 44 deletions apps/files/lib/Service/TagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@
*/
namespace OCA\Files\Service;

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\Files\Folder;
use OCP\Files\NotFoundException;
use OCP\ITags;
use OCP\IUser;
use OCP\IUserSession;

/**
Expand Down Expand Up @@ -61,56 +57,22 @@ public function updateFileTags($path, $tags) {
$newTags = array_diff($tags, $currentTags);
foreach ($newTags as $tag) {
if ($tag === ITags::TAG_FAVORITE) {
$this->addActivity(true, $fileId, $path);
$this->tagger->addToFavorites($fileId, $path);
} else {
$this->tagger->tagAs($fileId, $tag);
}
$this->tagger->tagAs($fileId, $tag);
}
$deletedTags = array_diff($currentTags, $tags);
foreach ($deletedTags as $tag) {
if ($tag === ITags::TAG_FAVORITE) {
$this->addActivity(false, $fileId, $path);
$this->tagger->removeFromFavorites($fileId, $path);
} else {
$this->tagger->unTag($fileId, $tag);
}
$this->tagger->unTag($fileId, $tag);
}

// TODO: re-read from tagger to make sure the
// list is up to date, in case of concurrent changes ?
return $tags;
}

/**
* @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) {
}
}
}
13 changes: 11 additions & 2 deletions lib/private/TagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
namespace OC;

use OC\Tagging\TagMapper;
use OCP\Activity\IManager;
use OCP\Db\Exception as DBException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\EventDispatcher\IEventListener;
use OCP\IDBConnection;
use OCP\ITagManager;
Expand All @@ -28,7 +30,14 @@ class TagManager implements ITagManager, IEventListener {
private IDBConnection $connection;
private LoggerInterface $logger;

public function __construct(TagMapper $mapper, IUserSession $userSession, IDBConnection $connection, LoggerInterface $logger) {
public function __construct(
TagMapper $mapper,
IUserSession $userSession,
IDBConnection $connection,
LoggerInterface $logger,
private IEventDispatcher $eventDispatcher,
private IManager $activityManager,
) {
$this->mapper = $mapper;
$this->userSession = $userSession;
$this->connection = $connection;
Expand Down Expand Up @@ -57,7 +66,7 @@ public function load($type, $defaultTags = [], $includeShared = false, $userId =
}
$userId = $this->userSession->getUser()->getUId();
}
return new Tags($this->mapper, $userId, $type, $this->logger, $this->connection, $defaultTags);
return new Tags($this->mapper, $userId, $type, $this->logger, $this->connection, $defaultTags, $this->userSession, $this->eventDispatcher, $this->activityManager);
}

/**
Expand Down
63 changes: 60 additions & 3 deletions lib/private/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@

use OC\Tagging\Tag;
use OC\Tagging\TagMapper;
use OCA\Files\Activity\FavoriteProvider;
use OCP\Activity\IManager;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Events\NodeAddedToFavorite;
use OCP\Files\Events\NodeRemovedFromFavorite;
use OCP\IDBConnection;
use OCP\ITags;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Share_Backend;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -62,7 +69,17 @@ class Tags implements ITags {
*
* since 20.0.0 $includeShared isn't used anymore
*/
public function __construct(TagMapper $mapper, string $user, string $type, LoggerInterface $logger, IDBConnection $connection, array $defaultTags = []) {
public function __construct(
TagMapper $mapper,
string $user,
string $type,
LoggerInterface $logger,
IDBConnection $connection,
array $defaultTags = [],
private IUserSession $userSession,
private IEventDispatcher $eventDispatcher,
private IManager $activityManager,
) {
$this->mapper = $mapper;
$this->user = $user;
$this->type = $type;
Expand Down Expand Up @@ -478,20 +495,23 @@ public function getFavorites() {
* @param int $objid The id of the object
* @return boolean
*/
public function addToFavorites($objid) {
public function addToFavorites($objid, $path = '') {
if (!$this->userHasTag(ITags::TAG_FAVORITE, $this->user)) {
$this->add(ITags::TAG_FAVORITE);
}
$this->addActivity(true, $objid, $path);
return $this->tagAs($objid, ITags::TAG_FAVORITE);
}

/**
* Remove an object from favorites
*
* @param int $objid The id of the object
* @param string $path The path of the object
* @return boolean
*/
public function removeFromFavorites($objid) {
public function removeFromFavorites($objid, $path = '') {
$this->addActivity(false, $objid, $path);
return $this->unTag($objid, ITags::TAG_FAVORITE);
}

Expand Down Expand Up @@ -684,4 +704,41 @@ private function tagMap(Tag $tag) {
'type' => $tag->getType()
];
}

/**
* @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->eventDispatcher->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) {
}
}

}
6 changes: 4 additions & 2 deletions lib/public/ITags.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,21 @@ public function getFavorites();
* Add an object to favorites
*
* @param int $objid The id of the object
* @param string $path The path of the object
* @return boolean
* @since 6.0.0
*/
public function addToFavorites($objid);
public function addToFavorites($objid, $path = '');

/**
* Remove an object from favorites
*
* @param int $objid The id of the object
* @param string $path The path of the object
* @return boolean
* @since 6.0.0
*/
public function removeFromFavorites($objid);
public function removeFromFavorites($objid, $path = '');

/**
* Creates a tag/object relation.
Expand Down

0 comments on commit f6dd0c2

Please sign in to comment.