Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ignore markdown on metadata #287

Merged
merged 9 commits into from
Oct 30, 2023
11 changes: 10 additions & 1 deletion app/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Spatie\Image\Manipulations;
Expand Down Expand Up @@ -154,7 +155,15 @@ public function scopeWithFeaturedCollections(Builder $query): Builder

public function metaDescription(): string
{
return $this->meta_description ?? Str::limit(strip_tags($this->content), 157);
return Cache::rememberForever("article:{$this->id}:meta_description", function () {
if (boolval($this->meta_description)) {
return $this->meta_description;
}

$rawContent = $this->renderedMarkdown();

return Str::limit(trim(preg_replace("/\r?\n/", ' ', html_entity_decode(strip_tags($rawContent)))), 157);
});
}

public function isNotPublished(): bool
Expand Down
18 changes: 18 additions & 0 deletions app/Observers/ArticleObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@

use App\Jobs\ConvertArticleToSpeech;
use App\Models\Article;
use Illuminate\Support\Facades\Cache;

class ArticleObserver
{
public function created(Article $article): void
{
$this->prepareArticleSpeech($article);

$this->forgetMetaDescriptionCache($article);
}

public function updated(Article $article): void
{
$this->prepareArticleSpeech($article);

$this->forgetMetaDescriptionCache($article);
}

private function prepareArticleSpeech(Article $article): void
Expand All @@ -39,4 +44,17 @@ private function prepareArticleSpeech(Article $article): void

ConvertArticleToSpeech::dispatch($article);
}

private function forgetMetaDescriptionCache(Article $article): void
{
if ($article->isNotPublished()) {
return;
}

if (! ($article->wasRecentlyCreated || $article->wasChanged(['meta_description', 'content', 'is_published']))) {
return;
}

Cache::forget("article:{$article->id}:meta_description");
}
}
20 changes: 20 additions & 0 deletions tests/App/Models/ArticleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Article;
use App\Models\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

it('should create an article', function () {
Expand Down Expand Up @@ -49,6 +50,25 @@
expect($article->metaDescription())->toBe('This is the content');
});

it('removes markdown in meta description', function () {
$article = Article::factory()->create([
'meta_description' => null,
'content' => "### This is the content\n\nwith some *markdown*\n",
]);

expect($article->metaDescription())->toBe('This is the content with some markdown');
});

it('gets meta description from cache', function () {
$article = Article::factory()->create();

Cache::shouldReceive('rememberForever')
->with('article:'.$article->id.':meta_description', Closure::class)
->andReturn('This is the content');

expect($article->metaDescription())->toBe('This is the content');
});

it('truncates the content if used as meta description', function () {
$article = Article::factory()->create([
'meta_description' => null,
Expand Down
75 changes: 75 additions & 0 deletions tests/App/Observers/ArticleObserverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use App\Jobs\ConvertArticleToSpeech;
use App\Models\Article;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Cache;

beforeEach(function () {
config(['dashbrd.text_to_speech.enabled' => true]);

Article::truncate();
});

it('should not start audio conversion if text-to-speech is not enabled', function () {
Expand Down Expand Up @@ -115,3 +118,75 @@

ConvertArticleToSpeech::enable();
});

it('should not clear the meta description cache if article is not published', function () {
Cache::shouldReceive('forget')->never();

Article::factory()->create([
'content' => 'Hello World',
'meta_description' => 'Hello World',
'published_at' => null,
]);
});

it('should not clear the meta description cache if article content or meta description did not change', function () {
$article = Article::factory()->create([
'content' => 'Hello World',
'meta_description' => 'Hello World',
'published_at' => now()->subMinutes(2),
])->fresh();

Cache::shouldReceive('forget')->never();

$article->update([
'title' => 'Updated title',
]);
});

it('should clear the meta description cache when an article created', function () {
Cache::shouldReceive('forget')->with('article:1:meta_description')->once();

Article::factory()->create([
'content' => 'Hello World',
'published_at' => now()->subMinutes(2),
]);
});

it('should clear the meta description cache when an article content is updated', function () {
$article = Article::factory()->create([
'content' => 'Hello World',
'published_at' => now()->subMinutes(2),
]);

Cache::shouldReceive('forget')->with('article:1:meta_description')->once();

$article->update([
'content' => 'Updated content',
]);
});

it('should clear the meta description cache when an article meta_description is updated', function () {
$article = Article::factory()->create([
'content' => 'Hello World',
'published_at' => now()->subMinutes(2),
]);

Cache::shouldReceive('forget')->with('article:1:meta_description')->once();

$article->update([
'meta_description' => 'Updated content',
]);
});

it('should clear the meta description cache when an article is published', function () {
$article = Article::factory()->create([
'content' => 'Hello World',
'published_at' => null,
]);

Cache::shouldReceive('forget')->with('article:1:meta_description')->once();

$article->update([
'published_at' => now()->subMinutes(2),
]);
});
Loading