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\Analytics\SyncTryPostPublication;
use App\Actions\Analytics\UpsertAnalyticsPublication;
use App\Models\Post;
use App\Services\Social\TikTokPublisher;
use Illuminate\Support\Facades\DB;
/**
* Moves a TikTok post from its Content Posting `publish_id` to the public
* video id TikTok reported for it, together with its analytics publication.
* A post published before it had one gets it first, so the move still drops
* an imported copy of the video and refuses a video another post holds.
*/
class AssignTikTokVideoId
{
/**
* The importer locks the publication before the post, this action the post
* before the publication; a rare deadlock between them is retried.
*/
private const int DEADLOCK_ATTEMPTS = 3;
public function __construct(
private readonly SyncTryPostPublication $syncPublication,
private readonly UpsertAnalyticsPublication $publications,
) {}
public function handle(Post $post, string $videoId): void
{
DB::transaction(function () use ($post, $videoId): void {
$locked = Post::query()->lockForUpdate()->find($post->id);
if (blank($locked)) {
return;
}
$account = $locked->socialAccount;
$previousUrl = $locked->platform_url;
$videoUrl = filled($account) ? TikTokPublisher::postUrl($account, $videoId) : null;
$publication = $locked->analyticsPublication()->first()
?? (filled($account) ? $this->syncPublication->handle($locked) : null);
if (filled($publication)) {
$this->publications->reconcileRemoteId($publication, $videoId);
if (filled($videoUrl) && (blank($publication->permalink) || $publication->permalink === $previousUrl)) {
$publication->update(['permalink' => $videoUrl]);
}
}
$locked->writePublication([
'platform_post_id' => $videoId,
'platform_url' => $videoUrl ?? $previousUrl,
]);
}, self::DEADLOCK_ATTEMPTS);
}
}