2026-01-18 16:59:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
refactor: settings redesign, Spanish translations, language system, strict_types
Settings pages:
- Redesign layout to match Sendkit (max-w-4xl, space-y-12, Separator sections)
- Merge Members page into Workspace settings with Table, invite Dialog, ConfirmDeleteModal
- Add workspace logo upload/delete routes and controller methods
- Translate all hardcoded strings in Workspace.vue modals
Language system:
- Drop languages table, replace language_id FK with locale string column on users
- Create config/languages.php for available languages and default locale
- Add Spanish (es) translations (13 files)
- Simplify HandleInertiaRequests, ProfileController, RegisteredUserController
Code quality:
- Add declare(strict_types=1) to all PHP files
- Fix MastodonPublisher using wrong attribute (filename -> original_filename)
- Fix HasMediaTest for new has_photo/photo_url accessors
- Fix PublishToSocialPlatformTest type error revealed by strict_types
- Remove orphaned Language model from AppServiceProvider morph map
- Update User TypeScript interface (has_photo, photo_url, locale)
- Eager load media relation on workspaces to prevent N+1
- Add 8 new tests for workspace logo upload/delete
- Update workspace settings test to assert members/invitations props
All 710 tests passing.
2026-03-30 03:20:43 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-01-18 16:59:43 +00:00
|
|
|
namespace App\Services\Social;
|
|
|
|
|
|
2026-06-25 13:57:57 +00:00
|
|
|
use App\Enums\Media\Type as MediaType;
|
2026-04-01 00:22:57 +00:00
|
|
|
use App\Enums\SocialAccount\Platform;
|
2026-04-01 00:04:39 +00:00
|
|
|
use App\Exceptions\Social\MastodonPublishException;
|
2026-01-18 16:59:43 +00:00
|
|
|
use App\Models\PostPlatform;
|
|
|
|
|
use App\Models\SocialAccount;
|
2026-04-01 00:22:57 +00:00
|
|
|
use App\Services\Media\MediaOptimizer;
|
2026-04-01 13:51:53 +00:00
|
|
|
use App\Services\Social\Concerns\HasSocialHttpClient;
|
2026-01-18 16:59:43 +00:00
|
|
|
use Illuminate\Http\Client\Response;
|
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
|
|
class MastodonPublisher
|
|
|
|
|
{
|
2026-04-01 13:51:53 +00:00
|
|
|
use HasSocialHttpClient;
|
|
|
|
|
|
2026-01-18 16:59:43 +00:00
|
|
|
public function publish(PostPlatform $postPlatform): array
|
|
|
|
|
{
|
2026-04-01 14:25:25 +00:00
|
|
|
$this->validateContentLength($postPlatform);
|
|
|
|
|
|
2026-04-15 23:11:36 +00:00
|
|
|
$content = $postPlatform->post->content ? app(ContentSanitizer::class)->sanitize($postPlatform->post->content, $postPlatform->platform) : null;
|
2026-04-01 15:02:15 +00:00
|
|
|
|
2026-01-18 16:59:43 +00:00
|
|
|
$account = $postPlatform->socialAccount;
|
2026-05-19 11:34:35 +00:00
|
|
|
$instance = $account->meta['instance'] ?? config('trypost.platforms.mastodon.default_instance');
|
2026-01-18 16:59:43 +00:00
|
|
|
|
2026-04-15 23:11:36 +00:00
|
|
|
$medias = $postPlatform->post->mediaItems;
|
2026-01-18 16:59:43 +00:00
|
|
|
$mediaIds = [];
|
|
|
|
|
|
|
|
|
|
// Upload media first (max 4)
|
|
|
|
|
foreach ($medias->take(4) as $media) {
|
Harden per-image alt text across publishers, validation, and attach paths
Publishing:
- Only send alt text for images (isImage guards on LinkedIn, X, Discord, Mastodon); never inject altText into video/document payloads.
- X sets alt via a best-effort media/metadata call so a metadata failure no longer blocks the tweet.
Validation:
- Validate media alt_text with a closure on media.*.meta so width/height/duration/slide_* survive a post update (Laravel's excludeUnvalidatedArrayKeys was stripping them).
- Add ALT_TEXT_MAX_LENGTH constant, a proper string-type error, and a localized attribute name.
Media attach (REST + MCP):
- Support per-image alt on attach-media-from-url via structured urls: [{url, alt?}] and on the MCP upload tool via an optional alt; alt is stored only for images.
- Carry submitted meta onto hosted external-URL media so alt is no longer dropped.
Composer:
- Alt-text dialog disables Save and reddens the counter over the limit, counting code points of the trimmed value to match the backend.
- Autosave shows 'Saved' only on a successful response; the lightbox alt overlay renders for images only.
Adds unit, feature, MCP, and browser tests covering every path above.
2026-07-16 16:55:33 +00:00
|
|
|
$mediaId = $this->uploadMedia($account, $instance, $media->url, $media->original_filename, $media->isImage() ? $media->altTextFor(Platform::Mastodon) : null);
|
2026-01-18 16:59:43 +00:00
|
|
|
if ($mediaId) {
|
|
|
|
|
$mediaIds[] = $mediaId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create status
|
|
|
|
|
$payload = [
|
2026-04-01 15:02:15 +00:00
|
|
|
'status' => $content ?? '',
|
2026-01-18 16:59:43 +00:00
|
|
|
'visibility' => 'public',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (! empty($mediaIds)) {
|
|
|
|
|
$payload['media_ids'] = $mediaIds;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 13:51:53 +00:00
|
|
|
$response = $this->socialHttp()->withToken($account->access_token)
|
2026-01-18 16:59:43 +00:00
|
|
|
->post("{$instance}/api/v1/statuses", $payload);
|
|
|
|
|
|
|
|
|
|
if ($response->failed()) {
|
|
|
|
|
Log::error('Mastodon post failed', [
|
|
|
|
|
'status' => $response->status(),
|
2026-04-01 13:51:53 +00:00
|
|
|
'body' => $this->redactResponseBody($response->body()),
|
2026-01-18 16:59:43 +00:00
|
|
|
]);
|
|
|
|
|
$this->handleApiError($response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = $response->json();
|
|
|
|
|
|
|
|
|
|
return [
|
2026-03-31 03:40:18 +00:00
|
|
|
'id' => data_get($data, 'id'),
|
|
|
|
|
'url' => data_get($data, 'url'),
|
2026-01-18 16:59:43 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-11 12:02:26 +00:00
|
|
|
private function uploadMedia(SocialAccount $account, string $instance, string $url, ?string $filename, ?string $altText): ?string
|
2026-01-18 16:59:43 +00:00
|
|
|
{
|
fix: overhaul social publishing — validation, uploads, token refresh
- Fix UpdatePostRequest missing content_type, synced, meta fields
(content_type was silently dropped, causing Instagram Reels to post as Feed)
- Create API FormRequests (StorePostRequest, UpdatePostRequest) replacing inline validation
- Fix syntax errors in all publishers ($media->isVideo() missing variable)
- Fix Instagram Feed with single video calling publishSingleImage instead of publishReel
- Fix TikTok hardcoded SELF_ONLY privacy — now queries creator_info API
- Refactor YouTubePublisher to use google/apiclient SDK with chunked resumable upload
- Fix all publishers using file_get_contents for large videos (memory overflow)
— X, LinkedIn, LinkedInPage, Pinterest, Bluesky, Mastodon now use temp file + stream
- Fix Media::isVideo/isImage to use mime_type instead of extension
- Fix Threads not saving refresh_token (was null, now saves access_token)
- Add Instagram token refresh to publisher and ConnectionVerifier
- Fix PublishToSocialPlatform job: tries 3→1 (prevents duplicate uploads),
timeout 60→600s, added failed() method for cleanup
- Increase Horizon worker timeout 60→630s, Redis retry_after 90→660s
- Increase upload limit 500MB→1GB
- Add mastodon to getDefaultContentType in Edit.vue
2026-03-31 22:25:19 +00:00
|
|
|
$tempFile = tempnam(sys_get_temp_dir(), 'masto_media_');
|
|
|
|
|
|
2026-01-18 16:59:43 +00:00
|
|
|
try {
|
2026-04-01 11:59:47 +00:00
|
|
|
$downloadResponse = Http::withOptions(['sink' => $tempFile])->timeout(600)->get($url);
|
|
|
|
|
|
|
|
|
|
if ($downloadResponse->failed()) {
|
|
|
|
|
throw new \Exception('Failed to download media: HTTP '.$downloadResponse->status());
|
|
|
|
|
}
|
fix: overhaul social publishing — validation, uploads, token refresh
- Fix UpdatePostRequest missing content_type, synced, meta fields
(content_type was silently dropped, causing Instagram Reels to post as Feed)
- Create API FormRequests (StorePostRequest, UpdatePostRequest) replacing inline validation
- Fix syntax errors in all publishers ($media->isVideo() missing variable)
- Fix Instagram Feed with single video calling publishSingleImage instead of publishReel
- Fix TikTok hardcoded SELF_ONLY privacy — now queries creator_info API
- Refactor YouTubePublisher to use google/apiclient SDK with chunked resumable upload
- Fix all publishers using file_get_contents for large videos (memory overflow)
— X, LinkedIn, LinkedInPage, Pinterest, Bluesky, Mastodon now use temp file + stream
- Fix Media::isVideo/isImage to use mime_type instead of extension
- Fix Threads not saving refresh_token (was null, now saves access_token)
- Add Instagram token refresh to publisher and ConnectionVerifier
- Fix PublishToSocialPlatform job: tries 3→1 (prevents duplicate uploads),
timeout 60→600s, added failed() method for cleanup
- Increase Horizon worker timeout 60→630s, Redis retry_after 90→660s
- Increase upload limit 500MB→1GB
- Add mastodon to getDefaultContentType in Edit.vue
2026-03-31 22:25:19 +00:00
|
|
|
|
|
|
|
|
if (filesize($tempFile) === 0) {
|
|
|
|
|
Log::error('Mastodon failed to download media', ['url' => $url]);
|
2026-01-18 16:59:43 +00:00
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 00:22:57 +00:00
|
|
|
// Optimize images (skip GIFs)
|
|
|
|
|
$detectedMime = mime_content_type($tempFile) ?: '';
|
2026-06-25 13:57:57 +00:00
|
|
|
if (MediaType::classify($detectedMime) === MediaType::Image && ! MediaType::isGif($detectedMime)) {
|
2026-04-01 00:22:57 +00:00
|
|
|
$optimizer = app(MediaOptimizer::class);
|
|
|
|
|
$optimizedPath = $optimizer->optimizeImage($tempFile, Platform::Mastodon);
|
|
|
|
|
@unlink($tempFile);
|
|
|
|
|
$tempFile = $optimizedPath;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 16:59:43 +00:00
|
|
|
$name = $filename ?? basename(parse_url($url, PHP_URL_PATH));
|
|
|
|
|
if (empty($name)) {
|
|
|
|
|
$name = 'media';
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 01:18:05 +00:00
|
|
|
$stream = fopen($tempFile, 'r');
|
|
|
|
|
|
2026-07-11 12:02:26 +00:00
|
|
|
$request = $this->socialHttp()->withToken($account->access_token)
|
|
|
|
|
->attach('file', $stream, $name);
|
|
|
|
|
|
|
|
|
|
if ($altText !== null) {
|
2026-07-11 13:34:57 +00:00
|
|
|
$request = $request->attach('description', $altText);
|
2026-07-11 12:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$response = $request->post("{$instance}/api/v1/media");
|
2026-01-18 16:59:43 +00:00
|
|
|
|
2026-04-01 01:18:05 +00:00
|
|
|
if (is_resource($stream)) {
|
|
|
|
|
fclose($stream);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 16:59:43 +00:00
|
|
|
if ($response->failed()) {
|
|
|
|
|
Log::error('Mastodon media upload failed', [
|
|
|
|
|
'status' => $response->status(),
|
2026-04-01 13:51:53 +00:00
|
|
|
'body' => $this->redactResponseBody($response->body()),
|
2026-01-18 16:59:43 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = $response->json();
|
|
|
|
|
|
2026-03-31 03:40:18 +00:00
|
|
|
return data_get($data, 'id');
|
2026-01-18 16:59:43 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::error('Mastodon media upload error', [
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
'url' => $url,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return null;
|
fix: overhaul social publishing — validation, uploads, token refresh
- Fix UpdatePostRequest missing content_type, synced, meta fields
(content_type was silently dropped, causing Instagram Reels to post as Feed)
- Create API FormRequests (StorePostRequest, UpdatePostRequest) replacing inline validation
- Fix syntax errors in all publishers ($media->isVideo() missing variable)
- Fix Instagram Feed with single video calling publishSingleImage instead of publishReel
- Fix TikTok hardcoded SELF_ONLY privacy — now queries creator_info API
- Refactor YouTubePublisher to use google/apiclient SDK with chunked resumable upload
- Fix all publishers using file_get_contents for large videos (memory overflow)
— X, LinkedIn, LinkedInPage, Pinterest, Bluesky, Mastodon now use temp file + stream
- Fix Media::isVideo/isImage to use mime_type instead of extension
- Fix Threads not saving refresh_token (was null, now saves access_token)
- Add Instagram token refresh to publisher and ConnectionVerifier
- Fix PublishToSocialPlatform job: tries 3→1 (prevents duplicate uploads),
timeout 60→600s, added failed() method for cleanup
- Increase Horizon worker timeout 60→630s, Redis retry_after 90→660s
- Increase upload limit 500MB→1GB
- Add mastodon to getDefaultContentType in Edit.vue
2026-03-31 22:25:19 +00:00
|
|
|
} finally {
|
|
|
|
|
@unlink($tempFile);
|
2026-01-18 16:59:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 01:41:26 +00:00
|
|
|
private function handleApiError(Response $response): never
|
2026-01-18 16:59:43 +00:00
|
|
|
{
|
2026-04-01 00:04:39 +00:00
|
|
|
throw MastodonPublishException::fromApiResponse($response);
|
2026-01-18 16:59:43 +00:00
|
|
|
}
|
|
|
|
|
}
|