diff --git a/app/Http/Controllers/MicropubController.php b/app/Http/Controllers/MicropubController.php index 7a9a271a..2bad586b 100644 --- a/app/Http/Controllers/MicropubController.php +++ b/app/Http/Controllers/MicropubController.php @@ -87,6 +87,7 @@ class MicropubController extends Controller //create checkin place if (array_key_exists('checkin', $request->input('properties'))) { $data['checkin'] = $request->input('properties.checkin.0.properties.url.0'); + $data['swarm-url'] = $request->input('properties.syndication.0'); try { $this->placeService->createPlaceFromCheckin($request->input('properties.checkin.0')); } catch (\Exception $e) { @@ -143,6 +144,9 @@ class MicropubController extends Controller $data['photo'][] = $photo; } } + if (starts_with($request->input('properties.syndication.0'), 'https://www.instagram.com')) { + $data['instagram-url'] = $request->input('properties.syndication.0'); + } } try { $note = $this->noteService->createNote($data); diff --git a/app/Media.php b/app/Media.php index bf329391..4226bfbe 100644 --- a/app/Media.php +++ b/app/Media.php @@ -13,6 +13,13 @@ class Media extends Model */ protected $table = 'media_endpoint'; + /** + * The attributes that are mass assignable. + * + * @var array + */ + protected $fillable = ['path']; + /** * Get the note that owns this media. */ diff --git a/app/Services/NoteService.php b/app/Services/NoteService.php index 83cc7f2d..b1d8f50c 100644 --- a/app/Services/NoteService.php +++ b/app/Services/NoteService.php @@ -3,7 +3,7 @@ declare(strict_types=1); namespace App\Services; - +use Log; use App\{Media, Note, Place}; use App\Jobs\{SendWebMentions, SyndicateToFacebook, SyndicateToTwitter}; @@ -17,6 +17,7 @@ class NoteService */ public function createNote(array $data): Note { + Log::info($data); //check the input if (array_key_exists('content', $data) === false) { throw new \Exception('No content defined'); //we can’t fudge the data @@ -63,6 +64,7 @@ class NoteService $place = Place::where('foursquare', $data['checkin'])->first(); if ($place !== null) { $note->place()->associate($place); + $note->swarm_url = $data['swarm-url']; } } @@ -84,12 +86,15 @@ class NoteService $media = Media::where('path', ltrim($path, '/'))->firstOrFail(); } else { $media = Media::firstOrNew(['path' => $photo]); - // currently assuming this is a photo from Swarm + // currently assuming this is a photo from Swarm or OwnYourGram $media->type = 'image'; $media->save(); } $note->media()->save($media); } + if (array_key_exists('instagram-url', $data)) { + $note->instagram_url = $data['instagram-url']; + } } $note->save(); diff --git a/database/migrations/2017_06_11_193737_update_notes_table_add_instagram_url.php b/database/migrations/2017_06_11_193737_update_notes_table_add_instagram_url.php new file mode 100644 index 00000000..252137ee --- /dev/null +++ b/database/migrations/2017_06_11_193737_update_notes_table_add_instagram_url.php @@ -0,0 +1,32 @@ +string('instagram_url')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('notes', function (Blueprint $table) { + $table->dropColumn('instagram_url'); + }); + } +} diff --git a/tests/Feature/OwnYourGramTest.php b/tests/Feature/OwnYourGramTest.php new file mode 100644 index 00000000..2016d5a8 --- /dev/null +++ b/tests/Feature/OwnYourGramTest.php @@ -0,0 +1,59 @@ +json( + 'POST', + '/api/post', + [ + 'type' => ['h-entry'], + 'properties' => [ + 'content' => ['How beautiful are the plates and chopsticks'], + 'published' => [\Carbon\Carbon::now()->toIso8601String()], + 'location' => ['geo:53.802419075834,-1.5431942917637'], + 'syndication' => ['https://www.instagram.com/p/BVC_nVTBFfi/'], + 'photo' => ['https://scontent-sjc2-1.cdninstagram.com/t51.2885-15/e35/18888604_425332491185600_326487281944756224_n.jpg'], + ], + ], + ['HTTP_Authorization' => 'Bearer ' . $this->getToken()] + ); + + $response->assertStatus(201)->assertJson([ + 'response' => 'created' + ]); + $this->assertDatabaseHas('media_endpoint', [ + 'path' => 'https://scontent-sjc2-1.cdninstagram.com/t51.2885-15/e35/18888604_425332491185600_326487281944756224_n.jpg' + ]); + $this->assertDatabaseHas('notes', [ + 'note' => 'How beautiful are the plates and chopsticks', + 'instagram_url' => 'https://www.instagram.com/p/BVC_nVTBFfi/' + ]); + } + + private function getToken() + { + $signer = new Sha256(); + $token = (new Builder()) + ->set('client_id', 'http://ownyourgram.com') + ->set('me', config('app.url')) + ->set('scope', 'create') + ->set('issued_at', time()) + ->sign($signer, env('APP_KEY')) + ->getToken(); + + return $token; + } +} diff --git a/tests/Feature/SwarmTest.php b/tests/Feature/SwarmTest.php index 8ae48974..16ae8cbe 100644 --- a/tests/Feature/SwarmTest.php +++ b/tests/Feature/SwarmTest.php @@ -46,6 +46,9 @@ class SwarmTest extends TestCase $this->assertDatabaseHas('places', [ 'foursquare' => 'https://foursquare.com/v/123456' ]); + $this->assertDatabaseHas('notes', [ + 'swarm_url' => 'https://www.swarmapp.com/checkin/abc' + ]); } /**