Newer
Older
<?php

Yassine Doghri
committed

Yassine Doghri
committed
declare(strict_types=1);
* @copyright 2020 Ad Aures
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Models;

Yassine Doghri
committed
use App\Entities\Episode;
use CodeIgniter\Model;
class EpisodeModel extends Model
{

Yassine Doghri
committed
/**

Yassine Doghri
committed
* TODO: remove, shouldn't be here
*

Yassine Doghri
committed
* @var array<string, array<string, string>>
*/
public static $themes = [
'light-transparent' => [
'style' =>
'background-color: #fff; background-image: linear-gradient(45deg, #ccc 12.5%, transparent 12.5%, transparent 50%, #ccc 50%, #ccc 62.5%, transparent 62.5%, transparent 100%); background-size: 5.66px 5.66px;',
'background' => 'transparent',
'text' => '#000',
'inverted' => '#fff',
],
'light' => [
'style' => 'background-color: #fff;',
'background' => '#fff',
'text' => '#000',
'inverted' => '#fff',
],
'dark-transparent' => [
'style' =>
'background-color: #001f1a; background-image: linear-gradient(45deg, #888 12.5%, transparent 12.5%, transparent 50%, #888 50%, #888 62.5%, transparent 62.5%, transparent 100%); background-size: 5.66px 5.66px;',
'background' => 'transparent',
'text' => '#fff',
'inverted' => '#000',
],
'dark' => [
'style' => 'background-color: #001f1a;',

Yassine Doghri
committed
'background' => '#313131',

Yassine Doghri
committed
'text' => '#fff',
'inverted' => '#000',
],
];

Yassine Doghri
committed
/**
* @var string
*/
protected $table = 'episodes';

Yassine Doghri
committed
/**
* @var string[]
*/
protected $allowedFields = [
'id',
'podcast_id',
'title',
'audio_id',
'description_markdown',
'description_html',
'cover_id',
'transcript_id',
'transcript_remote_url',
'chapters_id',
'chapters_remote_url',
'parental_advisory',
'season_number',
'type',
'is_blocked',
'location_name',
'location_geo',

Yassine Doghri
committed
'location_osm',

Benjamin Bellamy
committed
'custom_rss',
'posts_count',
'comments_count',
'published_at',
'created_by',
'updated_by',
];

Yassine Doghri
committed
/**
* @var string
*/
protected $returnType = Episode::class;

Yassine Doghri
committed
/**
* @var bool
*/
protected $useSoftDeletes = true;

Yassine Doghri
committed
/**
* @var bool
*/
protected $useTimestamps = true;

Yassine Doghri
committed
/**
* @var array<string, string>
*/
protected $validationRules = [
'podcast_id' => 'required',
'title' => 'required',
'slug' => 'required|regex_match[/^[a-zA-Z0-9\-]{1,128}$/]',
'audio_id' => 'required',
'description_markdown' => 'required',
'number' => 'is_natural_no_zero|permit_empty',
'season_number' => 'is_natural_no_zero|permit_empty',
'type' => 'required',
'transcript_remote_url' => 'valid_url|permit_empty',
'chapters_remote_url' => 'valid_url|permit_empty',
'published_at' => 'valid_date|permit_empty',
'created_by' => 'required',
'updated_by' => 'required',
];

Yassine Doghri
committed
/**
* @var string[]
*/

Yassine Doghri
committed
protected $afterInsert = ['writeEnclosureMetadata', 'clearCache'];

Yassine Doghri
committed
/**
* @var string[]
*/

Yassine Doghri
committed
protected $afterUpdate = ['clearCache', 'writeEnclosureMetadata'];
/**

Yassine Doghri
committed
* @var string[]
*/

Yassine Doghri
committed
protected $beforeDelete = ['clearCache'];
public function getEpisodeBySlug(string $podcastHandle, string $episodeSlug): ?Episode
$cacheName = "podcast-{$podcastHandle}_episode-{$episodeSlug}";
if (! ($found = cache($cacheName))) {

Yassine Doghri
committed
$found = $this->select('episodes.*')
->join('podcasts', 'podcasts.id = episodes.podcast_id')
->where('slug', $episodeSlug)
->where('podcasts.handle', $podcastHandle)

Yassine Doghri
committed
->where('`published_at` <= NOW()', null, false)
->first();
cache()
->save($cacheName, $found, DECADE);
}
return $found;
}
public function getEpisodeById(int $episodeId): ?Episode
// TODO: episode id should be a composite key. The cache should include podcast_id.
$cacheName = "podcast_episode#{$episodeId}";
if (! ($found = cache($cacheName))) {
$builder = $this->where([
'id' => $episodeId,
]);
$found = $builder->first();
cache()
->save($cacheName, $found, DECADE);
}
return $found;
}
public function getPublishedEpisodeById(int $podcastId, int $episodeId): ?Episode

Yassine Doghri
committed
{
$cacheName = "podcast#{$podcastId}_episode#{$episodeId}_published";
if (! ($found = cache($cacheName))) {
$found = $this->where([
'id' => $episodeId,
])
->where('podcast_id', $podcastId)
->where('`published_at` <= NOW()', null, false)
->first();

Yassine Doghri
committed
cache()
->save($cacheName, $found, DECADE);
}
return $found;

Yassine Doghri
committed
}

Yassine Doghri
committed
/**
* Gets all episodes for a podcast ordered according to podcast type Filtered depending on year or season

Yassine Doghri
committed
*

Yassine Doghri
committed
* @return Episode[]

Yassine Doghri
committed
*/
public function getPodcastEpisodes(
int $podcastId,
string $podcastType,
string $year = null,
string $season = null
): array {
$cacheName = implode(
'_',
array_filter(["podcast#{$podcastId}", $year, $season ? 'season' . $season : null, 'episodes']),
);
if (! ($found = cache($cacheName))) {

Yassine Doghri
committed
$where = [
'podcast_id' => $podcastId,
];
if ($year) {
$where['YEAR(published_at)'] = $year;
$where['season_number'] = null;
}
if ($season) {
$where['season_number'] = $season;
}
if ($podcastType === 'serial') {
// podcast is serial
$found = $this->where($where)

Yassine Doghri
committed
->where('`published_at` <= NOW()', null, false)
->orderBy('season_number DESC, number ASC')
->findAll();
} else {
$found = $this->where($where)

Yassine Doghri
committed
->where('`published_at` <= NOW()', null, false)
->orderBy('published_at', 'DESC')
->findAll();
}

Yassine Doghri
committed
$secondsToNextUnpublishedEpisode = $this->getSecondsToNextUnpublishedEpisode($podcastId);

Yassine Doghri
committed
cache()
->save(
$cacheName,
$found,
$secondsToNextUnpublishedEpisode

Yassine Doghri
committed
? $secondsToNextUnpublishedEpisode
: DECADE,
);
}
return $found;
}

Yassine Doghri
committed
/**
* Returns the timestamp difference in seconds between the next episode to publish and the current timestamp Returns
* false if there's no episode to publish

Yassine Doghri
committed
*

Yassine Doghri
committed
* @return int|false seconds

Yassine Doghri
committed
*/

Yassine Doghri
committed
public function getSecondsToNextUnpublishedEpisode(int $podcastId): int | false

Yassine Doghri
committed
{
$result = $this->select('TIMESTAMPDIFF(SECOND, NOW(), `published_at`) as timestamp_diff')

Yassine Doghri
committed
->where([
'podcast_id' => $podcastId,
])
->where('`published_at` > NOW()', null, false)
->orderBy('published_at', 'asc')
->get()
->getResultArray();

Yassine Doghri
committed
return $result !== []
? (int) $result[0]['timestamp_diff']
: false;

Yassine Doghri
committed
}

Yassine Doghri
committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
public function getCurrentSeasonNumber(int $podcastId): ?int
{
$result = $this->select('MAX(season_number) as current_season_number')
->where([
'podcast_id' => $podcastId,
$this->deletedField => null,
])
->get()
->getResultArray();
return $result[0]['current_season_number'] ? (int) $result[0]['current_season_number'] : null;
}
public function getNextEpisodeNumber(int $podcastId, ?int $seasonNumber): int
{
$result = $this->select('MAX(number) as next_episode_number')
->where([
'podcast_id' => $podcastId,
'season_number' => $seasonNumber,
$this->deletedField => null,
])->get()
->getResultArray();
return (int) $result[0]['next_episode_number'] + 1;
}
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
* @return array<string, int|Time>
*/
public function getPodcastStats(int $podcastId): array
{
$result = $this->select(
'COUNT(DISTINCT season_number) as number_of_seasons, COUNT(*) as number_of_episodes, MIN(published_at) as first_published_at'
)
->where([
'podcast_id' => $podcastId,
'published_at IS NOT' => null,
$this->deletedField => null,
])->get()
->getResultArray();
$stats = [
'number_of_seasons' => (int) $result[0]['number_of_seasons'],
'number_of_episodes' => (int) $result[0]['number_of_episodes'],
];
if ($result[0]['first_published_at'] !== null) {
$stats['first_published_at'] = new Time($result[0]['first_published_at']);
}
return $stats;
}
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
public function resetCommentsCount(): int | false
{
$episodeCommentsCount = $this->select('episodes.id, COUNT(*) as `comments_count`')
->join('episode_comments', 'episodes.id = episode_comments.episode_id')
->where('in_reply_to_id', null)
->groupBy('episodes.id')
->getCompiledSelect();
$episodePostsRepliesCount = $this
->select('episodes.id, COUNT(*) as `comments_count`')
->join(
config('Fediverse')
->tablesPrefix . 'posts',
'episodes.id = ' . config('Fediverse')->tablesPrefix . 'posts.episode_id'
)
->where('in_reply_to_id IS NOT', null)
->groupBy('episodes.id')
->getCompiledSelect();
$query = $this->db->query(
'SELECT `id`, SUM(`comments_count`) as `comments_count` FROM (' . $episodeCommentsCount . ' UNION ALL ' . $episodePostsRepliesCount . ') x GROUP BY `id`'
);
$countsPerEpisodeId = $query->getResultArray();
if ($countsPerEpisodeId !== []) {
return $this->updateBatch($countsPerEpisodeId, 'id');
}
return 0;
}
public function resetPostsCount(): int | false
{
$episodePostsCount = $this->select('episodes.id, COUNT(*) as `posts_count`')
->join(
config('Fediverse')
->tablesPrefix . 'posts',
'episodes.id = ' . config('Fediverse')->tablesPrefix . 'posts.episode_id'
)
->where('in_reply_to_id', null)
->groupBy('episodes.id')
->get()
->getResultArray();
if ($episodePostsCount !== []) {
return $this->updateBatch($episodePostsCount, 'id');
}
return 0;
}

Yassine Doghri
committed
/**
* @param mixed[] $data
*

Yassine Doghri
committed
* @return array<string, array<string|int, mixed>>
*/
public function clearCache(array $data): array
{

Yassine Doghri
committed
$episode = (new self())->find(is_array($data['id']) ? $data['id'][0] : $data['id']);

Yassine Doghri
committed
// delete podcast cache
cache()

Yassine Doghri
committed
->deleteMatching("podcast#{$episode->podcast_id}*");
cache()
->deleteMatching("podcast-{$episode->podcast->handle}*");
cache()
->delete("podcast_episode#{$episode->id}");
cache()

Yassine Doghri
committed
->deleteMatching("page_podcast#{$episode->podcast_id}*");
cache()
->deleteMatching('page_credits_*');
cache()
->delete('episodes_markers');

Yassine Doghri
committed
return $data;
}

Yassine Doghri
committed
/**
* @param mixed[] $data
*

Yassine Doghri
committed
* @return array<string, array<string|int, mixed>>
*/
protected function writeEnclosureMetadata(array $data): array
{
helper('id3');

Yassine Doghri
committed
$episode = (new self())->find(is_array($data['id']) ? $data['id'][0] : $data['id']);

Yassine Doghri
committed
write_audio_file_tags($episode);
return $data;
}