2828use OCA \DAV \Connector \Sabre \Directory ;
2929use OCA \DAV \Connector \Sabre \Node ;
3030use OCP \IGroupManager ;
31+ use OCP \IUser ;
3132use OCP \IUserSession ;
3233use OCP \SystemTag \ISystemTag ;
3334use OCP \SystemTag \ISystemTagManager ;
@@ -83,7 +84,7 @@ class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
8384
8485 /** @var array<int, int[]> */
8586 private array $ cachedTagMappings = [];
86- /** @var array<int , ISystemTag> */
87+ /** @var array<string , ISystemTag> */
8788 private array $ cachedTags = [];
8889
8990 private ISystemTagObjectMapper $ tagMapper ;
@@ -225,6 +226,8 @@ private function createTag($data, $contentType = 'application/json') {
225226 *
226227 * @param PropFind $propFind
227228 * @param \Sabre\DAV\INode $node
229+ *
230+ * @return void
228231 */
229232 public function handleGetProperties (
230233 PropFind $ propFind ,
@@ -279,34 +282,42 @@ private function propfindForFile(PropFind $propFind, Node $node): void {
279282 if ($ node instanceof Directory
280283 && $ propFind ->getDepth () !== 0
281284 && !is_null ($ propFind ->getStatus (self ::SYSTEM_TAGS_PROPERTYNAME ))) {
285+ $ fileIds = [$ node ->getId ()];
286+
282287 // note: pre-fetching only supported for depth <= 1
283288 $ folderContent = $ node ->getNode ()->getDirectoryListing ();
284- $ fileIds [] = (int )$ node ->getId ();
285289 foreach ($ folderContent as $ info ) {
286- $ fileIds [] = ( int ) $ info ->getId ();
290+ $ fileIds [] = $ info ->getId ();
287291 }
292+
288293 $ tags = $ this ->tagMapper ->getTagIdsForObjects ($ fileIds , 'files ' );
289294
290295 $ this ->cachedTagMappings = $ this ->cachedTagMappings + $ tags ;
291296 $ emptyFileIds = array_diff ($ fileIds , array_keys ($ tags ));
297+
292298 // also cache the ones that were not found
293299 foreach ($ emptyFileIds as $ fileId ) {
294300 $ this ->cachedTagMappings [$ fileId ] = [];
295301 }
296302 }
297303
298304 $ propFind ->handle (self ::SYSTEM_TAGS_PROPERTYNAME , function () use ($ node ) {
299- $ tags = $ this ->getTagsForFile ($ node ->getId ());
300- return new SystemTagList ($ tags , $ this ->tagManager , $ this ->userSession ->getUser ());
305+ $ user = $ this ->userSession ->getUser ();
306+ if ($ user === null ) {
307+ return ;
308+ }
309+
310+ $ tags = $ this ->getTagsForFile ($ node ->getId (), $ user );
311+ return new SystemTagList ($ tags , $ this ->tagManager , $ user );
301312 });
302313 }
303314
304315 /**
305316 * @param int $fileId
306317 * @return ISystemTag[]
307318 */
308- private function getTagsForFile (int $ fileId ): array {
309- $ user = $ this -> userSession -> getUser ();
319+ private function getTagsForFile (int $ fileId, IUser $ user ): array {
320+
310321 if (isset ($ this ->cachedTagMappings [$ fileId ])) {
311322 $ tagIds = $ this ->cachedTagMappings [$ fileId ];
312323 } else {
@@ -318,20 +329,23 @@ private function getTagsForFile(int $fileId): array {
318329 $ tagIds = [];
319330 }
320331 }
321- $ tags = array_filter (array_map (function ($ tagId ) {
332+
333+ $ tags = array_filter (array_map (function (string $ tagId ) {
322334 return $ this ->cachedTags [$ tagId ] ?? null ;
323335 }, $ tagIds ));
324336
325- $ uncachedTagIds = array_filter ($ tagIds , function ($ tagId ): bool {
337+ $ uncachedTagIds = array_filter ($ tagIds , function (string $ tagId ): bool {
326338 return !isset ($ this ->cachedTags [$ tagId ]);
327339 });
340+
328341 if (count ($ uncachedTagIds )) {
329342 $ retrievedTags = $ this ->tagManager ->getTagsByIds ($ uncachedTagIds );
330343 foreach ($ retrievedTags as $ tag ) {
331344 $ this ->cachedTags [$ tag ->getId ()] = $ tag ;
332345 }
333346 $ tags += $ retrievedTags ;
334347 }
348+
335349 return array_filter ($ tags , function (ISystemTag $ tag ) use ($ user ) {
336350 return $ this ->tagManager ->canUserSeeTag ($ tag , $ user );
337351 });
0 commit comments