Allow notes to be syndicated to Mastodon

This commit is contained in:
Jonny Barnes 2022-11-04 15:23:31 +00:00
parent ffe90b9399
commit 72cb4fd7eb
Signed by: jonny
SSH key fingerprint: SHA256:CTuSlns5U7qlD9jqHvtnVmfYV3Zwl2Z7WnJ4/dqOaL8
11 changed files with 200 additions and 5 deletions

View file

@ -0,0 +1,66 @@
<?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();
}
}
}

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Services;
use App\Jobs\SendWebMentions;
use App\Jobs\SyndicateNoteToMastodon;
use App\Jobs\SyndicateNoteToTwitter;
use App\Models\Media;
use App\Models\Note;
@ -58,6 +59,10 @@ class NoteService
dispatch(new SyndicateNoteToTwitter($note));
}
if (in_array('mastodon', $this->getSyndicationTargets($request), true)) {
dispatch(new SyndicateNoteToMastodon($note));
}
return $note;
}
@ -212,6 +217,9 @@ class NoteService
if ($target && $target->service_name === 'Twitter') {
$syndication[] = 'twitter';
}
if ($target && $target->service_name === 'Mastodon') {
$syndication[] = 'mastodon';
}
}
return $syndication;