Syndicate bookmarks via jobs

This commit is contained in:
Jonny Barnes 2017-10-13 13:45:57 +01:00
parent 917a673f05
commit f007b24065
9 changed files with 161 additions and 10 deletions

View file

@ -20,4 +20,12 @@ class Bookmark extends Model
{ {
return $this->belongsToMany('App\Tag'); return $this->belongsToMany('App\Tag');
} }
/**
* The full url of a bookmark
*/
public function getLongurlAttribute()
{
return config('app.url') . '/bookmarks/' . $this->id;
}
} }

View file

@ -0,0 +1,54 @@
<?php
namespace App\Jobs;
use App\Bookmark;
use GuzzleHttp\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class SyndicateBookmarkToFacebook implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
protected $bookmark;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Bookmark $bookmark)
{
$this->bookmark = $bookmark;
}
/**
* 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->bookmark->longurl,
'target' => 'https://brid.gy/publish/facebook',
'bridgy_omit_link' => 'maybe',
],
]
);
//parse for syndication URL
if ($response->getStatusCode() == 201) {
$json = json_decode((string) $response->getBody());
$this->bookmark->update(['syndicates->facebook' => $json->url]);
$this->bookmark->save();
}
}
}

View file

@ -0,0 +1,54 @@
<?php
namespace App\Jobs;
use App\Bookmark;
use GuzzleHttp\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class SyndicateBookmarkToTwitter implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
protected $bookmark;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Bookmark $bookmark)
{
$this->bookmark = $bookmark;
}
/**
* 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->bookmark->longurl,
'target' => 'https://brid.gy/publish/twitter',
'bridgy_omit_link' => 'maybe',
],
]
);
//parse for syndication URL
if ($response->getStatusCode() == 201) {
$json = json_decode((string) $response->getBody());
$this->bookmark->update(['syndicates->twitter' => $json->url]);
$this->bookmark->save();
}
}
}

View file

@ -9,7 +9,7 @@ use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
class SyndicateToFacebook implements ShouldQueue class SyndicateNoteToFacebook implements ShouldQueue
{ {
use InteractsWithQueue, Queueable, SerializesModels; use InteractsWithQueue, Queueable, SerializesModels;

View file

@ -9,7 +9,7 @@ use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
class SyndicateToTwitter implements ShouldQueue class SyndicateNoteToTwitter implements ShouldQueue
{ {
use InteractsWithQueue, Queueable, SerializesModels; use InteractsWithQueue, Queueable, SerializesModels;

View file

@ -8,6 +8,8 @@ use App\Tag;
use App\Bookmark; use App\Bookmark;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Jobs\ProcessBookmark; use App\Jobs\ProcessBookmark;
use App\Jobs\SyndicateBookmarkToTwitter;
use App\Jobs\SyndicateBookmarkToFacebook;
class BookmarkService class BookmarkService
{ {
@ -47,6 +49,35 @@ class BookmarkService
$bookmark->tags()->save($tag); $bookmark->tags()->save($tag);
} }
$targets = array_pluck(config('syndication.targets'), 'uid', 'service.name');
$mpSyndicateTo = null;
if ($request->has('mp-syndicate-to')) {
$mpSyndicateTo = $request->input('mp-syndicate-to');
}
if ($request->has('properties.mp-syndicate-to')) {
$mpSyndicateTo = $request->input('properties.mp-syndicate-to');
}
if (is_string($mpSyndicateTo)) {
$service = array_search($mpSyndicateTo, $targets);
if ($service == 'Twitter') {
SyndicateBookmarkToTwitter::dispatch($bookmark);
}
if ($service == 'Facebook') {
SyndicateBookmarkToFacebook::dispatch($bookmark);
}
}
if (is_array($mpSyndicateTo)) {
foreach ($mpSyndicateTo as $uid) {
$service = array_search($uid, $targets);
if ($service == 'Twitter') {
SyndicateBookmarkToTwitter::dispatch($bookmark);
}
if ($service == 'Facebook') {
SyndicateBookmarkToFacebook::dispatch($bookmark);
}
}
}
ProcessBookmark::dispatch($bookmark); ProcessBookmark::dispatch($bookmark);
return $bookmark; return $bookmark;

View file

@ -105,10 +105,10 @@ class NoteService
//syndication targets //syndication targets
if (in_array('twitter', $data['syndicate'])) { if (in_array('twitter', $data['syndicate'])) {
dispatch(new SyndicateToTwitter($note)); dispatch(new SyndicateNoteToTwitter($note));
} }
if (in_array('facebook', $data['syndicate'])) { if (in_array('facebook', $data['syndicate'])) {
dispatch(new SyndicateToFacebook($note)); dispatch(new SyndicateNoteToFacebook($note));
} }
return $note; return $note;

View file

@ -20,6 +20,7 @@ class CreateBookmarksTable extends Migration
$table->text('content')->nullable(); $table->text('content')->nullable();
$table->uuid('screenshot')->nullable(); $table->uuid('screenshot')->nullable();
$table->string('archive')->nullable(); $table->string('archive')->nullable();
$table->jsonb('syndicates')->nullable();
$table->timestamps(); $table->timestamps();
}); });
} }

View file

@ -25,11 +25,14 @@ Bookmark «
<p><a href="https://web.archive.org{{ $bookmark->archive }}">Internet Archive backup</a></p> <p><a href="https://web.archive.org{{ $bookmark->archive }}">Internet Archive backup</a></p>
@endisset @endisset
@if(count($bookmark->tags) > 0) @if(count($bookmark->tags) > 0)
<ul> <ul>
@foreach($bookmark->tags as $tag) @foreach($bookmark->tags as $tag)
<li><a href="/bookmarks/tagged/{{ $tag->tag }}">{{ $tag->tag }}</a></li> <li><a href="/bookmarks/tagged/{{ $tag->tag }}">{{ $tag->tag }}</a></li>
@endforeach @endforeach
</ul> </ul>
@endif @endif
<!-- these empty tags are for https://brid.gys publishing service -->
<a href="https://brid.gy/publish/twitter"></a>
<a href="https://brid.gy/publish/facebook"></a>
</div> </div>
@stop @stop