diff --git a/app/Jobs/SyndicateToFacebook.php b/app/Jobs/SyndicateToFacebook.php new file mode 100644 index 00000000..c8826396 --- /dev/null +++ b/app/Jobs/SyndicateToFacebook.php @@ -0,0 +1,53 @@ +note = $note; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle(Client $guzzle) + { + //send webmention + $response = $guzzle->request( + 'POST', + 'https://brid.gy/publish/webmention', + [ + 'form_params' => [ + 'source' => $this->note->longurl, + 'target' => 'https://brid.gy/publish/facebook', + ], + ] + ); + //parse for syndication URL + if ($response->getStatusCode() == 201) { + $json = json_decode((string) $response->getBody()); + $this->note->facebook_url = $json->url; + $this->note->save(); + } + } +} diff --git a/app/Jobs/SyndicateToTwitter.php b/app/Jobs/SyndicateToTwitter.php index 0ed814b5..fb91dc4b 100644 --- a/app/Jobs/SyndicateToTwitter.php +++ b/app/Jobs/SyndicateToTwitter.php @@ -2,13 +2,9 @@ namespace App\Jobs; -use Twitter; use App\Note; -use App\Place; -use App\Contact; +use GuzzleHttp\Client; use Illuminate\Bus\Queueable; -use Jonnybarnes\IndieWeb\Numbers; -use Jonnybarnes\IndieWeb\NotePrep; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; @@ -32,81 +28,28 @@ class SyndicateToTwitter implements ShouldQueue /** * Execute the job. * - * @param \Jonnybarnes\IndieWeb\Numbers $numbers - * @param \Jonnybarnes\IndieWeb\NotePrep $noteprep + * @param \GuzzleHttp\Client $guzzle * @return void */ - public function handle(Numbers $numbers, NotePrep $noteprep) + public function handle(Client $guzzle) { - $noteSwappedNames = $this->swapNames($this->note->getOriginal('note')); - $shorturl = 'https://' . config('url.shorturl') . '/t/' . $numbers->numto60($this->note->id); - $tweet = $noteprep->createNote($noteSwappedNames, $shorturl, 140, true); - $tweetOpts = ['status' => $tweet, 'format' => 'json']; - if ($this->note->in_reply_to) { - $tweetOpts['in_reply_to_status_id'] = $noteprep->replyTweetId($this->note->in_reply_to); - } - - if ($this->note->location) { - $explode = explode(':', $this->note->location); - $location = explode(',', $explode[0]); //the latlng will always be in [0] - $lat = trim($location[0]); - $lng = trim($location[1]); - } - if ($this->note->place_id) { - //we force the job to create a place model to get access - //to the postgis methods - $place = Place::find($this->note->place_id); - $lat = $place->location->getLat(); - $lng = $place->location->getLng(); - } - if (isset($lat) && isset($lng)) { - $tweetOpts['lat'] = $lat; - $tweetOpts['long'] = $lng; - } - - $mediaItems = $this->note->getMedia(); - if (count($mediaItems) > 0) { - foreach ($mediaItems as $item) { - $uploadedMedia = Twitter::uploadMedia(['media' => file_get_contents($item->getUrl())]); - $mediaIds[] = $uploadedMedia->media_id_string; - } - $tweetOpts['media_ids'] = implode(',', $mediaIds); - } - - $responseJson = Twitter::postTweet($tweetOpts); - $response = json_decode($responseJson); - $this->note->tweet_id = $response->id; - $this->note->save(); - } - - /** - * Swap @names in a note. - * - * When a note is being saved and we are posting it to twitter, we want - * to swap our @local_name to Twitter’s @twitter_name so the user get’s - * mentioned on Twitter. - * - * @param string $note - * @return string $noteSwappedNames - */ - private function swapNames($note) - { - $regex = '/\[.*?\](*SKIP)(*F)|@(\w+)/'; //match @alice but not [@bob](...) - $noteSwappedNames = preg_replace_callback( - $regex, - function ($matches) { - try { - $contact = Contact::where('nick', '=', mb_strtolower($matches[1]))->firstOrFail(); - } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { - return '@' . $matches[1]; - } - $twitterHandle = $contact->twitter; - - return '@' . $twitterHandle; - }, - $note + //send webmention + $response = $guzzle->request( + 'POST', + 'https://brid.gy/publish/webmention', + [ + 'form_params' => [ + 'source' => $this->note->longurl, + 'target' => 'https://brid.gy/publish/twitter', + ], + ] ); - - return $noteSwappedNames; + //parse for syndication URL + if ($response->getStatusCode() == 201) { + $json = json_decode((string) $response->getBody()); + $tweet_id = basename(parse_url($json->url, PHP_URL_PATH)); + $this->note->tweet_id = $tweet_id; + $this->note->save(); + } } } diff --git a/changelog.md b/changelog.md index e1331c62..d12316cf 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,10 @@ # Changelog +## Version {next} + - Modify SyndicateToTwitter to use bridgy publish + - Add a SyndicateToFacebook job which also uses bridgy publish (issue#24) + - Modify views to facilitate bridgy publish (issue#26) + ## Version 0.0.14.13 (2016-10-26) - Fix: correct the syntax of Link headers (issue#25) diff --git a/database/migrations/2016_10_26_170858_add_facebook_url_column_to_notes.php b/database/migrations/2016_10_26_170858_add_facebook_url_column_to_notes.php new file mode 100644 index 00000000..cc7d9c0d --- /dev/null +++ b/database/migrations/2016_10_26_170858_add_facebook_url_column_to_notes.php @@ -0,0 +1,30 @@ +string('facebook_url')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/resources/views/singlenote.blade.php b/resources/views/singlenote.blade.php index d9119bfd..6e91c262 100644 --- a/resources/views/singlenote.blade.php +++ b/resources/views/singlenote.blade.php @@ -17,10 +17,9 @@ @endforeach - @if(count($likes) > 0)