feat(channels): add Discord as a social channel
Connect a Discord server via OAuth (bot authorization) and schedule/publish
messages to its channels, with mentions and rich embeds.
- Connect: custom Socialite Discord provider (bot scope) maps the authorized
guild to a SocialAccount; throws if no server was authorized.
- Publish: DiscordPublisher posts via the global bot token, validates the chosen
channel belongs to the connected guild (anti cross-guild), optimizes media,
builds allowed_mentions only from explicit mention chips (no accidental pings),
and renders rich embeds.
- Compose: per-post channel picker (live lookup), mention autocomplete and an
embed editor, gated by a required-channel compliance rule; Discord post preview.
- Enum/config/content-type wiring, ConnectionVerifier health check, throttled
lookup endpoints, i18n (en/es/pt-BR), and tests.
Operators must create a Discord application and set DISCORD_CLIENT_ID,
DISCORD_CLIENT_SECRET, DISCORD_BOT_TOKEN and DISCORD_CLIENT_REDIRECT.
2026-06-16 17:44:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
use App\Enums\SocialAccount\Platform;
|
|
|
|
|
use App\Enums\SocialAccount\Status;
|
|
|
|
|
use App\Enums\UserWorkspace\Role;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use App\Models\Workspace;
|
2026-06-25 16:54:16 +00:00
|
|
|
use Inertia\Testing\AssertableInertia;
|
feat(channels): add Discord as a social channel
Connect a Discord server via OAuth (bot authorization) and schedule/publish
messages to its channels, with mentions and rich embeds.
- Connect: custom Socialite Discord provider (bot scope) maps the authorized
guild to a SocialAccount; throws if no server was authorized.
- Publish: DiscordPublisher posts via the global bot token, validates the chosen
channel belongs to the connected guild (anti cross-guild), optimizes media,
builds allowed_mentions only from explicit mention chips (no accidental pings),
and renders rich embeds.
- Compose: per-post channel picker (live lookup), mention autocomplete and an
embed editor, gated by a required-channel compliance rule; Discord post preview.
- Enum/config/content-type wiring, ConnectionVerifier health check, throttled
lookup endpoints, i18n (en/es/pt-BR), and tests.
Operators must create a Discord application and set DISCORD_CLIENT_ID,
DISCORD_CLIENT_SECRET, DISCORD_BOT_TOKEN and DISCORD_CLIENT_REDIRECT.
2026-06-16 17:44:00 +00:00
|
|
|
use Laravel\Socialite\Facades\Socialite;
|
|
|
|
|
use Laravel\Socialite\Two\User as SocialiteUser;
|
|
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
|
$this->user = User::factory()->create();
|
|
|
|
|
$this->workspace = Workspace::factory()->create(['user_id' => $this->user->id]);
|
|
|
|
|
$this->user->update(['current_workspace_id' => $this->workspace->id]);
|
|
|
|
|
$this->workspace->members()->attach($this->user->id, ['role' => Role::Member->value]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('discord connect redirects to the oauth provider', function () {
|
|
|
|
|
$driverMock = Mockery::mock();
|
|
|
|
|
$driverMock->shouldReceive('scopes')->andReturnSelf();
|
|
|
|
|
$driverMock->shouldReceive('redirect')->andReturn(Mockery::mock([
|
|
|
|
|
'getTargetUrl' => 'https://discord.com/api/oauth2/authorize?test=1',
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
Socialite::shouldReceive('driver')->with('discord')->andReturn($driverMock);
|
|
|
|
|
|
|
|
|
|
$this->actingAs($this->user)
|
|
|
|
|
->withHeader('X-Inertia', 'true')
|
|
|
|
|
->get(route('app.social.discord.connect'))
|
|
|
|
|
->assertStatus(409); // Inertia::location
|
|
|
|
|
|
|
|
|
|
expect(session('social_connect_workspace'))->toBe($this->workspace->id);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('discord oauth callback creates the server account', function () {
|
|
|
|
|
session(['social_connect_workspace' => $this->workspace->id]);
|
|
|
|
|
|
|
|
|
|
$socialiteUser = Mockery::mock(SocialiteUser::class);
|
|
|
|
|
$socialiteUser->shouldReceive('getId')->andReturn('999000111'); // guild id
|
|
|
|
|
$socialiteUser->shouldReceive('getNickname')->andReturn('My Server');
|
|
|
|
|
$socialiteUser->shouldReceive('getName')->andReturn('My Server');
|
|
|
|
|
$socialiteUser->shouldReceive('getAvatar')->andReturn(null);
|
|
|
|
|
$socialiteUser->token = 'discord-access-token';
|
|
|
|
|
$socialiteUser->refreshToken = 'discord-refresh-token';
|
|
|
|
|
$socialiteUser->expiresIn = null;
|
|
|
|
|
$socialiteUser->approvedScopes = ['bot', 'identify', 'guilds'];
|
|
|
|
|
|
|
|
|
|
Socialite::shouldReceive('driver')->with('discord')->andReturn(Mockery::mock(['user' => $socialiteUser]));
|
|
|
|
|
|
|
|
|
|
$response = $this->actingAs($this->user)->get(route('app.social.discord.callback'));
|
|
|
|
|
|
|
|
|
|
$response->assertOk();
|
2026-06-25 16:54:16 +00:00
|
|
|
$response->assertInertia(fn (AssertableInertia $page) => $page->where('success', true));
|
feat(channels): add Discord as a social channel
Connect a Discord server via OAuth (bot authorization) and schedule/publish
messages to its channels, with mentions and rich embeds.
- Connect: custom Socialite Discord provider (bot scope) maps the authorized
guild to a SocialAccount; throws if no server was authorized.
- Publish: DiscordPublisher posts via the global bot token, validates the chosen
channel belongs to the connected guild (anti cross-guild), optimizes media,
builds allowed_mentions only from explicit mention chips (no accidental pings),
and renders rich embeds.
- Compose: per-post channel picker (live lookup), mention autocomplete and an
embed editor, gated by a required-channel compliance rule; Discord post preview.
- Enum/config/content-type wiring, ConnectionVerifier health check, throttled
lookup endpoints, i18n (en/es/pt-BR), and tests.
Operators must create a Discord application and set DISCORD_CLIENT_ID,
DISCORD_CLIENT_SECRET, DISCORD_BOT_TOKEN and DISCORD_CLIENT_REDIRECT.
2026-06-16 17:44:00 +00:00
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('social_accounts', [
|
|
|
|
|
'workspace_id' => $this->workspace->id,
|
|
|
|
|
'platform' => Platform::Discord->value,
|
|
|
|
|
'platform_user_id' => '999000111',
|
|
|
|
|
'status' => Status::Connected->value,
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('discord callback fails gracefully when no server was authorized', function () {
|
|
|
|
|
session(['social_connect_workspace' => $this->workspace->id]);
|
|
|
|
|
|
|
|
|
|
// DiscordProvider throws when the token response carries no guild.
|
|
|
|
|
$mock = Mockery::mock();
|
|
|
|
|
$mock->shouldReceive('user')->andThrow(new RuntimeException('Discord authorization did not include a server.'));
|
|
|
|
|
|
|
|
|
|
Socialite::shouldReceive('driver')->with('discord')->andReturn($mock);
|
|
|
|
|
|
|
|
|
|
$response = $this->actingAs($this->user)->get(route('app.social.discord.callback'));
|
|
|
|
|
|
|
|
|
|
$response->assertOk();
|
2026-06-25 16:54:16 +00:00
|
|
|
$response->assertInertia(fn (AssertableInertia $page) => $page->where('success', false));
|
feat(channels): add Discord as a social channel
Connect a Discord server via OAuth (bot authorization) and schedule/publish
messages to its channels, with mentions and rich embeds.
- Connect: custom Socialite Discord provider (bot scope) maps the authorized
guild to a SocialAccount; throws if no server was authorized.
- Publish: DiscordPublisher posts via the global bot token, validates the chosen
channel belongs to the connected guild (anti cross-guild), optimizes media,
builds allowed_mentions only from explicit mention chips (no accidental pings),
and renders rich embeds.
- Compose: per-post channel picker (live lookup), mention autocomplete and an
embed editor, gated by a required-channel compliance rule; Discord post preview.
- Enum/config/content-type wiring, ConnectionVerifier health check, throttled
lookup endpoints, i18n (en/es/pt-BR), and tests.
Operators must create a Discord application and set DISCORD_CLIENT_ID,
DISCORD_CLIENT_SECRET, DISCORD_BOT_TOKEN and DISCORD_CLIENT_REDIRECT.
2026-06-16 17:44:00 +00:00
|
|
|
|
|
|
|
|
expect($this->workspace->socialAccounts()->where('platform', Platform::Discord)->count())->toBe(0);
|
|
|
|
|
});
|