trypost/app/Providers/AppServiceProvider.php

302 lines
10 KiB
PHP
Raw Normal View History

2026-01-15 01:13:44 +00:00
<?php
declare(strict_types=1);
2026-01-15 01:13:44 +00:00
namespace App\Providers;
use App\Ai\PlatformRules\BlueskyRules;
use App\Ai\PlatformRules\FacebookRules;
use App\Ai\PlatformRules\InstagramRules;
use App\Ai\PlatformRules\LinkedInRules;
use App\Ai\PlatformRules\MastodonRules;
use App\Ai\PlatformRules\PinterestRules;
use App\Ai\PlatformRules\Registry;
use App\Ai\PlatformRules\ThreadsRules;
use App\Ai\PlatformRules\TikTokRules;
use App\Ai\PlatformRules\XRules;
use App\Ai\PlatformRules\YouTubeRules;
use App\Ai\Providers\ExtendedGeminiProvider;
use App\Enums\SocialAccount\Platform;
use App\Listeners\StripeEventListener;
use App\Models\AccessToken;
use App\Models\Account;
use App\Models\AiUsageLog;
use App\Models\Invite;
use App\Models\Media;
use App\Models\Notification;
refactor: notification preferences, header slots, calendar layout, UI polish Notification preferences: - Create notification_preferences table (post_published, post_failed, account_disconnected booleans per user) - NotificationPreferenceController with firstOrCreate on first visit - SendNotification job respects email preferences before sending - Settings page with toggle switches, i18n in 3 languages - 8 new tests for preferences (controller + wantsEmailFor + job integration) Post published notification: - PostPublished mail + maizzle template - Notify owner on successful publish via SendNotification job - PostPublished type added to notification enum Header & Layout: - Rename AppSidebarHeader to AppHeader with left/center/right slots - showSidebarTrigger prop to hide sidebar toggle - Calendar: controls in header (left: nav, center: date, right: tabs + new post) - Fixed header with scrollable content (flex h-screen pattern) - fullWidth pages use overflow-y-auto (fixes month view scroll) UI improvements: - Action buttons moved to header-right: posts, hashtags, labels - Settings breadcrumbs: "Settings > Profile" pattern - Calendar: remove duplicate New Post button from day view - Remove size="sm" from Schedule/Publish buttons - Remove bg-background from header (inherits from SidebarInset) - Add Cancel button to labels and hashtags create/edit dialogs - Add common.cancel i18n key - Clean up orphaned Calendar breadcrumbs - Fix SocialAccountsGrid buttons to use shadcn Button ghost All 753 tests passing.
2026-03-30 21:18:17 +00:00
use App\Models\NotificationPreference;
use App\Models\Plan;
use App\Models\Post;
use App\Models\PostComment;
use App\Models\PostPlatform;
use App\Models\SocialAccount;
use App\Models\Subscription;
use App\Models\SubscriptionItem;
use App\Models\User;
use App\Models\Workspace;
use App\Models\WorkspaceInvite;
use App\Models\WorkspaceLabel;
use App\Models\WorkspaceSignature;
use App\Services\PostTemplate\Registry as PostTemplateRegistry;
use App\Socialite\InstagramProvider;
2026-01-15 17:24:39 +00:00
use App\Socialite\LinkedInPageExtendSocialite;
2026-01-15 01:13:44 +00:00
use Carbon\CarbonImmutable;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Notifications\Messages\MailMessage;
2026-01-15 01:13:44 +00:00
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\RateLimiter;
2026-01-15 01:13:44 +00:00
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\Rules\Password;
use Inertia\Inertia;
use Laravel\Ai\Ai;
use Laravel\Ai\Gateway\Gemini\GeminiGateway;
use Laravel\Cashier\Cashier;
use Laravel\Cashier\Events\WebhookReceived;
use Laravel\Nightwatch\Facades\Nightwatch;
use Laravel\Nightwatch\Records\CacheEvent;
use Laravel\Passport\Passport;
use Laravel\Pennant\Feature;
use Laravel\Socialite\Facades\Socialite;
2026-03-31 00:18:07 +00:00
use Laravel\Socialite\Two\GoogleProvider;
use PostHog\PostHog;
use SocialiteProviders\Facebook\FacebookExtendSocialite;
2026-01-15 01:13:44 +00:00
use SocialiteProviders\LinkedIn\LinkedInExtendSocialite;
use SocialiteProviders\Manager\SocialiteWasCalled;
use SocialiteProviders\Pinterest\PinterestExtendSocialite;
2026-01-15 01:13:44 +00:00
use SocialiteProviders\TikTok\TikTokExtendSocialite;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
$this->app->singleton(PostTemplateRegistry::class);
if ($this->app->environment('local') && class_exists(\Laravel\Telescope\TelescopeServiceProvider::class)) {
$this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
$this->app->register(TelescopeServiceProvider::class);
}
2026-01-15 01:13:44 +00:00
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
$this->configureDefaults();
$this->configureMorphMap();
$this->configureAi();
$this->configurePlatformRules();
2026-03-31 00:18:07 +00:00
$this->configurePostHog();
$this->configureRateLimiting();
2026-01-15 01:13:44 +00:00
$this->configureSocialite();
$this->configureStripeWebhooks();
Cashier::useCustomerModel(Account::class);
Cashier::useSubscriptionModel(Subscription::class);
Cashier::useSubscriptionItemModel(SubscriptionItem::class);
Feature::resolveScopeUsing(fn () => auth()->user()?->account);
Feature::useMorphMap();
Feature::discover();
$this->configurePassport();
}
protected function configurePassport(): void
{
Passport::useTokenModel(AccessToken::class);
Passport::tokensCan([
'mcp:use' => 'Use MCP server',
]);
Passport::authorizationView(fn ($parameters) => Inertia::render('oauth/Authorize', $parameters)
);
}
protected function configureAi(): void
{
Ai::extend('gemini', function ($app, array $config) {
return new ExtendedGeminiProvider(
new GeminiGateway($app['events']),
$config,
$app->make(Dispatcher::class),
);
});
}
protected function configurePlatformRules(): void
{
$map = [
Platform::Instagram->value => InstagramRules::class,
Platform::InstagramFacebook->value => InstagramRules::class,
Platform::Facebook->value => FacebookRules::class,
Platform::X->value => XRules::class,
Platform::TikTok->value => TikTokRules::class,
Platform::YouTube->value => YouTubeRules::class,
Platform::LinkedIn->value => LinkedInRules::class,
Platform::LinkedInPage->value => LinkedInRules::class,
Platform::Threads->value => ThreadsRules::class,
Platform::Pinterest->value => PinterestRules::class,
Platform::Bluesky->value => BlueskyRules::class,
Platform::Mastodon->value => MastodonRules::class,
];
foreach ($map as $value => $class) {
Registry::register(
Platform::from($value),
$class,
);
}
}
protected function configureMorphMap(): void
{
Relation::enforceMorphMap([
'accessToken' => AccessToken::class,
'account' => Account::class,
'aiUsageLog' => AiUsageLog::class,
'invite' => Invite::class,
'media' => Media::class,
'notification' => Notification::class,
refactor: notification preferences, header slots, calendar layout, UI polish Notification preferences: - Create notification_preferences table (post_published, post_failed, account_disconnected booleans per user) - NotificationPreferenceController with firstOrCreate on first visit - SendNotification job respects email preferences before sending - Settings page with toggle switches, i18n in 3 languages - 8 new tests for preferences (controller + wantsEmailFor + job integration) Post published notification: - PostPublished mail + maizzle template - Notify owner on successful publish via SendNotification job - PostPublished type added to notification enum Header & Layout: - Rename AppSidebarHeader to AppHeader with left/center/right slots - showSidebarTrigger prop to hide sidebar toggle - Calendar: controls in header (left: nav, center: date, right: tabs + new post) - Fixed header with scrollable content (flex h-screen pattern) - fullWidth pages use overflow-y-auto (fixes month view scroll) UI improvements: - Action buttons moved to header-right: posts, hashtags, labels - Settings breadcrumbs: "Settings > Profile" pattern - Calendar: remove duplicate New Post button from day view - Remove size="sm" from Schedule/Publish buttons - Remove bg-background from header (inherits from SidebarInset) - Add Cancel button to labels and hashtags create/edit dialogs - Add common.cancel i18n key - Clean up orphaned Calendar breadcrumbs - Fix SocialAccountsGrid buttons to use shadcn Button ghost All 753 tests passing.
2026-03-30 21:18:17 +00:00
'notificationPreference' => NotificationPreference::class,
'plan' => Plan::class,
'post' => Post::class,
'postComment' => PostComment::class,
'postPlatform' => PostPlatform::class,
'socialAccount' => SocialAccount::class,
'subscription' => Subscription::class,
'subscriptionItem' => SubscriptionItem::class,
'user' => User::class,
'workspace' => Workspace::class,
'workspaceInvite' => WorkspaceInvite::class,
'workspaceLabel' => WorkspaceLabel::class,
'workspaceSignature' => WorkspaceSignature::class,
]);
}
2026-03-31 00:18:07 +00:00
protected function configurePostHog(): void
{
$apiKey = config('services.posthog.api_key');
if ($apiKey) {
PostHog::init($apiKey, [
'host' => config('services.posthog.host'),
]);
}
}
protected function configureRateLimiting(): void
{
RateLimiter::for('api', function (Request $request) {
if ($this->app->environment('local')) {
return Limit::none();
}
return Limit::perMinute(60)->by($request->workspace?->id ?: $request->ip());
});
}
protected function configureStripeWebhooks(): void
{
Event::listen(WebhookReceived::class, StripeEventListener::class);
2026-01-15 01:13:44 +00:00
}
protected function configureSocialite(): void
{
2026-03-31 00:18:07 +00:00
// Google Auth (login/signup) - separate from YouTube OAuth
Socialite::extend('google-auth', function ($app) {
$config = $app['config']['services.google-auth'];
return Socialite::buildProvider(GoogleProvider::class, $config);
});
// Instagram Business Login
Socialite::extend('instagram', function ($app) {
$config = $app['config']['services.instagram'];
return Socialite::buildProvider(InstagramProvider::class, $config);
});
Event::listen(SocialiteWasCalled::class, FacebookExtendSocialite::class);
2026-01-15 01:13:44 +00:00
Event::listen(SocialiteWasCalled::class, LinkedInExtendSocialite::class);
Event::listen(SocialiteWasCalled::class, LinkedInPageExtendSocialite::class);
Event::listen(SocialiteWasCalled::class, PinterestExtendSocialite::class);
2026-01-15 01:13:44 +00:00
Event::listen(SocialiteWasCalled::class, TikTokExtendSocialite::class);
}
protected function configureDefaults(): void
{
Date::use(CarbonImmutable::class);
// Disable wrapping of JSON resources
JsonResource::withoutWrapping();
Model::shouldBeStrict(! $this->app->isProduction());
// DB::prohibitDestructiveCommands(
// app()->isProduction(),
// );
2026-01-15 01:13:44 +00:00
Password::defaults(fn (): ?Password => app()->isProduction()
? Password::min(12)
->mixedCase()
->letters()
->numbers()
->symbols()
->uncompromised()
: null
);
Nightwatch::rejectCacheEvents(function (CacheEvent $cacheEvent) {
return in_array($cacheEvent->key, [
'illuminate:foundation:down',
'illuminate:queue:restart',
'illuminate:schedule:interrupt',
]);
});
// Custom email verification template
VerifyEmail::toMailUsing(function (User $user, string $url) {
return (new MailMessage)
->from(config('mail.from.address'), config('mail.from.name'))
->subject('Verify your email address')
->view('mail.email-verification', [
'title' => 'Verify your email address',
'previewText' => 'Please verify your email address.',
'user' => $user,
'url' => $url,
]);
});
// Custom password reset template
ResetPassword::toMailUsing(function (User $user, string $token) {
$url = url(route('password.reset', [
'token' => $token,
'email' => $user->getEmailForPasswordReset(),
], false));
return (new MailMessage)
->from(config('mail.from.address'), config('mail.from.name'))
->subject('Reset your password')
->view('mail.password-reset', [
'title' => 'Reset your password',
'previewText' => 'Reset your password.',
'user' => $user,
'url' => $url,
]);
});
2026-01-15 01:13:44 +00:00
}
}