2026-01-15 17:24:39 +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-15 17:24:39 +00:00
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
|
|
2026-01-17 17:44:37 +00:00
|
|
|
use App\Enums\SocialAccount\Platform as SocialPlatform;
|
|
|
|
|
use App\Enums\SocialAccount\Status;
|
2026-06-22 00:28:47 +00:00
|
|
|
use App\Exceptions\SocialAccount\NetworkAlreadyConnectedException;
|
2026-01-15 17:24:39 +00:00
|
|
|
use App\Models\Workspace;
|
2026-07-03 14:26:48 +00:00
|
|
|
use App\Services\Social\TokenRedactor;
|
2026-01-15 17:24:39 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
use Inertia\Inertia;
|
2026-06-25 16:54:16 +00:00
|
|
|
use Inertia\Response as InertiaResponse;
|
2026-01-15 17:24:39 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
|
|
|
|
|
class ThreadsController extends SocialController
|
|
|
|
|
{
|
|
|
|
|
protected SocialPlatform $platform = SocialPlatform::Threads;
|
|
|
|
|
|
|
|
|
|
protected array $scopes = [
|
|
|
|
|
'threads_basic',
|
|
|
|
|
'threads_content_publish',
|
2026-04-02 20:57:06 +00:00
|
|
|
'threads_manage_insights',
|
2026-01-15 17:24:39 +00:00
|
|
|
];
|
|
|
|
|
|
2026-06-25 17:30:15 +00:00
|
|
|
public function connect(Request $request): Response
|
2026-01-15 17:24:39 +00:00
|
|
|
{
|
2026-01-16 15:36:52 +00:00
|
|
|
$this->ensurePlatformEnabled();
|
2026-01-17 02:46:30 +00:00
|
|
|
|
|
|
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
|
|
2026-01-15 17:24:39 +00:00
|
|
|
$this->authorize('manageAccounts', $workspace);
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
session([
|
|
|
|
|
'social_connect_workspace' => $workspace->id,
|
2026-04-14 22:48:35 +00:00
|
|
|
'social_reconnect_id' => null,
|
2026-01-17 02:46:30 +00:00
|
|
|
]);
|
2026-01-15 17:24:39 +00:00
|
|
|
|
|
|
|
|
$state = bin2hex(random_bytes(16));
|
|
|
|
|
session(['threads_oauth_state' => $state]);
|
|
|
|
|
|
|
|
|
|
$params = http_build_query([
|
|
|
|
|
'client_id' => config('services.threads.client_id'),
|
|
|
|
|
'redirect_uri' => config('services.threads.redirect'),
|
|
|
|
|
'scope' => implode(',', $this->scopes),
|
|
|
|
|
'response_type' => 'code',
|
|
|
|
|
'state' => $state,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return Inertia::location("https://threads.net/oauth/authorize?{$params}");
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-25 16:54:16 +00:00
|
|
|
public function callback(Request $request): InertiaResponse
|
2026-01-15 17:24:39 +00:00
|
|
|
{
|
|
|
|
|
$workspaceId = session('social_connect_workspace');
|
|
|
|
|
$savedState = session('threads_oauth_state');
|
|
|
|
|
|
|
|
|
|
if (! $workspaceId) {
|
2026-01-17 02:46:30 +00:00
|
|
|
session()->forget(['threads_oauth_state', 'social_reconnect_id']);
|
|
|
|
|
|
feat: localize OAuth popup callback messages across 12 controllers
User reported the social-account OAuth callback popup ('Threads account
connected!') stayed in English regardless of the active locale. The
hardcoded message was wired through SocialController and 11 platform
controllers (Bluesky, Facebook, Instagram, InstagramFacebook, LinkedIn,
LinkedInPage, Mastodon, Pinterest, Threads, TikTok, YouTube), plus the
Blade view that the popup renders.
Adds an accounts.popup_callback i18n block (en/pt-BR/es) covering:
- The popup chrome (title, closing/close-now status text).
- Generic success/reconnect messages (one shared 'Account connected!'
/ 'Account reconnected!' line — the popup already shows a checkmark
and lives for ~2s so platform-specific wording wasn't pulling weight).
- Error variants (account/page/channel) and contextual edge cases
(page not found, no Facebook pages, no YouTube channels, etc.).
Updates every popupCallback() callsite to read from these keys, plus
the Blade view's title and submessage. Regenerates the JSON locale
bundle so laravel-vue-i18n stays in sync.
2026-05-07 17:03:52 +00:00
|
|
|
return $this->popupCallback(false, __('accounts.popup_callback.session_expired'), $this->platform->value);
|
2026-01-15 17:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($request->state !== $savedState) {
|
2026-01-17 02:46:30 +00:00
|
|
|
session()->forget(['threads_oauth_state', 'social_reconnect_id']);
|
|
|
|
|
|
feat: localize OAuth popup callback messages across 12 controllers
User reported the social-account OAuth callback popup ('Threads account
connected!') stayed in English regardless of the active locale. The
hardcoded message was wired through SocialController and 11 platform
controllers (Bluesky, Facebook, Instagram, InstagramFacebook, LinkedIn,
LinkedInPage, Mastodon, Pinterest, Threads, TikTok, YouTube), plus the
Blade view that the popup renders.
Adds an accounts.popup_callback i18n block (en/pt-BR/es) covering:
- The popup chrome (title, closing/close-now status text).
- Generic success/reconnect messages (one shared 'Account connected!'
/ 'Account reconnected!' line — the popup already shows a checkmark
and lives for ~2s so platform-specific wording wasn't pulling weight).
- Error variants (account/page/channel) and contextual edge cases
(page not found, no Facebook pages, no YouTube channels, etc.).
Updates every popupCallback() callsite to read from these keys, plus
the Blade view's title and submessage. Regenerates the JSON locale
bundle so laravel-vue-i18n stays in sync.
2026-05-07 17:03:52 +00:00
|
|
|
return $this->popupCallback(false, __('accounts.popup_callback.invalid_state'), $this->platform->value);
|
2026-01-15 17:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$workspace = Workspace::find($workspaceId);
|
|
|
|
|
|
|
|
|
|
if (! $workspace || ! $request->user()->can('manageAccounts', $workspace)) {
|
2026-01-17 02:46:30 +00:00
|
|
|
session()->forget(['threads_oauth_state', 'social_reconnect_id']);
|
|
|
|
|
|
feat: localize OAuth popup callback messages across 12 controllers
User reported the social-account OAuth callback popup ('Threads account
connected!') stayed in English regardless of the active locale. The
hardcoded message was wired through SocialController and 11 platform
controllers (Bluesky, Facebook, Instagram, InstagramFacebook, LinkedIn,
LinkedInPage, Mastodon, Pinterest, Threads, TikTok, YouTube), plus the
Blade view that the popup renders.
Adds an accounts.popup_callback i18n block (en/pt-BR/es) covering:
- The popup chrome (title, closing/close-now status text).
- Generic success/reconnect messages (one shared 'Account connected!'
/ 'Account reconnected!' line — the popup already shows a checkmark
and lives for ~2s so platform-specific wording wasn't pulling weight).
- Error variants (account/page/channel) and contextual edge cases
(page not found, no Facebook pages, no YouTube channels, etc.).
Updates every popupCallback() callsite to read from these keys, plus
the Blade view's title and submessage. Regenerates the JSON locale
bundle so laravel-vue-i18n stays in sync.
2026-05-07 17:03:52 +00:00
|
|
|
return $this->popupCallback(false, __('accounts.popup_callback.workspace_not_found'), $this->platform->value);
|
2026-01-15 17:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Exchange code for short-lived token
|
2026-05-02 16:49:20 +00:00
|
|
|
$tokenResponse = Http::asForm()->post(config('trypost.platforms.threads.auth_api').'/oauth/access_token', [
|
2026-01-15 17:24:39 +00:00
|
|
|
'client_id' => config('services.threads.client_id'),
|
|
|
|
|
'client_secret' => config('services.threads.client_secret'),
|
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
|
'redirect_uri' => config('services.threads.redirect'),
|
|
|
|
|
'code' => $request->code,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($tokenResponse->failed()) {
|
|
|
|
|
Log::error('Threads token exchange failed', [
|
|
|
|
|
'status' => $tokenResponse->status(),
|
2026-07-03 14:26:48 +00:00
|
|
|
'body' => TokenRedactor::redact($tokenResponse->body()),
|
2026-01-15 17:24:39 +00:00
|
|
|
]);
|
|
|
|
|
throw new \Exception('Failed to exchange token');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$tokenData = $tokenResponse->json();
|
|
|
|
|
$shortLivedToken = $tokenData['access_token'];
|
|
|
|
|
$userId = $tokenData['user_id'];
|
|
|
|
|
|
|
|
|
|
// Exchange for long-lived token
|
2026-05-02 16:49:20 +00:00
|
|
|
$longLivedResponse = Http::get(config('trypost.platforms.threads.auth_api').'/access_token', [
|
2026-01-15 17:24:39 +00:00
|
|
|
'grant_type' => 'th_exchange_token',
|
|
|
|
|
'client_secret' => config('services.threads.client_secret'),
|
|
|
|
|
'access_token' => $shortLivedToken,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-07-03 13:33:26 +00:00
|
|
|
if ($longLivedResponse->failed()) {
|
|
|
|
|
Log::error('Threads long-lived token exchange failed', [
|
|
|
|
|
'status' => $longLivedResponse->status(),
|
2026-07-03 14:26:48 +00:00
|
|
|
'body' => TokenRedactor::redact($longLivedResponse->body()),
|
2026-07-03 13:33:26 +00:00
|
|
|
]);
|
|
|
|
|
throw new \Exception('Failed to exchange long-lived token');
|
2026-01-15 17:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-03 13:33:26 +00:00
|
|
|
$longLivedData = $longLivedResponse->json();
|
|
|
|
|
$longLivedToken = $longLivedData['access_token'] ?? $shortLivedToken;
|
2026-07-03 16:11:36 +00:00
|
|
|
$expiresIn = $longLivedData['expires_in'] ?? $this->platform->defaultTokenTtlSeconds();
|
2026-07-03 13:33:26 +00:00
|
|
|
|
2026-01-15 17:24:39 +00:00
|
|
|
// Fetch user profile
|
2026-05-02 16:49:20 +00:00
|
|
|
$profileResponse = Http::get(config('trypost.platforms.threads.graph_api')."/{$userId}", [
|
2026-01-15 17:24:39 +00:00
|
|
|
'access_token' => $longLivedToken,
|
|
|
|
|
'fields' => 'id,username,name,threads_profile_picture_url',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($profileResponse->failed()) {
|
|
|
|
|
Log::error('Threads profile fetch failed', [
|
|
|
|
|
'body' => $profileResponse->body(),
|
|
|
|
|
]);
|
|
|
|
|
throw new \Exception('Failed to fetch profile');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$profile = $profileResponse->json();
|
feat: social account toggle action, API, MCP + full test coverage
- Extract ToggleSocialAccount action from SocialController
- Add API endpoints: GET /social-accounts, PUT /social-accounts/{id}/toggle
- Add MCP tools: ListSocialAccountsTool, ToggleSocialAccountTool
- Fix all MCP tools: findOrFail → find + Response::error for graceful errors
- Fix MCP tools using $request->validated() without validate() call
- Fix return types to Response|ResponseFactory for error paths
- Add SocialAccountResource is_active/status fields (no tokens exposed)
- Add 43 MCP tests covering all 18 tools (CRUD, validation, cross-workspace)
- Add API response structure tests for posts, hashtags, labels, workspace
- Add API validation tests for post create/update, api-key expiry, label color
- Add API cross-workspace delete tests for hashtags and labels
- Add app validation tests for hashtag/label update, invite fields, password
- Add auth required tests for notifications, profile delete, api-keys index
- Add media reorder validation tests
2026-03-31 04:42:39 +00:00
|
|
|
$avatarPath = uploadFromUrl(data_get($profile, 'threads_profile_picture_url', null));
|
2026-01-15 17:24:39 +00:00
|
|
|
|
2026-04-15 12:46:18 +00:00
|
|
|
$workspace->socialAccounts()->updateOrCreate(
|
|
|
|
|
[
|
|
|
|
|
'platform' => $this->platform->value,
|
|
|
|
|
'platform_user_id' => data_get($profile, 'id'),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'username' => data_get($profile, 'username'),
|
|
|
|
|
'display_name' => data_get($profile, 'name', data_get($profile, 'username')),
|
|
|
|
|
'avatar_url' => $avatarPath,
|
|
|
|
|
'access_token' => $longLivedToken,
|
|
|
|
|
'refresh_token' => null,
|
2026-07-03 13:33:26 +00:00
|
|
|
'token_expires_at' => now()->addSeconds($expiresIn),
|
2026-04-15 12:46:18 +00:00
|
|
|
'scopes' => $this->scopes,
|
|
|
|
|
'status' => Status::Connected,
|
|
|
|
|
'error_message' => null,
|
|
|
|
|
'disconnected_at' => null,
|
|
|
|
|
],
|
|
|
|
|
);
|
2026-01-15 17:24:39 +00:00
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
session()->forget(['threads_oauth_state', 'social_reconnect_id']);
|
2026-01-15 17:24:39 +00:00
|
|
|
|
feat: localize OAuth popup callback messages across 12 controllers
User reported the social-account OAuth callback popup ('Threads account
connected!') stayed in English regardless of the active locale. The
hardcoded message was wired through SocialController and 11 platform
controllers (Bluesky, Facebook, Instagram, InstagramFacebook, LinkedIn,
LinkedInPage, Mastodon, Pinterest, Threads, TikTok, YouTube), plus the
Blade view that the popup renders.
Adds an accounts.popup_callback i18n block (en/pt-BR/es) covering:
- The popup chrome (title, closing/close-now status text).
- Generic success/reconnect messages (one shared 'Account connected!'
/ 'Account reconnected!' line — the popup already shows a checkmark
and lives for ~2s so platform-specific wording wasn't pulling weight).
- Error variants (account/page/channel) and contextual edge cases
(page not found, no Facebook pages, no YouTube channels, etc.).
Updates every popupCallback() callsite to read from these keys, plus
the Blade view's title and submessage. Regenerates the JSON locale
bundle so laravel-vue-i18n stays in sync.
2026-05-07 17:03:52 +00:00
|
|
|
return $this->popupCallback(true, __('accounts.popup_callback.connected'), $this->platform->value);
|
2026-06-22 00:28:47 +00:00
|
|
|
} catch (NetworkAlreadyConnectedException) {
|
|
|
|
|
return $this->popupCallback(false, __('accounts.popup_callback.network_taken'), $this->platform->value);
|
2026-01-15 17:24:39 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::error('Threads OAuth Error', [
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
'trace' => $e->getTraceAsString(),
|
|
|
|
|
]);
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
session()->forget(['threads_oauth_state', 'social_reconnect_id']);
|
|
|
|
|
|
feat: localize OAuth popup callback messages across 12 controllers
User reported the social-account OAuth callback popup ('Threads account
connected!') stayed in English regardless of the active locale. The
hardcoded message was wired through SocialController and 11 platform
controllers (Bluesky, Facebook, Instagram, InstagramFacebook, LinkedIn,
LinkedInPage, Mastodon, Pinterest, Threads, TikTok, YouTube), plus the
Blade view that the popup renders.
Adds an accounts.popup_callback i18n block (en/pt-BR/es) covering:
- The popup chrome (title, closing/close-now status text).
- Generic success/reconnect messages (one shared 'Account connected!'
/ 'Account reconnected!' line — the popup already shows a checkmark
and lives for ~2s so platform-specific wording wasn't pulling weight).
- Error variants (account/page/channel) and contextual edge cases
(page not found, no Facebook pages, no YouTube channels, etc.).
Updates every popupCallback() callsite to read from these keys, plus
the Blade view's title and submessage. Regenerates the JSON locale
bundle so laravel-vue-i18n stays in sync.
2026-05-07 17:03:52 +00:00
|
|
|
return $this->popupCallback(false, __('accounts.popup_callback.error_connecting'), $this->platform->value);
|
2026-01-15 17:24:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|