feat(analytics): add OP3 analytics service option + update episode audio url

This commit is contained in:
Yassine Doghri 2022-12-09 15:04:42 +00:00
commit 16527ed529
19 changed files with 214 additions and 145 deletions

View file

@ -17,13 +17,13 @@ use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Config\Services;
use Modules\Analytics\Config\Analytics;
use Modules\PremiumPodcasts\Models\SubscriptionModel;
use Psr\Log\LoggerInterface;
class EpisodeAnalyticsController extends Controller
{
public mixed $config;
/**
* An array of helpers to be loaded automatically upon class instantiation. These helpers will be available to all
* other controllers that extend Analytics.
@ -32,7 +32,7 @@ class EpisodeAnalyticsController extends Controller
*/
protected $helpers = ['analytics'];
protected Analytics $config;
protected Analytics $analyticsConfig;
/**
* Constructor.
@ -52,70 +52,26 @@ class EpisodeAnalyticsController extends Controller
$this->config = config('Analytics');
}
public function hit(string $base64EpisodeData, string ...$audioPath): RedirectResponse|ResponseInterface
/**
* @deprecated Replaced by EpisodeController::audio method
*/
public function hit(string $base64EpisodeData, string ...$audioPath): RedirectResponse
{
$session = Services::session();
$session->start();
$serviceName = '';
if ($this->request->getGet('_from')) {
$serviceName = $this->request->getGet('_from');
} elseif ($session->get('embed_domain') !== null) {
$serviceName = $session->get('embed_domain');
} elseif ($session->get('referer') !== null && $session->get('referer') !== '- Direct -') {
$serviceName = parse_url((string) $session->get('referer'), PHP_URL_HOST);
}
$episodeData = unpack(
'IpodcastId/IepisodeId/IbytesThreshold/IfileSize/Iduration/IpublicationDate',
base64_url_decode($base64EpisodeData),
);
if (! $episodeData) {
if ($episodeData === false) {
throw PageNotFoundException::forPageNotFound();
}
// check if episode is premium?
$episode = (new EpisodeModel())->getEpisodeById($episodeData['episodeId']);
if (! $episode instanceof Episode) {
return $this->response->setStatusCode(404);
throw PageNotFoundException::forPageNotFound();
}
$subscription = null;
// check if podcast is already unlocked before any token validation
if ($episode->is_premium && ($subscription = service('premium_podcasts')->subscription(
$episode->podcast->handle
)) === null) {
// look for token as GET parameter
if (($token = $this->request->getGet('token')) === null) {
return $this->response->setStatusCode(
401,
'Episode is premium, you must provide a token to unlock it.'
);
}
// check if there's a valid subscription for the provided token
if (($subscription = (new SubscriptionModel())->validateSubscription(
$episode->podcast->handle,
$token
)) === null) {
return $this->response->setStatusCode(401, 'Invalid token!');
}
}
podcast_hit(
$episodeData['podcastId'],
$episodeData['episodeId'],
$episodeData['bytesThreshold'],
$episodeData['fileSize'],
$episodeData['duration'],
$episodeData['publicationDate'],
$serviceName,
$subscription !== null ? $subscription->id : null
);
return redirect()->to($this->config->getAudioUrl($episode->audio->file_path));
return redirect()->route('episode-audio', [$episode->podcast->handle, $episode->slug]);
}
}