Skip to content

Commit a846eea

Browse files
artongenickvergessen
authored andcommitted
fix(files_sharing): Fix getting shares by path
- Match both parent and child share - Improve path matching with `/_%`. - Filter share by path in `resolveSharesForRecipient` Signed-off-by: Louis Chmn <louis@chmn.me>
1 parent ca1f809 commit a846eea

File tree

1 file changed

+50
-8
lines changed

1 file changed

+50
-8
lines changed

lib/Share/RoomShareProvider.php

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use OC\Files\Cache\Cache;
1212
use OC\User\LazyUser;
13+
use OCA\Talk\Config;
1314
use OCA\Talk\Events\BeforeDuplicateShareSentEvent;
1415
use OCA\Talk\Exceptions\ParticipantNotFoundException;
1516
use OCA\Talk\Exceptions\RoomNotFoundException;
@@ -71,6 +72,7 @@ public function __construct(
7172
private IL10N $l,
7273
private IMimeTypeLoader $mimeTypeLoader,
7374
private IUserManager $userManager,
75+
protected Config $config,
7476
) {
7577
$this->sharesByIdCache = new CappedMemoryCache();
7678
}
@@ -717,7 +719,7 @@ public function getSharesByIds(array $ids, ?string $recipientId = null): array {
717719
* @param bool $allRoomShares indicates that the passed in shares are all room shares for the user
718720
* @return list<IShare>
719721
*/
720-
private function resolveSharesForRecipient(array $shareMap, string $userId, bool $allRoomShares = false): array {
722+
private function resolveSharesForRecipient(array $shareMap, string $userId, ?string $path = null, bool $forChildren = false): array {
721723
$qb = $this->dbConnection->getQueryBuilder();
722724

723725
$query = $qb->select('parent', 'permissions', 'file_target')
@@ -730,7 +732,17 @@ private function resolveSharesForRecipient(array $shareMap, string $userId, bool
730732
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
731733
));
732734

733-
if ($allRoomShares) {
735+
if ($path !== null) {
736+
$path = str_replace('/' . $userId . '/files', '', $path);
737+
$path = rtrim($path, '/');
738+
739+
if ($forChildren) {
740+
$qb->andWhere($qb->expr()->like('file_target', $qb->createNamedParameter($this->dbConnection->escapeLikeParameter($path) . '/_%')));
741+
} else {
742+
$nonChildPath = $path === '' ? '/' : $path;
743+
$qb->andWhere($qb->expr()->eq('file_target', $qb->createNamedParameter($nonChildPath)));
744+
}
745+
734746
$stmt = $query->executeQuery();
735747

736748
while ($data = $stmt->fetchAssociative()) {
@@ -858,14 +870,44 @@ private function _getSharedWith(
858870
}
859871

860872
if ($path !== null) {
861-
$qb->leftJoin('s', 'share', 'sc', $qb->expr()->eq('s.id', 'sc.parent'))
862-
->andWhere($qb->expr()->eq('sc.share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERROOM)))
863-
->andWhere($qb->expr()->eq('sc.share_with', $qb->createNamedParameter($userId)));
873+
$onClause = $qb->expr()->andX(
874+
$qb->expr()->eq('sc.parent', 's.id'),
875+
$qb->expr()->eq('sc.share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERROOM)),
876+
$qb->expr()->eq('sc.share_with', $qb->createNamedParameter($userId)),
877+
);
878+
$qb->leftJoin('s', 'share', 'sc', $onClause);
879+
880+
$path = str_replace('/' . $userId . '/files', '', $path);
881+
$path = rtrim($path, '/');
864882

865883
if ($forChildren) {
866-
$qb->andWhere($qb->expr()->like('sc.file_target', $qb->createNamedParameter($this->dbConnection->escapeLikeParameter($path) . '_%')));
884+
$attachmentFolder ??= $this->config->getAttachmentFolder($userId);
885+
$escapedAttachmentFolder = preg_quote($attachmentFolder, '/');
886+
$pathWithPlaceholder = preg_replace("/^$escapedAttachmentFolder/", self::TALK_FOLDER_PLACEHOLDER, $path);
887+
888+
$childPathTemplate = $this->dbConnection->escapeLikeParameter($path) . '/_%';
889+
$childPathTemplatePlaceholder = $this->dbConnection->escapeLikeParameter($pathWithPlaceholder) . '/_%';
890+
891+
$qb->andWhere(
892+
$qb->expr()->orX(
893+
$qb->expr()->like('sc.file_target', $qb->createNamedParameter($childPathTemplate, IQueryBuilder::PARAM_STR)),
894+
$qb->expr()->andX(
895+
$qb->expr()->isNull('sc.file_target'),
896+
$qb->expr()->like('s.file_target', $qb->createNamedParameter($childPathTemplatePlaceholder, IQueryBuilder::PARAM_STR)),
897+
),
898+
),
899+
);
867900
} else {
868-
$qb->andWhere($qb->expr()->eq('sc.file_target', $qb->createNamedParameter($path)));
901+
$nonChildPath = $path === '' ? '/' : $path;
902+
$qb->andWhere(
903+
$qb->expr()->orX(
904+
$qb->expr()->eq('sc.file_target', $qb->createNamedParameter($nonChildPath, IQueryBuilder::PARAM_STR)),
905+
$qb->expr()->andX(
906+
$qb->expr()->isNull('sc.file_target'),
907+
$qb->expr()->eq('s.file_target', $qb->createNamedParameter($nonChildPath, IQueryBuilder::PARAM_STR)),
908+
),
909+
),
910+
);
869911
}
870912
}
871913

@@ -896,7 +938,7 @@ private function _getSharedWith(
896938
$cursor->closeCursor();
897939
}
898940

899-
$shares = $this->resolveSharesForRecipient($shares, $userId, true);
941+
$shares = $this->resolveSharesForRecipient($shares, $userId, $path, $forChildren);
900942

901943
return $shares;
902944
}

0 commit comments

Comments
 (0)