diff --git a/app/Controllers/HomeController.php b/app/Controllers/HomeController.php index aeaaff0a..54abb0ff 100644 --- a/app/Controllers/HomeController.php +++ b/app/Controllers/HomeController.php @@ -27,7 +27,12 @@ class HomeController extends BaseController return redirect()->to(rtrim(host_url(), '/') . $route); } - $allPodcasts = (new PodcastModel())->findAll(); + $sortOptions = ['activity', 'created_desc', 'created_asc']; + $sortBy = in_array($this->request->getGet('sort'), $sortOptions, true) ? $this->request->getGet( + 'sort' + ) : 'activity'; + + $allPodcasts = (new PodcastModel())->getAllPodcasts($sortBy); // check if there's only one podcast to redirect user to it if (count($allPodcasts) === 1) { @@ -38,6 +43,7 @@ class HomeController extends BaseController $data = [ 'metatags' => get_home_metatags(), 'podcasts' => $allPodcasts, + 'sortBy' => $sortBy, ]; return view('home', $data); diff --git a/app/Language/en/Home.php b/app/Language/en/Home.php index eda08142..26d83502 100644 --- a/app/Language/en/Home.php +++ b/app/Language/en/Home.php @@ -10,5 +10,11 @@ declare(strict_types=1); return [ 'all_podcasts' => 'All podcasts', + 'sort_by' => 'Sort by', + 'sort_options' => [ + 'activity' => 'Recent activity', + 'created_desc' => 'Newest first', + 'created_asc' => 'Oldest first', + ], 'no_podcast' => 'No podcast found', ]; diff --git a/app/Language/fr/Home.php b/app/Language/fr/Home.php index ab847e8f..6a0627c5 100644 --- a/app/Language/fr/Home.php +++ b/app/Language/fr/Home.php @@ -10,5 +10,11 @@ declare(strict_types=1); return [ 'all_podcasts' => 'Tous les podcasts', + 'sort_by' => 'Trier par', + 'sort_options' => [ + 'activity' => 'Activité récente', + 'created_desc' => 'Le plus récent d’abord', + 'created_asc' => 'Le plus ancien d’abord', + ], 'no_podcast' => 'Aucun podcast trouvé', ]; diff --git a/app/Models/PodcastModel.php b/app/Models/PodcastModel.php index 9b639d69..68d1e1d1 100644 --- a/app/Models/PodcastModel.php +++ b/app/Models/PodcastModel.php @@ -166,6 +166,42 @@ class PodcastModel extends Model return $found; } + /** + * @param 'activity'|'created_asc'|'created_desc' $orderBy + * + * @return Podcast[] + */ + public function getAllPodcasts(string $orderBy = null): array + { + if ($orderBy === 'activity') { + $prefix = $this->db->getPrefix(); + + $fediverseTablePrefix = config('Fediverse') + ->tablesPrefix; + $this->select( + 'podcasts.*, MAX(' . $prefix . $fediverseTablePrefix . 'posts.published_at' . ') as max_published_at' + ) + ->join( + $fediverseTablePrefix . 'posts', + $fediverseTablePrefix . 'posts.actor_id = podcasts.actor_id', + 'left' + ) + ->where( + '`' . $prefix . $fediverseTablePrefix . 'posts`.`published_at` <= NOW()', + null, + false + )->orWhere($fediverseTablePrefix . 'posts.published_at', null) + ->groupBy('cp_podcasts.actor_id') + ->orderBy('max_published_at', 'DESC'); + } elseif ($orderBy === 'created_desc') { + $this->orderBy('created_at', 'DESC'); + } elseif ($orderBy === 'created_asc') { + $this->orderBy('created_at', 'ASC'); + } + + return $this->findAll(); + } + /** * Gets all the podcasts a given user is contributing to * @@ -378,6 +414,10 @@ class PodcastModel extends Model { $podcast = (new self())->getPodcastById(is_array($data['id']) ? $data['id'][0] : $data['id']); + // delete cache for users' podcasts + cache() + ->deleteMatching('user*podcasts'); + if ($podcast !== null) { // delete cache all podcast pages cache() diff --git a/app/Resources/icons/sort.svg b/app/Resources/icons/sort.svg new file mode 100644 index 00000000..51df3545 --- /dev/null +++ b/app/Resources/icons/sort.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/themes/cp_app/home.php b/themes/cp_app/home.php index 39abc5cd..b9c1d542 100644 --- a/themes/cp_app/home.php +++ b/themes/cp_app/home.php @@ -47,10 +47,33 @@
- ( + () -
+ + +
+