@@ -99,12 +99,6 @@ class Collection
9999 #[Database \Column(computed: true )]
100100 public ?int $ number_links = null ;
101101
102- #[Database \Column(computed: true )]
103- public ?int $ number_streams = null ;
104-
105- #[Database \Column(computed: true )]
106- public ?string $ time_filter = null ;
107-
108102 public function __construct ()
109103 {
110104 $ this ->id = \Minz \Random::timebased ();
@@ -261,25 +255,39 @@ public function preloadOwner(?User $owner): void
261255 $ this ->memoizeValue ('owner ' , $ owner );
262256 }
263257
258+ /**
259+ * Set the publishers of the collection without querying the database.
260+ *
261+ * @see collections\Preloader
262+ *
263+ * @param User[] $publishers
264+ */
265+ public function preloadPublishers (array $ publishers ): void
266+ {
267+ $ this ->memoizeValue ('publishers ' , $ publishers );
268+ }
269+
264270 /**
265271 * Return the list of users with write access.
266272 *
267273 * @return User[]
268274 */
269275 public function publishers (): array
270276 {
271- $ owner = $ this ->owner ();
272- $ shares = $ this ->shares (['access_type ' => 'write ' ]);
277+ return $ this ->memoize ('publishers ' , function (): array {
278+ $ owner = $ this ->owner ();
279+ $ shares = $ this ->shares (['access_type ' => 'write ' ]);
273280
274- $ publishers = array_map (function (CollectionShare $ share ): User {
275- return $ share ->user ();
276- }, $ shares );
281+ $ publishers = array_map (function (CollectionShare $ share ): User {
282+ return $ share ->user ();
283+ }, $ shares );
277284
278- if ($ owner ) {
279- array_unshift ($ publishers , $ owner );
280- }
285+ if ($ owner ) {
286+ array_unshift ($ publishers , $ owner );
287+ }
281288
282- return $ publishers ;
289+ return $ publishers ;
290+ });
283291 }
284292
285293 /**
@@ -395,17 +403,48 @@ public function groupForUser(string $user_id): ?Group
395403 /**
396404 * Return the number of streams of the given user in which this collection
397405 * is a source.
398- *
399- * This is the same information as the number_streams computed property,
400- * for a collection that has been loaded on its own.
401406 */
402407 public function countStreamsByUser (User $ user ): int
403408 {
404409 return $ this ->memoize ("count_streams_ {$ user ->id }" , function () use ($ user ): int {
405- return StreamToFollow::countByUserAndSource ($ user , $ this );
410+ $ counts = StreamToFollow::countByUserAndSources ($ user , [$ this ]);
411+ return $ counts [$ this ->id ] ?? 0 ;
406412 });
407413 }
408414
415+ /**
416+ * Set the number of streams of a user without querying the database.
417+ *
418+ * @see collections\Preloader
419+ */
420+ public function preloadCountStreamsByUser (User $ user , int $ count ): void
421+ {
422+ $ this ->memoizeValue ("count_streams_ {$ user ->id }" , $ count );
423+ }
424+
425+ /**
426+ * Return the time filter applied by the given user to this collection, or
427+ * null if the user doesn't follow it.
428+ */
429+ public function timeFilterByUser (User $ user ): ?string
430+ {
431+ return $ this ->memoize ("time_filter_ {$ user ->id }" , function () use ($ user ): ?string {
432+ $ follows = FollowedCollection::listByUserAndCollections ($ user , [$ this ]);
433+ $ follow = $ follows [$ this ->id ] ?? null ;
434+ return $ follow ?->time_filter;
435+ });
436+ }
437+
438+ /**
439+ * Set the time filter of a user without querying the database.
440+ *
441+ * @see collections\Preloader
442+ */
443+ public function preloadTimeFilterByUser (User $ user , ?string $ time_filter ): void
444+ {
445+ $ this ->memoizeValue ("time_filter_ {$ user ->id }" , $ time_filter );
446+ }
447+
409448 /**
410449 * Return the topics attached to the current collection.
411450 *
@@ -457,6 +496,8 @@ public function shareWith(User $user, string $access_type): void
457496 {
458497 $ collection_share = new CollectionShare ($ user ->id , $ this ->id , $ access_type );
459498 $ collection_share ->save ();
499+
500+ $ this ->unmemoize ('publishers ' );
460501 }
461502
462503 /**
@@ -468,6 +509,8 @@ public function unshareWith(User $user): void
468509 'collection_id ' => $ this ->id ,
469510 'user_id ' => $ user ->id ,
470511 ]);
512+
513+ $ this ->unmemoize ('publishers ' );
471514 }
472515
473516 /**
0 commit comments