From ea8395a65185f0fc40ff69c65902e7d0f75910b1 Mon Sep 17 00:00:00 2001
From: Jonny Barnes
Date: Sat, 22 Oct 2022 14:18:21 +0100
Subject: [PATCH 1/5] 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 */
From 0ddec78d09c63eceea4964a572f84ebe2b0db512 Mon Sep 17 00:00:00 2001
From: Jonny Barnes
Date: Sun, 23 Oct 2022 13:11:31 +0100
Subject: [PATCH 2/5] Allow syndication targets to be added/edited in admin
interface
---
.../Admin/SyndicationTargetsController.php | 99 +++++++++++++++++++
app/Models/SyndicationTarget.php | 16 +++
.../views/admin/syndication/create.blade.php | 45 +++++++++
.../views/admin/syndication/edit.blade.php | 52 ++++++++++
.../views/admin/syndication/index.blade.php | 22 +++++
resources/views/admin/welcome.blade.php | 6 ++
routes/web.php | 11 +++
7 files changed, 251 insertions(+)
create mode 100644 app/Http/Controllers/Admin/SyndicationTargetsController.php
create mode 100644 resources/views/admin/syndication/create.blade.php
create mode 100644 resources/views/admin/syndication/edit.blade.php
create mode 100644 resources/views/admin/syndication/index.blade.php
diff --git a/app/Http/Controllers/Admin/SyndicationTargetsController.php b/app/Http/Controllers/Admin/SyndicationTargetsController.php
new file mode 100644
index 00000000..f8c93260
--- /dev/null
+++ b/app/Http/Controllers/Admin/SyndicationTargetsController.php
@@ -0,0 +1,99 @@
+validate([
+ 'uid' => 'required|string',
+ 'name' => 'required|string',
+ ]);
+
+ SyndicationTarget::create($validated);
+
+ return redirect('/admin/syndication');
+ }
+
+ /**
+ * Show a form to edit a syndication target.
+ *
+ * @param SyndicationTarget $syndicationTarget
+ * @return View
+ */
+ public function edit(SyndicationTarget $syndicationTarget): View
+ {
+ return view('admin.syndication.edit', [
+ 'syndication_target' => $syndicationTarget,
+ ]);
+ }
+
+ /**
+ * Process the request to edit a client name.
+ *
+ * @param Request $request
+ * @param SyndicationTarget $syndicationTarget
+ * @return RedirectResponse
+ */
+ public function update(Request $request, SyndicationTarget $syndicationTarget): RedirectResponse
+ {
+ $validated = $request->validate([
+ 'uid' => 'required|string',
+ 'name' => 'required|string',
+ ]);
+
+ $syndicationTarget->update($validated);
+
+ return redirect('/admin/syndication');
+ }
+
+ /**
+ * Process a request to delete a client.
+ *
+ * @param SyndicationTarget $syndicationTarget
+ * @return RedirectResponse
+ */
+ public function destroy(SyndicationTarget $syndicationTarget): RedirectResponse
+ {
+ $syndicationTarget->delete();
+
+ return redirect('/admin/syndication');
+ }
+}
diff --git a/app/Models/SyndicationTarget.php b/app/Models/SyndicationTarget.php
index 74366afa..85d674c1 100644
--- a/app/Models/SyndicationTarget.php
+++ b/app/Models/SyndicationTarget.php
@@ -12,6 +12,22 @@ class SyndicationTarget extends Model
{
use HasFactory;
+ /**
+ * The attributes that are mass assignable.
+ *
+ * @var array
+ */
+ protected $fillable = [
+ 'uid',
+ 'name',
+ 'service_name',
+ 'service_url',
+ 'service_photo',
+ 'user_name',
+ 'user_url',
+ 'user_photo',
+ ];
+
/**
* The attributes that are visible when serializing the model.
*
diff --git a/resources/views/admin/syndication/create.blade.php b/resources/views/admin/syndication/create.blade.php
new file mode 100644
index 00000000..df196e93
--- /dev/null
+++ b/resources/views/admin/syndication/create.blade.php
@@ -0,0 +1,45 @@
+@extends('master')
+
+@section('title')New Syndication Target « Admin CP « @stop
+
+@section('content')
+ New Syndication Target
+
+@stop
diff --git a/resources/views/admin/syndication/edit.blade.php b/resources/views/admin/syndication/edit.blade.php
new file mode 100644
index 00000000..400ff5d4
--- /dev/null
+++ b/resources/views/admin/syndication/edit.blade.php
@@ -0,0 +1,52 @@
+@extends('master')
+
+@section('title')Edit Syndication Target « Admin CP « @stop
+
+@section('content')
+ Edit syndication target
+
+
+
+@stop
diff --git a/resources/views/admin/syndication/index.blade.php b/resources/views/admin/syndication/index.blade.php
new file mode 100644
index 00000000..513e2c76
--- /dev/null
+++ b/resources/views/admin/syndication/index.blade.php
@@ -0,0 +1,22 @@
+@extends('master')
+
+@section('title')List Syndication Targets « Admin CP « @stop
+
+@section('content')
+ Syndication Targets
+ @if($targets->isEmpty())
+ No saved syndication targets.
+ @else
+
+ @foreach($targets as $target)
+ -
+ {{ $target['uid'] }}
+ edit?
+
+ @endforeach
+
+ @endif
+
+ Create a new syndication target?
+
+@stop
diff --git a/resources/views/admin/welcome.blade.php b/resources/views/admin/welcome.blade.php
index d29468b0..46ac746f 100644
--- a/resources/views/admin/welcome.blade.php
+++ b/resources/views/admin/welcome.blade.php
@@ -40,4 +40,10 @@
You can either create new places,
or edit them.
+
+ Syndication
+
+ You can either create new syndication targets,
+ or edit them.
+
@stop
diff --git a/routes/web.php b/routes/web.php
index 4d83f871..f721a2df 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -18,6 +18,7 @@ use App\Http\Controllers\Admin\HomeController;
use App\Http\Controllers\Admin\LikesController as AdminLikesController;
use App\Http\Controllers\Admin\NotesController as AdminNotesController;
use App\Http\Controllers\Admin\PlacesController as AdminPlacesController;
+use App\Http\Controllers\Admin\SyndicationTargetsController;
use App\Http\Controllers\ArticlesController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\BookmarksController;
@@ -122,6 +123,16 @@ Route::group(['domain' => config('url.longurl')], function () {
Route::put('/{id}', [AdminLikesController::class, 'update']);
Route::delete('/{id}', [AdminLikesController::class, 'destroy']);
});
+
+ // Syndication Targets
+ Route::group(['prefix' => 'syndication'], function () {
+ Route::get('/', [SyndicationTargetsController::class, 'index']);
+ Route::get('/create', [SyndicationTargetsController::class, 'create']);
+ Route::post('/', [SyndicationTargetsController::class, 'store']);
+ Route::get('/{syndicationTarget}/edit', [SyndicationTargetsController::class, 'edit']);
+ Route::put('/{syndicationTarget}', [SyndicationTargetsController::class, 'update']);
+ Route::delete('/{syndicationTarget}', [SyndicationTargetsController::class, 'destroy']);
+ });
});
// Blog pages using ArticlesController
From 1d85c4c8cff87e4dc3ee484db4d1f08979af4cc5 Mon Sep 17 00:00:00 2001
From: Jonny Barnes
Date: Sun, 23 Oct 2022 13:15:16 +0100
Subject: [PATCH 3/5] Fix one of the unit tests
---
tests/Feature/MicropubControllerTest.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tests/Feature/MicropubControllerTest.php b/tests/Feature/MicropubControllerTest.php
index 9cdd07fb..e34e218a 100644
--- a/tests/Feature/MicropubControllerTest.php
+++ b/tests/Feature/MicropubControllerTest.php
@@ -100,7 +100,7 @@ class MicropubControllerTest extends TestCase
public function micropubClientCanRequestEndpointConfig(): void
{
$response = $this->get('/api/post?q=config', ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
- $response->assertJsonFragment(['uid' => 'https://twitter.com/jonnybarnes']);
+ $response->assertJsonFragment(['media-endpoint' => route('media-endpoint')]);
}
/** @test */
From 976929e9b93154bbf9935efca69f568bb4776e8a Mon Sep 17 00:00:00 2001
From: Jonny Barnes
Date: Sun, 23 Oct 2022 13:16:24 +0100
Subject: [PATCH 4/5] Remove the hard-coded syndication config
---
config/syndication.php | 26 --------------------------
1 file changed, 26 deletions(-)
delete mode 100644 config/syndication.php
diff --git a/config/syndication.php b/config/syndication.php
deleted file mode 100644
index 7a0a92f9..00000000
--- a/config/syndication.php
+++ /dev/null
@@ -1,26 +0,0 @@
- [];
- 'targets' => [
- [
- 'uid' => 'https://twitter.com/jonnybarnes',
- 'name' => 'jonnybarnes on Twitter',
- 'service' => [
- 'name' => 'Twitter',
- 'url' => 'https://twitter.com',
- 'photo' => 'https://upload.wikimedia.org/wikipedia/commons/4/4f/Twitter-logo.svg',
- ],
- 'user' => [
- 'name' => 'jonnybarnes',
- 'url' => 'https://twitter.com/jonnybarnes',
- 'photo' => 'https://pbs.twimg.com/profile_images/875422855932121089/W628ZI8w_400x400.jpg',
- ],
- ],
- ],
-];
From 66257e7e9b10ce5c6dffdeb63a4190f080b68bd8 Mon Sep 17 00:00:00 2001
From: Jonny Barnes
Date: Sun, 23 Oct 2022 20:38:12 +0100
Subject: [PATCH 5/5] Use new syndication target model when processing MicroPub
requests
---
app/Http/Controllers/MicropubController.php | 28 +++++++++++----------
app/Services/BookmarkService.php | 19 ++++++--------
app/Services/Micropub/HEntryService.php | 12 +++------
app/Services/NoteService.php | 27 +++++++-------------
tests/Feature/BookmarksTest.php | 16 ++++++++++++
tests/Feature/MicropubControllerTest.php | 11 ++++++++
6 files changed, 61 insertions(+), 52 deletions(-)
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(