2025-06-29 21:20:45 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Modules\Lemmy\Services;
|
|
|
|
|
|
2025-06-30 18:18:30 +02:00
|
|
|
use App\Enums\PlatformEnum;
|
|
|
|
|
use App\Exceptions\PlatformAuthException;
|
|
|
|
|
use App\Exceptions\PublishException;
|
2025-06-29 21:20:45 +02:00
|
|
|
use App\Models\Article;
|
|
|
|
|
use App\Models\ArticlePublication;
|
|
|
|
|
use Exception;
|
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
|
|
|
|
|
class LemmyPublisher
|
|
|
|
|
{
|
|
|
|
|
private LemmyApiService $api;
|
|
|
|
|
private string $username;
|
|
|
|
|
private string $community;
|
|
|
|
|
|
|
|
|
|
public function __construct(string $instance, string $username, string $community)
|
|
|
|
|
{
|
|
|
|
|
$this->api = new LemmyApiService($instance);
|
|
|
|
|
$this->username = $username;
|
|
|
|
|
$this->community = $community;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function fromConfig(): self
|
|
|
|
|
{
|
|
|
|
|
return new self(
|
|
|
|
|
config('lemmy.instance'),
|
|
|
|
|
config('lemmy.username'),
|
|
|
|
|
config('lemmy.community')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 18:18:30 +02:00
|
|
|
/**
|
|
|
|
|
* @throws PublishException
|
|
|
|
|
*/
|
2025-06-29 21:20:45 +02:00
|
|
|
public function publish(Article $article, array $extractedData): ArticlePublication
|
|
|
|
|
{
|
2025-06-30 18:18:30 +02:00
|
|
|
try {
|
|
|
|
|
$token = $this->getAuthToken();
|
|
|
|
|
$communityId = $this->getCommunityId();
|
2025-06-29 21:20:45 +02:00
|
|
|
|
2025-06-30 18:18:30 +02:00
|
|
|
$postData = $this->api->createPost(
|
|
|
|
|
$token,
|
|
|
|
|
$extractedData['title'] ?? 'Untitled',
|
|
|
|
|
$extractedData['description'] ?? '',
|
|
|
|
|
$communityId,
|
|
|
|
|
$article->url,
|
|
|
|
|
$extractedData['thumbnail'] ?? null
|
|
|
|
|
);
|
2025-06-29 21:20:45 +02:00
|
|
|
|
2025-06-30 18:18:30 +02:00
|
|
|
return $this->createPublicationRecord($article, $postData, $communityId);
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
throw new PublishException($article, PlatformEnum::LEMMY, $e);
|
|
|
|
|
}
|
2025-06-29 21:20:45 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-30 18:18:30 +02:00
|
|
|
private function getAuthToken(): string
|
2025-06-29 21:20:45 +02:00
|
|
|
{
|
2025-06-30 21:28:15 +02:00
|
|
|
$cachedToken = Cache::get('lemmy_jwt_token');
|
|
|
|
|
|
|
|
|
|
if ($cachedToken) {
|
|
|
|
|
return $cachedToken;
|
|
|
|
|
}
|
2025-06-30 18:18:30 +02:00
|
|
|
|
2025-06-30 21:28:15 +02:00
|
|
|
$username = config('lemmy.username');
|
|
|
|
|
$password = config('lemmy.password');
|
2025-06-30 18:18:30 +02:00
|
|
|
|
2025-06-30 21:28:15 +02:00
|
|
|
if (!$username || !$password) {
|
|
|
|
|
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Missing credentials');
|
|
|
|
|
}
|
2025-06-30 19:54:43 +02:00
|
|
|
|
2025-06-30 21:28:15 +02:00
|
|
|
$token = $this->api->login($username, $password);
|
2025-06-30 18:18:30 +02:00
|
|
|
|
2025-06-30 21:28:15 +02:00
|
|
|
if (!$token) {
|
|
|
|
|
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Login failed');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Cache::put('lemmy_jwt_token', $token, 3600);
|
|
|
|
|
|
|
|
|
|
return $token;
|
2025-06-29 21:20:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getCommunityId(): int
|
|
|
|
|
{
|
2025-06-30 21:28:15 +02:00
|
|
|
$cacheKey = "lemmy_community_id_{$this->community}";
|
|
|
|
|
$cachedId = Cache::get($cacheKey);
|
|
|
|
|
|
|
|
|
|
if ($cachedId) {
|
|
|
|
|
return $cachedId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$communityId = $this->api->getCommunityId($this->community);
|
|
|
|
|
Cache::put($cacheKey, $communityId, 3600);
|
|
|
|
|
|
|
|
|
|
return $communityId;
|
2025-06-29 21:20:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function createPublicationRecord(Article $article, array $postData, int $communityId): ArticlePublication
|
|
|
|
|
{
|
|
|
|
|
return ArticlePublication::create([
|
|
|
|
|
'article_id' => $article->id,
|
|
|
|
|
'post_id' => $postData['post_view']['post']['id'],
|
|
|
|
|
'community_id' => $communityId,
|
|
|
|
|
'published_by' => $this->username,
|
|
|
|
|
'published_at' => now(),
|
|
|
|
|
'platform' => 'lemmy',
|
|
|
|
|
'publication_data' => $postData,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2025-06-30 18:18:30 +02:00
|
|
|
}
|