castopod/app/Entities/Podcast.php
Benjamin Bellamy 03e23a28bf feat: update analytics so to meet IABv2 requirements
- https://iabtechlab.com/wp-content/uploads/2017/12/Podcast_Measurement_v2-Dec-20-2017.pdf
  - no IP address is ever stored on the server. Only aggregate data is stored in the dababase.
  - rolling 24-hour window
  - castopod does not do pre-load
  - IP Blacklisting https://github.com/client9/ipcat
  - user-agent Filtering https://github.com/opawg/user-agents
  - ignores 2 bytes range "Range: 0-1" (performed by official Apple iOS Podcast app)
  - in case of partial content, adds up all requests to check >1mn was downloaded
  - identifying Uniques is done with a combination of IP Address and User Agent
- add AMcharts
- add some graphs
- add regions to analytics
- add ipcat blacklist
- enhance useragents performances
- add filesize and header size in order to calculate 1mn downloads
- update publisher ID3 field
- update castopod icon
- add disclaimer and warning import form translation
- update docs/setup-development.md

closes #10
2020-10-15 14:42:02 +00:00

268 lines
6.1 KiB
PHP

<?php
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Entities;
use App\Models\CategoryModel;
use App\Models\EpisodeModel;
use App\Models\PlatformModel;
use CodeIgniter\Entity;
use App\Models\UserModel;
use League\CommonMark\CommonMarkConverter;
class Podcast extends Entity
{
/**
* @var string
*/
protected $link;
/**
* @var \App\Entities\Image
*/
protected $image;
/**
* @var \App\Entities\Episode[]
*/
protected $episodes;
/**
* @var \App\Entities\Category
*/
protected $category;
/**
* @var \App\Entities\Category[]
*/
protected $other_categories;
/**
* @var integer[]
*/
protected $other_categories_ids;
/**
* @var \App\Entities\User[]
*/
protected $contributors;
/**
* @var string
*/
protected $description_html;
/**
* @var \App\Entities\Platform
*/
protected $platforms;
protected $casts = [
'id' => 'integer',
'title' => 'string',
'name' => 'string',
'description' => 'string',
'image_uri' => 'string',
'language' => 'string',
'category_id' => 'integer',
'parental_advisory' => '?string',
'publisher' => '?string',
'owner_name' => '?string',
'owner_email' => '?string',
'type' => 'string',
'copyright' => '?string',
'block' => 'boolean',
'complete' => 'boolean',
'episode_description_footer' => '?string',
'created_by' => 'integer',
'updated_by' => 'integer',
'imported_feed_url' => '?string',
'new_feed_url' => '?string',
];
/**
* Saves a cover image to the corresponding podcast folder in `public/media/podcast_name/`
*
* @param \CodeIgniter\HTTP\Files\UploadedFile|\CodeIgniter\Files\File $image
*
*/
public function setImage($image = null)
{
if ($image) {
helper('media');
$this->attributes['image_uri'] = save_podcast_media(
$image,
$this->attributes['name'],
'cover'
);
$this->image = new \App\Entities\Image(
$this->attributes['image_uri']
);
$this->image->saveSizes();
}
return $this;
}
public function getImage()
{
return new \App\Entities\Image($this->attributes['image_uri']);
}
public function getLink()
{
return base_url(route_to('podcast', $this->attributes['name']));
}
public function getFeedUrl()
{
return base_url(route_to('podcast_feed', $this->attributes['name']));
}
/**
* Returns the podcast's episodes
*
* @return \App\Entities\Episode[]
*/
public function getEpisodes()
{
if (empty($this->id)) {
throw new \RuntimeException(
'Podcast must be created before getting episodes.'
);
}
if (empty($this->episodes)) {
$this->episodes = (new EpisodeModel())->getPodcastEpisodes(
$this->id,
$this->type
);
}
return $this->episodes;
}
/**
* Returns the podcast category entity
*
* @return \App\Entities\Category
*/
public function getCategory()
{
if (empty($this->id)) {
throw new \RuntimeException(
'Podcast must be created before getting category.'
);
}
if (empty($this->category)) {
$this->category = (new CategoryModel())->find($this->category_id);
}
return $this->category;
}
/**
* Returns all podcast contributors
*
* @return \App\Entities\User[]
*/
public function getContributors()
{
if (empty($this->id)) {
throw new \RuntimeException(
'Podcasts must be created before getting contributors.'
);
}
if (empty($this->contributors)) {
$this->contributors = (new UserModel())->getPodcastContributors(
$this->id
);
}
return $this->contributors;
}
public function getDescriptionHtml()
{
$converter = new CommonMarkConverter([
'html_input' => 'strip',
'allow_unsafe_links' => false,
]);
return $converter->convertToHtml($this->attributes['description']);
}
public function setCreatedBy(\App\Entities\User $user)
{
$this->attributes['created_by'] = $user->id;
return $this;
}
public function setUpdatedBy(\App\Entities\User $user)
{
$this->attributes['updated_by'] = $user->id;
return $this;
}
/**
* Returns the podcast's platform links
*
* @return \App\Entities\Platform[]
*/
public function getPlatforms()
{
if (empty($this->id)) {
throw new \RuntimeException(
'Podcast must be created before getting platform links.'
);
}
if (empty($this->platforms)) {
$this->platforms = (new PlatformModel())->getPodcastPlatformLinks(
$this->id
);
}
return $this->platforms;
}
public function getOtherCategories()
{
if (empty($this->id)) {
throw new \RuntimeException(
'Podcast must be created before getting other categories.'
);
}
if (empty($this->other_categories)) {
$this->other_categories = (new CategoryModel())->getPodcastCategories(
$this->id
);
}
return $this->other_categories;
}
public function getOtherCategoriesIds()
{
if (empty($this->other_categories_ids)) {
$this->other_categories_ids = array_column(
$this->getOtherCategories(),
'id'
);
}
return $this->other_categories_ids;
}
}