Skip to content

Commit

Permalink
refactor: depricate getNumberOfUnreadCommentsForFolder and redo it's …
Browse files Browse the repository at this point in the history
…implementation based on getNumberOfUnreadCommentsForObjects

Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Mar 7, 2024
1 parent be3449c commit eb5832b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 81 deletions.
86 changes: 26 additions & 60 deletions lib/private/Comments/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@
namespace OC\Comments;

use Doctrine\DBAL\Exception\DriverException;
use OCA\DAV\Connector\Sabre\File;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Comments\CommentsEvent;
use OCP\Comments\IComment;
use OCP\Comments\ICommentsEventHandler;
use OCP\Comments\ICommentsManager;
use OCP\Comments\NotFoundException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\FileInfo;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IEmojiHelper;
Expand All @@ -46,12 +50,6 @@
use Psr\Log\LoggerInterface;

class Manager implements ICommentsManager {
protected IDBConnection $dbConn;
protected LoggerInterface $logger;
protected IConfig $config;
protected ITimeFactory $timeFactory;
protected IEmojiHelper $emojiHelper;
protected IInitialStateService $initialStateService;
/** @var IComment[] */
protected array $commentsCache = [];

Expand All @@ -64,18 +62,15 @@ class Manager implements ICommentsManager {
/** @var \Closure[] */
protected array $displayNameResolvers = [];

public function __construct(IDBConnection $dbConn,
LoggerInterface $logger,
IConfig $config,
ITimeFactory $timeFactory,
IEmojiHelper $emojiHelper,
IInitialStateService $initialStateService) {
$this->dbConn = $dbConn;
$this->logger = $logger;
$this->config = $config;
$this->timeFactory = $timeFactory;
$this->emojiHelper = $emojiHelper;
$this->initialStateService = $initialStateService;
public function __construct(
protected IDBConnection $dbConn,
protected LoggerInterface $logger,
protected IConfig $config,
protected ITimeFactory $timeFactory,
protected IEmojiHelper $emojiHelper,
protected IInitialStateService $initialStateService,
protected IRootFolder $rootFolder,
) {
}

/**
Expand Down Expand Up @@ -820,54 +815,25 @@ public function getLastCommentDateByActor(
/**
* Get the number of unread comments for all files in a folder
*
* This is unused since 8bd39fccf411195839f2dadee085fad18ec52c23
*
* @param int $folderId
* @param IUser $user
* @return array [$fileId => $unreadCount]
*/
public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) {
$qb = $this->dbConn->getQueryBuilder();

$query = $qb->select('f.fileid')
->addSelect($qb->func()->count('c.id', 'num_ids'))
->from('filecache', 'f')
->leftJoin('f', 'comments', 'c', $qb->expr()->andX(
$qb->expr()->eq('f.fileid', $qb->expr()->castColumn('c.object_id', IQueryBuilder::PARAM_INT)),
$qb->expr()->eq('c.object_type', $qb->createNamedParameter('files'))
))
->leftJoin('c', 'comments_read_markers', 'm', $qb->expr()->andX(
$qb->expr()->eq('c.object_id', 'm.object_id'),
$qb->expr()->eq('m.object_type', $qb->createNamedParameter('files'))
))
->where(
$qb->expr()->andX(
$qb->expr()->eq('f.parent', $qb->createNamedParameter($folderId)),
$qb->expr()->orX(
$qb->expr()->eq('c.object_type', $qb->createNamedParameter('files')),
$qb->expr()->isNull('c.object_type')
),
$qb->expr()->orX(
$qb->expr()->eq('m.object_type', $qb->createNamedParameter('files')),
$qb->expr()->isNull('m.object_type')
),
$qb->expr()->orX(
$qb->expr()->eq('m.user_id', $qb->createNamedParameter($user->getUID())),
$qb->expr()->isNull('m.user_id')
),
$qb->expr()->orX(
$qb->expr()->gt('c.creation_timestamp', 'm.marker_datetime'),
$qb->expr()->isNull('m.marker_datetime')
)
)
)->groupBy('f.fileid');

$resultStatement = $query->execute();

$results = [];
while ($row = $resultStatement->fetch()) {
$results[$row['fileid']] = (int) $row['num_ids'];
$directory = $this->rootFolder->getFirstNodeById($folderId);
if (!$directory instanceof Folder) {
return [];
}
$resultStatement->closeCursor();
return $results;
$children = $directory->getDirectoryListing();
$ids = array_map(fn (FileInfo $child) => (string) $child->getId(), $children);

$ids[] = (string) $directory->getId();
$counts = $this->getNumberOfUnreadCommentsForObjects('files', $ids, $user);
return array_filter($counts, function (int $count) {
return $count > 0;
});
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/public/Comments/ICommentsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ public function getLastCommentDateByActor(
* @param IUser $user
* @return array [$fileId => $unreadCount]
* @since 12.0.0
* @deprecated 29.0.0 use getNumberOfUnreadCommentsForObjects instead
*/
public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user);

Expand Down
46 changes: 25 additions & 21 deletions tests/lib/Comments/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use OCP\Comments\ICommentsEventHandler;
use OCP\Comments\ICommentsManager;
use OCP\Comments\NotFoundException;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IInitialStateService;
Expand All @@ -26,11 +28,14 @@
class ManagerTest extends TestCase {
/** @var IDBConnection */
private $connection;
/** @var \PHPUnit\Framework\MockObject\MockObject|IRootFolder */
private $rootFolder;

protected function setUp(): void {
parent::setUp();

$this->connection = \OC::$server->getDatabaseConnection();
$this->rootFolder = $this->createMock(IRootFolder::class);

$sql = $this->connection->getDatabasePlatform()->getTruncateTableSQL('`*PREFIX*comments`');
$this->connection->prepare($sql)->execute();
Expand Down Expand Up @@ -80,7 +85,8 @@ protected function getManager() {
$this->createMock(IConfig::class),
$this->createMock(ITimeFactory::class),
new EmojiHelper($this->connection),
$this->createMock(IInitialStateService::class)
$this->createMock(IInitialStateService::class),
$this->rootFolder,
);
}

Expand Down Expand Up @@ -329,23 +335,19 @@ public function testGetNumberOfCommentsForObject() {
}

public function testGetNumberOfUnreadCommentsForFolder() {
$query = $this->connection->getQueryBuilder();
$query->insert('filecache')
->values([
'parent' => $query->createNamedParameter(1000),
'size' => $query->createNamedParameter(10),
'mtime' => $query->createNamedParameter(10),
'storage_mtime' => $query->createNamedParameter(10),
'path' => $query->createParameter('path'),
'path_hash' => $query->createParameter('path'),
]);

$fileIds = [];
for ($i = 0; $i < 4; $i++) {
$query->setParameter('path', 'path_' . $i);
$query->execute();
$fileIds[] = $query->getLastInsertId();
}
$folder = $this->createMock(Folder::class);
$fileIds = range(1111, 1114);
$children = array_map(function (int $id) {
$file = $this->createMock(Folder::class);
$file->method('getId')
->willReturn($id);
return $file;
}, $fileIds);
$folder->method('getId')->willReturn(1000);
$folder->method('getDirectoryListing')->willReturn($children);
$this->rootFolder->method('getFirstNodeById')
->with($folder->getId())
->willReturn($folder);

// 2 comment for 1111 with 1 before read marker
// 2 comments for 1112 with no read marker
Expand All @@ -367,7 +369,7 @@ public function testGetNumberOfUnreadCommentsForFolder() {
$manager->setReadMark('files', (string) $fileIds[0], (new \DateTime())->modify('-1 days'), $user);
$manager->setReadMark('files', (string) $fileIds[2], (new \DateTime()), $user);

$amount = $manager->getNumberOfUnreadCommentsForFolder(1000, $user);
$amount = $manager->getNumberOfUnreadCommentsForFolder($folder->getId(), $user);
$this->assertEquals([
$fileIds[0] => 1,
$fileIds[1] => 2,
Expand Down Expand Up @@ -750,7 +752,8 @@ public function testDeleteCommentsExpiredAtObjectTypeAndId(): void {
$this->createMock(IConfig::class),
Server::get(ITimeFactory::class),
new EmojiHelper($this->connection),
$this->createMock(IInitialStateService::class)
$this->createMock(IInitialStateService::class),
$this->rootFolder,
);

// just to make sure they are really set, with correct actor data
Expand Down Expand Up @@ -795,7 +798,8 @@ public function testDeleteCommentsExpiredAtObjectType(): void {
$this->createMock(IConfig::class),
Server::get(ITimeFactory::class),
new EmojiHelper($this->connection),
$this->createMock(IInitialStateService::class)
$this->createMock(IInitialStateService::class),
$this->rootFolder,
);

$deleted = $manager->deleteCommentsExpiredAtObject('files');
Expand Down

0 comments on commit eb5832b

Please sign in to comment.