-
-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #495 from pinkary-project/feat/hashtag-storage
Feat(hashtags): Add hashtag storage and relation syncing
- Loading branch information
Showing
13 changed files
with
394 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\EventActions; | ||
|
||
use App\Models\Hashtag; | ||
use App\Models\Question; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Str; | ||
|
||
final readonly class UpdateQuestionHashtags | ||
{ | ||
/** | ||
* Create a new class instance. | ||
*/ | ||
public function __construct( | ||
public Question $question, | ||
) { | ||
// | ||
} | ||
|
||
/** | ||
* @return array{attached: array<int, int>, detached: array<int, int>, updated: array<int, int>} | ||
*/ | ||
public function handle(): array | ||
{ | ||
$parsedHashtags = $this->parsedHashtagNames(); | ||
|
||
$existingHashtags = Hashtag::query()->whereIn('name', $parsedHashtags->all())->get(); | ||
|
||
$newHashtags = $parsedHashtags->diff($existingHashtags->pluck('name')) | ||
->map(fn (string $name): Hashtag => Hashtag::query()->create(['name' => $name])); | ||
|
||
return $this->question->hashtags()->sync($existingHashtags->merge($newHashtags)); | ||
} | ||
|
||
/** | ||
* Get the unique hashtag names found in the question. | ||
* | ||
* @return Collection<int, string> | ||
*/ | ||
private function parsedHashtagNames(): Collection | ||
{ | ||
$matches = []; | ||
|
||
preg_match_all( | ||
'/(<(a|code|pre)\s+[^>]*>.*?<\/\2>)|(?<!&)#([a-z0-9]+)/is', | ||
"{$this->question->answer} {$this->question->content}", | ||
$matches, | ||
); | ||
|
||
return collect($matches[3] ?? []) | ||
->filter() | ||
->unique() | ||
->values() | ||
->map(fn (string $hashtag): string => Str::limit($hashtag, 50, '')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Models; | ||
|
||
use Carbon\Carbon; | ||
use Database\Factories\HashtagFactory; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsToMany; | ||
|
||
/** | ||
* @property int $id | ||
* @property string $name | ||
* @property Carbon $created_at | ||
* @property Carbon $updated_at | ||
* @property-read Collection<int, Question> $questions | ||
*/ | ||
final class Hashtag extends Model | ||
{ | ||
/** @use HasFactory<HashtagFactory> */ | ||
use HasFactory; | ||
|
||
/** | ||
* The attributes that should be cast. | ||
* | ||
* @return array<string, string> | ||
*/ | ||
public function casts(): array | ||
{ | ||
return [ | ||
'created_at' => 'datetime', | ||
'updated_at' => 'datetime', | ||
]; | ||
} | ||
|
||
/** | ||
* @return BelongsToMany<Question> | ||
*/ | ||
public function questions(): BelongsToMany | ||
{ | ||
return $this->belongsToMany(Question::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Database\Factories; | ||
|
||
use App\Models\Hashtag; | ||
use Database\Factories\Concerns\RefreshOnCreate; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
/** | ||
* @extends Factory<Hashtag> | ||
*/ | ||
final class HashtagFactory extends Factory | ||
{ | ||
use RefreshOnCreate; | ||
|
||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function definition(): array | ||
{ | ||
return [ | ||
'name' => $this->faker->word(), | ||
]; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
database/migrations/2024_08_07_203106_create_hashtags_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use App\Models\Hashtag; | ||
use App\Models\Question; | ||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('hashtags', function (Blueprint $table): void { | ||
$table->id(); | ||
$table->string('name')->unique(); | ||
$table->timestamps(); | ||
|
||
$table->rawIndex('name collate nocase', 'name_collate_nocase'); | ||
}); | ||
|
||
Schema::create('hashtag_question', function (Blueprint $table): void { | ||
$table->id(); | ||
$table->foreignIdFor(Hashtag::class)->constrained()->cascadeOnDelete(); | ||
$table->foreignIdFor(Question::class)->constrained()->cascadeOnDelete(); | ||
|
||
$table->unique(['hashtag_id', 'question_id']); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use App\EventActions\UpdateQuestionHashtags; | ||
use App\Models\Hashtag; | ||
use App\Models\Question; | ||
|
||
it('attaches the newly parsed hashtags', function () { | ||
$question = Question::factory()->create(); | ||
|
||
$question->answer = '#hashtag1 #hashtag2'; | ||
|
||
$synced = (new UpdateQuestionHashtags($question))->handle(); | ||
|
||
$hashtag1 = Hashtag::query()->firstWhere('name', 'hashtag1'); | ||
$hashtag2 = Hashtag::query()->firstWhere('name', 'hashtag2'); | ||
|
||
expect($synced)->toBe([ | ||
'attached' => [ | ||
$hashtag1->id, | ||
$hashtag2->id, | ||
], | ||
'detached' => [], | ||
'updated' => [], | ||
]) | ||
->and($question->hashtags->pluck('name')->all())->toBe(['hashtag1', 'hashtag2']); | ||
}); | ||
|
||
it('detaches hashtags no longer found in the question', function () { | ||
$question = Question::factory()->create([ | ||
'answer' => '#hashtag1 #hashtag2', | ||
]); | ||
|
||
expect($question->hashtags->pluck('name')->all())->toBe(['hashtag1', 'hashtag2']); | ||
|
||
$question->answer = '#hashtag3'; | ||
|
||
$synced = (new UpdateQuestionHashtags($question))->handle(); | ||
|
||
$hashtag1 = Hashtag::query()->firstWhere('name', 'hashtag1'); | ||
$hashtag2 = Hashtag::query()->firstWhere('name', 'hashtag2'); | ||
$hashtag3 = Hashtag::query()->firstWhere('name', 'hashtag3'); | ||
|
||
expect($synced)->toBe([ | ||
'attached' => [ | ||
$hashtag3->id, | ||
], | ||
'detached' => [ | ||
$hashtag1->id, | ||
$hashtag2->id, | ||
], | ||
'updated' => [], | ||
]) | ||
->and($question->refresh()->hashtags->pluck('name')->all())->toBe(['hashtag3']); | ||
}); | ||
|
||
it('will not parse hashtags within code and links', function () { | ||
$question = Question::factory()->create(); | ||
|
||
$question->answer = <<<'ANSWER' | ||
```php | ||
// some code with a #hashtag here. | ||
$url = https://example.com/route#segment | ||
``` | ||
Check out this link with a segment: https://example.com/route#segment | ||
But the #cool hashtag should be synced! | ||
ANSWER; | ||
|
||
(new UpdateQuestionHashtags($question))->handle(); | ||
|
||
expect($question->hashtags->pluck('name')->all())->toBe(['cool']); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use App\Models\Hashtag; | ||
use App\Models\Question; | ||
|
||
test('to array', function () { | ||
$question = Hashtag::factory()->create(); | ||
|
||
expect(array_keys($question->toArray()))->toBe([ | ||
'id', | ||
'name', | ||
'created_at', | ||
'updated_at', | ||
]); | ||
}); | ||
|
||
test('relations', function () { | ||
$hashtag = Hashtag::factory() | ||
->hasQuestions(1) | ||
->create(); | ||
|
||
expect($hashtag->questions)->each->toBeInstanceOf(Question::class); | ||
}); |
Oops, something went wrong.