Skip to content

Commit

Permalink
Resolve Respekt redirects when getting URL content
Browse files Browse the repository at this point in the history
  • Loading branch information
rootpd committed Mar 27, 2024
1 parent 29acc1b commit ede45c6
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 11 deletions.
40 changes: 29 additions & 11 deletions Mailer/app/Models/PageMeta/Content/RespektContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Remp\Mailer\Models\PageMeta\Content;

use GuzzleHttp\Exception\RequestException;
use Nette\Utils\Json;
use Nette\Utils\JsonException;
use Remp\Mailer\Models\PageMeta\RespektMeta;
use Remp\Mailer\Models\PageMeta\Transport\RespektApiTransport;
use Remp\MailerModule\Models\PageMeta\Content\ContentInterface;
use Remp\MailerModule\Models\PageMeta\Content\InvalidUrlException;
use Remp\MailerModule\Models\PageMeta\Meta;
use Remp\MailerModule\Models\PageMeta\Transport\TransportInterface;
use Tracy\Debugger;
Expand All @@ -27,19 +30,34 @@ public function __construct(private TransportInterface $transport)

public function fetchUrlMeta(string $url): ?Meta
{
$data = $this->transport->getContent($url);
if ($data === null) {
return null;
}

try {
$data = Json::decode($data, true);
} catch (JsonException $e) {
Debugger::log($e->getMessage(), ILogger::ERROR);
return null;
}
$data = $this->transport->getContent($url);
if ($data === null) {
return null;
}

$article = $data['data']['getArticle'];
try {
$data = Json::decode($data, true);
} catch (JsonException $e) {
Debugger::log($e->getMessage(), ILogger::ERROR);
return null;
}

$article = $data['data']['getArticle'];
if ($article === null) {
// URL has changed, we need to follow redirects and determine new URL
if ($this->transport instanceof RespektApiTransport) {
$resolvedUrl = $this->transport->resolveRedirects($url);
if ($url && $resolvedUrl !== $url) {
return $this->fetchUrlMeta($resolvedUrl);
}
}

return null;
}
} catch (RequestException $e) {
throw new InvalidUrlException("Invalid URL: {$url}", 0, $e);
}

// get article title
$title = $article['title'];
Expand Down
48 changes: 48 additions & 0 deletions Mailer/app/Models/PageMeta/Transport/RespektApiTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Uri;
use Nette\Utils\Json;
use Remp\MailerModule\Models\PageMeta\Transport\TransportInterface;
use Tracy\Debugger;
use Tracy\ILogger;
Expand Down Expand Up @@ -60,4 +61,51 @@ public function getContent(string $url): ?string

return $response->getBody()->getContents();
}

public function resolveRedirects(string $url): ?string
{
$uri = new Uri($url);
$contentPath = trim($uri->getPath(), '/');

$client = new Client();
try {
$query = <<<'GRAPHQL'
query GetRedirectUrlForMailer($articleUrl: String) {
getUrl(by: { url: $articleUrl}) {
redirectType
target {
internalTarget {
url
}
}
}
}
GRAPHQL;

$response = $client->post($this->respektContentUrl, [
'headers' => ['Authorization' => 'Bearer ' . $this->respektContentToken],
'json' => [
'query' => $query,
'variables' => [
'articleUrl' => $contentPath
]
]
]);
} catch (GuzzleException $e) {
Debugger::log($e->getMessage(), ILogger::ERROR);
return null;
}

$response = $response->getBody()->getContents();
$json = Json::decode($response);

$path = $json->data->getUrl->target->internalTarget->url ?? null;
if (!$path) {
return null;
}

$resolvedUrl = new Uri($url);
$resolvedUrl = $resolvedUrl->withPath($path);
return (string) $resolvedUrl;
}
}

0 comments on commit ede45c6

Please sign in to comment.