From ea8395a65185f0fc40ff69c65902e7d0f75910b1 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Sat, 22 Oct 2022 14:18:21 +0100 Subject: [PATCH] Add model to store syndication target data --- app/Http/Controllers/MicropubController.php | 13 ++-- app/Models/SyndicationTarget.php | 68 +++++++++++++++++++ .../factories/SyndicationTargetFactory.php | 30 ++++++++ ...55721_create_syndication_targets_table.php | 39 +++++++++++ tests/Feature/MicropubControllerTest.php | 13 +++- 5 files changed, 154 insertions(+), 9 deletions(-) create mode 100644 app/Models/SyndicationTarget.php create mode 100644 database/factories/SyndicationTargetFactory.php create mode 100644 database/migrations/2022_10_21_155721_create_syndication_targets_table.php diff --git a/app/Http/Controllers/MicropubController.php b/app/Http/Controllers/MicropubController.php index ea7d1413..8cecf46f 100644 --- a/app/Http/Controllers/MicropubController.php +++ b/app/Http/Controllers/MicropubController.php @@ -6,6 +6,7 @@ namespace App\Http\Controllers; use App\Http\Responses\MicropubResponses; use App\Models\Place; +use App\Models\SyndicationTarget; use App\Services\Micropub\HCardService; use App\Services\Micropub\HEntryService; use App\Services\Micropub\UpdateService; @@ -121,21 +122,19 @@ class MicropubController extends Controller { try { $tokenData = $this->tokenService->validateToken(request()->input('access_token')); - } catch (RequiredConstraintsViolated | InvalidTokenStructure $exception) { - $micropubResponses = new MicropubResponses(); - - return $micropubResponses->invalidTokenResponse(); + } catch (RequiredConstraintsViolated | InvalidTokenStructure) { + return (new MicropubResponses())->invalidTokenResponse(); } if (request()->input('q') === 'syndicate-to') { return response()->json([ - 'syndicate-to' => config('syndication.targets'), + 'syndicate-to' => SyndicationTarget::all(), ]); } - if (request()->input('q') == 'config') { + if (request()->input('q') === 'config') { return response()->json([ - 'syndicate-to' => config('syndication.targets'), + 'syndicate-to' => SyndicationTarget::all(), 'media-endpoint' => route('media-endpoint'), ]); } diff --git a/app/Models/SyndicationTarget.php b/app/Models/SyndicationTarget.php new file mode 100644 index 00000000..74366afa --- /dev/null +++ b/app/Models/SyndicationTarget.php @@ -0,0 +1,68 @@ + + */ + protected $visible = [ + 'uid', + 'name', + 'service', + 'user', + ]; + + /** + * The accessors to append to the model's array form. + * + * @var array + */ + protected $appends = [ + 'service', + 'user', + ]; + + /** + * Get the service data as a single attribute. + * + * @vreturn Attribute + */ + protected function service(): Attribute + { + return Attribute::get( + get: fn ($value, $attributes) => [ + 'name' => $attributes['service_name'], + 'url' => $attributes['service_url'], + 'photo' => $attributes['service_photo'], + ], + ); + } + + /** + * Get the user data as a single attribute. + * + * @vreturn Attribute + */ + protected function user(): Attribute + { + return Attribute::get( + get: fn ($value, $attributes) => [ + 'name' => $attributes['user_name'], + 'url' => $attributes['user_url'], + 'photo' => $attributes['user_photo'], + ], + ); + } +} diff --git a/database/factories/SyndicationTargetFactory.php b/database/factories/SyndicationTargetFactory.php new file mode 100644 index 00000000..c877c9d4 --- /dev/null +++ b/database/factories/SyndicationTargetFactory.php @@ -0,0 +1,30 @@ + + */ +class SyndicationTargetFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition() + { + return [ + 'uid' => $this->faker->url, + 'name' => $this->faker->name, + 'service_name' => $this->faker->name, + 'service_url' => $this->faker->url, + 'service_photo' => $this->faker->url, + 'user_name' => $this->faker->name, + 'user_url' => $this->faker->url, + 'user_photo' => $this->faker->url, + ]; + } +} diff --git a/database/migrations/2022_10_21_155721_create_syndication_targets_table.php b/database/migrations/2022_10_21_155721_create_syndication_targets_table.php new file mode 100644 index 00000000..01b3d10b --- /dev/null +++ b/database/migrations/2022_10_21_155721_create_syndication_targets_table.php @@ -0,0 +1,39 @@ +id(); + $table->string('uid'); + $table->string('name'); + $table->string('service_name'); + $table->string('service_url'); + $table->string('service_photo'); + $table->string('user_name'); + $table->string('user_url'); + $table->string('user_photo'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('syndication_targets'); + } +}; diff --git a/tests/Feature/MicropubControllerTest.php b/tests/Feature/MicropubControllerTest.php index 688ca1b8..9cdd07fb 100644 --- a/tests/Feature/MicropubControllerTest.php +++ b/tests/Feature/MicropubControllerTest.php @@ -9,6 +9,7 @@ use App\Jobs\SyndicateNoteToTwitter; use App\Models\Media; use App\Models\Note; use App\Models\Place; +use App\Models\SyndicationTarget; use Carbon\Carbon; use Faker\Factory; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -51,10 +52,18 @@ class MicropubControllerTest extends TestCase } /** @test */ - public function micropubClientsCanRequestSyndicationTargets(): void + public function micropubClientsCanRequestSyndicationTargetsCanBeEmpty(): void { $response = $this->get('/api/post?q=syndicate-to', ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]); - $response->assertJsonFragment(['uid' => 'https://twitter.com/jonnybarnes']); + $response->assertJsonFragment(['syndicate-to' => []]); + } + + /** @test */ + public function micropubClientsCanRequestSyndicationTargetsPopulatesFromModel(): void + { + $syndicationTarget = SyndicationTarget::factory()->create(); + $response = $this->get('/api/post?q=syndicate-to', ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]); + $response->assertJsonFragment(['uid' => $syndicationTarget->uid]); } /** @test */