Commit 7b89da61 authored by Yassine Doghri's avatar Yassine Doghri
Browse files

feat(home): sort podcasts by recent activity + add dropdown menu to choose between sorting options

fixes #164
parent 91128fad
Loading
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -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);
+6 −0
Original line number Diff line number Diff line
@@ -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',
];
+6 −0
Original line number Diff line number Diff line
@@ -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é',
];
+40 −0
Original line number Diff line number Diff line
@@ -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()
+6 −0
Original line number Diff line number Diff line
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
  <g>
    <path d="M0 0H24V24H0Z" fill="none"/>
    <path d="M3 18H7V16H3ZM3 6V8H21V6Zm0 7H15V11H3Z"/>
  </g>
</svg>
Loading