Commit 7fe9d425 authored by Benjamin Bellamy's avatar Benjamin Bellamy 💬
Browse files

fix(analytics): update service management so that it works with new OPAWG slug values

parent 8b3c689f
Loading
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -23,22 +23,24 @@ class Feed extends Controller
            throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
        }

        $service = null;
        $serviceSlug = '';
        try {
            $service = \Opawg\UserAgentsPhp\UserAgentsRSS::find(
                $_SERVER['HTTP_USER_AGENT']
            );
            if ($service) {
                $serviceSlug = $service['slug'];
            }
        } catch (\Exception $e) {
            // If things go wrong the show must go on and the user must be able to download the file
            log_message('critical', $e);
        }

        $cacheName =
            "podcast{$podcast->id}_feed" .
            ($service ? "_{$service['slug']}" : '');
            "podcast{$podcast->id}_feed" . ($service ? "_{$serviceSlug}" : '');

        if (!($found = cache($cacheName))) {
            $found = get_rss_feed($podcast, $service ? $service['name'] : '');
            $found = get_rss_feed($podcast, $serviceSlug);

            // The page cache is set to expire after next episode publication or a decade by default so it is deleted manually upon podcast update
            $secondsToNextUnpublishedEpisode = (new EpisodeModel())->getSecondsToNextUnpublishedEpisode(
+1 −1
Original line number Diff line number Diff line
@@ -78,7 +78,7 @@ class FakePodcastsAnalyticsSeeder extends Seeder
                        $service =
                            $jsonRSSUserAgents[
                                rand(1, count($jsonRSSUserAgents) - 1)
                            ]['name'];
                            ]['slug'];
                        $app = isset($player['app']) ? $player['app'] : '';
                        $device = isset($player['device'])
                            ? $player['device']
+5 −0
Original line number Diff line number Diff line
@@ -14,6 +14,11 @@ use CodeIgniter\Entity;

class AnalyticsPodcastsByCountry extends Entity
{
    /**
     * @var string
     */
    protected $labels;

    protected $casts = [
        'podcast_id' => 'integer',
        'country_code' => 'string',
+38 −0
Original line number Diff line number Diff line
<?php

/**
 * Class AnalyticsPodcastsByService
 * Entity for AnalyticsPodcastsByService
 * @copyright  2020 Podlibre
 * @license    https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
 * @link       https://castopod.org/
 */

namespace App\Entities;

use CodeIgniter\Entity;

class AnalyticsPodcastsByService extends Entity
{
    /**
     * @var string
     */
    protected $labels;

    protected $casts = [
        'podcast_id' => 'integer',
        'app' => '?string',
        'device' => '?string',
        'os' => '?string',
        'is_bot' => 'boolean',
        'date' => 'datetime',
        'hits' => 'integer',
    ];

    public function getLabels()
    {
        return \Opawg\UserAgentsPhp\UserAgentsRSS::getName(
            $this->attributes['labels']
        ) ?? $this->attributes['labels'];
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@ use Config\Mimes;
 * @param string $service The name of the service that fetches the RSS feed for future reference when the audio file is eventually downloaded
 * @return string rss feed as xml
 */
function get_rss_feed($podcast, $serviceName = '')
function get_rss_feed($podcast, $serviceSlug = '')
{
    $episodes = $podcast->episodes;

@@ -185,7 +185,7 @@ function get_rss_feed($podcast, $serviceName = '')
        $enclosure->addAttribute(
            'url',
            $episode->enclosure_url .
                (empty($serviceName) ? '' : '?_from=' . urlencode($serviceName))
                (empty($serviceSlug) ? '' : '?_from=' . urlencode($serviceSlug))
        );
        $enclosure->addAttribute('length', $episode->enclosure_filesize);
        $enclosure->addAttribute('type', $episode->enclosure_mimetype);
Loading