diff options
Diffstat (limited to 'app/Models/Category.php')
| -rw-r--r-- | app/Models/Category.php | 54 |
1 files changed, 48 insertions, 6 deletions
diff --git a/app/Models/Category.php b/app/Models/Category.php index 1f5b4dc61..6674b4e72 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -95,7 +95,7 @@ class FreshRSS_Category extends Minz_Model { } /** - * @return array<FreshRSS_Feed> + * @return array<int,FreshRSS_Feed> * @throws Minz_ConfigurationNamespaceException * @throws Minz_PDOConnectionException */ @@ -110,10 +110,8 @@ class FreshRSS_Category extends Minz_Model { $this->nbNotRead += $feed->nbNotRead(); $this->hasFeedsWithError |= ($feed->inError() && !$feed->mute()); } - $this->sortFeeds(); } - return $this->feeds ?? []; } @@ -143,7 +141,6 @@ class FreshRSS_Category extends Minz_Model { if (!is_array($values)) { $values = [$values]; } - $this->feeds = $values; $this->sortFeeds(); } @@ -157,7 +154,6 @@ class FreshRSS_Category extends Minz_Model { } $feed->_category($this); $this->feeds[] = $feed; - $this->sortFeeds(); } @@ -243,8 +239,54 @@ class FreshRSS_Category extends Minz_Model { if ($this->feeds === null) { return; } - usort($this->feeds, static function (FreshRSS_Feed $a, FreshRSS_Feed $b) { + uasort($this->feeds, static function (FreshRSS_Feed $a, FreshRSS_Feed $b) { return strnatcasecmp($a->name(), $b->name()); }); } + + /** + * Access cached feed + * @param array<FreshRSS_Category> $categories + */ + public static function findFeed(array $categories, int $feed_id): ?FreshRSS_Feed { + foreach ($categories as $category) { + foreach ($category->feeds() as $feed) { + if ($feed->id() === $feed_id) { + $feed->_category($category); // Should already be done; just to be safe + return $feed; + } + } + } + return null; + } + + /** + * Access cached feeds + * @param array<FreshRSS_Category> $categories + * @return array<int,FreshRSS_Feed> + */ + public static function findFeeds(array $categories): array { + $result = []; + foreach ($categories as $category) { + foreach ($category->feeds() as $feed) { + $result[$feed->id()] = $feed; + } + } + return $result; + } + + /** + * @param array<FreshRSS_Category> $categories + */ + public static function countUnread(array $categories, int $minPriority = 0): int { + $n = 0; + foreach ($categories as $category) { + foreach ($category->feeds() as $feed) { + if ($feed->priority() >= $minPriority) { + $n += $feed->nbNotRead(); + } + } + } + return $n; + } } |
