fedi-feed-router/app/Modules/Lemmy/Services/LemmyPublisher.php

72 lines
2.1 KiB
PHP
Raw Normal View History

2025-06-29 21:20:45 +02:00
<?php
namespace App\Modules\Lemmy\Services;
2025-07-05 02:19:59 +02:00
use App\Exceptions\PlatformAuthException;
2025-06-29 21:20:45 +02:00
use App\Models\Article;
2025-07-05 01:55:53 +02:00
use App\Models\PlatformAccount;
2025-07-05 18:26:04 +02:00
use App\Models\PlatformChannel;
2025-07-02 21:32:37 +02:00
use App\Services\Auth\LemmyAuthService;
2025-06-29 21:20:45 +02:00
use Exception;
class LemmyPublisher
{
private LemmyApiService $api;
2025-07-05 01:55:53 +02:00
private PlatformAccount $account;
2025-06-29 21:20:45 +02:00
2025-07-05 01:55:53 +02:00
public function __construct(PlatformAccount $account)
2025-06-29 21:20:45 +02:00
{
2025-07-05 01:55:53 +02:00
$this->api = new LemmyApiService($account->instance_url);
$this->account = $account;
2025-06-29 21:20:45 +02:00
}
2025-07-05 01:55:53 +02:00
/**
* @param array<string, mixed> $extractedData
2025-07-07 00:51:32 +02:00
* @return array<string, mixed>
*
2025-07-05 02:19:59 +02:00
* @throws PlatformAuthException
2025-07-05 01:55:53 +02:00
* @throws Exception
*/
2025-07-05 18:26:04 +02:00
public function publishToChannel(Article $article, array $extractedData, PlatformChannel $channel): array
2025-06-29 21:20:45 +02:00
{
$authService = resolve(LemmyAuthService::class);
$token = $authService->getToken($this->account);
2025-07-05 02:19:59 +02:00
try {
return $this->createPost($token, $extractedData, $channel, $article);
} catch (Exception $e) {
// If the cached token was stale, refresh and retry once
if (str_contains($e->getMessage(), 'not_logged_in') || str_contains($e->getMessage(), 'Unauthorized')) {
$token = $authService->refreshToken($this->account);
return $this->createPost($token, $extractedData, $channel, $article);
}
throw $e;
}
}
/**
* @param array<string, mixed> $extractedData
* @return array<string, mixed>
*/
private function createPost(string $token, array $extractedData, PlatformChannel $channel, Article $article): array
{
2025-07-05 18:26:04 +02:00
$languageId = $extractedData['language_id'] ?? null;
$communityId = is_numeric($channel->channel_id)
2025-08-12 01:53:59 +02:00
? (int) $channel->channel_id
: $this->api->getCommunityId($channel->channel_id, $token);
2025-07-05 18:26:04 +02:00
return $this->api->createPost(
2025-07-05 02:19:59 +02:00
$token,
$extractedData['title'] ?? 'Untitled',
$extractedData['description'] ?? '',
2025-08-12 01:53:59 +02:00
$communityId,
2025-07-05 02:19:59 +02:00
$article->url,
$extractedData['thumbnail'] ?? null,
$languageId
);
2025-06-29 21:20:45 +02:00
}
2025-06-30 18:18:30 +02:00
}