Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.
PHP ai-agentsblueskyfacebookinstagramlaravellinkedinmastodonmcpmcp-serveropen-sourcerest-apiself-hostedsocial-mediasocial-media-managementsocial-media-schedulerthreadstiktokvuejsxyoutube
<?php
declare(strict_types=1);
namespace App\Actions\Post;
use App\Actions\Media\DeleteOwnedMedia;
use App\Enums\Post\Origin;
use App\Enums\SocialAccount\Platform;
use App\Events\PostDeleted;
use App\Models\AnalyticsPublication;
use App\Models\Post;
use App\Support\PostStatusRules;
use App\Support\Social\GoogleBusinessDerivativeCleaner;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
class DeletePost
{
/**
* Locks the publication rows before the post, the same order the analytics
* writers use, so a delete overlapping a metrics write cannot deadlock.
*
* @throws ValidationException
*/
public static function execute(Post $post, bool $respectStatus = false): void
{
$postId = $post->id;
$workspaceId = $post->workspace_id;
$isImported = $post->origin === Origin::Network;
$deleted = DB::transaction(function () use ($post, $postId, $respectStatus): bool {
AnalyticsPublication::query()
->where('post_id', $postId)
->orderBy('id')
->lockForUpdate()
->pluck('id');
$locked = Post::query()->whereKey($postId)->lockForUpdate()->first();
if ($locked === null) {
return false;
}
if ($respectStatus && PostStatusRules::blocksDeletion($locked)) {
throw ValidationException::withMessages(['post' => __('posts.flash.cannot_delete_published')]);
}
AnalyticsPublication::query()
->where('post_id', $post->id)
->update(['post_dismissed_at' => now()]);
DeleteOwnedMedia::forPosts([$postId]);
$post->delete();
if ($post->platform === Platform::GoogleBusiness) {
DB::afterCommit(fn () => app(GoogleBusinessDerivativeCleaner::class)->cleanup($post));
}
return true;
});
if ($deleted && ! $isImported) {
PostDeleted::dispatch($postId, $workspaceId);
}
}
}