Squashed commit of the following:

commit d198ef5fc3a535b6384f5de33ac41cf8d2bd9a40
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Oct 26 22:00:11 2016 +0100

    Add latest bridgy related changes

commit b1c72c8f70df03134bea14d2668fb90915fb7853
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Oct 26 19:13:15 2016 +0100

    Have links on Facebook

commit e99618618ae1fc84081db241d05fd15ce65af66f
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Oct 26 18:15:47 2016 +0100

    Add a job to syndicate to facebook

commit c37653e2b1a2237172b24dfa3b0bc4da30b52927
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Oct 26 18:15:25 2016 +0100

    Fix some typos in the comments

commit c65bd20a94478cc1e5832b22d48d824a00411f92
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Oct 26 17:54:13 2016 +0100

    Add necessary a links to facilitate bridgy publishing

commit b62d3a4daf39ebf8c495f381702a25ed0715e1fe
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Oct 26 17:46:04 2016 +0100

    Send webmentions to bridgy in order to syndicate
This commit is contained in:
Jonny Barnes 2016-10-26 22:00:26 +01:00
parent af7e58b0d1
commit fafab6e013
5 changed files with 113 additions and 79 deletions

View file

@ -0,0 +1,53 @@
<?php
namespace App\Jobs;
use App\Note;
use GuzzleHttp\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class SyndicateToFacebook implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
protected $note;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Note $note)
{
$this->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();
}
}
}

View file

@ -2,13 +2,9 @@
namespace App\Jobs; namespace App\Jobs;
use Twitter;
use App\Note; use App\Note;
use App\Place; use GuzzleHttp\Client;
use App\Contact;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Jonnybarnes\IndieWeb\Numbers;
use Jonnybarnes\IndieWeb\NotePrep;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
@ -32,81 +28,28 @@ class SyndicateToTwitter implements ShouldQueue
/** /**
* Execute the job. * Execute the job.
* *
* @param \Jonnybarnes\IndieWeb\Numbers $numbers * @param \GuzzleHttp\Client $guzzle
* @param \Jonnybarnes\IndieWeb\NotePrep $noteprep
* @return void * @return void
*/ */
public function handle(Numbers $numbers, NotePrep $noteprep) public function handle(Client $guzzle)
{ {
$noteSwappedNames = $this->swapNames($this->note->getOriginal('note')); //send webmention
$shorturl = 'https://' . config('url.shorturl') . '/t/' . $numbers->numto60($this->note->id); $response = $guzzle->request(
$tweet = $noteprep->createNote($noteSwappedNames, $shorturl, 140, true); 'POST',
$tweetOpts = ['status' => $tweet, 'format' => 'json']; 'https://brid.gy/publish/webmention',
if ($this->note->in_reply_to) { [
$tweetOpts['in_reply_to_status_id'] = $noteprep->replyTweetId($this->note->in_reply_to); 'form_params' => [
} 'source' => $this->note->longurl,
'target' => 'https://brid.gy/publish/twitter',
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 Twitters @twitter_name so the user gets
* 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
); );
//parse for syndication URL
return $noteSwappedNames; 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();
}
} }
} }

View file

@ -1,5 +1,10 @@
# Changelog # 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) ## Version 0.0.14.13 (2016-10-26)
- Fix: correct the syntax of Link headers (issue#25) - Fix: correct the syntax of Link headers (issue#25)

View file

@ -0,0 +1,30 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddFacebookUrlColumnToNotes extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('notes', function (Blueprint $table) {
$table->string('facebook_url')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}

View file

@ -17,10 +17,9 @@
</div> </div>
</div> </div>
@endforeach @endforeach
</div>
@if(count($likes) > 0)<h1 class="notes-subtitle">Likes</h1>@endif @if(count($likes) > 0)<h1 class="notes-subtitle">Likes</h1>@endif
@foreach($likes as $like) @foreach($likes as $like)
<a href="{{ $like['url'] }}"><img src="{{ $like['photo'] }}" alt="profile picture of {{ $like['name'] }}" class="like-photo"></a> <a href="{{ $like['url'] }}"><img src="{{ $like['photo'] }}" alt="profile picture of {{ $like['name'] }}" class="like-photo"></a>
@endforeach @endforeach
@if(count($reposts) > 0)<h1 class="notes-subtitle">Reposts</h1>@endif @if(count($reposts) > 0)<h1 class="notes-subtitle">Reposts</h1>@endif
@foreach($reposts as $repost) @foreach($reposts as $repost)
@ -28,6 +27,10 @@
<img src="{{ $repost['photo'] }}" alt="profile picture of {{ $repost['name'] }}" class="photo u-photo logo"> <span class="fn">{{ $repost['name'] }}</span> <img src="{{ $repost['photo'] }}" alt="profile picture of {{ $repost['name'] }}" class="photo u-photo logo"> <span class="fn">{{ $repost['name'] }}</span>
</a> reposted this at <a href="{{ $repost['source'] }}">{{ $repost['date'] }}</a>.</p> </a> reposted this at <a href="{{ $repost['source'] }}">{{ $repost['date'] }}</a>.</p>
@endforeach @endforeach
<!-- these empty tags are for https://brid.gys publishing service -->
<a href="https://brid.gy/publish/twitter">
<a href="https://brid.gy/publish/facebook">
</div>
@stop @stop
@section('scripts') @section('scripts')