Add Cross-Posting support for multiple social accounts
- Create post_social_account pivot table for many-to-many relationship - Add migration to migrate existing social_account_id to pivot table - Remove social_account_id column from posts table - Update Post model with socialAccounts() BelongsToMany relationship - Update Posts Livewire component to use array of selectedAccountIds - Update Posts view with checkboxes for selecting multiple accounts - Update PublishPostJob to iterate through all social accounts of matching type - Update Publisher interface to accept optional SocialAccount parameter - Update all Publishers (WordPress, Reddit, Twitter, LinkedIn) to support cross-posting - Display multiple social accounts as badges in posts table Now a single post can be published to multiple social accounts of the same type. For example: a Twitter post can be published to multiple Twitter accounts. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
@@ -21,7 +21,7 @@ class PublishPostJob implements ShouldQueue
|
||||
|
||||
public function handle(PublisherManager $publishers): void
|
||||
{
|
||||
$post = Post::with(['site', 'socialAccount'])->find($this->postId);
|
||||
$post = Post::with(['site', 'socialAccounts'])->find($this->postId);
|
||||
|
||||
if (! $post) {
|
||||
return;
|
||||
@@ -34,6 +34,24 @@ class PublishPostJob implements ShouldQueue
|
||||
return;
|
||||
}
|
||||
|
||||
$results = [];
|
||||
$errors = [];
|
||||
$urls = [];
|
||||
|
||||
// Get all social accounts that match the post's type
|
||||
$socialAccounts = $post->socialAccounts->filter(
|
||||
fn ($account) => $account->network === $post->type
|
||||
);
|
||||
|
||||
if ($socialAccounts->isEmpty()) {
|
||||
$post->update([
|
||||
'status' => 'draft',
|
||||
'publish_error' => "No social accounts linked to this post for type '{$post->type}'.",
|
||||
]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$publisher = $publishers->resolve($post);
|
||||
|
||||
if (! $publisher) {
|
||||
@@ -42,19 +60,39 @@ class PublishPostJob implements ShouldQueue
|
||||
return;
|
||||
}
|
||||
|
||||
$result = $publisher->publish($post);
|
||||
// Publish to each social account
|
||||
foreach ($socialAccounts as $socialAccount) {
|
||||
$result = $publisher->publish($post, $socialAccount);
|
||||
$results[] = $result;
|
||||
|
||||
if ($result->success) {
|
||||
$post->update(['status' => 'published', 'published_url' => $result->url, 'publish_error' => null]);
|
||||
if ($result->success) {
|
||||
$urls[] = $result->url;
|
||||
} else {
|
||||
$errors[] = $result->error;
|
||||
}
|
||||
}
|
||||
|
||||
// If at least one succeeded, mark as published with the first URL
|
||||
if (! empty($urls)) {
|
||||
$post->update([
|
||||
'status' => 'published',
|
||||
'published_url' => $urls[0],
|
||||
'publish_error' => ! empty($errors) ? implode('; ', $errors) : null,
|
||||
]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// All failed
|
||||
$post->update([
|
||||
'status' => $result->attempted ? 'failed' : 'draft',
|
||||
'publish_error' => $result->error,
|
||||
'status' => 'failed',
|
||||
'published_url' => null,
|
||||
'publish_error' => implode('; ', $errors),
|
||||
]);
|
||||
|
||||
Log::error('post publish failed', ['post_id' => $post->id, 'error' => $result->error]);
|
||||
Log::error('post publish failed for all accounts', [
|
||||
'post_id' => $post->id,
|
||||
'errors' => $errors,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user