From 012fa3b1111c999edef774b660a1d07c6a8e51a4 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Wed, 13 Sep 2017 16:00:30 +0100 Subject: [PATCH] Move activity streams logic into its own service class --- app/Http/Controllers/NotesController.php | 37 ++---------------- app/Services/ActivityStreamsService.php | 50 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 34 deletions(-) create mode 100644 app/Services/ActivityStreamsService.php diff --git a/app/Http/Controllers/NotesController.php b/app/Http/Controllers/NotesController.php index 6e7b379c..89b0af82 100644 --- a/app/Http/Controllers/NotesController.php +++ b/app/Http/Controllers/NotesController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\Note; use Illuminate\Http\Request; use Jonnybarnes\IndieWeb\Numbers; +use App\Services\ActivityStreamsService; // Need to sort out Twitter and webmentions! @@ -18,15 +19,7 @@ class NotesController extends Controller public function index() { if (request()->wantsActivityStream()) { - $data = json_encode([ - '@context' => 'https://www.w3.org/ns/activitystreams', - 'id' => config('app.url'), - 'type' => 'Person', - 'name' => config('app.display_name'), - 'preferredUsername' => 'jonnybarnes', - ]); - - return response($data)->header('Content-Type', 'application/activity+json'); + return (new ActivityStreamsService)->siteOwnerResponse(); } $notes = Note::orderBy('id', 'desc') @@ -49,31 +42,7 @@ class NotesController extends Controller $note = Note::nb60($urlId)->with('webmentions')->firstOrFail(); if (request()->wantsActivityStream()) { - $data = json_encode([ - '@context' => 'https://www.w3.org/ns/activitystreams', - 'summary' => 'Jonny added a note to his microblog', - 'type' => 'Add', - 'published' => $note->updated_at->toW3cString(), - 'actor' => [ - 'type' => 'Person', - 'id' => config('app.url'), - 'name' => config('app.display_name'), - 'url' => config('app.url'), - 'image' => [ - 'type' => 'Link', - 'href' => config('app.url') . '/assets/img/jmb-bw.jpg', - 'mediaType' => '/image/jpeg', - ], - ], - 'object' => [ - 'id' => $note->longurl, - 'type' => 'Note', - 'url' => $note->longurl, - 'name' => strip_tags($note->note) - ], - ]); - - return response($data)->header('Content-Type', 'application/activity+json'); + return (new ActivityStreamsService)->singleNoteResponse($note); } return view('notes.show', compact('note')); diff --git a/app/Services/ActivityStreamsService.php b/app/Services/ActivityStreamsService.php new file mode 100644 index 00000000..6d948ea0 --- /dev/null +++ b/app/Services/ActivityStreamsService.php @@ -0,0 +1,50 @@ + 'https://www.w3.org/ns/activitystreams', + 'id' => config('app.url'), + 'type' => 'Person', + 'name' => config('app.display_name'), + 'preferredUsername' => 'jonnybarnes', + ]); + + return response($data)->header('Content-Type', 'application/activity+json'); + } + + public function singleNoteResponse(Note $note) + { + $data = json_encode([ + '@context' => 'https://www.w3.org/ns/activitystreams', + 'summary' => 'Jonny added a note to his microblog', + 'type' => 'Add', + 'published' => $note->updated_at->toW3cString(), + 'actor' => [ + 'type' => 'Person', + 'id' => config('app.url'), + 'name' => config('app.display_name'), + 'url' => config('app.url'), + 'image' => [ + 'type' => 'Link', + 'href' => config('app.url') . '/assets/img/jmb-bw.jpg', + 'mediaType' => '/image/jpeg', + ], + ], + 'object' => [ + 'id' => $note->longurl, + 'type' => 'Note', + 'url' => $note->longurl, + 'name' => strip_tags($note->note) + ], + ]); + + return response($data)->header('Content-Type', 'application/activity+json'); + } +}