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\Services\Social;
use App\Dto\MediaItem;
use App\Enums\Media\Type as MediaType;
use App\Enums\PostPlatform\ContentType;
use App\Enums\SocialAccount\Platform;
use App\Exceptions\PlatformUnavailableException;
use App\Exceptions\Social\ErrorCategory;
use App\Exceptions\Social\MastodonPublishException;
use App\Models\Post;
use App\Models\SocialAccount;
use App\Services\Media\MediaOptimizer;
use App\Services\Social\Concerns\HasSocialHttpClient;
use App\Services\Social\Concerns\PublishesThreads;
use App\Services\Social\Mastodon\MastodonApi;
use App\Support\Social\MastodonPoll;
use App\Support\Social\ThreadProgress;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
class MastodonPublisher
{
use HasSocialHttpClient;
use PublishesThreads;
public function publish(Post $post): array
{
$this->validateContentLength($post);
$content = app(ContentSanitizer::class)->forPost($post);
$account = $post->socialAccount;
$instance = $account->mastodon_instance;
$isPoll = $post->content_type === ContentType::MastodonPoll;
$rootHash = ThreadProgress::hash((string) $content, $post->mediaItems->map(fn (MediaItem $item): string => $item->id)->all(), $this->rootPoll($post, $isPoll));
return $this->publishThread(
$post,
$rootHash,
fn (): array => $this->publishRoot($post, $account, $instance, $content, $isPoll, "{$post->id}:{$rootHash}"),
fn (string $text, Collection $media, array $parent, array $root, ?array $poll): array => $this->createStatus(
$account,
$instance,
[
...$this->replyPayload($post, $text, (string) $parent['id']),
...$this->attachments($account, $instance, $text, $media, filled($poll), ['poll' => $poll]),
],
"{$post->id}:{$parent['id']}:".ThreadProgress::hash($text, $media->map(fn (MediaItem $item): string => $item->id)->all(), $poll),
),
);
}
/**
* @return array{id: string, url: ?string}
*/
private function publishRoot(Post $post, SocialAccount $account, string $instance, ?string $content, bool $isPoll, string $idempotencyKey): array
{
$payload = [
'status' => $content ?? '',
'visibility' => 'public',
...$this->attachments($account, $instance, $content, $post->mediaItems, $isPoll, $post->meta),
];
return $this->createStatus($account, $instance, [...$payload, ...$this->contentWarning($post)], $idempotencyKey);
}
/**
* The post's own poll, part of the root's identity: it is the
* Idempotency-Key, so a poll changed before a retry is a new status, never
* the one Mastodon kept from the attempt before. Null off a poll, which
* keeps the hash every other status had.
*
* @return array<string, mixed>|null
*/
private function rootPoll(Post $post, bool $isPoll): ?array
{
if (! $isPoll) {
return null;
}
return MastodonPoll::payload($post->meta);
}
/**
* What a status of the thread attaches: its poll, or its media. A poll
* needs its question (that status's text), distinct choices and a length,
* and cannot carry media. `$meta` holds the poll under `poll`: the post's
* meta, or the reply itself.
*
* @param Collection<int, MediaItem> $media
* @return array<string, mixed>
*/
private function attachments(SocialAccount $account, string $instance, ?string $text, Collection $media, bool $isPoll, ?array $meta): array
{
if (! $isPoll) {
return $this->mediaPayload($account, $instance, $media);
}
$violation = MastodonPoll::publishViolation($text, $media->isNotEmpty(), $meta);
if (filled($violation)) {
throw new MastodonPublishException(userMessage: $violation, category: ErrorCategory::ContentPolicy);
}
return ['poll' => MastodonPoll::payload($meta)];
}
/**
* Uploads up to four media and returns the status's `media_ids`, empty
* when the post has none.
*
* @param Collection<int, MediaItem> $media
* @return array{media_ids?: list<string>}
*/
private function mediaPayload(SocialAccount $account, string $instance, Collection $media): array
{
$mediaIds = [];
foreach ($media->take(4) as $item) {
$mediaIds[] = $this->uploadMedia($account, $instance, $item->url, $item->original_filename, $item->altTextFor(Platform::Mastodon));
}
return $mediaIds === [] ? [] : ['media_ids' => $mediaIds];
}
/**
* A reply repeats the root's content warning, so the thread stays behind it.
*
* @return array<string, mixed>
*/
private function replyPayload(Post $post, string $text, string $inReplyToId): array
{
return [
'status' => $text,
'visibility' => 'public',
'in_reply_to_id' => $inReplyToId,
...$this->contentWarning($post),
];
}
/**
* @return array{spoiler_text?: string}
*/
private function contentWarning(Post $post): array
{
$spoilerText = Str::trim((string) data_get($post->meta, 'spoiler_text'));
return $spoilerText === '' ? [] : ['spoiler_text' => $spoilerText];
}
/**
* The idempotency key makes Mastodon answer a resent status with the one it
* already created (kept for an hour), covering a crash before the checkpoint.
*
* @param array<string, mixed> $payload
* @return array{id: string, url: ?string}
*/
private function createStatus(SocialAccount $account, string $instance, array $payload, string $idempotencyKey): array
{
$response = $this->socialHttp()->withToken($account->access_token)
->withHeaders(['Idempotency-Key' => $idempotencyKey])
->post(MastodonApi::url($instance, 'statuses'), $payload);
if ($response->failed()) {
Log::error('Mastodon post failed', [
'status' => $response->status(),
'body' => $this->redactResponseBody($response->body()),
]);
$this->handleApiError($response);
}
$data = $response->json();
return [
'id' => (string) data_get($data, 'id'),
'url' => data_get($data, 'url'),
];
}
/**
* Uploads one media item and returns its id. A failure stops the post
* instead of publishing it without the media: a 5xx or a dropped
* connection is retried (the status does not exist yet), anything else
* Mastodon refused fails with its mapped error.
*/
private function uploadMedia(SocialAccount $account, string $instance, string $url, ?string $filename, ?string $altText): string
{
$tempFile = tempnam(sys_get_temp_dir(), 'masto_media_');
try {
$downloadResponse = Http::withOptions(['sink' => $tempFile])->timeout(600)->get($url);
if ($downloadResponse->failed() || filesize($tempFile) === 0) {
Log::error('Mastodon failed to download media', ['url' => $url, 'status' => $downloadResponse->status()]);
throw new MastodonPublishException(
userMessage: __('posts.errors.media_unavailable', ['platform' => Platform::Mastodon->label()]),
category: ErrorCategory::ServerError,
);
}
// Optimize images (skip GIFs)
$detectedMime = File::mimeType($tempFile) ?: '';
if (MediaType::classify($detectedMime) === MediaType::Image && ! MediaType::isGif($detectedMime)) {
$optimizer = app(MediaOptimizer::class);
$optimizedPath = $optimizer->optimizeImage($tempFile, Platform::Mastodon);
@unlink($tempFile);
$tempFile = $optimizedPath;
}
$name = $filename ?? basename(parse_url($url, PHP_URL_PATH));
if (empty($name)) {
$name = 'media';
}
$stream = fopen($tempFile, 'r');
$request = $this->socialHttp()->withToken($account->access_token)
->attach('file', $stream, $name);
if ($altText !== null) {
$request = $request->attach('description', $altText);
}
try {
$response = $request->post(MastodonApi::url($instance, 'media'));
} catch (ConnectionException $e) {
throw new PlatformUnavailableException("Mastodon media upload failed to connect: {$e->getMessage()}");
} finally {
if (is_resource($stream)) {
fclose($stream);
}
}
if ($response->failed()) {
Log::error('Mastodon media upload failed', [
'status' => $response->status(),
'body' => $this->redactResponseBody($response->body()),
]);
if ($response->serverError()) {
throw new PlatformUnavailableException('Mastodon media upload returned a server error', $response->status());
}
$this->handleApiError($response);
}
$mediaId = data_get($response->json(), 'id');
if (! is_scalar($mediaId) || (string) $mediaId === '') {
throw new MastodonPublishException(
userMessage: __('posts.errors.unrecognized_error', ['platform' => Platform::Mastodon->label()]),
category: ErrorCategory::Unknown,
rawResponse: $this->redactResponseBody($response->body()),
);
}
return (string) $mediaId;
} finally {
@unlink($tempFile);
}
}
private function handleApiError(Response $response): never
{
throw MastodonPublishException::fromApiResponse($response);
}
}