feat: add downloads count to episode list

This commit is contained in:
Yassine Doghri 2023-02-28 16:47:13 +00:00
parent fc009f3d00
commit b63c1dc9b1
5 changed files with 57 additions and 4 deletions

View File

@ -72,6 +72,7 @@ use RuntimeException;
* @property bool $is_published_on_hubs
* @property int $posts_count
* @property int $comments_count
* @property int $downloads
* @property EpisodeComment[]|null $comments
* @property bool $is_premium
* @property int $created_by
@ -109,6 +110,8 @@ class Episode extends Entity
protected ?Chapters $chapters = null;
protected int $downloads = 0;
/**
* @var Person[]|null
*/

View File

@ -447,3 +447,36 @@ if (! function_exists('category_label')) {
}
// ------------------------------------------------------------------------
if (! function_exists('downloads_abbr')) {
function downloads_abbr(int $downloads): string
{
if ($downloads < 1000) {
return (string) $downloads;
}
$option = match (true) {
$downloads < 1_000_000 => [
'divider' => 1_000,
'suffix' => 'K',
],
$downloads < 1_000_000_000 => [
'divider' => 1_000_000,
'suffix' => 'M',
],
default => [
'divider' => 1_000_000_000,
'suffix' => 'B',
],
};
$formatter = new NumberFormatter(service('request')->getLocale(), NumberFormatter::DECIMAL);
$formatter->setPattern('#,##0.##');
$abbr = $formatter->format($downloads / $option['divider']) . $option['suffix'];
return <<<HTML
<abbr title="{$downloads}">{$abbr}</abbr>
HTML;
}
}

View File

@ -71,19 +71,28 @@ class EpisodeController extends BaseController
// Use LIKE operator as a fallback.
if (strlen($query) < 4) {
$episodes = (new EpisodeModel())
->where('podcast_id', $this->podcast->id)
->select('episodes.*, IFNULL(SUM(ape.hits),0) as downloads')
->join('analytics_podcasts_by_episode ape', 'episodes.id=ape.episode_id', 'left')
->where('episodes.podcast_id', $this->podcast->id)
->like('title', $query)
->orLike('description_markdown', $query)
->groupBy('episodes.id')
->orderBy('-`published_at`', '', false)
->orderBy('created_at', 'desc');
} else {
$episodes = (new EpisodeModel())
->where('podcast_id', $this->podcast->id)
->where("MATCH (title, description_markdown) AGAINST ('{$query}')");
->select('episodes.*, IFNULL(SUM(ape.hits),0) as downloads')
->join('analytics_podcasts_by_episode ape', 'episodes.id=ape.episode_id', 'left')
->where('episodes.podcast_id', $this->podcast->id)
->where("MATCH (title, description_markdown) AGAINST ('{$query}')")
->groupBy('episodes.id');
}
} else {
$episodes = (new EpisodeModel())
->where('podcast_id', $this->podcast->id)
->select('episodes.*, IFNULL(SUM(ape.hits),0) as downloads')
->join('analytics_podcasts_by_episode ape', 'episodes.id=ape.episode_id', 'left')
->where('episodes.podcast_id', $this->podcast->id)
->groupBy('episodes.id')
->orderBy('-`published_at`', '', false)
->orderBy('created_at', 'desc');
}

View File

@ -55,6 +55,7 @@ return [
}',
'episode' => 'Episode',
'visibility' => 'Visibility',
'downloads' => 'Downloads',
'comments' => 'Comments',
'actions' => 'Actions',
],

View File

@ -82,9 +82,16 @@ data_table(
return publication_pill(
$episode->published_at,
$episode->publication_status,
'text-sm'
);
},
],
[
'header' => lang('Episode.list.downloads'),
'cell' => function ($episode): string {
return downloads_abbr($episode->downloads);
},
],
[
'header' => lang('Episode.list.comments'),
'cell' => function ($episode): int {