Move activity streams logic into its own service class
This commit is contained in:
parent
f3cfa9324c
commit
012fa3b111
2 changed files with 53 additions and 34 deletions
|
@ -5,6 +5,7 @@ namespace App\Http\Controllers;
|
||||||
use App\Note;
|
use App\Note;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Jonnybarnes\IndieWeb\Numbers;
|
use Jonnybarnes\IndieWeb\Numbers;
|
||||||
|
use App\Services\ActivityStreamsService;
|
||||||
|
|
||||||
// Need to sort out Twitter and webmentions!
|
// Need to sort out Twitter and webmentions!
|
||||||
|
|
||||||
|
@ -18,15 +19,7 @@ class NotesController extends Controller
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
if (request()->wantsActivityStream()) {
|
if (request()->wantsActivityStream()) {
|
||||||
$data = json_encode([
|
return (new ActivityStreamsService)->siteOwnerResponse();
|
||||||
'@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');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$notes = Note::orderBy('id', 'desc')
|
$notes = Note::orderBy('id', 'desc')
|
||||||
|
@ -49,31 +42,7 @@ class NotesController extends Controller
|
||||||
$note = Note::nb60($urlId)->with('webmentions')->firstOrFail();
|
$note = Note::nb60($urlId)->with('webmentions')->firstOrFail();
|
||||||
|
|
||||||
if (request()->wantsActivityStream()) {
|
if (request()->wantsActivityStream()) {
|
||||||
$data = json_encode([
|
return (new ActivityStreamsService)->singleNoteResponse($note);
|
||||||
'@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 view('notes.show', compact('note'));
|
return view('notes.show', compact('note'));
|
||||||
|
|
50
app/Services/ActivityStreamsService.php
Normal file
50
app/Services/ActivityStreamsService.php
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
use App\Note;
|
||||||
|
|
||||||
|
class ActivityStreamsService
|
||||||
|
{
|
||||||
|
public function siteOwnerResponse()
|
||||||
|
{
|
||||||
|
$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');
|
||||||
|
}
|
||||||
|
|
||||||
|
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');
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue