fedi-feed-router/app/Modules/Lemmy/Services/LemmyPublisher.php
2025-07-03 21:17:13 +02:00

138 lines
4.1 KiB
PHP

<?php
namespace App\Modules\Lemmy\Services;
use App\Enums\PlatformEnum;
use App\Exceptions\PublishException;
use App\Models\Article;
use App\Models\ArticlePublication;
use App\Services\Auth\LemmyAuthService;
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')
);
}
/**
* @throws PublishException
*/
public function publish(Article $article, array $extractedData): ArticlePublication
{
try {
$token = LemmyAuthService::getToken();
$communityId = $this->getCommunityId();
$languageId = $this->getLanguageIdForSource($article->url);
$postData = $this->api->createPost(
$token,
$extractedData['title'] ?? 'Untitled',
$extractedData['description'] ?? '',
$communityId,
$article->url,
$extractedData['thumbnail'] ?? null,
$languageId
);
return $this->createPublicationRecord($article, $postData, $communityId);
} catch (Exception $e) {
throw new PublishException($article, PlatformEnum::LEMMY, $e);
}
}
private function getCommunityId(): int
{
$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;
}
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,
]);
}
private function getLanguageIdForSource(string $url): ?int
{
// TODO this will be obsolete when sources can be created from the UI, so we can remove these hard-coded sources
// VRT articles are in Dutch
if (str_contains($url, 'vrt.be')) {
return $this->getLanguageId('nl'); // Dutch
}
// Belga articles are in English (based on URL structure)
if (str_contains($url, 'belganewsagency.eu')) {
return $this->getLanguageId('en'); // English
}
return null; // Default to no language specified
}
private function getLanguageId(string $languageCode): ?int
{
$cacheKey = "lemmy_language_id_{$languageCode}";
$cachedId = Cache::get($cacheKey);
if ($cachedId !== null) {
return $cachedId;
}
try {
$languages = $this->api->getLanguages();
foreach ($languages as $language) {
if (isset($language['code']) && $language['code'] === $languageCode) {
$languageId = $language['id'];
Cache::put($cacheKey, $languageId, 3600);
return $languageId;
}
}
// Cache null result to avoid repeated API calls
Cache::put($cacheKey, null, 3600);
return null;
} catch (Exception $e) {
logger()->warning('Failed to get language ID', [
'language_code' => $languageCode,
'error' => $e->getMessage()
]);
return null;
}
}
}