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 0636e84
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 42 deletions.
60 changes: 18 additions & 42 deletions lib/private/Comments/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,25 @@
namespace OC\Comments;

use Doctrine\DBAL\Exception\DriverException;
use OCA\DAV\Connector\Sabre\Directory;
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;
use OCP\IInitialStateService;
use OCP\IUser;
use OCP\PreConditionNotMetException;
use OCP\Server;
use OCP\Util;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -820,54 +826,24 @@ 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'];
// todo, replace logic with exception once deprecation period is over
$rootFolder = Server::get(IRootFolder::class);
$directory = $rootFolder->getFirstNodeById($folderId);
if (!$directory instanceof Folder) {
return [];
}
$resultStatement->closeCursor();
return $results;
$children = $directory->getChildren();

Check failure

Code scanning / Psalm

UndefinedInterfaceMethod Error

Method OCP\Files\Folder::getChildren does not exist

Check failure on line 842 in lib/private/Comments/Manager.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedInterfaceMethod

lib/private/Comments/Manager.php:842:27: UndefinedInterfaceMethod: Method OCP\Files\Folder::getChildren does not exist (see https://psalm.dev/181)
$ids = array_map(fn (FileInfo $child) => (string) $child->getId(), $children);

$ids[] = (string) $directory->getId();
return $this->getNumberOfUnreadCommentsForObjects('files', $ids, $user);
}

/**
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

0 comments on commit 0636e84

Please sign in to comment.