diff --git a/backend/app/Http/Controllers/Api/V1/OnboardingController.php b/backend/app/Http/Controllers/Api/V1/OnboardingController.php index 562c15c..c2cf8a7 100644 --- a/backend/app/Http/Controllers/Api/V1/OnboardingController.php +++ b/backend/app/Http/Controllers/Api/V1/OnboardingController.php @@ -237,6 +237,7 @@ public function createFeed(Request $request): JsonResponse /** * Create channel for onboarding + * @throws ValidationException */ public function createChannel(Request $request): JsonResponse { @@ -253,6 +254,22 @@ public function createChannel(Request $request): JsonResponse $validated = $validator->validated(); + // Get the platform instance to check for active accounts + $platformInstance = PlatformInstance::findOrFail($validated['platform_instance_id']); + + // Check if there are active platform accounts for this instance + $activeAccounts = PlatformAccount::where('instance_url', $platformInstance->url) + ->where('is_active', true) + ->get(); + + if ($activeAccounts->isEmpty()) { + return $this->sendError( + 'Cannot create channel: No active platform accounts found for this instance. Please create a platform account first.', + [], + 422 + ); + } + $channel = PlatformChannel::create([ 'platform_instance_id' => $validated['platform_instance_id'], 'channel_id' => $validated['name'], // For Lemmy, this is the community name @@ -263,14 +280,25 @@ public function createChannel(Request $request): JsonResponse 'is_active' => true, ]); + // Automatically attach the first active account to the channel + $firstAccount = $activeAccounts->first(); + $channel->platformAccounts()->attach($firstAccount->id, [ + 'is_active' => true, + 'priority' => 1, + 'created_at' => now(), + 'updated_at' => now(), + ]); + return $this->sendResponse( - new PlatformChannelResource($channel->load(['platformInstance', 'language'])), - 'Channel created successfully.' + new PlatformChannelResource($channel->load(['platformInstance', 'language', 'platformAccounts'])), + 'Channel created successfully and linked to platform account.' ); } /** * Create route for onboarding + * + * @throws ValidationException */ public function createRoute(Request $request): JsonResponse { @@ -304,7 +332,7 @@ public function createRoute(Request $request): JsonResponse */ public function complete(): JsonResponse { - // In a real implementation, you might want to update a user preference + // In a real implementation, we might want to update a user preference // or create a setting that tracks onboarding completion // For now, we'll just return success since the onboarding status // is determined by the existence of platform accounts, feeds, and channels diff --git a/backend/app/Http/Controllers/Api/V1/PlatformChannelsController.php b/backend/app/Http/Controllers/Api/V1/PlatformChannelsController.php index df6f42d..9e7fcfa 100644 --- a/backend/app/Http/Controllers/Api/V1/PlatformChannelsController.php +++ b/backend/app/Http/Controllers/Api/V1/PlatformChannelsController.php @@ -44,11 +44,36 @@ public function store(Request $request): JsonResponse $validated['is_active'] = $validated['is_active'] ?? true; + // Get the platform instance to check for active accounts + $platformInstance = \App\Models\PlatformInstance::findOrFail($validated['platform_instance_id']); + + // Check if there are active platform accounts for this instance + $activeAccounts = PlatformAccount::where('instance_url', $platformInstance->url) + ->where('is_active', true) + ->get(); + + if ($activeAccounts->isEmpty()) { + return $this->sendError( + 'Cannot create channel: No active platform accounts found for this instance. Please create a platform account first.', + [], + 422 + ); + } + $channel = PlatformChannel::create($validated); + // Automatically attach the first active account to the channel + $firstAccount = $activeAccounts->first(); + $channel->platformAccounts()->attach($firstAccount->id, [ + 'is_active' => true, + 'priority' => 1, + 'created_at' => now(), + 'updated_at' => now(), + ]); + return $this->sendResponse( - new PlatformChannelResource($channel->load('platformInstance')), - 'Platform channel created successfully!', + new PlatformChannelResource($channel->load(['platformInstance', 'platformAccounts'])), + 'Platform channel created successfully and linked to platform account!', 201 ); } catch (ValidationException $e) { diff --git a/backend/app/Jobs/PublishToLemmyJob.php b/backend/app/Jobs/PublishToLemmyJob.php index 58cd317..9a436b1 100644 --- a/backend/app/Jobs/PublishToLemmyJob.php +++ b/backend/app/Jobs/PublishToLemmyJob.php @@ -19,7 +19,7 @@ class PublishToLemmyJob implements ShouldQueue public function __construct( private readonly Article $article ) { - $this->onQueue('lemmy-posts'); + $this->onQueue('publishing'); } public function handle(): void diff --git a/backend/app/Listeners/PublishArticle.php b/backend/app/Listeners/PublishArticle.php index 7c3d98d..8f37ce4 100644 --- a/backend/app/Listeners/PublishArticle.php +++ b/backend/app/Listeners/PublishArticle.php @@ -8,7 +8,7 @@ class PublishArticle implements ShouldQueue { - public string|null $queue = 'lemmy-publish'; + public string|null $queue = 'publishing'; public int $delay = 300; public int $tries = 3; public int $backoff = 300; diff --git a/backend/app/Models/PlatformChannel.php b/backend/app/Models/PlatformChannel.php index ea38936..5179d0f 100644 --- a/backend/app/Models/PlatformChannel.php +++ b/backend/app/Models/PlatformChannel.php @@ -10,6 +10,7 @@ /** * @method static findMany(mixed $channel_ids) + * @method static create(array $array) * @property integer $id * @property integer $platform_instance_id * @property PlatformInstance $platformInstance diff --git a/backend/app/Modules/Lemmy/Services/LemmyPublisher.php b/backend/app/Modules/Lemmy/Services/LemmyPublisher.php index 5b153d0..c19262f 100644 --- a/backend/app/Modules/Lemmy/Services/LemmyPublisher.php +++ b/backend/app/Modules/Lemmy/Services/LemmyPublisher.php @@ -33,11 +33,16 @@ public function publishToChannel(Article $article, array $extractedData, Platfor // Use the language ID from extracted data (should be set during validation) $languageId = $extractedData['language_id'] ?? null; + // Resolve community name to numeric ID if needed + $communityId = is_numeric($channel->channel_id) + ? (int) $channel->channel_id + : $this->api->getCommunityId($channel->channel_id, $token); + return $this->api->createPost( $token, $extractedData['title'] ?? 'Untitled', $extractedData['description'] ?? '', - (int) $channel->channel_id, + $communityId, $article->url, $extractedData['thumbnail'] ?? null, $languageId diff --git a/backend/config/horizon.php b/backend/config/horizon.php index c53d505..34761ea 100644 --- a/backend/config/horizon.php +++ b/backend/config/horizon.php @@ -182,7 +182,7 @@ 'defaults' => [ 'supervisor-1' => [ 'connection' => 'redis', - 'queue' => ['default', 'lemmy-posts', 'lemmy-publish', 'feed-discovery'], + 'queue' => ['default', 'publishing', 'feed-discovery'], 'balance' => 'auto', 'autoScalingStrategy' => 'time', 'maxProcesses' => 1,