From fafab6e0132125a769795c139cce335bafa1749d Mon Sep 17 00:00:00 2001
From: Jonny Barnes
Date: Wed, 26 Oct 2016 22:00:26 +0100
Subject: [PATCH] Squashed commit of the following:
commit d198ef5fc3a535b6384f5de33ac41cf8d2bd9a40
Author: Jonny Barnes
Date: Wed Oct 26 22:00:11 2016 +0100
Add latest bridgy related changes
commit b1c72c8f70df03134bea14d2668fb90915fb7853
Author: Jonny Barnes
Date: Wed Oct 26 19:13:15 2016 +0100
Have links on Facebook
commit e99618618ae1fc84081db241d05fd15ce65af66f
Author: Jonny Barnes
Date: Wed Oct 26 18:15:47 2016 +0100
Add a job to syndicate to facebook
commit c37653e2b1a2237172b24dfa3b0bc4da30b52927
Author: Jonny Barnes
Date: Wed Oct 26 18:15:25 2016 +0100
Fix some typos in the comments
commit c65bd20a94478cc1e5832b22d48d824a00411f92
Author: Jonny Barnes
Date: Wed Oct 26 17:54:13 2016 +0100
Add necessary a links to facilitate bridgy publishing
commit b62d3a4daf39ebf8c495f381702a25ed0715e1fe
Author: Jonny Barnes
Date: Wed Oct 26 17:46:04 2016 +0100
Send webmentions to bridgy in order to syndicate
---
app/Jobs/SyndicateToFacebook.php | 53 ++++++++++
app/Jobs/SyndicateToTwitter.php | 97 ++++---------------
changelog.md | 5 +
...70858_add_facebook_url_column_to_notes.php | 30 ++++++
resources/views/singlenote.blade.php | 7 +-
5 files changed, 113 insertions(+), 79 deletions(-)
create mode 100644 app/Jobs/SyndicateToFacebook.php
create mode 100644 database/migrations/2016_10_26_170858_add_facebook_url_column_to_notes.php
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)Likes
@endif
@foreach($likes as $like)
-
+
@endforeach
@if(count($reposts) > 0)Reposts
@endif
@foreach($reposts as $repost)
@@ -28,6 +27,10 @@
{{ $repost['name'] }}
reposted this at {{ $repost['date'] }}.
@endforeach
+
+
+
+
@stop
@section('scripts')