Files
muninn/app/Livewire/Posts/Index.php
T
m105 90f313c134 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>
2026-07-27 21:24:49 +03:00

186 lines
5.4 KiB
PHP

<?php
namespace App\Livewire\Posts;
use App\Jobs\PublishPostJob;
use App\Models\Post;
use App\Models\Site;
use App\Models\SocialAccount;
use App\Models\Template;
use App\Support\Publishing\PublisherManager;
use Livewire\Attributes\Layout;
use Livewire\Component;
#[Layout('components.layouts.app')]
class Index extends Component
{
public bool $showForm = false;
public ?int $editingId = null;
public ?int $templateId = null;
public string $siteId = '';
public array $selectedAccountIds = [];
public string $type = '';
public string $title = '';
public string $body = '';
public string $target = '';
public string $imageUrl = '';
public string $status = 'draft';
public string $scheduledAt = '';
public ?string $publishNotice = null;
public function render()
{
$posts = Post::with(['site', 'socialAccounts'])->orderByDesc('created_at')->get();
return view('livewire.posts.index', [
'posts' => $posts,
'sites' => Site::orderBy('name')->get(),
'accounts' => SocialAccount::orderBy('username')->get(),
'types' => Post::TYPES,
'statuses' => Post::STATUSES,
'templates' => Template::orderBy('name')->get(),
'hasPublishing' => $posts->contains('status', 'publishing'),
]);
}
public function publish(int $id): void
{
$this->publishNotice = null;
$post = Post::with('site')->findOrFail($id);
if (! app(PublisherManager::class)->resolve($post)) {
$this->publishNotice = "No publisher available yet for post type '{$post->type}'.";
return;
}
$post->update(['status' => 'publishing', 'publish_error' => null]);
PublishPostJob::dispatch($post->id);
}
public function create(): void
{
$this->resetForm();
$this->showForm = true;
}
public function createFromTemplate(int $templateId): void
{
$template = Template::findOrFail($templateId);
$this->resetForm();
$this->templateId = $template->id;
// Pre-fill form from template
if ($template->type) {
$this->type = $template->type;
}
if ($template->title) {
$this->title = $template->title;
}
if ($template->body) {
$this->body = $template->body;
}
if ($template->target) {
$this->target = $template->target;
}
$this->showForm = true;
}
public function applyTemplate(): void
{
if ($this->templateId) {
$this->createFromTemplate($this->templateId);
}
}
public function edit(int $id): void
{
$post = Post::with('socialAccounts')->findOrFail($id);
$this->editingId = $post->id;
$this->siteId = (string) $post->site_id;
$this->selectedAccountIds = $post->socialAccounts->pluck('id')->map(fn ($id) => (string) $id)->toArray();
$this->type = $post->type;
$this->title = (string) $post->title;
$this->body = $post->body;
$this->target = (string) $post->target;
$this->imageUrl = (string) $post->image_url;
$this->status = $post->status;
$this->scheduledAt = $post->scheduled_at?->format('Y-m-d\TH:i') ?? '';
$this->showForm = true;
}
public function save(): void
{
$validated = $this->validate([
'siteId' => ['required', 'integer', 'exists:sites,id'],
'selectedAccountIds' => ['array'],
'selectedAccountIds.*' => ['integer', 'exists:social_accounts,id'],
'type' => ['required', 'string', 'in:' . implode(',', Post::TYPES)],
'title' => ['nullable', 'string', 'max:255'],
'body' => ['required', 'string'],
'target' => ['nullable', 'string', 'max:255'],
'imageUrl' => ['nullable', 'url', 'max:2048'],
'status' => ['required', 'string', 'in:' . implode(',', Post::STATUSES)],
'scheduledAt' => ['nullable', 'date'],
]);
$post = Post::updateOrCreate(['id' => $this->editingId], [
'site_id' => $validated['siteId'],
'type' => $validated['type'],
'title' => $validated['title'] ?: null,
'body' => $validated['body'],
'target' => $validated['target'] ?: null,
'image_url' => $validated['imageUrl'] ?: null,
'status' => $validated['status'],
'scheduled_at' => $validated['scheduledAt'] ?: null,
]);
// Sync social accounts using pivot table
if ($this->editingId) {
$post->socialAccounts()->sync($validated['selectedAccountIds'] ?: []);
} else {
$post->socialAccounts()->attach($validated['selectedAccountIds'] ?: []);
}
$this->resetForm();
$this->showForm = false;
}
public function delete(int $id): void
{
Post::findOrFail($id)->delete();
}
public function resetForm(): void
{
$this->editingId = null;
$this->templateId = null;
$this->siteId = '';
$this->selectedAccountIds = [];
$this->type = '';
$this->title = '';
$this->body = '';
$this->target = '';
$this->imageUrl = '';
$this->status = 'draft';
$this->scheduledAt = '';
$this->resetValidation();
}
}