trove/packages/Lvl0/FediDiscover/src/Clients/MastodonClient.php

37 lines
1.1 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Clients;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use Lvl0\FediDiscover\Models\Instance;
use Lvl0\FediDiscover\ValueObjects\FediversePost;
class MastodonClient implements FediverseClientInterface
{
public function fetchPostsSince(Instance $instance, ?string $lastSeenId): Collection
{
$url = 'https://' . parse_url($instance->url, PHP_URL_HOST) . '/api/v1/timelines/public';
$params = $lastSeenId !== null ? ['min_id' => $lastSeenId] : [];
$response = Http::withHeaders([
'User-Agent' => config('fedi-discover.http.user_agent'),
])->get($url, $params);
if (! $response->successful() || ! is_array($response->json())) {
return collect();
}
return collect($response->json())
->map(fn (array $t) => new FediversePost(
cursorId: $t['id'],
selfUrl: $t['url'] ?? $t['uri'],
body: $t['content'],
publishedAt: $t['created_at'] ?? null
));
}
}