Set language when posting

This commit is contained in:
myrmidex 2025-07-02 20:26:28 +02:00
parent c16f8efbf0
commit 7653797e71
2 changed files with 86 additions and 7 deletions

View file

@ -107,7 +107,7 @@ public function syncChannelPosts(string $token, int $communityId, string $commun
}
}
public function createPost(string $token, string $title, string $body, int $communityId, ?string $url = null, ?string $thumbnail = null): array
public function createPost(string $token, string $title, string $body, int $communityId, ?string $url = null, ?string $thumbnail = null, ?int $languageId = null): array
{
try {
$request = new LemmyRequest($this->instance, $token);
@ -126,6 +126,10 @@ public function createPost(string $token, string $title, string $body, int $comm
$postData['custom_thumbnail'] = $thumbnail;
}
if ($languageId) {
$postData['language_id'] = $languageId;
}
$response = $request->post('post', $postData);
if (!$response->successful()) {
@ -138,4 +142,27 @@ public function createPost(string $token, string $title, string $body, int $comm
throw $e;
}
}
public function getLanguages(): array
{
try {
$request = new LemmyRequest($this->instance);
$response = $request->get('site');
if (!$response->successful()) {
logger()->warning('Failed to fetch site languages', [
'status' => $response->status()
]);
return [];
}
$data = $response->json();
return $data['all_languages'] ?? [];
} catch (Exception $e) {
logger()->error('Exception while fetching languages', [
'error' => $e->getMessage()
]);
return [];
}
}
}

View file

@ -41,13 +41,16 @@ public function publish(Article $article, array $extractedData): ArticlePublicat
$token = $this->getAuthToken();
$communityId = $this->getCommunityId();
$languageId = $this->getLanguageIdForSource($article->url);
$postData = $this->api->createPost(
$token,
$extractedData['title'] ?? 'Untitled',
$extractedData['description'] ?? '',
$communityId,
$article->url,
$extractedData['thumbnail'] ?? null
$extractedData['thumbnail'] ?? null,
$languageId
);
return $this->createPublicationRecord($article, $postData, $communityId);
@ -109,4 +112,53 @@ private function createPublicationRecord(Article $article, array $postData, int
'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;
}
}
}