@@ -22,6 +22,7 @@ class CommentPropertiesPlugin extends ServerPlugin {
2222
2323 protected ?Server $ server = null ;
2424 private array $ cachedUnreadCount = [];
25+ private array $ cachedCount = [];
2526 private array $ cachedDirectories = [];
2627
2728 public function __construct (
@@ -48,7 +49,11 @@ public function initialize(\Sabre\DAV\Server $server) {
4849 $ this ->server ->on ('propFind ' , [$ this , 'handleGetProperties ' ]);
4950 }
5051
51- private function cacheDirectory (Directory $ directory ): void {
52+ private function cacheDirectory (Directory $ directory , PropFind $ propFind ): void {
53+ if (is_null ($ propFind ->getStatus (self ::PROPERTY_NAME_UNREAD )) && is_null ($ propFind ->getStatus (self ::PROPERTY_NAME_COUNT ))) {
54+ return ;
55+ }
56+
5257 $ children = $ directory ->getChildren ();
5358
5459 $ ids = [];
@@ -66,15 +71,30 @@ private function cacheDirectory(Directory $directory): void {
6671 }
6772
6873 $ ids [] = (string )$ directory ->getId ();
69- $ unread = $ this ->commentsManager ->getNumberOfUnreadCommentsForObjects ('files ' , $ ids , $ this ->userSession ->getUser ());
74+ if (!is_null ($ propFind ->getStatus (self ::PROPERTY_NAME_UNREAD ))) {
75+ $ user = $ this ->userSession ->getUser ();
76+ if ($ user ) {
77+ $ unread = $ this ->commentsManager ->getNumberOfUnreadCommentsForObjects ('files ' , $ ids , $ user );
78+ foreach ($ unread as $ id => $ count ) {
79+ $ this ->cachedUnreadCount [(int )$ id ] = $ count ;
80+ }
81+ } else {
82+ foreach ($ ids as $ id ) {
83+ $ this ->cachedUnreadCount [(int )$ id ] = null ;
84+ }
85+ }
86+ }
7087
71- foreach ($ unread as $ id => $ count ) {
72- $ this ->cachedUnreadCount [(int )$ id ] = $ count ;
88+ if (!is_null ($ propFind ->getStatus (self ::PROPERTY_NAME_COUNT ))) {
89+ $ commentCounts = $ this ->commentsManager ->getNumberOfCommentsForObjects ('files ' , $ ids );
90+ foreach ($ commentCounts as $ id => $ count ) {
91+ $ this ->cachedCount [(int )$ id ] = $ count ;
92+ }
7393 }
94+
7495 }
7596
76- private function preloadCollection (PropFind $ propFind , ICollection $ collection ):
77- void {
97+ private function preloadCollection (PropFind $ propFind , ICollection $ collection ): void {
7898 if (!($ collection instanceof Directory)) {
7999 return ;
80100 }
@@ -83,7 +103,7 @@ private function preloadCollection(PropFind $propFind, ICollection $collection):
83103 if (!isset ($ this ->cachedDirectories [$ collectionPath ]) && $ propFind ->getStatus (
84104 self ::PROPERTY_NAME_UNREAD
85105 ) !== null ) {
86- $ this ->cacheDirectory ($ collection );
106+ $ this ->cacheDirectory ($ collection, $ propFind );
87107 $ this ->cachedDirectories [$ collectionPath ] = true ;
88108 }
89109 }
@@ -105,7 +125,7 @@ public function handleGetProperties(
105125 }
106126
107127 $ propFind ->handle (self ::PROPERTY_NAME_COUNT , function () use ($ node ): int {
108- return $ this ->commentsManager ->getNumberOfCommentsForObject ('files ' , (string )$ node ->getId ());
128+ return $ this ->cachedCount [ $ node -> getId ()] ?? $ this -> commentsManager ->getNumberOfCommentsForObject ('files ' , (string )$ node ->getId ());
109129 });
110130
111131 $ propFind ->handle (self ::PROPERTY_NAME_HREF , function () use ($ node ): ?string {
@@ -141,8 +161,7 @@ public function getUnreadCount(Node $node): ?int {
141161 return null ;
142162 }
143163
144- $ lastRead = $ this ->commentsManager ->getReadMark ('files ' , (string )$ node ->getId (), $ user );
145-
146- return $ this ->commentsManager ->getNumberOfCommentsForObject ('files ' , (string )$ node ->getId (), $ lastRead );
164+ $ objectId = (string )$ node ->getId ();
165+ return $ this ->commentsManager ->getNumberOfUnreadCommentsForObjects ('files ' , [$ objectId ], $ user )[$ objectId ];
147166 }
148167}
0 commit comments