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
Publish per-image alt text to X via media metadata
Paulo Castellano4beced64a82e
diff --git a/app/Services/Social/XPublisher.php b/app/Services/Social/XPublisher.phpindex 6b82f1a7e..5b794ac5a 100644--- a/app/Services/Social/XPublisher.php+++ b/app/Services/Social/XPublisher.php@@ -4,6 +4,7 @@ declare(strict_types=1);namespace App\Services\Social;+use App\DataTransferObjects\MediaItem;use App\Enums\Media\Type as MediaType;use App\Enums\SocialAccount\Platform;use App\Exceptions\Social\ErrorCategory;@@ -60,6 +61,7 @@ class XPublisher$mediaId = data_get($uploadedMedia, 'data.id', data_get($uploadedMedia, 'media_id'));if ($mediaId) {$mediaIds[] = $mediaId;+ $this->uploadAltText($mediaId, $mediaItem);}}}@@ -106,6 +108,28 @@ class XPublisher]);}+ /**+ * Sets the image's accessibility description on X via the v2 media+ * metadata endpoint. Skipped entirely when no alt text was provided.+ */+ private function uploadAltText(string $mediaId, MediaItem $mediaItem): void+ {+ $alt = $mediaItem->altText();++ if ($alt === null) {+ return;+ }++ $this->getHttpClient()->post("{$this->baseUrl}/media/metadata", [+ 'id' => $mediaId,+ 'metadata' => [+ 'alt_text' => [+ 'text' => mb_substr($alt, 0, Platform::X->altTextMaxLength()),+ ],+ ],+ ]);+ }+private function uploadMedia($mediaItem): ?array{$mimeType = $mediaItem->mime_type;diff --git a/tests/Feature/Services/Social/XPublisherTest.php b/tests/Feature/Services/Social/XPublisherTest.phpindex b684199a9..f63290cc4 100644--- a/tests/Feature/Services/Social/XPublisherTest.php+++ b/tests/Feature/Services/Social/XPublisherTest.php@@ -319,6 +319,166 @@ test('x publisher recovers a missing mime type from the downloaded bytes', functHttp::assertSent(fn ($request) => str_contains($request->url(), '/media/upload'));});+test('x publisher sends image alt text to the media metadata endpoint', function () {+ $this->post->update([+ 'media' => [+ [+ 'id' => 'test-media-id',+ 'path' => 'media/2026-01/test-image.jpg',+ 'url' => 'https://example.com/media/2026-01/test-image.jpg',+ 'mime_type' => 'image/jpeg',+ 'original_filename' => 'test.jpg',+ 'meta' => ['alt_text' => 'a red bike'],+ ],+ ],+ ]);++ $mockOptimizer = Mockery::mock(MediaOptimizer::class);+ $mockOptimizer->shouldReceive('optimizeImage')->andReturnUsing(function (string $tempFile) {+ $optimized = tempnam(sys_get_temp_dir(), 'x_opt_');+ copy($tempFile, $optimized);++ return $optimized;+ });+ app()->instance(MediaOptimizer::class, $mockOptimizer);++ Http::fake(function ($request) {+ $url = $request->url();++ if (str_contains($url, '/media/metadata')) {+ return Http::response([], 200);+ }++ if (str_contains($url, '/media/upload')) {+ return Http::response(['data' => ['id' => 'media_id_alt_1']], 200);+ }++ if (str_contains($url, '/2/tweets')) {+ return Http::response(['data' => ['id' => '1212121212', 'text' => 'Hello from X!']], 200);+ }++ return Http::response(+ file_get_contents(__DIR__.'/../../../fixtures/1x1.png'),+ 200,+ ['Content-Type' => 'image/png'],+ );+ });++ $this->publisher->publish($this->postPlatform);++ Http::assertSent(function ($request) {+ if (! str_contains($request->url(), '/media/metadata')) {+ return false;+ }++ return data_get($request->data(), 'id') === 'media_id_alt_1'+ && data_get($request->data(), 'metadata.alt_text.text') === 'a red bike';+ });+});++test('x publisher truncates alt text to the platform max length', function () {+ $longAlt = str_repeat('a', 1200);++ $this->post->update([+ 'media' => [+ [+ 'id' => 'test-media-id',+ 'path' => 'media/2026-01/test-image.jpg',+ 'url' => 'https://example.com/media/2026-01/test-image.jpg',+ 'mime_type' => 'image/jpeg',+ 'original_filename' => 'test.jpg',+ 'meta' => ['alt_text' => $longAlt],+ ],+ ],+ ]);++ $mockOptimizer = Mockery::mock(MediaOptimizer::class);+ $mockOptimizer->shouldReceive('optimizeImage')->andReturnUsing(function (string $tempFile) {+ $optimized = tempnam(sys_get_temp_dir(), 'x_opt_');+ copy($tempFile, $optimized);++ return $optimized;+ });+ app()->instance(MediaOptimizer::class, $mockOptimizer);++ Http::fake(function ($request) {+ $url = $request->url();++ if (str_contains($url, '/media/metadata')) {+ return Http::response([], 200);+ }++ if (str_contains($url, '/media/upload')) {+ return Http::response(['data' => ['id' => 'media_id_alt_2']], 200);+ }++ if (str_contains($url, '/2/tweets')) {+ return Http::response(['data' => ['id' => '1212121213', 'text' => 'Hello from X!']], 200);+ }++ return Http::response(+ file_get_contents(__DIR__.'/../../../fixtures/1x1.png'),+ 200,+ ['Content-Type' => 'image/png'],+ );+ });++ $this->publisher->publish($this->postPlatform);++ Http::assertSent(function ($request) {+ if (! str_contains($request->url(), '/media/metadata')) {+ return false;+ }++ return data_get($request->data(), 'metadata.alt_text.text') === str_repeat('a', 1000);+ });+});++test('x publisher does not call media metadata when no alt text is set', function () {+ $this->post->update([+ 'media' => [+ [+ 'id' => 'test-media-id',+ 'path' => 'media/2026-01/test-image.jpg',+ 'url' => 'https://example.com/media/2026-01/test-image.jpg',+ 'mime_type' => 'image/jpeg',+ 'original_filename' => 'test.jpg',+ ],+ ],+ ]);++ $mockOptimizer = Mockery::mock(MediaOptimizer::class);+ $mockOptimizer->shouldReceive('optimizeImage')->andReturnUsing(function (string $tempFile) {+ $optimized = tempnam(sys_get_temp_dir(), 'x_opt_');+ copy($tempFile, $optimized);++ return $optimized;+ });+ app()->instance(MediaOptimizer::class, $mockOptimizer);++ Http::fake(function ($request) {+ $url = $request->url();++ if (str_contains($url, '/media/upload')) {+ return Http::response(['data' => ['id' => 'media_id_alt_3']], 200);+ }++ if (str_contains($url, '/2/tweets')) {+ return Http::response(['data' => ['id' => '1212121214', 'text' => 'Hello from X!']], 200);+ }++ return Http::response(+ file_get_contents(__DIR__.'/../../../fixtures/1x1.png'),+ 200,+ ['Content-Type' => 'image/png'],+ );+ });++ $this->publisher->publish($this->postPlatform);++ Http::assertNotSent(fn ($request) => str_contains($request->url(), '/media/metadata'));+});+test('x publisher uploads video via chunked upload', function () {$this->post->update(['media' => [