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 205809a
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 134 deletions.
1 change: 1 addition & 0 deletions apps/dav/appinfo/v1/publicwebdav.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
\OC::$server->getRequest(),
\OC::$server->getPreviewManager(),
$eventDispatcher,
\OC::$server->getActivityManager(),
\OC::$server->getL10N('dav')
);

Expand Down
1 change: 1 addition & 0 deletions apps/dav/appinfo/v1/webdav.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
\OC::$server->getRequest(),
\OC::$server->getPreviewManager(),
$dispatcher,
\OC::$server->getActivityManager(),
\OC::$server->getL10N('dav')
);

Expand Down
2 changes: 2 additions & 0 deletions apps/dav/appinfo/v2/publicremote.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCA\DAV\Storage\PublicOwnerWrapper;
use OCA\DAV\Storage\PublicShareWrapper;
use OCA\FederatedFileSharing\FederatedShareProvider;
use OCP\Activity\IManager as IActivityManager;
use OCP\BeforeSabrePubliclyLoadedEvent;
use OCP\Constants;
use OCP\EventDispatcher\IEventDispatcher;
Expand Down Expand Up @@ -68,6 +69,7 @@
$request,
Server::get(IPreview::class),
$eventDispatcher,
Server::get(IActivityManager::class),
$l10nFactory->get('dav'),
);

Expand Down
4 changes: 3 additions & 1 deletion apps/dav/lib/Connector/Sabre/ServerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCA\DAV\DAV\ViewOnlyPlugin;
use OCA\DAV\Files\ErrorPagePlugin;
use OCA\Theming\ThemingDefaults;
use OCP\Activity\IManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Folder;
use OCP\Files\IFilenameValidator;
Expand Down Expand Up @@ -41,6 +42,7 @@ public function __construct(
private IRequest $request,
private IPreview $previewManager,
private IEventDispatcher $eventDispatcher,
private IManager $activityManager,
private IL10N $l10n,
) {
}
Expand Down Expand Up @@ -134,7 +136,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, $this->userSession, $this->eventDispatcher, $this->activityManager));
$server->addPlugin(new SharesPlugin(
$objectTree,
$this->userSession,
Expand Down
49 changes: 4 additions & 45 deletions apps/dav/lib/Connector/Sabre/TagsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
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\ITagManager;
use OCP\ITags;
use OCP\IUserSession;
use Sabre\DAV\PropFind;
use Sabre\DAV\PropPatch;
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) {
}
}
}
10 changes: 4 additions & 6 deletions apps/dav/lib/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
use OCP\IConfig;
use OCP\IPreview;
use OCP\IRequest;
use OCP\IUser;
use OCP\ITagManager;
use OCP\IUserSession;
use OCP\Mail\IMailer;
use OCP\Profiler\IProfiler;
Expand Down Expand Up @@ -257,10 +257,10 @@ public function __construct(
\OC::$server->getUserFolder(),
));
// custom properties plugin must be the last one
$eventDispatcher = \OCP\Server::get(IEventDispatcher::class);
$activityManager = \OCP\Server::get(IManager::class);
$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 +294,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->get(ITagManager::class), $userSession, $eventDispatcher, $activityManager)
);

// 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 205809a

Please sign in to comment.