From ca6205d2a6824adc68bcc2c046c604165e90db93 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Tue, 29 Nov 2022 19:58:44 +0000 Subject: [PATCH] Allow micropub to post notes and articles --- app/Models/Article.php | 11 +++++-- app/Services/ArticleService.php | 19 +++++++++++ app/Services/BookmarkService.php | 4 +-- app/Services/LikeService.php | 4 +-- app/Services/Micropub/HEntryService.php | 11 +++++-- app/Services/NoteService.php | 41 +++--------------------- app/Services/Service.php | 30 +++++++++++++++++ psalm.xml | 1 - tests/Feature/MicropubControllerTest.php | 30 ++++++++++++++++- 9 files changed, 102 insertions(+), 49 deletions(-) create mode 100644 app/Services/ArticleService.php create mode 100644 app/Services/Service.php diff --git a/app/Models/Article.php b/app/Models/Article.php index 11256946..7871cac3 100644 --- a/app/Models/Article.php +++ b/app/Models/Article.php @@ -53,11 +53,16 @@ class Article extends Model } /** - * We shall set a blacklist of non-modifiable model attributes. + * The attributes that are mass assignable. * - * @var array + * @var array */ - protected $guarded = ['id']; + protected $fillable = [ + 'url', + 'title', + 'main', + 'published', + ]; protected function html(): Attribute { diff --git a/app/Services/ArticleService.php b/app/Services/ArticleService.php new file mode 100644 index 00000000..195f7051 --- /dev/null +++ b/app/Services/ArticleService.php @@ -0,0 +1,19 @@ + $this->getDataByKey($request, 'name'), + 'main' => $this->getDataByKey($request, 'content'), + 'published' => true, + ]); + } +} diff --git a/app/Services/BookmarkService.php b/app/Services/BookmarkService.php index d9012912..1d77d362 100644 --- a/app/Services/BookmarkService.php +++ b/app/Services/BookmarkService.php @@ -18,7 +18,7 @@ use Ramsey\Uuid\Uuid; use Spatie\Browsershot\Browsershot; use Spatie\Browsershot\Exceptions\CouldNotTakeBrowsershot; -class BookmarkService +class BookmarkService extends Service { /** * Create a new Bookmark. @@ -26,7 +26,7 @@ class BookmarkService * @param array $request Data from request()->all() * @return Bookmark */ - public function createBookmark(array $request): Bookmark + public function create(array $request, ?string $client = null): Bookmark { if (Arr::get($request, 'properties.bookmark-of.0')) { //micropub request diff --git a/app/Services/LikeService.php b/app/Services/LikeService.php index 1c199223..99840838 100644 --- a/app/Services/LikeService.php +++ b/app/Services/LikeService.php @@ -8,7 +8,7 @@ use App\Jobs\ProcessLike; use App\Models\Like; use Illuminate\Support\Arr; -class LikeService +class LikeService extends Service { /** * Create a new Like. @@ -16,7 +16,7 @@ class LikeService * @param array $request * @return Like $like */ - public function createLike(array $request): Like + public function create(array $request, ?string $client = null): Like { if (Arr::get($request, 'properties.like-of.0')) { //micropub request diff --git a/app/Services/Micropub/HEntryService.php b/app/Services/Micropub/HEntryService.php index 2f8a2779..9488c949 100644 --- a/app/Services/Micropub/HEntryService.php +++ b/app/Services/Micropub/HEntryService.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Services\Micropub; +use App\Services\ArticleService; use App\Services\BookmarkService; use App\Services\LikeService; use App\Services\NoteService; @@ -21,13 +22,17 @@ class HEntryService public function process(array $request, ?string $client = null): ?string { if (Arr::get($request, 'properties.like-of') || Arr::get($request, 'like-of')) { - return resolve(LikeService::class)->createLike($request)->longurl; + return resolve(LikeService::class)->create($request)->longurl; } if (Arr::get($request, 'properties.bookmark-of') || Arr::get($request, 'bookmark-of')) { - return resolve(BookmarkService::class)->createBookmark($request)->longurl; + return resolve(BookmarkService::class)->create($request)->longurl; } - return resolve(NoteService::class)->createNote($request, $client)->longurl; + if (Arr::get($request, 'properties.name') || Arr::get($request, 'name')) { + return resolve(ArticleService::class)->create($request)->longurl; + } + + return resolve(NoteService::class)->create($request, $client)->longurl; } } diff --git a/app/Services/NoteService.php b/app/Services/NoteService.php index 156acbfb..a37b917f 100644 --- a/app/Services/NoteService.php +++ b/app/Services/NoteService.php @@ -14,7 +14,7 @@ use App\Models\SyndicationTarget; use Illuminate\Support\Arr; use Illuminate\Support\Str; -class NoteService +class NoteService extends Service { /** * Create a new note. @@ -23,12 +23,12 @@ class NoteService * @param string|null $client * @return Note */ - public function createNote(array $request, ?string $client = null): Note + public function create(array $request, ?string $client = null): Note { $note = Note::create( [ - 'note' => $this->getContent($request), - 'in_reply_to' => $this->getInReplyTo($request), + 'note' => $this->getDataByKey($request, 'content'), + 'in_reply_to' => $this->getDataByKey($request, 'in-reploy-to'), 'client_id' => $client, ] ); @@ -66,39 +66,6 @@ class NoteService return $note; } - /** - * Get the content from the request to create a new note. - * - * @param array $request Data from request()->all() - * @return string|null - */ - private function getContent(array $request): ?string - { - if (Arr::get($request, 'properties.content.0.html')) { - return Arr::get($request, 'properties.content.0.html'); - } - if (is_string(Arr::get($request, 'properties.content.0'))) { - return Arr::get($request, 'properties.content.0'); - } - - return Arr::get($request, 'content'); - } - - /** - * Get the in-reply-to from the request to create a new note. - * - * @param array $request Data from request()->all() - * @return string|null - */ - private function getInReplyTo(array $request): ?string - { - if (Arr::get($request, 'properties.in-reply-to.0')) { - return Arr::get($request, 'properties.in-reply-to.0'); - } - - return Arr::get($request, 'in-reply-to'); - } - /** * Get the published time from the request to create a new note. * diff --git a/app/Services/Service.php b/app/Services/Service.php new file mode 100644 index 00000000..cb480d7c --- /dev/null +++ b/app/Services/Service.php @@ -0,0 +1,30 @@ + text; @@ -691,4 +691,32 @@ class MicropubControllerTest extends TestCase $response->assertJson(['response' => 'created']); $this->assertDatabaseHas('notes', ['note' => $note]); } + + /** @test */ + public function micropubClientApiRequestCreatesArticlesWhenItIncludesTheNameProperty(): void + { + $faker = Factory::create(); + $name = $faker->text(50); + $content = $faker->paragraphs(5, true); + + $response = $this->postJson( + '/api/post', + [ + 'type' => ['h-entry'], + 'properties' => [ + 'name' => $name, + 'content' => $content, + ], + ], + ['HTTP_Authorization' => 'Bearer ' . $this->getToken()] + ); + + $response + ->assertJson(['response' => 'created']) + ->assertStatus(201); + $this->assertDatabaseHas('articles', [ + 'title' => $name, + 'main' => $content, + ]); + } }