Fix publishing system

This commit is contained in:
myrmidex 2025-08-12 01:53:59 +02:00
parent 65fefb9534
commit 1b29c3fc13
7 changed files with 68 additions and 9 deletions

View file

@ -237,6 +237,7 @@ public function createFeed(Request $request): JsonResponse
/** /**
* Create channel for onboarding * Create channel for onboarding
* @throws ValidationException
*/ */
public function createChannel(Request $request): JsonResponse public function createChannel(Request $request): JsonResponse
{ {
@ -253,6 +254,22 @@ public function createChannel(Request $request): JsonResponse
$validated = $validator->validated(); $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([ $channel = PlatformChannel::create([
'platform_instance_id' => $validated['platform_instance_id'], 'platform_instance_id' => $validated['platform_instance_id'],
'channel_id' => $validated['name'], // For Lemmy, this is the community name 'channel_id' => $validated['name'], // For Lemmy, this is the community name
@ -263,14 +280,25 @@ public function createChannel(Request $request): JsonResponse
'is_active' => true, '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( return $this->sendResponse(
new PlatformChannelResource($channel->load(['platformInstance', 'language'])), new PlatformChannelResource($channel->load(['platformInstance', 'language', 'platformAccounts'])),
'Channel created successfully.' 'Channel created successfully and linked to platform account.'
); );
} }
/** /**
* Create route for onboarding * Create route for onboarding
*
* @throws ValidationException
*/ */
public function createRoute(Request $request): JsonResponse public function createRoute(Request $request): JsonResponse
{ {
@ -304,7 +332,7 @@ public function createRoute(Request $request): JsonResponse
*/ */
public function complete(): 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 // or create a setting that tracks onboarding completion
// For now, we'll just return success since the onboarding status // For now, we'll just return success since the onboarding status
// is determined by the existence of platform accounts, feeds, and channels // is determined by the existence of platform accounts, feeds, and channels

View file

@ -44,11 +44,36 @@ public function store(Request $request): JsonResponse
$validated['is_active'] = $validated['is_active'] ?? true; $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); $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( return $this->sendResponse(
new PlatformChannelResource($channel->load('platformInstance')), new PlatformChannelResource($channel->load(['platformInstance', 'platformAccounts'])),
'Platform channel created successfully!', 'Platform channel created successfully and linked to platform account!',
201 201
); );
} catch (ValidationException $e) { } catch (ValidationException $e) {

View file

@ -19,7 +19,7 @@ class PublishToLemmyJob implements ShouldQueue
public function __construct( public function __construct(
private readonly Article $article private readonly Article $article
) { ) {
$this->onQueue('lemmy-posts'); $this->onQueue('publishing');
} }
public function handle(): void public function handle(): void

View file

@ -8,7 +8,7 @@
class PublishArticle implements ShouldQueue class PublishArticle implements ShouldQueue
{ {
public string|null $queue = 'lemmy-publish'; public string|null $queue = 'publishing';
public int $delay = 300; public int $delay = 300;
public int $tries = 3; public int $tries = 3;
public int $backoff = 300; public int $backoff = 300;

View file

@ -10,6 +10,7 @@
/** /**
* @method static findMany(mixed $channel_ids) * @method static findMany(mixed $channel_ids)
* @method static create(array $array)
* @property integer $id * @property integer $id
* @property integer $platform_instance_id * @property integer $platform_instance_id
* @property PlatformInstance $platformInstance * @property PlatformInstance $platformInstance

View file

@ -33,11 +33,16 @@ public function publishToChannel(Article $article, array $extractedData, Platfor
// Use the language ID from extracted data (should be set during validation) // Use the language ID from extracted data (should be set during validation)
$languageId = $extractedData['language_id'] ?? null; $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( return $this->api->createPost(
$token, $token,
$extractedData['title'] ?? 'Untitled', $extractedData['title'] ?? 'Untitled',
$extractedData['description'] ?? '', $extractedData['description'] ?? '',
(int) $channel->channel_id, $communityId,
$article->url, $article->url,
$extractedData['thumbnail'] ?? null, $extractedData['thumbnail'] ?? null,
$languageId $languageId

View file

@ -182,7 +182,7 @@
'defaults' => [ 'defaults' => [
'supervisor-1' => [ 'supervisor-1' => [
'connection' => 'redis', 'connection' => 'redis',
'queue' => ['default', 'lemmy-posts', 'lemmy-publish', 'feed-discovery'], 'queue' => ['default', 'publishing', 'feed-discovery'],
'balance' => 'auto', 'balance' => 'auto',
'autoScalingStrategy' => 'time', 'autoScalingStrategy' => 'time',
'maxProcesses' => 1, 'maxProcesses' => 1,