diff --git a/app/Http/Controllers/MicropubController.php b/app/Http/Controllers/MicropubController.php index 8cecf46f..b540ca18 100644 --- a/app/Http/Controllers/MicropubController.php +++ b/app/Http/Controllers/MicropubController.php @@ -12,6 +12,7 @@ use App\Services\Micropub\HEntryService; use App\Services\Micropub\UpdateService; use App\Services\TokenService; use Illuminate\Http\JsonResponse; +use Illuminate\Http\Request; use Lcobucci\JWT\Encoding\CannotDecodeContent; use Lcobucci\JWT\Token\InvalidTokenStructure; use Lcobucci\JWT\Validation\RequiredConstraintsViolated; @@ -44,13 +45,14 @@ class MicropubController extends Controller * This function receives an API request, verifies the authenticity * then passes over the info to the relevant Service class. * + * @param Request $request * @return JsonResponse */ - public function post(): JsonResponse + public function post(Request $request): JsonResponse { try { - $tokenData = $this->tokenService->validateToken(request()->input('access_token')); - } catch (RequiredConstraintsViolated | InvalidTokenStructure | CannotDecodeContent $exception) { + $tokenData = $this->tokenService->validateToken($request->input('access_token')); + } catch (RequiredConstraintsViolated | InvalidTokenStructure | CannotDecodeContent) { $micropubResponses = new MicropubResponses(); return $micropubResponses->invalidTokenResponse(); @@ -62,15 +64,15 @@ class MicropubController extends Controller return $micropubResponses->tokenHasNoScopeResponse(); } - $this->logMicropubRequest(request()->all()); + $this->logMicropubRequest($request->all()); - if ((request()->input('h') == 'entry') || (request()->input('type.0') == 'h-entry')) { - if (stristr($tokenData->claims()->get('scope'), 'create') === false) { + if (($request->input('h') === 'entry') || ($request->input('type.0') === 'h-entry')) { + if (stripos($tokenData->claims()->get('scope'), 'create') === false) { $micropubResponses = new MicropubResponses(); return $micropubResponses->insufficientScopeResponse(); } - $location = $this->hentryService->process(request()->all(), $this->getCLientId()); + $location = $this->hentryService->process($request->all(), $this->getCLientId()); return response()->json([ 'response' => 'created', @@ -78,13 +80,13 @@ class MicropubController extends Controller ], 201)->header('Location', $location); } - if (request()->input('h') == 'card' || request()->input('type.0') == 'h-card') { - if (stristr($tokenData->claims()->get('scope'), 'create') === false) { + if ($request->input('h') === 'card' || $request->input('type.0') === 'h-card') { + if (stripos($tokenData->claims()->get('scope'), 'create') === false) { $micropubResponses = new MicropubResponses(); return $micropubResponses->insufficientScopeResponse(); } - $location = $this->hcardService->process(request()->all()); + $location = $this->hcardService->process($request->all()); return response()->json([ 'response' => 'created', @@ -92,14 +94,14 @@ class MicropubController extends Controller ], 201)->header('Location', $location); } - if (request()->input('action') == 'update') { - if (stristr($tokenData->claims()->get('scope'), 'update') === false) { + if ($request->input('action') === 'update') { + if (stripos($tokenData->claims()->get('scope'), 'update') === false) { $micropubResponses = new MicropubResponses(); return $micropubResponses->insufficientScopeResponse(); } - return $this->updateService->process(request()->all()); + return $this->updateService->process($request->all()); } return response()->json([ diff --git a/app/Services/BookmarkService.php b/app/Services/BookmarkService.php index 2bf71d5f..d9012912 100644 --- a/app/Services/BookmarkService.php +++ b/app/Services/BookmarkService.php @@ -8,6 +8,7 @@ use App\Exceptions\InternetArchiveException; use App\Jobs\ProcessBookmark; use App\Jobs\SyndicateBookmarkToTwitter; use App\Models\Bookmark; +use App\Models\SyndicationTarget; use App\Models\Tag; use GuzzleHttp\Client; use GuzzleHttp\Exception\ClientException; @@ -52,7 +53,6 @@ class BookmarkService $bookmark->tags()->save($tag); } - $targets = Arr::pluck(config('syndication.targets'), 'uid', 'service.name'); $mpSyndicateTo = null; if (Arr::get($request, 'mp-syndicate-to')) { $mpSyndicateTo = Arr::get($request, 'mp-syndicate-to'); @@ -60,18 +60,13 @@ class BookmarkService if (Arr::get($request, 'properties.mp-syndicate-to')) { $mpSyndicateTo = Arr::get($request, 'properties.mp-syndicate-to'); } - if (is_string($mpSyndicateTo)) { - $service = array_search($mpSyndicateTo, $targets); - if ($service == 'Twitter') { + $mpSyndicateTo = Arr::wrap($mpSyndicateTo); + foreach ($mpSyndicateTo as $uid) { + $target = SyndicationTarget::where('uid', $uid)->first(); + if ($target && $target->service_name === 'Twitter') { SyndicateBookmarkToTwitter::dispatch($bookmark); - } - } - if (is_array($mpSyndicateTo)) { - foreach ($mpSyndicateTo as $uid) { - $service = array_search($uid, $targets); - if ($service == 'Twitter') { - SyndicateBookmarkToTwitter::dispatch($bookmark); - } + + break; } } diff --git a/app/Services/Micropub/HEntryService.php b/app/Services/Micropub/HEntryService.php index bf19201f..2f8a2779 100644 --- a/app/Services/Micropub/HEntryService.php +++ b/app/Services/Micropub/HEntryService.php @@ -21,19 +21,13 @@ class HEntryService public function process(array $request, ?string $client = null): ?string { if (Arr::get($request, 'properties.like-of') || Arr::get($request, 'like-of')) { - $like = resolve(LikeService::class)->createLike($request); - - return $like->longurl; + return resolve(LikeService::class)->createLike($request)->longurl; } if (Arr::get($request, 'properties.bookmark-of') || Arr::get($request, 'bookmark-of')) { - $bookmark = resolve(BookmarkService::class)->createBookmark($request); - - return $bookmark->longurl; + return resolve(BookmarkService::class)->createBookmark($request)->longurl; } - $note = resolve(NoteService::class)->createNote($request, $client); - - return $note->longurl; + return resolve(NoteService::class)->createNote($request, $client)->longurl; } } diff --git a/app/Services/NoteService.php b/app/Services/NoteService.php index 9c4dd357..57458389 100644 --- a/app/Services/NoteService.php +++ b/app/Services/NoteService.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 Illuminate\Support\Arr; use Illuminate\Support\Str; @@ -18,7 +19,7 @@ class NoteService * Create a new note. * * @param array $request Data from request()->all() - * @param string $client + * @param string|null $client * @return Note */ public function createNote(array $request, ?string $client = null): Note @@ -52,11 +53,9 @@ class NoteService dispatch(new SendWebMentions($note)); - //syndication targets - if (count($this->getSyndicationTargets($request)) > 0) { - if (in_array('twitter', $this->getSyndicationTargets($request))) { - dispatch(new SyndicateNoteToTwitter($note)); - } + // Syndication targets + if (in_array('twitter', $this->getSyndicationTargets($request), true)) { + dispatch(new SyndicateNoteToTwitter($note)); } return $note; @@ -206,22 +205,14 @@ class NoteService private function getSyndicationTargets(array $request): array { $syndication = []; - $targets = Arr::pluck(config('syndication.targets'), 'uid', 'service.name'); $mpSyndicateTo = Arr::get($request, 'mp-syndicate-to') ?? Arr::get($request, 'properties.mp-syndicate-to'); - if (is_string($mpSyndicateTo)) { - $service = array_search($mpSyndicateTo, $targets); - if ($service == 'Twitter') { + $mpSyndicateTo = Arr::wrap($mpSyndicateTo); + foreach ($mpSyndicateTo as $uid) { + $target = SyndicationTarget::where('uid', $uid)->first(); + if ($target && $target->service_name === 'Twitter') { $syndication[] = 'twitter'; } } - if (is_array($mpSyndicateTo)) { - foreach ($mpSyndicateTo as $uid) { - $service = array_search($uid, $targets); - if ($service == 'Twitter') { - $syndication[] = 'twitter'; - } - } - } return $syndication; } diff --git a/tests/Feature/BookmarksTest.php b/tests/Feature/BookmarksTest.php index 8b298dbf..b1f25982 100644 --- a/tests/Feature/BookmarksTest.php +++ b/tests/Feature/BookmarksTest.php @@ -7,6 +7,7 @@ namespace Tests\Feature; use App\Jobs\ProcessBookmark; use App\Jobs\SyndicateBookmarkToTwitter; use App\Models\Bookmark; +use App\Models\SyndicationTarget; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\Queue; use Tests\TestCase; @@ -36,6 +37,11 @@ class BookmarksTest extends TestCase { Queue::fake(); + SyndicationTarget::factory()->create([ + 'uid' => 'https://twitter.com/jonnybarnes', + 'service_name' => 'Twitter', + ]); + $response = $this->withHeaders([ 'Authorization' => 'Bearer ' . $this->getToken(), ])->post('/api/post', [ @@ -58,6 +64,11 @@ class BookmarksTest extends TestCase { Queue::fake(); + SyndicationTarget::factory()->create([ + 'uid' => 'https://twitter.com/jonnybarnes', + 'service_name' => 'Twitter', + ]); + $response = $this->withHeaders([ 'Authorization' => 'Bearer ' . $this->getToken(), ])->json('POST', '/api/post', [ @@ -82,6 +93,11 @@ class BookmarksTest extends TestCase { Queue::fake(); + SyndicationTarget::factory()->create([ + 'uid' => 'https://twitter.com/jonnybarnes', + 'service_name' => 'Twitter', + ]); + $response = $this->withHeaders([ 'Authorization' => 'Bearer ' . $this->getToken(), ])->post('/api/post', [ diff --git a/tests/Feature/MicropubControllerTest.php b/tests/Feature/MicropubControllerTest.php index e34e218a..12604ad3 100644 --- a/tests/Feature/MicropubControllerTest.php +++ b/tests/Feature/MicropubControllerTest.php @@ -126,6 +126,12 @@ class MicropubControllerTest extends TestCase public function micropubClientCanRequestTheNewNoteIsSyndicatedToTwitter(): void { Queue::fake(); + + SyndicationTarget::factory()->create([ + 'uid' => 'https://twitter.com/jonnybarnes', + 'service_name' => 'Twitter', + ]); + $faker = Factory::create(); $note = $faker->text; $response = $this->post( @@ -233,6 +239,11 @@ class MicropubControllerTest extends TestCase 'path' => 'test-photo.jpg', 'type' => 'image', ]); + SyndicationTarget::factory()->create([ + 'uid' => 'https://twitter.com/jonnybarnes', + 'service_name' => 'Twitter', + ]); + $faker = Factory::create(); $note = $faker->text; $response = $this->postJson(