deblan.io-murph/src/Api/MastodonClient.php
Simon Vieille 9dc3272d40
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
[wip] mastodon integration
2023-07-27 18:28:25 +02:00

61 lines
1.4 KiB
PHP

<?php
namespace App\Api;
use Fundevogel\Mastodon\Api as Client;
/**
* class MastodonClient.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class MastodonClient
{
protected ?Client $client = null;
public function __construct(
protected ?string $host,
protected ?string $clientId,
protected ?string $clientSecret,
protected ?string $clientAccessToken
) {
}
public function __call(string $name, array $arguments)
{
if (!$this->client) {
$this->createClient();
}
return call_user_func_array([$this->client, $name], $arguments);
}
protected function createClient(): self
{
$this->client = new Client($this->host);
$this->client->accessToken = $this->clientAccessToken;
return $this;
}
public function extractId(string $url): string
{
return basename($url);
}
public function getTree(string $postId, array &$tree = []): array
{
$tree['id'] = $postId;
$tree = array_merge($tree, $this->statuses()->get($postId)->data);
$tree = array_merge($tree, $this->statuses()->context($postId)->data);
foreach ($tree['descendants'] as $key => $descendant) {
if ($descendant['in_reply_to_id'] === $postId) {
$descendants['descendants'][$key] = $this->getTree($descendant['id'], $descendants['descendants'][$key]);
}
}
return $tree;
}
}