Skip to content

Commit 3d0daaa

Browse files
committed
wip
1 parent 8718896 commit 3d0daaa

1 file changed

Lines changed: 72 additions & 101 deletions

File tree

lib/FilesHooks.php

Lines changed: 72 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
use OCP\BackgroundJob\IJobList;
1919
use OCP\Constants;
2020
use OCP\Files\Config\IUserMountCache;
21+
use OCP\Files\File;
2122
use OCP\Files\IRootFolder;
2223
use OCP\Files\Node;
2324
use OCP\Files\NotFoundException;
25+
use OCP\Files\Storage\ISharedStorage;
2426
use OCP\IConfig;
2527
use OCP\IDBConnection;
2628
use OCP\IGroup;
@@ -655,36 +657,31 @@ public function share($share) {
655657
case IShare::TYPE_USER:
656658
$this->shareWithUser(
657659
$share->getSharedWith(),
658-
$share->getNodeId(),
659-
$share->getNodeType(),
660+
$share->getNode(),
660661
$share->getTarget()
661662
);
662663
break;
663664
case IShare::TYPE_GROUP:
664665
$this->shareWithGroup(
665666
$share->getSharedWith(),
666-
$share->getNodeId(),
667-
$share->getNodeType(),
667+
$share->getNode(),
668668
$share->getTarget(),
669669
(int)$share->getId()
670670
);
671671
break;
672672
case IShare::TYPE_CIRCLE:
673673
$this->shareWithTeam(
674674
$share->getSharedWith(),
675-
$share->getNodeId(),
676-
$share->getNodeType(),
675+
$share->getNode(),
677676
$share->getTarget(),
678677
(int)$share->getId(),
679678
$share->getSharedBy(),
680679
);
681680
break;
682681
case IShare::TYPE_LINK:
683682
$this->shareByLink(
684-
$share->getNodeId(),
685-
$share->getNodeType(),
683+
$share->getNode(),
686684
$share->getSharedBy(),
687-
$share->getNode()->getPath(),
688685
);
689686
break;
690687
default:
@@ -697,21 +694,20 @@ public function share($share) {
697694
* Sharing a file or folder with a user
698695
*
699696
* @param string $shareWith
700-
* @param int $fileSource File ID that is being shared
701-
* @param string $itemType File type that is being shared (file or folder)
697+
* @param Node $fileSource File that is being shared
702698
* @param string $fileTarget File path
703699
*/
704-
protected function shareWithUser($shareWith, $fileSource, $itemType, $fileTarget) {
700+
protected function shareWithUser(string $shareWith, Node $fileSource, string $fileTarget) {
705701
// User performing the share
706-
$this->shareNotificationForSharer('shared_user_self', $shareWith, $fileSource, $itemType);
702+
$this->shareNotificationForSharer('shared_user_self', $shareWith, $fileSource);
707703
if ($this->currentUser->getUID() !== null) {
708-
$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), 'reshared_user_by', $shareWith, $fileSource, $itemType);
704+
$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), 'reshared_user_by', $shareWith, $fileSource);
709705
}
710706

711707
// New shared user
712708
$this->addNotificationsForUser(
713-
$shareWith, 'shared_with_by', [[$fileSource => $fileTarget], $this->currentUser->getUserIdentifier()],
714-
(int)$fileSource, $fileTarget, $itemType === 'file',
709+
$shareWith, 'shared_with_by', [[$fileSource->getId() => $fileTarget], $this->currentUser->getUserIdentifier()],
710+
$fileSource->getId(), $fileTarget, $fileSource instanceof File,
715711
$this->userSettings->getUserSetting($shareWith, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($shareWith, 'setting', 'batchtime') : false,
716712
(bool)$this->userSettings->getUserSetting($shareWith, 'notification', Files_Sharing::TYPE_SHARED)
717713
);
@@ -721,29 +717,28 @@ protected function shareWithUser($shareWith, $fileSource, $itemType, $fileTarget
721717
* Sharing a file or folder with a group
722718
*
723719
* @param string $shareWith
724-
* @param int $fileSource File ID that is being shared
725-
* @param string $itemType File type that is being shared (file or folder)
720+
* @param Node $fileSource File that is being shared
726721
* @param string $fileTarget File path
727722
* @param int $shareId The Share ID of this share
728723
*/
729-
protected function shareWithGroup($shareWith, $fileSource, $itemType, $fileTarget, $shareId) {
724+
protected function shareWithGroup(string $shareWith, Node $fileSource, string $fileTarget, int $shareId) {
730725
// Members of the new group
731726
$group = $this->groupManager->get($shareWith);
732727
if (!($group instanceof IGroup)) {
733728
return;
734729
}
735730

736731
// User performing the share
737-
$this->shareNotificationForSharer('shared_group_self', $shareWith, $fileSource, $itemType);
732+
$this->shareNotificationForSharer('shared_group_self', $shareWith, $fileSource);
738733
if ($this->currentUser->getUID() !== null) {
739-
$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), 'reshared_group_by', $shareWith, $fileSource, $itemType);
734+
$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), 'reshared_group_by', $shareWith, $fileSource);
740735
}
741736

742737
$offset = 0;
743738
$users = $group->searchUsers('', self::USER_BATCH_SIZE, $offset);
744739
while (!empty($users)) {
745740
$userIds = array_map(fn (IUser $user) => $user->getUID(), $users);
746-
$this->addNotificationsForUsers($userIds, 'shared_with_by', $fileSource, $itemType, $fileTarget, $shareId);
741+
$this->addNotificationsForUsers($userIds, 'shared_with_by', $fileSource->getId(), $fileSource->getType(), $fileTarget, $shareId);
747742
$offset += self::USER_BATCH_SIZE;
748743
$users = $group->searchUsers('', self::USER_BATCH_SIZE, $offset);
749744
}
@@ -752,24 +747,17 @@ protected function shareWithGroup($shareWith, $fileSource, $itemType, $fileTarge
752747
/**
753748
* Sharing a file or folder via link/public
754749
*
755-
* @param int $fileSource File ID that is being shared
756-
* @param string $itemType File type that is being shared (file or folder)
750+
* @param Node $fileSource File that is being shared
757751
* @param string $linkOwner
758752
*/
759-
protected function shareByLink($fileSource, $itemType, $linkOwner, $absolutePath) {
760-
$this->view->chroot('/' . $linkOwner . '/files');
761-
762-
try {
763-
$path = $this->view->getRelativePath($absolutePath);
764-
} catch (NotFoundException $e) {
765-
return;
766-
}
767-
768-
$this->shareNotificationForOriginalOwners($linkOwner, 'reshared_link_by', '', $fileSource, $itemType);
753+
protected function shareByLink(Node $fileSource, string $linkOwner) {
754+
$ownerFolder = $this->rootFolder->getUserFolder($linkOwner);
755+
$relativePath = $ownerFolder->getRelativePath($fileSource->getPath());
756+
$this->shareNotificationForOriginalOwners($linkOwner, 'reshared_link_by', '', $fileSource);
769757

770758
$this->addNotificationsForUser(
771-
$linkOwner, 'shared_link_self', [[$fileSource => $path]],
772-
(int)$fileSource, $path, $itemType === 'file',
759+
$linkOwner, 'shared_link_self', [[$fileSource->getId() => $relativePath]],
760+
$fileSource->getId(), $relativePath, $fileSource instanceof File,
773761
$this->userSettings->getUserSetting($linkOwner, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($linkOwner, 'setting', 'batchtime') : false,
774762
(bool)$this->userSettings->getUserSetting($linkOwner, 'notification', Files_Sharing::TYPE_SHARED)
775763
);
@@ -779,12 +767,11 @@ protected function shareByLink($fileSource, $itemType, $linkOwner, $absolutePath
779767
* Sharing a file or folder with a team
780768
*
781769
* @param string $shareWith
782-
* @param int $fileSource File ID that is being shared
783-
* @param string $itemType File type that is being shared (file or folder)
770+
* @param Node $fileSource File that is being shared
784771
* @param string $fileTarget File path
785772
* @param int $shareId The Share ID of this share
786773
*/
787-
protected function shareWithTeam(string $shareWith, int $fileSource, string $itemType, string $fileTarget, int $shareId, string $sharer): void {
774+
protected function shareWithTeam(string $shareWith, Node $fileSource, string $fileTarget, int $shareId, string $sharer): void {
788775
if ($this->teamManager === null) {
789776
return;
790777
}
@@ -802,13 +789,13 @@ protected function shareWithTeam(string $shareWith, int $fileSource, string $ite
802789
}
803790

804791
// Activity for user performing the share
805-
$this->shareNotificationForSharer('shared_team_self', $shareWith, $fileSource, $itemType);
792+
$this->shareNotificationForSharer('shared_team_self', $shareWith, $fileSource);
806793
// Activity for original owner of the file (re-sharing)
807794
if ($this->currentUser->getUID() !== null) {
808-
$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), 're-shared_team_by', $shareWith, $fileSource, $itemType);
795+
$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), 're-shared_team_by', $shareWith, $fileSource);
809796
}
810797
// Activity for all affected users
811-
$this->addNotificationsForUsers($userIds, 'shared_with_by', $fileSource, $itemType, $fileTarget, $shareId);
798+
$this->addNotificationsForUsers($userIds, 'shared_with_by', $fileSource->getId(), $fileSource->getType(), $fileTarget, $shareId);
812799
}
813800

814801
/**
@@ -868,11 +855,11 @@ protected function unshareFromUser(IShare $share) {
868855
}
869856

870857
// User performing the share
871-
$this->shareNotificationForSharer($actionSharer, $share->getSharedWith(), $share->getNodeId(), $share->getNodeType());
858+
$this->shareNotificationForSharer($actionSharer, $share->getSharedWith(), $share->getNode());
872859

873860
// Owner
874861
if ($this->currentUser->getUID() !== null) {
875-
$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), $actionOwner, $share->getSharedWith(), $share->getNodeId(), $share->getNodeType());
862+
$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), $actionOwner, $share->getSharedWith(), $share->getNode());
876863
}
877864

878865
// Recipient
@@ -892,11 +879,11 @@ protected function unshareFromUser(IShare $share) {
892879
*/
893880
protected function selfUnshareFromUser(IShare $share) {
894881
// User performing the share
895-
$this->shareNotificationForSharer('self_unshared', $share->getSharedWith(), $share->getNodeId(), $share->getNodeType());
882+
$this->shareNotificationForSharer('self_unshared', $share->getSharedWith(), $share->getNode());
896883

897884
// Owner
898885
if ($this->currentUser->getUID() !== null) {
899-
$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), 'self_unshared_by', $share->getSharedWith(), $share->getNodeId(), $share->getNodeType());
886+
$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), 'self_unshared_by', $share->getSharedWith(), $share->getNode());
900887
}
901888
}
902889

@@ -924,9 +911,9 @@ protected function unshareFromGroup(IShare $share) {
924911
}
925912

926913
// User performing the share
927-
$this->shareNotificationForSharer($actionSharer, $share->getSharedWith(), $share->getNodeId(), $share->getNodeType());
914+
$this->shareNotificationForSharer($actionSharer, $share->getSharedWith(), $share->getNode());
928915
if ($this->currentUser->getUID() !== null) {
929-
$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), $actionOwner, $share->getSharedWith(), $share->getNodeId(), $share->getNodeType());
916+
$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), $actionOwner, $share->getSharedWith(), $share->getNode());
930917
}
931918

932919
$offset = 0;
@@ -949,11 +936,11 @@ protected function unshareFromGroup(IShare $share) {
949936
*/
950937
protected function unshareFromSelfGroup(IShare $share) {
951938
// User performing the unshare
952-
$this->shareNotificationForSharer('self_unshared', $this->currentUser->getUID(), $share->getNodeId(), $share->getNodeType());
939+
$this->shareNotificationForSharer('self_unshared', $this->currentUser->getUID(), $share->getNode());
953940

954941
// Owner
955942
if ($this->currentUser->getUID() !== null) {
956-
$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), 'self_unshared_by', $this->currentUser->getUID(), $share->getNodeId(), $share->getNodeType());
943+
$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), 'self_unshared_by', $this->currentUser->getUID(), $share->getNode());
957944
}
958945
}
959946

@@ -1060,26 +1047,19 @@ protected function fixPathsForShareExceptions(array $affectedUsers, $shareId) {
10601047
*
10611048
* @param string $subject
10621049
* @param string $shareWith
1063-
* @param int $fileSource
1064-
* @param string $itemType
1050+
* @param Node $fileSource
10651051
*/
1066-
protected function shareNotificationForSharer($subject, $shareWith, $fileSource, $itemType) {
1052+
protected function shareNotificationForSharer(string $subject, string $shareWith, Node $fileSource) {
10671053
$sharer = $this->currentUser->getUID();
10681054
if ($sharer === null) {
10691055
return;
10701056
}
1071-
1072-
$this->view->chroot('/' . $sharer . '/files');
1073-
1074-
try {
1075-
$path = $this->view->getPath($fileSource);
1076-
} catch (NotFoundException $e) {
1077-
return;
1078-
}
1057+
$sharerFolder = $this->rootFolder->getUserFolder($sharer);
1058+
$path = $sharerFolder->getRelativePath($sharerFolder->getPath());
10791059

10801060
$this->addNotificationsForUser(
1081-
$sharer, $subject, [[$fileSource => $path], $shareWith],
1082-
$fileSource, $path, ($itemType === 'file'),
1061+
$sharer, $subject, [[$fileSource->getId() => $path], $shareWith],
1062+
$fileSource->getId(), $path, $fileSource instanceof File,
10831063
$this->userSettings->getUserSetting($sharer, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($sharer, 'setting', 'batchtime') : false,
10841064
(bool)$this->userSettings->getUserSetting($sharer, 'notification', Files_Sharing::TYPE_SHARED)
10851065
);
@@ -1091,21 +1071,16 @@ protected function shareNotificationForSharer($subject, $shareWith, $fileSource,
10911071
* @param string $owner
10921072
* @param string $subject
10931073
* @param string $shareWith
1094-
* @param int $fileSource
1095-
* @param string $itemType
1074+
* @param Node $fileSource
10961075
*/
1097-
protected function reshareNotificationForSharer($owner, $subject, $shareWith, $fileSource, $itemType) {
1098-
$this->view->chroot('/' . $owner . '/files');
1099-
1100-
try {
1101-
$path = $this->view->getPath($fileSource);
1102-
} catch (NotFoundException $e) {
1103-
return;
1104-
}
1076+
protected function reshareNotificationForSharer($owner, $subject, $shareWith, $fileSource) {
1077+
$ownerNode = $this->getNodeForUser($owner, $fileSource);
1078+
$ownerFolder = $this->rootFolder->getUserFolder($owner);
1079+
$path = $ownerFolder->getRelativePath($ownerNode->getPath());
11051080

11061081
$this->addNotificationsForUser(
1107-
$owner, $subject, [[$fileSource => $path], $this->currentUser->getUserIdentifier(), $shareWith],
1108-
$fileSource, $path, ($itemType === 'file'),
1082+
$owner, $subject, [[$ownerNode->getId() => $path], $this->currentUser->getUserIdentifier(), $shareWith],
1083+
$ownerNode->getId(), $path, $ownerNode instanceof File,
11091084
$this->userSettings->getUserSetting($owner, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($owner, 'setting', 'batchtime') : false,
11101085
(bool)$this->userSettings->getUserSetting($owner, 'notification', Files_Sharing::TYPE_SHARED)
11111086
);
@@ -1117,50 +1092,34 @@ protected function reshareNotificationForSharer($owner, $subject, $shareWith, $f
11171092
* @param string $currentOwner
11181093
* @param string $subject
11191094
* @param string $shareWith
1120-
* @param int $fileSource
1121-
* @param string $itemType
1095+
* @param Node $fileSource
11221096
*/
1123-
protected function shareNotificationForOriginalOwners($currentOwner, $subject, $shareWith, $fileSource, $itemType) {
1124-
// Get the full path of the current user
1125-
$this->view->chroot('/' . $currentOwner . '/files');
1126-
1127-
try {
1128-
$path = $this->view->getPath($fileSource);
1129-
} catch (NotFoundException $e) {
1097+
protected function shareNotificationForOriginalOwners(string $currentOwner, string $subject, string $shareWith, Node $fileSource) {
1098+
$ownerNode = $this->getNodeForUser($currentOwner, $fileSource);
1099+
if (!$ownerNode) {
11301100
return;
11311101
}
11321102

11331103
/**
11341104
* Get the original owner and his path
11351105
*/
1136-
$owner = $this->view->getOwner($path);
1106+
$owner = $ownerNode->getOwner();
11371107
if ($owner !== $currentOwner) {
1138-
$this->reshareNotificationForSharer($owner, $subject, $shareWith, $fileSource, $itemType);
1139-
}
1140-
1141-
/**
1142-
* Get the sharee who shared the item with the currentUser
1143-
*/
1144-
$this->view->chroot('/' . $currentOwner . '/files');
1145-
1146-
try {
1147-
$mount = $this->view->getMount($path);
1148-
} catch (NotFoundException $ex) {
1149-
return;
1108+
$this->reshareNotificationForSharer($owner, $subject, $shareWith, $ownerNode);
11501109
}
11511110

1152-
$storage = $mount->getStorage();
1153-
if (!$storage->instanceOfStorage('OCA\Files_Sharing\SharedStorage')) {
1111+
$storage = $ownerNode->getStorage();
1112+
if (!$storage->instanceOfStorage(ISharedStorage::class)) {
11541113
return;
11551114
}
11561115

11571116
/** @var \OCA\Files_Sharing\SharedStorage $storage */
11581117
$shareOwner = $storage->getSharedFrom();
1159-
if ($shareOwner === '' || $shareOwner === null || $shareOwner === $owner || $shareOwner === $currentOwner) {
1118+
if ($shareOwner === '' || $shareOwner === $owner || $shareOwner === $currentOwner) {
11601119
return;
11611120
}
11621121

1163-
$this->reshareNotificationForSharer($shareOwner, $subject, $shareWith, $fileSource, $itemType);
1122+
$this->reshareNotificationForSharer($shareOwner, $subject, $shareWith, $ownerNode);
11641123
}
11651124

11661125
/**
@@ -1417,4 +1376,16 @@ private function isDeletedNode(string $owner, int $nodeId): bool {
14171376
return true;
14181377
}
14191378
}
1379+
1380+
/**
1381+
* Get a node of a specific file for a user, or null if the user doesn't have access to the file
1382+
*/
1383+
private function getNodeForUser(string $user, Node $node): ?Node {
1384+
if (str_starts_with($node->getPath(), '/' . $user)) {
1385+
return $node;
1386+
} else {
1387+
$ownerFolder = $this->rootFolder->getUserFolder($user);
1388+
return $ownerFolder->getFirstNodeById($node->getId());
1389+
}
1390+
}
14201391
}

0 commit comments

Comments
 (0)