Skip to content

Commit

Permalink
fix: move favorite activity logging to tags
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 10, 2024
1 parent fbd6419 commit 23737a9
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 134 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, $this->userSession, $this->eventDispatcher, \OCP\Server::get(\OCP\Activity\IManager::class)));
$server->addPlugin(new \OCA\DAV\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager));
$server->addPlugin(new \OCA\DAV\Connector\Sabre\SharesPlugin(
$objectTree,
$this->userSession,
Expand Down
63 changes: 2 additions & 61 deletions apps/dav/lib/Connector/Sabre/TagsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@
*
*/

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 @@ -58,15 +51,6 @@ 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 @@ -92,17 +76,11 @@ class TagsPlugin extends \Sabre\DAV\ServerPlugin {
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 @@ -286,11 +264,9 @@ public function handleUpdateProperties(string $path, PropPatch $propPatch) {

$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);
$this->getTagger()->addToFavorites($node->getId(), $path);
} else {
$this->addActivity(false, $node->getId(), $path);
$this->getTagger()->unTag($node->getId(), self::TAG_FAVORITE);
$this->getTagger()->removeFromFavorites($node->getId(), $path);
}

if (is_null($favState)) {
Expand All @@ -302,39 +278,4 @@ public function handleUpdateProperties(string $path, PropPatch $propPatch) {
});
}

/**
* @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) {
}
}
}
4 changes: 1 addition & 3 deletions apps/dav/lib/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,7 @@ public function __construct(IRequest $request, string $baseUri) {

// if (!$user instanceOf IUser )
$this->server->addPlugin(
new TagsPlugin(
$this->server->tree, \OC::$server->getTagManager(), $userSession, $dispatcher, $activityManager
)
new TagsPlugin($this->server->tree, \OC::$server->getTagManager())
);

// TODO: switch to LazyUserFolder
Expand Down
5 changes: 0 additions & 5 deletions apps/files/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@
use OCA\Files\Service\TagService;
use OCA\Files\Service\UserConfig;
use OCA\Files\Service\ViewConfig;
use OCP\Activity\IManager as IActivityManager;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Collaboration\Reference\RenderReferenceEvent;
use OCP\Collaboration\Resources\IProviderManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Cache\CacheEntryRemovedEvent;
use OCP\Files\Events\Node\BeforeNodeCopiedEvent;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
Expand Down Expand Up @@ -97,11 +95,8 @@ public function register(IRegistrationContext $context): void {
$server = $c->get(IServerContainer::class);

return new TagService(
$c->get(IUserSession::class),
$c->get(IActivityManager::class),
$c->get(ITagManager::class)->load(self::APP_ID),
$server->getUserFolder(),
$c->get(IEventDispatcher::class),
);
});

Expand Down
61 changes: 4 additions & 57 deletions apps/files/lib/Service/TagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,25 @@
*/
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\ITags;
use OCP\IUser;
use OCP\IUserSession;

/**
* Service class to manage tags on files.
*/
class TagService {

/** @var IUserSession */
private $userSession;
/** @var IManager */
private $activityManager;
/** @var ITags|null */
private $tagger;
/** @var Folder|null */
private $homeFolder;
/** @var IEventDispatcher */
private $dispatcher;

public function __construct(
IUserSession $userSession,
IManager $activityManager,
?ITags $tagger,
?Folder $homeFolder,
IEventDispatcher $dispatcher,
) {
$this->userSession = $userSession;
$this->activityManager = $activityManager;
$this->tagger = $tagger;
$this->homeFolder = $homeFolder;
$this->dispatcher = $dispatcher;
}

/**
Expand Down Expand Up @@ -76,14 +57,16 @@ 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);
continue;
}
$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);
continue;
}
$this->tagger->unTag($fileId, $tag);
}
Expand All @@ -92,40 +75,4 @@ public function updateFileTags($path, $tags) {
// 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) {
}
}
}
10 changes: 8 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 @@ -25,12 +27,16 @@
class TagManager implements ITagManager, IEventListener {
private TagMapper $mapper;
private IUserSession $userSession;
private IEventDispatcher $dispatcher;
private IManager $activityManager;
private IDBConnection $connection;
private LoggerInterface $logger;

public function __construct(TagMapper $mapper, IUserSession $userSession, IDBConnection $connection, LoggerInterface $logger) {
public function __construct(TagMapper $mapper, IUserSession $userSession, IEventDispatcher $dispatcher, IManager $activityManager, IDBConnection $connection, LoggerInterface $logger) {
$this->mapper = $mapper;
$this->userSession = $userSession;
$this->dispatcher = $dispatcher;
$this->activityManager = $activityManager;
$this->connection = $connection;
$this->logger = $logger;
}
Expand All @@ -57,7 +63,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, $this->userSession, $this->dispatcher, $this->activityManager, $userId, $type, $this->logger, $this->connection, $defaultTags);
}

/**
Expand Down
Loading

0 comments on commit 23737a9

Please sign in to comment.