onQueue('lemmy-posts'); } public static function dispatchForLemmy(): void { $communityId = config('lemmy.community_id'); $communityName = config('lemmy.community'); if ($communityName) { // Use a placeholder ID if community_id is not set - we'll resolve it in the job $communityId = $communityId ?: 'resolve_from_name'; self::dispatch(PlatformEnum::LEMMY, (string) $communityId, $communityName); } else { logger()->warning('Cannot dispatch Lemmy sync job: missing community configuration'); } } public function handle(): void { echo "Starting channel posts sync job...\n"; if ($this->platform === PlatformEnum::LEMMY) { $this->syncLemmyChannelPosts(); } echo "Channel posts sync job completed!\n"; } private function syncLemmyChannelPosts(): void { try { $api = new LemmyApiService(config('lemmy.instance')); $token = $this->getAuthToken($api); // 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); 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 { $cachedToken = Cache::get('lemmy_jwt_token'); if ($cachedToken) { return $cachedToken; } $username = config('lemmy.username'); $password = config('lemmy.password'); if (!$username || !$password) { throw new PlatformAuthException(PlatformEnum::LEMMY, 'Missing credentials'); } $token = $api->login($username, $password); if (!$token) { throw new PlatformAuthException(PlatformEnum::LEMMY, 'Login failed'); } Cache::put('lemmy_jwt_token', $token, 3600); return $token; } }