67 lines
1.7 KiB
PHP
67 lines
1.7 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Jobs;
|
||
|
|
||
|
use App\Models\Note;
|
||
|
use GuzzleHttp\Client;
|
||
|
use Illuminate\Bus\Queueable;
|
||
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||
|
use Illuminate\Queue\InteractsWithQueue;
|
||
|
use Illuminate\Queue\SerializesModels;
|
||
|
use JsonException;
|
||
|
|
||
|
class SyndicateNoteToMastodon implements ShouldQueue
|
||
|
{
|
||
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||
|
|
||
|
/**
|
||
|
* Create a new job instance.
|
||
|
*
|
||
|
* @param Note $note
|
||
|
* @return void
|
||
|
*/
|
||
|
public function __construct(
|
||
|
protected Note $note
|
||
|
) {}
|
||
|
|
||
|
/**
|
||
|
* Execute the job.
|
||
|
*
|
||
|
* @param Client $guzzle
|
||
|
* @return void
|
||
|
*/
|
||
|
public function handle(Client $guzzle)
|
||
|
{
|
||
|
// We can only maks the request if we have an access token
|
||
|
if (config('bridgy.mastodon_token') === null) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Make micropub request
|
||
|
$response = $guzzle->request(
|
||
|
'POST',
|
||
|
'https://brid.gy/micropub',
|
||
|
[
|
||
|
'headers' => [
|
||
|
'Authorization' => 'Bearer ' . config('bridgy.mastodon_token'),
|
||
|
],
|
||
|
'json' => [
|
||
|
'type' => ['h-entry'],
|
||
|
'properties' => [
|
||
|
'content' => [$this->note->note],
|
||
|
],
|
||
|
],
|
||
|
]
|
||
|
);
|
||
|
|
||
|
// Parse for syndication URL
|
||
|
if ($response->getStatusCode() === 201) {
|
||
|
$mastodonUrl = $response->getHeader('Location')[0];
|
||
|
$this->note->mastodon_url = $mastodonUrl;
|
||
|
$this->note->save();
|
||
|
}
|
||
|
}
|
||
|
}
|