fedi-feed-router/app/Jobs/SyncChannelPostsJob.php

106 lines
3.1 KiB
PHP
Raw Normal View History

2025-06-30 19:54:43 +02:00
<?php
namespace App\Jobs;
use App\Enums\PlatformEnum;
use App\Exceptions\PlatformAuthException;
use App\Modules\Lemmy\Services\LemmyApiService;
use Exception;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Cache;
class SyncChannelPostsJob implements ShouldQueue
{
use Queueable;
public function __construct(
private readonly PlatformEnum $platform,
private readonly string $channelId,
private readonly string $channelName
) {
$this->onQueue('lemmy-posts');
}
2025-07-02 21:10:35 +02:00
public static function dispatchForLemmy(): void
{
$communityId = config('lemmy.community_id');
$communityName = config('lemmy.community');
2025-07-03 21:16:22 +02:00
if ($communityName) {
// Use a placeholder ID if community_id is not set - we'll resolve it in the job
$communityId = $communityId ?: 'resolve_from_name';
2025-07-02 21:10:35 +02:00
self::dispatch(PlatformEnum::LEMMY, (string) $communityId, $communityName);
} else {
logger()->warning('Cannot dispatch Lemmy sync job: missing community configuration');
}
}
2025-06-30 19:54:43 +02:00
public function handle(): void
{
2025-07-03 21:16:22 +02:00
echo "Starting channel posts sync job...\n";
2025-06-30 19:54:43 +02:00
if ($this->platform === PlatformEnum::LEMMY) {
$this->syncLemmyChannelPosts();
}
2025-07-03 21:16:22 +02:00
echo "Channel posts sync job completed!\n";
2025-06-30 19:54:43 +02:00
}
private function syncLemmyChannelPosts(): void
{
try {
$api = new LemmyApiService(config('lemmy.instance'));
$token = $this->getAuthToken($api);
2025-07-03 21:16:22 +02:00
// Resolve community ID if it's a placeholder
$communityId = $this->channelId === 'resolve_from_name'
? $api->getCommunityId($this->channelName)
: (int) $this->channelId;
$api->syncChannelPosts($token, $communityId, $this->channelName);
2025-06-30 19:54:43 +02:00
logger()->info('Channel posts synced successfully', [
'platform' => $this->platform->value,
'channel_id' => $this->channelId,
'channel_name' => $this->channelName
]);
} catch (Exception $e) {
logger()->error('Failed to sync channel posts', [
'platform' => $this->platform->value,
'channel_id' => $this->channelId,
'error' => $e->getMessage()
]);
throw $e;
}
}
private function getAuthToken(LemmyApiService $api): string
{
2025-06-30 21:28:15 +02:00
$cachedToken = Cache::get('lemmy_jwt_token');
if ($cachedToken) {
return $cachedToken;
}
2025-06-30 19:54:43 +02:00
2025-06-30 21:28:15 +02:00
$username = config('lemmy.username');
$password = config('lemmy.password');
2025-06-30 19:54:43 +02:00
2025-06-30 21:28:15 +02:00
if (!$username || !$password) {
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Missing credentials');
}
$token = $api->login($username, $password);
if (!$token) {
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Login failed');
}
2025-06-30 19:54:43 +02:00
2025-06-30 21:28:15 +02:00
Cache::put('lemmy_jwt_token', $token, 3600);
return $token;
2025-06-30 19:54:43 +02:00
}
}