Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"pestphp/pest-plugin-livewire": "^1.0.0",
"phpunit/phpunit": "^9.5",
"orchestra/testbench": "^6.2",
"spatie/pest-plugin-snapshots": "^1.0"
"spatie/pest-plugin-snapshots": "^1.0",
"guzzlehttp/guzzle": "^7.3"
},
"autoload": {
"psr-4": {
Expand Down
214 changes: 212 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 30 additions & 9 deletions src/Extensions/Image/TwitterRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace ARKEcosystem\CommonMark\Extensions\Image;

use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

Expand All @@ -11,14 +13,33 @@ public static function render(MediaUrl $url)
{
$url = 'https://twitter.com/'.$url->getId();

return Cache::rememberForever(md5($url), fn () => Http::get('https://publish.twitter.com/oembed', [
'url' => $url,
'hide_thread' => 1,
'hide_media' => 0,
'omit_script' => true,
'dnt' => true,
'limit' => 20,
'chrome' => 'nofooter',
])->json()['html']);
$result = Cache::rememberForever(md5($url), function () use ($url) {
try {
$response = Http::get('https://publish.twitter.com/oembed', [
'url' => $url,
'hide_thread' => 1,
'hide_media' => 0,
'omit_script' => true,
'dnt' => true,
'limit' => 20,
'chrome' => 'nofooter',
])->json();

return Arr::get($response, 'html', '');
} catch (ConnectionException $e) {
return false;
}
});

// If the result is `false`, means we had a connection error
// (publish.twitter.com is down) in that case the results should not be
// cached forever. We'll cache the result for 5 minutes.
if ($result === false) {
Cache::forget(md5($url));

return Cache::remember(md5($url), now()->addMinutes(5), fn () => '');
}

return $result;
}
}
93 changes: 93 additions & 0 deletions tests/Extensions/Image/TwitterRendererTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

use ARKEcosystem\CommonMark\Extensions\Image\MediaUrl;
use ARKEcosystem\CommonMark\Extensions\Image\TwitterRenderer;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

it('should render twitter embeds', function () {
$response = [
'url' => 'https://twitter.com/arkecosystem/status/1234',
'html' => '<blockquote>...</blockquote>',
];

Http::fake([
'publish.twitter.com/*' => $response,
]);

$mediaUrl = new MediaUrl('twitter', 'arkecosystem/status/1234');
$subject = new TwitterRenderer();

expect($subject->render($mediaUrl))->toBe('<blockquote>...</blockquote>');
});

it('gets the response from the cache if set', function () {
$id = 'arkecosystem/status/1234';
Cache::set(md5('https://twitter.com/'.$id), '<blockquote>cached</blockquote>');

$mediaUrl = new MediaUrl('twitter', 'arkecosystem/status/1234');
$subject = new TwitterRenderer();

expect($subject->render($mediaUrl))->toBe('<blockquote>cached</blockquote>');
});

it('returns an empty string if receives an invalid response', function () {
$response = [
'invalid' => 'invalid',
];

Http::fake([
'publish.twitter.com/*' => $response,
]);

$mediaUrl = new MediaUrl('twitter', 'arkecosystem/status/1234');

$subject = new TwitterRenderer();

expect($subject->render($mediaUrl))->toBe('');
});

it('returns an empty string if twitter server is down', function () {
Http::fake([
'publish.twitter.com/*' => function () {
throw new ConnectionException();
},
]);

$mediaUrl = new MediaUrl('twitter', 'arkecosystem/status/1234');

$subject = new TwitterRenderer();

expect($subject->render($mediaUrl))->toBe('');
});

it('caches the response for 5 minutes if server is down', function () {
$id = 'arkecosystem/status/1234';
$cacheKey = md5('https://twitter.com/'.$id);

Cache::shouldReceive('rememberForever')
->once()
->with($cacheKey, \Closure::class)
->andReturn(false)
->shouldReceive('forget')
->once()
->with($cacheKey)
->shouldReceive('remember')
->with($cacheKey, Carbon::class, \Closure::class)
->once()
->andReturn('');

Http::fake([
'publish.twitter.com/*' => function () {
throw new ConnectionException();
},
]);

$mediaUrl = new MediaUrl('twitter', $id);

$subject = new TwitterRenderer();

expect($subject->render($mediaUrl))->toBe('');
});