1313use OCA \Approval \Exceptions \OutdatedEtagException ;
1414use OCA \DAV \Connector \Sabre \Node as SabreNode ;
1515use OCP \App \IAppManager ;
16+ use OCP \Files \File ;
1617use OCP \Files \FileInfo ;
1718use OCP \Files \IRootFolder ;
1819use OCP \Files \Node ;
@@ -218,6 +219,7 @@ private function userIsAuthorizedByRule(string $userId, array $rule, string $rol
218219 * @return array
219220 */
220221 public function getPendingNodes (string $ userId , ?int $ since = null ): array {
222+ /** @var array<int, array{node: Node, ruleId: int}> $pendingNodes */
221223 $ pendingNodes = [];
222224 // get pending tags i can approve
223225 $ rules = $ this ->getBasicUserRules ($ userId , 'approvers ' );
@@ -230,27 +232,29 @@ public function getPendingNodes(string $userId, ?int $since = null): array {
230232 // this actually does not work with tag IDs, only with tag names (not even sure it's about system tags...)
231233 // $nodes = $userFolder->searchByTag($pendingTagId, $userId);
232234 foreach ($ nodeIdsWithTag as $ nodeId ) {
235+ $ nodeId = (int )$ nodeId ;
236+ if (isset ($ pendingNodes [$ nodeId ])) {
237+ continue ;
238+ }
233239 // is the node in the user storage (does the user have access to this node)?
234- $ nodeInUserStorage = $ userFolder ->getById ((int )$ nodeId );
235- if (count ($ nodeInUserStorage ) > 0 && !isset ($ pendingNodes [$ nodeId ])) {
236- $ node = $ nodeInUserStorage [0 ];
240+ $ nodeInUserStorage = $ userFolder ->getFirstNodeById ($ nodeId );
241+ if ($ nodeInUserStorage === null ) {
237242 $ pendingNodes [$ nodeId ] = [
238- 'node ' => $ node ,
243+ 'node ' => $ nodeInUserStorage ,
239244 'ruleId ' => $ ruleId ,
240245 ];
241246 }
242247 }
243248 }
244249 // get extra information
245- $ that = $ this ;
246- $ result = array_map (function ($ pendingNode ) use ($ that ) {
250+ $ result = array_map (function (array $ pendingNode ): array {
247251 $ node = $ pendingNode ['node ' ];
248252 $ ruleId = $ pendingNode ['ruleId ' ];
249253 return [
250254 'file_id ' => $ node ->getId (),
251255 'file_name ' => $ node ->getName (),
252256 'mimetype ' => $ node ->getMimetype (),
253- 'activity ' => $ that ->ruleService ->getLastAction ($ node ->getId (), $ ruleId , Application::STATE_PENDING ),
257+ 'activity ' => $ this ->ruleService ->getLastAction ($ node ->getId (), $ ruleId , Application::STATE_PENDING ),
254258
255259 ];
256260 }, array_values ($ pendingNodes ));
@@ -271,10 +275,6 @@ public function getPendingNodes(string $userId, ?int $since = null): array {
271275 return $ result ;
272276 }
273277
274- /**
275- * @param int $fileId
276- * @return string
277- */
278278 public function getEtag (int $ fileId ): string {
279279 $ file = $ this ->root ->getFirstNodeById ($ fileId );
280280 if ($ file !== null ) {
@@ -535,19 +535,17 @@ private function shareWithApprovers(int $fileId, array $rule, string $userId): a
535535 $ createdShares = [];
536536 // get node
537537 $ userFolder = $ this ->root ->getUserFolder ($ userId );
538- $ nodeResults = $ userFolder ->getById ($ fileId );
539- if (count ($ nodeResults ) > 0 ) {
540- $ node = $ nodeResults [0 ];
541- // get the node again from the owner's storage to avoid sharing permission issues
542- $ ownerId = $ node ->getOwner ()->getUID ();
543- $ ownerFolder = $ this ->root ->getUserFolder ($ ownerId );
544- $ ownerNodeResults = $ ownerFolder ->getById ($ fileId );
545- if (count ($ ownerNodeResults ) > 0 ) {
546- $ node = $ ownerNodeResults [0 ];
547- }
548- } else {
538+ $ node = $ userFolder ->getFirstNodeById ($ fileId );
539+ if ($ node === null ) {
549540 return [];
550541 }
542+ // get the node again from the owner's storage to avoid sharing permission issues
543+ $ ownerId = $ node ->getOwner ()->getUID ();
544+ $ ownerFolder = $ this ->root ->getUserFolder ($ ownerId );
545+ $ ownerNode = $ ownerFolder ->getFirstNodeById ($ fileId );
546+ if ($ ownerNode !== null ) {
547+ $ node = $ ownerNode ;
548+ }
551549 $ label = $ this ->l10n ->t ('Please check my approval request ' );
552550 $ fileOwner = $ node ->getOwner ()->getUID ();
553551
@@ -599,9 +597,8 @@ private function sendApprovalNotification(int $fileId, ?string $approverId, bool
599597 $ thisUserId = $ user ->getUID ();
600598 if ($ thisUserId !== $ approverId ) {
601599 $ userFolder = $ root ->getUserFolder ($ thisUserId );
602- $ found = $ userFolder ->getById ($ fileId );
603- if (count ($ found ) > 0 ) {
604- $ node = $ found [0 ];
600+ $ node = $ userFolder ->getFirstNodeById ($ fileId );
601+ if ($ node !== null ) {
605602 $ path = $ userFolder ->getRelativePath ($ node ->getPath ());
606603 $ type = $ node ->getType () === FileInfo::TYPE_FILE
607604 ? 'file '
@@ -718,10 +715,8 @@ public function handleTagAssignmentEvent(int $fileId, array $tags): void {
718715 // if there is no activity, the tag was assigned manually (or via auto-tagging flows)
719716 // => perform the request here (share, store action and trigger activity event)
720717 if ($ activity === null ) {
721- $ found = $ this ->root ->getById ($ fileId );
722- if (count ($ found ) > 0 ) {
723- $ node = $ found [0 ];
724- } else {
718+ $ node = $ this ->root ->getFirstNodeById ($ fileId );
719+ if ($ node === null ) {
725720 $ this ->logger ->error ('Could not request approval of file ' . $ fileId . ': file not found. ' , ['app ' => Application::APP_ID ]);
726721 return ;
727722 }
@@ -771,9 +766,8 @@ public function sendRequestNotification(int $fileId, array $rule, string $reques
771766 // only notify users having access to the file
772767 foreach ($ rulesUserIds as $ userId ) {
773768 $ userFolder = $ root ->getUserFolder ($ userId );
774- $ found = $ userFolder ->getById ($ fileId );
775- if (count ($ found ) > 0 ) {
776- $ node = $ found [0 ];
769+ $ node = $ userFolder ->getFirstNodeById ($ fileId );
770+ if ($ node !== null ) {
777771 $ path = $ userFolder ->getRelativePath ($ node ->getPath ());
778772 $ type = $ node ->getType () === FileInfo::TYPE_FILE
779773 ? 'file '
@@ -790,9 +784,8 @@ public function sendRequestNotification(int $fileId, array $rule, string $reques
790784 // we don't check if users have access to the file because they might not have yet (share is not effective yet)
791785 // => notify every approver
792786 foreach ($ rulesUserIds as $ userId ) {
793- $ found = $ root ->getById ($ fileId );
794- if (count ($ found ) > 0 ) {
795- $ node = $ found [0 ];
787+ $ node = $ root ->getFirstNodeById ($ fileId );
788+ if ($ node !== null ) {
796789 // we don't know the path in user storage
797790 $ path = '' ;
798791 $ type = $ node ->getType () === FileInfo::TYPE_FILE
0 commit comments