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);
use App\Enums\PostPlatform\ContentType;
use App\Exceptions\Social\ContentLimitException;
use App\Mcp\Servers\TryPostServer;
use App\Mcp\Tools\Post\PreviewPostTool;
use App\Models\Post;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
use App\Rules\ContentFitsPlatformLimits;
use App\Services\Social\XPublisher;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use Illuminate\Testing\Fluent\AssertableJson;
beforeEach(function () {
$this->user = User::factory()->create();
$this->workspace = Workspace::factory()->create(['user_id' => $this->user->id]);
$this->workspace->members()->attach($this->user->id, membershipPivot('member'));
$this->user->update(['current_workspace_id' => $this->workspace->id]);
});
test('the content rule measures an x account against its own tier', function (array $meta, bool $passes) {
$account = SocialAccount::factory()->x()->create(['workspace_id' => $this->workspace->id, 'meta' => $meta]);
$validator = Validator::make(
['content' => str_repeat('a', 1000)],
['content' => [new ContentFitsPlatformLimits(collect([$account]))]],
);
expect($validator->passes())->toBe($passes);
})->with([
'free account' => [[], false],
'premium account' => [['x_subscription_type' => 'Premium'], true],
]);
test('the composer receives the account limit', function () {
$premium = SocialAccount::factory()->x()->create(['workspace_id' => $this->workspace->id, 'meta' => ['x_subscription_type' => 'Premium']]);
$free = SocialAccount::factory()->x()->create(['workspace_id' => $this->workspace->id, 'meta' => []]);
$this->actingAs($this->user)->get(route('app.posts.index'))
->assertOk()
->assertInertia(fn ($page) => $page
->where("composer.accounts.{$premium->id}.platform_config.maxContentLength", 25000)
->where("composer.accounts.{$free->id}.platform_config.maxContentLength", 280));
});
test('a premium x account publishes a long post', function () {
$account = SocialAccount::factory()->x()->create([
'workspace_id' => $this->workspace->id,
'meta' => ['x_subscription_type' => 'Premium'],
'token_expires_at' => now()->addHours(2),
]);
$post = Post::factory()->forAccount($account, ContentType::XPost)->create(['user_id' => $this->user->id, 'content' => str_repeat('a', 2000)]);
Http::fake([config('trypost.platforms.x.api').'/tweets' => Http::response(['data' => ['id' => '99']], 201)]);
expect((new XPublisher)->publish($post)['id'])->toBe('99');
Http::assertSent(fn (Request $request): bool => mb_strlen((string) data_get($request->data(), 'text')) === 2000);
});
test('an account that lost premium fails the long post at publish', function () {
$account = SocialAccount::factory()->x()->create([
'workspace_id' => $this->workspace->id,
'meta' => ['x_subscription_type' => 'None'],
'token_expires_at' => now()->addHours(2),
]);
$post = Post::factory()->forAccount($account, ContentType::XPost)->create(['user_id' => $this->user->id, 'content' => str_repeat('a', 2000)]);
Http::fake();
expect(fn () => (new XPublisher)->publish($post))
->toThrow(ContentLimitException::class, __('posts.errors.content_too_long', ['platform' => 'X', 'max' => 280, 'provided' => 2000]));
Http::assertNothingSent();
});
test('post content accepts up to the long post size on the api', function () {
$result = createApiTestToken();
$account = SocialAccount::factory()->x()->create(['workspace_id' => $result['workspace']->id, 'meta' => ['x_subscription_type' => 'Premium']]);
$this->withHeaders(['Authorization' => "Bearer {$result['plain_token']}"])
->postJson(route('api.posts.store'), [
'content' => str_repeat('a', 20000),
'social_account_id' => $account->id,
'content_type' => ContentType::XPost->value,
])->assertCreated();
});
test('post content stores the longest post in four byte characters', function () {
$post = Post::factory()->create([
'workspace_id' => $this->workspace->id,
'user_id' => $this->user->id,
'content' => str_repeat('😀', Post::CONTENT_MAX_LENGTH),
]);
expect(mb_strlen((string) $post->fresh()->content))->toBe(Post::CONTENT_MAX_LENGTH);
});
test('the post content cap counts the text a reader sees, not the editor markup', function (string $content, bool $accepted) {
$result = createApiTestToken();
$account = SocialAccount::factory()->x()->create(['workspace_id' => $result['workspace']->id, 'meta' => ['x_subscription_type' => 'Premium']]);
$response = $this->withHeaders(['Authorization' => "Bearer {$result['plain_token']}"])
->postJson(route('api.posts.store'), [
'content' => $content,
'social_account_id' => $account->id,
'content_type' => ContentType::XPost->value,
]);
$accepted ? $response->assertCreated() : $response->assertUnprocessable()->assertJsonValidationErrors(['content']);
})->with([
'markup over the cap, text under it' => [str_repeat('<p>abcd</p>', 4000), true],
'text over the cap' => [str_repeat('a', 25001), false],
]);
test('a poll question is capped at 280 characters even with long posts', function (ContentType $type, bool $passes) {
$account = SocialAccount::factory()->x()->create(['workspace_id' => $this->workspace->id, 'meta' => ['x_subscription_type' => 'Premium']]);
$validator = Validator::make(
['content' => Str::repeat('a', 281)],
['content' => [new ContentFitsPlatformLimits(collect([$account]), [[]], [$type->value])]],
);
expect($validator->passes())->toBe($passes);
})->with([
'poll' => [ContentType::XPoll, false],
'post' => [ContentType::XPost, true],
]);
test('the publisher refuses a 281-character poll question', function () {
$account = SocialAccount::factory()->x()->create([
'workspace_id' => $this->workspace->id,
'meta' => ['x_subscription_type' => 'Premium'],
'token_expires_at' => now()->addHours(2),
]);
$post = Post::factory()->forAccount($account, ContentType::XPoll)->create([
'user_id' => $this->user->id,
'content' => Str::repeat('a', 281),
'meta' => ['poll' => ['options' => ['Yes', 'No'], 'duration_minutes' => 1440]],
]);
Http::fake();
expect(fn () => (new XPublisher)->publish($post))->toThrow(ContentLimitException::class);
Http::assertNothingSent();
});
test('the api and mcp previews report the limit of the post type on a long post account', function (ContentType $type, int $limit) {
$token = createApiTestToken();
$account = SocialAccount::factory()->x()->create(['workspace_id' => $token['workspace']->id, 'meta' => ['x_subscription_type' => 'Premium']]);
$post = Post::factory()->forAccount($account, $type)->create([
'user_id' => $token['user']->id,
'content' => 'Tabs or spaces?',
'meta' => $type === ContentType::XPoll ? ['poll' => ['options' => ['Tabs', 'Spaces'], 'duration_minutes' => 60]] : [],
]);
$this->withToken($token['plain_token'])->getJson(route('api.posts.preview', $post))
->assertOk()
->assertJsonPath('max_content_length', $limit);
TryPostServer::actingAs($token['user'])->tool(PreviewPostTool::class, ['post_id' => $post->id])
->assertOk()
->assertStructuredContent(fn (AssertableJson $json) => $json->where('max_content_length', $limit)->etc());
})->with([
'poll' => [ContentType::XPoll, 280],
'post' => [ContentType::XPost, 25000],
]);