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 <salimbelakkaf@outlook.de>
  • Loading branch information
grnd-alt committed Oct 22, 2024
1 parent 9c6d7c5 commit 30b0797
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 136 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 @@ -134,7 +134,7 @@ public function createServer(string $baseUri,
));

if ($this->userSession->isLoggedIn()) {
$server->addPlugin(new TagsPlugin($objectTree, $this->tagManager, $this->userSession, $this->eventDispatcher, \OCP\Server::get(\OCP\Activity\IManager::class)));
$server->addPlugin(new TagsPlugin($objectTree, $this->tagManager));
$server->addPlugin(new SharesPlugin(
$objectTree,
$this->userSession,
Expand Down
51 changes: 5 additions & 46 deletions apps/dav/lib/Connector/Sabre/TagsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,11 @@
*/
use OCP\ITagManager;
use OCP\ITags;
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;
use OCP\IUserSession;
use OCP\Activity\IManager;
use OCP\EventDispatcher\IEventDispatcher;

class TagsPlugin extends \Sabre\DAV\ServerPlugin {

Expand Down Expand Up @@ -266,11 +262,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 @@ -282,39 +276,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) {
}
}
}
8 changes: 1 addition & 7 deletions apps/dav/lib/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
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\AppFramework\Utility\ITimeFactory;
use OCP\Defaults;
Expand All @@ -75,7 +74,6 @@
use OCP\IConfig;
use OCP\IPreview;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Mail\IMailer;
use OCP\Profiler\IProfiler;
Expand Down Expand Up @@ -259,8 +257,6 @@ public function __construct(
// 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 = Filesystem::getView();
$config = \OCP\Server::get(IConfig::class);
Expand Down Expand Up @@ -294,9 +290,7 @@ public function __construct(
new QuotaPlugin($view));
}
$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
8 changes: 4 additions & 4 deletions apps/dav/tests/unit/Connector/Sabre/TagsPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ public function testUpdateFav(): void {

// set favorite tag
$this->tagger->expects($this->once())
->method('tagAs')
->with(123, self::TAG_FAVORITE);
->method('addToFavorites')
->with(123, '/dummypath');

// properties to set
$propPatch = new \Sabre\DAV\PropPatch([
Expand All @@ -424,8 +424,8 @@ public function testUpdateFav(): void {
// unfavorite now
// set favorite tag
$this->tagger->expects($this->once())
->method('unTag')
->with(123, self::TAG_FAVORITE);
->method('removeFromFavorites')
->with(123, '/dummypath');

// properties to set
$propPatch = new \Sabre\DAV\PropPatch([
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
52 changes: 4 additions & 48 deletions apps/files/lib/Service/TagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,18 @@
*/
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;

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

public function __construct(
private IUserSession $userSession,
private IManager $activityManager,
private ?ITags $tagger,
private ?Folder $homeFolder,
private IEventDispatcher $dispatcher,
) {
}

Expand Down Expand Up @@ -61,14 +51,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 @@ -77,40 +69,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) {
}
}
}
17 changes: 3 additions & 14 deletions apps/files/tests/Service/TagServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,8 @@ protected function setUp(): void {
protected function getTagService(array $methods = []) {
return $this->getMockBuilder(TagService::class)
->setConstructorArgs([
$this->userSession,
$this->activityManager,
$this->tagger,
$this->root,
$this->dispatcher,
])
->setMethods($methods)
->getMock();
Expand All @@ -107,9 +104,6 @@ public function testUpdateFileTags(): void {
$tag1 = 'tag1';
$tag2 = 'tag2';

$this->tagService->expects($this->never())
->method('addActivity');

$subdir = $this->root->newFolder('subdir');
$testFile = $subdir->newFile('test.txt');
$testFile->putContent('test contents');
Expand Down Expand Up @@ -148,19 +142,14 @@ public function testFavoriteActivity(): void {
$subdir = $this->root->newFolder('subdir');
$file = $subdir->newFile('test.txt');

$this->tagService->expects($this->exactly(2))
->method('addActivity')
->withConsecutive(
[true, $file->getId(), 'subdir/test.txt'],
[false, $file->getId(), 'subdir/test.txt']
);

// set tags
$this->tagService->updateFileTags('subdir/test.txt', [ITags::TAG_FAVORITE]);

$this->assertEquals([$file->getId()], $this->tagger->getFavorites());

// remove tag
$this->tagService->updateFileTags('subdir/test.txt', []);

$this->assertEquals([], $this->tagger->getFavorites());

$subdir->delete();
}
Expand Down
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 30b0797

Please sign in to comment.