225 lines
8.4 KiB
PHP
225 lines
8.4 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use Illuminate\Cache\RateLimiting\Limit;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use App\Models\Artwork;
|
|
use App\Models\ArtworkAward;
|
|
use App\Models\ArtworkComment;
|
|
use App\Models\ArtworkFavourite;
|
|
use App\Models\ArtworkReaction;
|
|
use App\Observers\ArtworkAwardObserver;
|
|
use App\Observers\ArtworkCommentObserver;
|
|
use App\Observers\ArtworkFavouriteObserver;
|
|
use App\Observers\ArtworkObserver;
|
|
use App\Observers\ArtworkReactionObserver;
|
|
use App\Services\Upload\Contracts\UploadDraftServiceInterface;
|
|
use App\Services\Upload\UploadDraftService;
|
|
use Illuminate\Support\Facades\View;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Queue\Events\JobFailed;
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Register any application services.
|
|
*/
|
|
public function register(): void
|
|
{
|
|
// Bind UploadDraftService interface to implementation
|
|
$this->app->singleton(UploadDraftServiceInterface::class, function ($app) {
|
|
return new UploadDraftService($app->make('filesystem'));
|
|
});
|
|
|
|
// Bind vector adapter interface for similarity system (resolves via factory)
|
|
$this->app->bind(
|
|
\App\Services\Recommendations\VectorSimilarity\VectorAdapterInterface::class,
|
|
fn () => \App\Services\Recommendations\VectorSimilarity\VectorAdapterFactory::make(),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Bootstrap any application services.
|
|
*/
|
|
public function boot(): void
|
|
{
|
|
// Map the 'legacy' view namespace to resources/views/_legacy so all
|
|
// view('legacy::foo') and @include('legacy::foo') calls resolve correctly
|
|
// after the folder was renamed from legacy/ to _legacy/.
|
|
View::addNamespace('legacy', resource_path('views/_legacy'));
|
|
|
|
$this->configureAuthRateLimiters();
|
|
$this->configureUploadRateLimiters();
|
|
$this->configureMessagingRateLimiters();
|
|
$this->configureMailFailureLogging();
|
|
|
|
ArtworkAward::observe(ArtworkAwardObserver::class);
|
|
Artwork::observe(ArtworkObserver::class);
|
|
ArtworkFavourite::observe(ArtworkFavouriteObserver::class);
|
|
ArtworkComment::observe(ArtworkCommentObserver::class);
|
|
ArtworkReaction::observe(ArtworkReactionObserver::class);
|
|
|
|
// Provide toolbar counts and user info to layout views (port of legacy toolbar logic)
|
|
View::composer(['layouts.nova', 'layouts.nova.*'], function ($view) {
|
|
$uploadCount = $favCount = $msgCount = $noticeCount = 0;
|
|
$avatarHash = null;
|
|
$displayName = null;
|
|
$userId = null;
|
|
|
|
if (Auth::check()) {
|
|
$userId = Auth::id();
|
|
try {
|
|
$uploadCount = DB::table('artworks')->where('user_id', $userId)->count();
|
|
} catch (\Throwable $e) {
|
|
$uploadCount = 0;
|
|
}
|
|
|
|
try {
|
|
$favCount = DB::table('artwork_favourites')->where('user_id', $userId)->count();
|
|
} catch (\Throwable $e) {
|
|
$favCount = 0;
|
|
}
|
|
|
|
try {
|
|
$msgCount = (int) DB::table('conversation_participants as cp')
|
|
->join('messages as m', 'm.conversation_id', '=', 'cp.conversation_id')
|
|
->where('cp.user_id', $userId)
|
|
->whereNull('cp.left_at')
|
|
->whereNull('m.deleted_at')
|
|
->where('m.sender_id', '!=', $userId)
|
|
->where(function ($q) {
|
|
$q->whereNull('cp.last_read_at')
|
|
->orWhereColumn('m.created_at', '>', 'cp.last_read_at');
|
|
})
|
|
->count();
|
|
} catch (\Throwable $e) {
|
|
$msgCount = 0;
|
|
}
|
|
|
|
try {
|
|
$noticeCount = DB::table('notification')->where('user_id', $userId)->where('new', 1)->count();
|
|
} catch (\Throwable $e) {
|
|
$noticeCount = 0;
|
|
}
|
|
|
|
try {
|
|
$profile = DB::table('user_profiles')->where('user_id', $userId)->first();
|
|
$avatarHash = $profile->avatar_hash ?? null;
|
|
} catch (\Throwable $e) {
|
|
$avatarHash = null;
|
|
}
|
|
|
|
$displayName = Auth::user()->name ?: (Auth::user()->username ?? '');
|
|
}
|
|
|
|
$view->with(compact('userId','uploadCount', 'favCount', 'msgCount', 'noticeCount', 'avatarHash', 'displayName'));
|
|
});
|
|
}
|
|
|
|
private function configureAuthRateLimiters(): void
|
|
{
|
|
RateLimiter::for('register-ip', function (Request $request): Limit {
|
|
$limit = max(1, (int) config('registration.ip_per_minute_limit', 3));
|
|
|
|
return Limit::perMinute($limit)->by('register:ip:' . $request->ip());
|
|
});
|
|
|
|
RateLimiter::for('register-ip-daily', function (Request $request): Limit {
|
|
$limit = max(1, (int) config('registration.ip_per_day_limit', 20));
|
|
|
|
return Limit::perDay($limit)->by('register:ip:daily:' . $request->ip());
|
|
});
|
|
|
|
RateLimiter::for('register', function (Request $request): array {
|
|
$emailKey = strtolower((string) $request->input('email', 'unknown'));
|
|
$ipLimit = (int) config('registration.ip_per_minute_limit', 3);
|
|
$emailLimit = (int) config('registration.email_per_minute_limit', 6);
|
|
|
|
return [
|
|
Limit::perMinute($ipLimit)->by('register:ip:' . $request->ip()),
|
|
Limit::perMinute($emailLimit)->by('register:email:' . $emailKey),
|
|
];
|
|
});
|
|
}
|
|
|
|
private function configureMailFailureLogging(): void
|
|
{
|
|
Event::listen(JobFailed::class, function (JobFailed $event): void {
|
|
if (! str_contains(strtolower($event->job->resolveName()), 'sendqueuedmailable')) {
|
|
return;
|
|
}
|
|
|
|
Log::warning('mail delivery failed', [
|
|
'transport' => config('mail.default'),
|
|
'job_name' => $event->job->resolveName(),
|
|
'queue' => $event->job->getQueue(),
|
|
'connection' => $event->connectionName,
|
|
'exception' => $event->exception->getMessage(),
|
|
]);
|
|
});
|
|
}
|
|
|
|
private function configureUploadRateLimiters(): void
|
|
{
|
|
RateLimiter::for('uploads-init', function (Request $request): array {
|
|
return $this->buildUploadLimits($request, 'init');
|
|
});
|
|
|
|
RateLimiter::for('uploads-finish', function (Request $request): array {
|
|
return $this->buildUploadLimits($request, 'finish');
|
|
});
|
|
|
|
RateLimiter::for('uploads-status', function (Request $request): array {
|
|
return $this->buildUploadLimits($request, 'status');
|
|
});
|
|
}
|
|
|
|
private function buildUploadLimits(Request $request, string $key): array
|
|
{
|
|
$config = (array) config('uploads.rate_limits.' . $key, []);
|
|
$decay = (int) config('uploads.rate_limits.decay_minutes', 1);
|
|
$perUser = (int) ($config['per_user'] ?? 0);
|
|
$perIp = (int) ($config['per_ip'] ?? 0);
|
|
|
|
$limits = [];
|
|
|
|
if ($perUser > 0) {
|
|
$userId = $request->user()?->id ?? 'guest';
|
|
$limits[] = Limit::perMinutes($decay, $perUser)->by('u:' . $userId);
|
|
}
|
|
|
|
if ($perIp > 0) {
|
|
$limits[] = Limit::perMinutes($decay, $perIp)->by('ip:' . $request->ip());
|
|
}
|
|
|
|
return $limits;
|
|
}
|
|
|
|
private function configureMessagingRateLimiters(): void
|
|
{
|
|
RateLimiter::for('messages-send', function (Request $request): array {
|
|
$userId = $request->user()?->id ?? 'guest';
|
|
|
|
return [
|
|
Limit::perMinute(20)->by('messages:user:' . $userId),
|
|
Limit::perMinute(40)->by('messages:ip:' . $request->ip()),
|
|
];
|
|
});
|
|
|
|
RateLimiter::for('messages-react', function (Request $request): array {
|
|
$userId = $request->user()?->id ?? 'guest';
|
|
|
|
return [
|
|
Limit::perMinute(60)->by('messages:react:user:' . $userId),
|
|
Limit::perMinute(120)->by('messages:react:ip:' . $request->ip()),
|
|
];
|
|
});
|
|
}
|
|
}
|