2020-05-31 19:15:52 +00:00
|
|
|
<?php
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-06-10 15:00:12 +00:00
|
|
|
/**
|
2022-02-19 16:06:11 +00:00
|
|
|
* @copyright 2021 Ad Aures
|
2020-06-10 15:00:12 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
2020-05-31 19:15:52 +00:00
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2023-04-14 11:11:53 +00:00
|
|
|
use App\Entities\Actor;
|
2021-05-14 17:59:35 +00:00
|
|
|
use App\Entities\Podcast;
|
2021-04-02 17:20:02 +00:00
|
|
|
use CodeIgniter\HTTP\URI;
|
2020-05-31 19:15:52 +00:00
|
|
|
use CodeIgniter\Model;
|
2023-08-27 13:26:06 +00:00
|
|
|
use Config\Fediverse;
|
2021-04-02 17:20:02 +00:00
|
|
|
use phpseclib\Crypt\RSA;
|
2020-05-31 19:15:52 +00:00
|
|
|
|
|
|
|
class PodcastModel extends Model
|
|
|
|
{
|
2021-05-14 17:59:35 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-05-31 19:15:52 +00:00
|
|
|
protected $table = 'podcasts';
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-05-31 19:15:52 +00:00
|
|
|
protected $primaryKey = 'id';
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2020-05-31 19:15:52 +00:00
|
|
|
protected $allowedFields = [
|
2020-06-10 15:00:12 +00:00
|
|
|
'id',
|
2021-06-21 11:58:43 +00:00
|
|
|
'guid',
|
2020-05-31 19:15:52 +00:00
|
|
|
'title',
|
2021-07-26 13:10:46 +00:00
|
|
|
'handle',
|
2020-10-29 15:45:19 +00:00
|
|
|
'description_markdown',
|
|
|
|
'description_html',
|
|
|
|
'episode_description_footer_markdown',
|
|
|
|
'episode_description_footer_html',
|
2021-12-14 16:41:10 +00:00
|
|
|
'cover_id',
|
|
|
|
'banner_id',
|
2020-10-29 15:45:19 +00:00
|
|
|
'language_code',
|
2020-08-21 08:41:09 +00:00
|
|
|
'category_id',
|
2020-10-02 15:38:16 +00:00
|
|
|
'parental_advisory',
|
2020-05-31 19:15:52 +00:00
|
|
|
'owner_name',
|
|
|
|
'owner_email',
|
2024-01-01 10:11:29 +00:00
|
|
|
'is_owner_email_removed_from_feed',
|
2020-10-02 15:38:16 +00:00
|
|
|
'publisher',
|
2020-05-31 19:15:52 +00:00
|
|
|
'type',
|
2024-02-05 16:51:04 +00:00
|
|
|
'medium',
|
2020-05-31 19:15:52 +00:00
|
|
|
'copyright',
|
2020-08-21 08:41:09 +00:00
|
|
|
'imported_feed_url',
|
2020-10-06 15:39:27 +00:00
|
|
|
'new_feed_url',
|
2020-10-29 15:45:19 +00:00
|
|
|
'is_blocked',
|
|
|
|
'is_completed',
|
|
|
|
'is_locked',
|
2020-12-23 14:11:38 +00:00
|
|
|
'location_name',
|
|
|
|
'location_geo',
|
2021-05-17 17:11:23 +00:00
|
|
|
'location_osm',
|
2020-11-26 18:53:52 +00:00
|
|
|
'payment_pointer',
|
2021-03-19 16:12:36 +00:00
|
|
|
'custom_rss',
|
2022-03-15 16:47:35 +00:00
|
|
|
'is_published_on_hubs',
|
2021-03-30 16:21:00 +00:00
|
|
|
'partner_id',
|
|
|
|
'partner_link_url',
|
|
|
|
'partner_image_url',
|
2022-09-28 15:02:09 +00:00
|
|
|
'is_premium_by_default',
|
2022-07-05 16:39:20 +00:00
|
|
|
'published_at',
|
2020-10-29 15:45:19 +00:00
|
|
|
'created_by',
|
|
|
|
'updated_by',
|
2020-05-31 19:15:52 +00:00
|
|
|
];
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $returnType = Podcast::class;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2020-05-31 19:15:52 +00:00
|
|
|
protected $useTimestamps = true;
|
2020-06-30 18:17:41 +02:00
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
2020-07-16 10:08:23 +00:00
|
|
|
protected $validationRules = [
|
2023-08-26 13:03:01 +00:00
|
|
|
'id' => 'permit_empty|is_natural_no_zero',
|
2023-06-12 14:47:38 +00:00
|
|
|
'title' => 'required',
|
|
|
|
'handle' => 'required|regex_match[/^[a-zA-Z0-9\_]{1,32}$/]|is_unique[podcasts.handle,id,{id}]',
|
2020-10-29 15:45:19 +00:00
|
|
|
'description_markdown' => 'required',
|
2023-06-12 14:47:38 +00:00
|
|
|
'cover_id' => 'required',
|
|
|
|
'language_code' => 'required',
|
|
|
|
'category_id' => 'required',
|
|
|
|
'owner_email' => 'required|valid_email',
|
|
|
|
'new_feed_url' => 'valid_url_strict|permit_empty',
|
|
|
|
'type' => 'required',
|
|
|
|
'published_at' => 'valid_date|permit_empty',
|
|
|
|
'created_by' => 'required',
|
|
|
|
'updated_by' => 'required',
|
2020-07-16 10:08:23 +00:00
|
|
|
];
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-09-21 15:51:04 +00:00
|
|
|
protected $beforeInsert = ['setPodcastGUID', 'createPodcastActor'];
|
2021-05-18 17:16:36 +00:00
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-04-22 17:20:28 +00:00
|
|
|
protected $afterInsert = ['setActorAvatar'];
|
2021-05-18 17:16:36 +00:00
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $afterUpdate = ['updatePodcastActor'];
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
/**
|
2021-05-18 17:16:36 +00:00
|
|
|
* clear cache before update if by any chance, the podcast name changes, so will the podcast link
|
2021-05-19 16:35:13 +00:00
|
|
|
*
|
2021-05-14 17:59:35 +00:00
|
|
|
* @var string[]
|
|
|
|
*/
|
2020-08-18 16:31:28 +00:00
|
|
|
protected $beforeUpdate = ['clearCache'];
|
2021-05-18 17:16:36 +00:00
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2020-07-16 10:08:23 +00:00
|
|
|
protected $beforeDelete = ['clearCache'];
|
|
|
|
|
2021-07-26 13:10:46 +00:00
|
|
|
public function getPodcastByHandle(string $podcastHandle): ?Podcast
|
2020-09-04 09:09:26 +00:00
|
|
|
{
|
2021-07-26 13:10:46 +00:00
|
|
|
$cacheName = "podcast-{$podcastHandle}";
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($found = cache($cacheName))) {
|
2021-07-26 13:10:46 +00:00
|
|
|
$found = $this->where('handle', $podcastHandle)
|
2022-07-05 16:39:20 +00:00
|
|
|
->where('`published_at` <= UTC_TIMESTAMP()', null, false)
|
2021-05-19 16:35:13 +00:00
|
|
|
->first();
|
|
|
|
cache()
|
2021-07-26 13:10:46 +00:00
|
|
|
->save("podcast-{$podcastHandle}", $found, DECADE);
|
2020-09-04 09:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function getPodcastById(int $podcastId): ?Podcast
|
2020-09-04 09:09:26 +00:00
|
|
|
{
|
2021-04-20 13:43:38 +00:00
|
|
|
$cacheName = "podcast#{$podcastId}";
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($found = cache($cacheName))) {
|
2020-09-04 09:09:26 +00:00
|
|
|
$found = $this->find($podcastId);
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
|
|
|
->save($cacheName, $found, DECADE);
|
2020-09-04 09:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function getPodcastByActorId(int $actorId): ?Podcast
|
2021-04-22 17:20:28 +00:00
|
|
|
{
|
|
|
|
$cacheName = "podcast_actor#{$actorId}";
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($found = cache($cacheName))) {
|
|
|
|
$found = $this->where('actor_id', $actorId)
|
|
|
|
->first();
|
2021-04-22 17:20:28 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
|
|
|
->save($cacheName, $found, DECADE);
|
2021-04-22 17:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
|
2022-02-05 16:57:20 +00:00
|
|
|
/**
|
|
|
|
* @return Podcast[]
|
|
|
|
*/
|
|
|
|
public function getAllPodcasts(string $orderBy = null): array
|
|
|
|
{
|
2022-07-05 16:39:20 +00:00
|
|
|
$prefix = $this->db->getPrefix();
|
2022-02-05 16:57:20 +00:00
|
|
|
|
2022-07-05 16:39:20 +00:00
|
|
|
if ($orderBy === 'activity') {
|
2022-06-13 16:30:34 +00:00
|
|
|
$this->builder()
|
2023-08-26 13:03:01 +00:00
|
|
|
->select('podcasts.*, MAX(`' . $prefix . 'fediverse_posts`.`published_at`) as max_published_at')
|
|
|
|
->join('fediverse_posts', 'fediverse_posts.actor_id = podcasts.actor_id', 'left')
|
2023-08-22 15:00:01 +00:00
|
|
|
->groupStart()
|
2022-02-05 16:57:20 +00:00
|
|
|
->where(
|
2023-08-26 13:03:01 +00:00
|
|
|
'`' . $prefix . 'fediverse_posts`.`published_at` <= UTC_TIMESTAMP()',
|
2022-02-05 16:57:20 +00:00
|
|
|
null,
|
|
|
|
false
|
2023-08-26 13:03:01 +00:00
|
|
|
)->orWhere('fediverse_posts.published_at', null)
|
2023-08-22 15:00:01 +00:00
|
|
|
->groupEnd()
|
2022-04-16 08:36:28 +00:00
|
|
|
->groupBy('podcasts.actor_id')
|
2022-02-05 16:57:20 +00:00
|
|
|
->orderBy('max_published_at', 'DESC');
|
|
|
|
} elseif ($orderBy === 'created_desc') {
|
|
|
|
$this->orderBy('created_at', 'DESC');
|
|
|
|
} elseif ($orderBy === 'created_asc') {
|
|
|
|
$this->orderBy('created_at', 'ASC');
|
|
|
|
}
|
|
|
|
|
2022-07-05 16:39:20 +00:00
|
|
|
return $this->where('`' . $prefix . 'podcasts`.`published_at` <= UTC_TIMESTAMP()', null, false)->findAll();
|
2022-02-05 16:57:20 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 10:08:23 +00:00
|
|
|
/**
|
2021-05-19 16:35:13 +00:00
|
|
|
* Gets all the podcasts a given user is contributing to
|
2020-07-16 10:08:23 +00:00
|
|
|
*
|
2022-10-15 11:22:08 +00:00
|
|
|
* @param string[] $userPodcastIds
|
2021-05-14 17:59:35 +00:00
|
|
|
* @return Podcast[] podcasts
|
2020-07-16 10:08:23 +00:00
|
|
|
*/
|
2022-10-15 11:22:08 +00:00
|
|
|
public function getUserPodcasts(int $userId, array $userPodcastIds): array
|
2020-07-16 10:08:23 +00:00
|
|
|
{
|
2021-04-20 13:43:38 +00:00
|
|
|
$cacheName = "user{$userId}_podcasts";
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($found = cache($cacheName))) {
|
2022-10-15 11:22:08 +00:00
|
|
|
$found = $userPodcastIds === [] ? [] : $this->whereIn('id', $userPodcastIds)
|
2020-09-04 09:09:26 +00:00
|
|
|
->findAll();
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
|
|
|
->save($cacheName, $found, DECADE);
|
2020-09-04 09:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
2020-07-16 10:08:23 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 11:22:08 +00:00
|
|
|
public function getContributorGroup(int $userId, int $podcastId): int | false
|
2020-07-16 10:08:23 +00:00
|
|
|
{
|
2022-10-15 11:22:08 +00:00
|
|
|
$userPodcast = $this->db
|
|
|
|
->table('auth_groups_users')
|
|
|
|
->select('user_id, group')
|
|
|
|
->where('user_id', $userId)
|
|
|
|
->like('group', "podcast#{$podcastId}")
|
|
|
|
->get()
|
|
|
|
->getResultObject();
|
2020-07-31 16:05:10 +00:00
|
|
|
|
2021-08-11 15:47:23 +00:00
|
|
|
return $userPodcast !== []
|
2022-10-15 11:22:08 +00:00
|
|
|
? (int) $userPodcast[0]->group
|
2020-07-31 16:05:10 +00:00
|
|
|
: false;
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
/**
|
|
|
|
* @return array<string, string>[]
|
|
|
|
*/
|
2021-04-20 13:43:38 +00:00
|
|
|
public function getYears(int $podcastId): array
|
2020-06-30 18:17:41 +02:00
|
|
|
{
|
2021-04-20 13:43:38 +00:00
|
|
|
$cacheName = "podcast#{$podcastId}_years";
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($found = cache($cacheName))) {
|
2021-04-20 13:43:38 +00:00
|
|
|
$episodeModel = new EpisodeModel();
|
|
|
|
$found = $episodeModel
|
2022-06-13 16:30:34 +00:00
|
|
|
->builder()
|
2021-06-09 12:40:22 +00:00
|
|
|
->select('YEAR(published_at) as year, count(*) as number_of_episodes')
|
2021-04-20 13:43:38 +00:00
|
|
|
->where([
|
2024-01-25 11:58:39 +00:00
|
|
|
'podcast_id' => $podcastId,
|
|
|
|
'season_number' => null,
|
2021-04-20 13:43:38 +00:00
|
|
|
])
|
2022-04-14 14:33:53 +00:00
|
|
|
->where('`published_at` <= UTC_TIMESTAMP()', null, false)
|
2021-04-20 13:43:38 +00:00
|
|
|
->groupBy('year')
|
|
|
|
->orderBy('year', 'DESC')
|
|
|
|
->get()
|
|
|
|
->getResultArray();
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
$secondsToNextUnpublishedEpisode = $episodeModel->getSecondsToNextUnpublishedEpisode($podcastId);
|
2020-06-30 18:17:41 +02:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
|
|
|
->save(
|
|
|
|
$cacheName,
|
|
|
|
$found,
|
|
|
|
$secondsToNextUnpublishedEpisode
|
2021-04-20 13:43:38 +00:00
|
|
|
? $secondsToNextUnpublishedEpisode
|
|
|
|
: DECADE,
|
2021-05-19 16:35:13 +00:00
|
|
|
);
|
2020-10-21 16:04:18 +00:00
|
|
|
}
|
2020-07-16 10:08:23 +00:00
|
|
|
|
2021-04-20 13:43:38 +00:00
|
|
|
return $found;
|
|
|
|
}
|
2020-09-04 09:09:26 +00:00
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
/**
|
|
|
|
* @return array<string, string>[]
|
|
|
|
*/
|
2021-04-20 13:43:38 +00:00
|
|
|
public function getSeasons(int $podcastId): array
|
|
|
|
{
|
|
|
|
$cacheName = "podcast#{$podcastId}_seasons";
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($found = cache($cacheName))) {
|
2021-04-20 13:43:38 +00:00
|
|
|
$episodeModel = new EpisodeModel();
|
|
|
|
$found = $episodeModel
|
2022-06-13 16:30:34 +00:00
|
|
|
->builder()
|
2021-04-20 13:43:38 +00:00
|
|
|
->select('season_number, count(*) as number_of_episodes')
|
|
|
|
->where([
|
2023-06-12 14:47:38 +00:00
|
|
|
'podcast_id' => $podcastId,
|
2021-04-20 13:43:38 +00:00
|
|
|
'season_number is not' => null,
|
|
|
|
])
|
2022-04-14 14:33:53 +00:00
|
|
|
->where('`published_at` <= UTC_TIMESTAMP()', null, false)
|
2021-04-20 13:43:38 +00:00
|
|
|
->groupBy('season_number')
|
|
|
|
->orderBy('season_number', 'ASC')
|
|
|
|
->get()
|
|
|
|
->getResultArray();
|
2020-10-20 10:31:40 +00:00
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
$secondsToNextUnpublishedEpisode = $episodeModel->getSecondsToNextUnpublishedEpisode($podcastId);
|
2020-10-20 10:31:40 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
|
|
|
->save(
|
|
|
|
$cacheName,
|
|
|
|
$found,
|
|
|
|
$secondsToNextUnpublishedEpisode
|
2021-04-20 13:43:38 +00:00
|
|
|
? $secondsToNextUnpublishedEpisode
|
|
|
|
: DECADE,
|
2021-05-19 16:35:13 +00:00
|
|
|
);
|
2020-10-20 10:31:40 +00:00
|
|
|
}
|
2021-04-20 13:43:38 +00:00
|
|
|
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the default query for displaying the episode list on the podcast page
|
|
|
|
*
|
2021-05-14 17:59:35 +00:00
|
|
|
* @return array<string, mixed>|null
|
2021-04-20 13:43:38 +00:00
|
|
|
*/
|
2021-05-14 17:59:35 +00:00
|
|
|
public function getDefaultQuery(int $podcastId): ?array
|
2021-04-20 13:43:38 +00:00
|
|
|
{
|
|
|
|
$cacheName = "podcast#{$podcastId}_defaultQuery";
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($defaultQuery = cache($cacheName))) {
|
2021-04-20 13:43:38 +00:00
|
|
|
$seasons = $this->getSeasons($podcastId);
|
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
if ($seasons !== []) {
|
2021-04-20 13:43:38 +00:00
|
|
|
// get latest season
|
2021-05-19 16:35:13 +00:00
|
|
|
$defaultQuery = [
|
|
|
|
'type' => 'season',
|
|
|
|
'data' => end($seasons),
|
|
|
|
];
|
2021-04-20 13:43:38 +00:00
|
|
|
} else {
|
|
|
|
$years = $this->getYears($podcastId);
|
2021-05-19 16:35:13 +00:00
|
|
|
$defaultQuery = $years === [] ? null : [
|
|
|
|
'type' => 'year',
|
|
|
|
'data' => $years[0],
|
|
|
|
];
|
2020-10-20 10:31:40 +00:00
|
|
|
}
|
2021-04-20 13:43:38 +00:00
|
|
|
|
2021-07-29 15:42:01 +00:00
|
|
|
$secondsToNextUnpublishedEpisode = (new EpisodeModel())->getSecondsToNextUnpublishedEpisode($podcastId);
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
2021-07-29 15:42:01 +00:00
|
|
|
->save(
|
|
|
|
$cacheName,
|
|
|
|
$defaultQuery,
|
|
|
|
$secondsToNextUnpublishedEpisode ? $secondsToNextUnpublishedEpisode : DECADE
|
|
|
|
);
|
2020-10-20 10:31:40 +00:00
|
|
|
}
|
2022-03-04 14:33:48 +00:00
|
|
|
|
2021-04-20 13:43:38 +00:00
|
|
|
return $defaultQuery;
|
|
|
|
}
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
/**
|
|
|
|
* @param mixed[] $data
|
|
|
|
*
|
|
|
|
* @return mixed[]
|
|
|
|
*/
|
|
|
|
public function clearCache(array $data): array
|
|
|
|
{
|
2021-06-08 09:52:11 +00:00
|
|
|
$podcast = (new self())->getPodcastById(is_array($data['id']) ? $data['id'][0] : $data['id']);
|
2021-05-18 17:16:36 +00:00
|
|
|
|
2022-02-05 16:57:20 +00:00
|
|
|
// delete cache for users' podcasts
|
|
|
|
cache()
|
|
|
|
->deleteMatching('user*podcasts');
|
|
|
|
|
2023-04-14 11:11:53 +00:00
|
|
|
if ($podcast instanceof Podcast) {
|
2021-06-08 09:52:11 +00:00
|
|
|
// delete cache all podcast pages
|
|
|
|
cache()
|
|
|
|
->deleteMatching("page_podcast#{$podcast->id}*");
|
2021-05-18 17:16:36 +00:00
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
// delete all cache for podcast actor
|
|
|
|
cache()
|
2023-08-27 13:26:06 +00:00
|
|
|
->deleteMatching(config(Fediverse::class) ->cachePrefix . "actor#{$podcast->actor_id}*");
|
2021-05-18 17:16:36 +00:00
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
// delete model requests cache, includes feed / query / episode lists, etc.
|
|
|
|
cache()
|
|
|
|
->deleteMatching("podcast#{$podcast->id}*");
|
|
|
|
cache()
|
2021-07-26 13:10:46 +00:00
|
|
|
->delete("podcast-{$podcast->handle}");
|
2021-06-08 09:52:11 +00:00
|
|
|
}
|
2021-05-18 17:16:36 +00:00
|
|
|
|
|
|
|
// clear cache for every credit page
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
|
|
|
->deleteMatching('page_credits_*');
|
2021-05-18 17:16:36 +00:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2021-04-20 13:43:38 +00:00
|
|
|
|
2023-06-21 10:07:31 +00:00
|
|
|
public function getFullTextMatchClauseForPodcasts(string $table, string $value): string
|
|
|
|
{
|
|
|
|
return '
|
|
|
|
MATCH (
|
|
|
|
' . $table . '.title ,
|
|
|
|
' . $table . '.description_markdown,
|
|
|
|
' . $table . '.handle,
|
|
|
|
' . $table . '.location_name
|
|
|
|
)
|
2023-06-21 16:17:11 +00:00
|
|
|
AGAINST(' . $this->db->escape($value) . ')
|
2023-06-21 10:07:31 +00:00
|
|
|
';
|
|
|
|
}
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
/**
|
2021-05-19 16:35:13 +00:00
|
|
|
* Creates an actor linked to the podcast (Triggered before insert)
|
2021-04-02 17:20:02 +00:00
|
|
|
*
|
2021-05-14 17:59:35 +00:00
|
|
|
* @param mixed[] $data
|
2021-05-19 16:35:13 +00:00
|
|
|
*
|
2021-05-14 17:59:35 +00:00
|
|
|
* @return mixed[]
|
2021-04-02 17:20:02 +00:00
|
|
|
*/
|
2021-05-14 17:59:35 +00:00
|
|
|
protected function createPodcastActor(array $data): array
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
$rsa = new RSA();
|
|
|
|
$rsa->setHash('sha256');
|
|
|
|
|
|
|
|
// extracts $privatekey and $publickey variables
|
2021-05-12 14:00:25 +00:00
|
|
|
$rsaKey = $rsa->createKey(2048);
|
|
|
|
$privatekey = $rsaKey['privatekey'];
|
|
|
|
$publickey = $rsaKey['publickey'];
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
$url = new URI(base_url());
|
2021-07-26 13:10:46 +00:00
|
|
|
$username = $data['data']['handle'];
|
2021-04-02 17:20:02 +00:00
|
|
|
$domain =
|
|
|
|
$url->getHost() . ($url->getPort() ? ':' . $url->getPort() : '');
|
|
|
|
|
|
|
|
$actorId = (new ActorModel())->insert(
|
|
|
|
[
|
2023-08-29 13:29:01 +00:00
|
|
|
'uri' => url_to('podcast-activity', $username),
|
2023-06-12 14:47:38 +00:00
|
|
|
'username' => $username,
|
|
|
|
'domain' => $domain,
|
|
|
|
'private_key' => $privatekey,
|
|
|
|
'public_key' => $publickey,
|
|
|
|
'display_name' => $data['data']['title'],
|
|
|
|
'summary' => $data['data']['description_html'],
|
|
|
|
'inbox_url' => url_to('inbox', $username),
|
|
|
|
'outbox_url' => url_to('outbox', $username),
|
2021-04-02 17:20:02 +00:00
|
|
|
'followers_url' => url_to('followers', $username),
|
|
|
|
],
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
|
|
|
|
$data['data']['actor_id'] = $actorId;
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
/**
|
|
|
|
* @param mixed[] $data
|
2021-05-19 16:35:13 +00:00
|
|
|
*
|
2021-05-14 17:59:35 +00:00
|
|
|
* @return mixed[]
|
|
|
|
*/
|
|
|
|
protected function setActorAvatar(array $data): array
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-06-08 09:52:11 +00:00
|
|
|
$podcast = (new self())->getPodcastById(is_array($data['id']) ? $data['id'][0] : $data['id']);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2023-04-14 11:11:53 +00:00
|
|
|
if ($podcast instanceof Podcast) {
|
2021-11-01 17:12:03 +00:00
|
|
|
$podcastActor = (new ActorModel())->find($podcast->actor_id);
|
2021-04-22 17:20:28 +00:00
|
|
|
|
2023-06-13 16:05:02 +00:00
|
|
|
if (! $podcastActor instanceof Actor) {
|
|
|
|
return $data;
|
2021-11-01 17:12:03 +00:00
|
|
|
}
|
2023-06-13 16:05:02 +00:00
|
|
|
|
|
|
|
$podcastActor->avatar_image_url = $podcast->cover->federation_url;
|
|
|
|
$podcastActor->avatar_image_mimetype = $podcast->cover->federation_mimetype;
|
|
|
|
|
|
|
|
(new ActorModel())->update($podcast->actor_id, $podcastActor);
|
2021-11-01 17:12:03 +00:00
|
|
|
}
|
2022-03-04 14:33:48 +00:00
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
/**
|
|
|
|
* @param mixed[] $data
|
2021-05-19 16:35:13 +00:00
|
|
|
*
|
2021-05-14 17:59:35 +00:00
|
|
|
* @return mixed[]
|
|
|
|
*/
|
|
|
|
protected function updatePodcastActor(array $data): array
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-06-08 09:52:11 +00:00
|
|
|
$podcast = (new self())->getPodcastById(is_array($data['id']) ? $data['id'][0] : $data['id']);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2023-04-14 11:11:53 +00:00
|
|
|
if ($podcast instanceof Podcast) {
|
2021-06-08 09:52:11 +00:00
|
|
|
$actorModel = new ActorModel();
|
|
|
|
$actor = $actorModel->getActorById($podcast->actor_id);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2023-04-14 11:11:53 +00:00
|
|
|
if ($actor instanceof Actor) {
|
2021-11-01 17:12:03 +00:00
|
|
|
// update values
|
|
|
|
$actor->display_name = $podcast->title;
|
|
|
|
$actor->summary = $podcast->description_html;
|
2021-11-23 11:54:34 +00:00
|
|
|
$actor->avatar_image_url = $podcast->cover->federation_url;
|
2023-03-16 13:00:05 +00:00
|
|
|
$actor->avatar_image_mimetype = $podcast->cover->federation_mimetype;
|
2023-04-13 11:45:03 +00:00
|
|
|
$actor->cover_image_url = get_podcast_banner_url($podcast, 'federation');
|
|
|
|
$actor->cover_image_mimetype = get_podcast_banner_mimetype($podcast, 'federation');
|
2021-11-01 17:12:03 +00:00
|
|
|
|
|
|
|
if ($actor->hasChanged()) {
|
|
|
|
$actorModel->update($actor->id, $actor);
|
|
|
|
}
|
2021-06-08 09:52:11 +00:00
|
|
|
}
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2021-09-21 15:51:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed[] $data
|
|
|
|
*
|
2023-06-21 16:17:11 +00:00
|
|
|
* Sets the UUIDv5 for a podcast. For more information, see
|
2023-07-29 08:04:19 +00:00
|
|
|
* https://podcastindex.org/namespace/1.0#guid
|
2023-06-21 16:17:11 +00:00
|
|
|
*
|
2021-09-21 15:51:04 +00:00
|
|
|
* @return mixed[]
|
|
|
|
*/
|
|
|
|
protected function setPodcastGUID(array $data): array
|
|
|
|
{
|
2023-12-26 15:52:46 +00:00
|
|
|
if (! array_key_exists(
|
|
|
|
'guid',
|
|
|
|
$data['data']
|
|
|
|
) || $data['data']['guid'] === null || $data['data']['guid'] === '') {
|
2023-06-21 16:17:11 +00:00
|
|
|
$uuid = service('uuid');
|
|
|
|
$feedUrl = url_to('podcast-rss-feed', $data['data']['handle']);
|
|
|
|
// 'ead4c236-bf58-58c6-a2c6-a6b28d128cb6' is the uuid of the podcast namespace
|
|
|
|
$data['data']['guid'] = $uuid->uuid5('ead4c236-bf58-58c6-a2c6-a6b28d128cb6', $feedUrl)->toString();
|
2021-09-21 15:51:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2020-05-31 19:15:52 +00:00
|
|
|
}
|