Implement academy analytics, billing, and web stories updates
This commit is contained in:
19
.env.example
19
.env.example
@@ -210,6 +210,25 @@ YOLO_HTTP_RETRIES=1
|
|||||||
YOLO_HTTP_RETRY_DELAY_MS=200
|
YOLO_HTTP_RETRY_DELAY_MS=200
|
||||||
YOLO_PHOTOGRAPHY_ONLY=true
|
YOLO_PHOTOGRAPHY_ONLY=true
|
||||||
|
|
||||||
|
# Academy feature flags
|
||||||
|
SKINBASE_ACADEMY_ENABLED=true
|
||||||
|
SKINBASE_ACADEMY_PAYMENTS_ENABLED=false
|
||||||
|
ACADEMY_BILLING_ENABLED=false
|
||||||
|
ACADEMY_STRIPE_SUBSCRIPTION_NAME=academy
|
||||||
|
|
||||||
|
# Stripe / Cashier
|
||||||
|
STRIPE_KEY=pk_test_xxx
|
||||||
|
STRIPE_SECRET=sk_test_xxx
|
||||||
|
STRIPE_WEBHOOK_SECRET=whsec_xxx
|
||||||
|
CASHIER_CURRENCY=eur
|
||||||
|
CASHIER_CURRENCY_LOCALE=sl_SI
|
||||||
|
|
||||||
|
# Academy billing price IDs
|
||||||
|
ACADEMY_CREATOR_MONTHLY_PRICE_ID=price_xxx
|
||||||
|
ACADEMY_PRO_MONTHLY_PRICE_ID=price_xxx
|
||||||
|
|
||||||
|
# Stripe expects real price object IDs that start with price_, not product IDs like prod_...
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
# Production examples (uncomment and adjust)
|
# Production examples (uncomment and adjust)
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
|||||||
183
app/Console/Commands/AcademyAnalyticsHealthCommand.php
Normal file
183
app/Console/Commands/AcademyAnalyticsHealthCommand.php
Normal file
@@ -0,0 +1,183 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Models\AcademyContentMetricDaily;
|
||||||
|
use App\Models\AcademyEvent;
|
||||||
|
use App\Models\AcademyLike;
|
||||||
|
use App\Models\AcademySave;
|
||||||
|
use App\Models\AcademySearchLog;
|
||||||
|
use App\Models\AcademyUserProgress;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsEventType;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
final class AcademyAnalyticsHealthCommand extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'academy:analytics-health {--json : Output machine-readable JSON}';
|
||||||
|
|
||||||
|
protected $description = 'Inspect Academy analytics collection health, rollup freshness, and privacy safeguards';
|
||||||
|
|
||||||
|
private const RETENTION_DAYS = 180;
|
||||||
|
|
||||||
|
public function handle(): int
|
||||||
|
{
|
||||||
|
$report = $this->buildReport();
|
||||||
|
|
||||||
|
if ((bool) $this->option('json')) {
|
||||||
|
$this->line(json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) ?: '{}');
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->line('Academy Analytics Health Check');
|
||||||
|
$this->line('==============================');
|
||||||
|
$this->newLine();
|
||||||
|
$this->line(sprintf('Events last 24h: %d', $report['events_last_24h']));
|
||||||
|
$this->line(sprintf('Events last 7d: %d', $report['events_last_7d']));
|
||||||
|
$this->line(sprintf('Latest event: %s', $report['latest_event_at'] ?? 'none'));
|
||||||
|
$this->line(sprintf('Latest rollup date: %s', $report['latest_rollup_date'] ?? 'none'));
|
||||||
|
$this->line(sprintf('Search logs: %d', $report['search_logs']));
|
||||||
|
$this->line(sprintf('Search clicks: %d', $report['search_clicks']));
|
||||||
|
$this->line(sprintf('Likes: %d', $report['likes']));
|
||||||
|
$this->line(sprintf('Saves: %d', $report['saves']));
|
||||||
|
$this->line(sprintf('Progress records: %d', $report['progress_records']));
|
||||||
|
$this->line(sprintf('Prompt copies: %d', $report['prompt_copies']));
|
||||||
|
$this->line(sprintf('Upgrade clicks: %d', $report['upgrade_clicks']));
|
||||||
|
$this->line(sprintf('Human events: %d', $report['human_events']));
|
||||||
|
$this->line(sprintf('Bot/admin events: %d', $report['bot_admin_events']));
|
||||||
|
$this->line(sprintf('Recent daily metric rows: %d', $report['recent_daily_metric_rows']));
|
||||||
|
$this->line(sprintf('Raw IP storage detected: %s', $report['raw_ip_storage_detected'] ? 'yes' : 'no'));
|
||||||
|
$this->line(sprintf('Events older than retention: %d', $report['events_older_than_retention']));
|
||||||
|
$this->newLine();
|
||||||
|
|
||||||
|
foreach ($report['warnings'] as $warning) {
|
||||||
|
$this->warn(sprintf('WARNING: %s', $warning));
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->info(sprintf('Status: %s', $report['status']));
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
private function buildReport(): array
|
||||||
|
{
|
||||||
|
$now = now();
|
||||||
|
$last24Hours = $now->copy()->subDay();
|
||||||
|
$last7Days = $now->copy()->subDays(7);
|
||||||
|
$retentionCutoff = $now->copy()->subDays(self::RETENTION_DAYS);
|
||||||
|
$warnings = [];
|
||||||
|
$rawIpStorageDetected = $this->rawIpStorageDetected();
|
||||||
|
$eventsTableExists = Schema::hasTable('academy_events');
|
||||||
|
$metricsTableExists = Schema::hasTable('academy_content_metrics_daily');
|
||||||
|
$searchLogsTableExists = Schema::hasTable('academy_search_logs');
|
||||||
|
$likesTableExists = Schema::hasTable('academy_likes');
|
||||||
|
$savesTableExists = Schema::hasTable('academy_saves');
|
||||||
|
$progressTableExists = Schema::hasTable('academy_user_progress');
|
||||||
|
|
||||||
|
$latestEvent = $eventsTableExists ? AcademyEvent::query()->latest('occurred_at')->value('occurred_at') : null;
|
||||||
|
$latestRollup = $metricsTableExists ? AcademyContentMetricDaily::query()->latest('date')->value('date') : null;
|
||||||
|
$searchLogCount = $searchLogsTableExists ? AcademySearchLog::query()->count() : 0;
|
||||||
|
$searchClickCount = $searchLogsTableExists ? AcademySearchLog::query()->whereNotNull('clicked_content_id')->count() : 0;
|
||||||
|
$eventsOlderThanRetention = $eventsTableExists ? AcademyEvent::query()->where('occurred_at', '<', $retentionCutoff)->count() : 0;
|
||||||
|
$recentDailyMetricRows = $metricsTableExists
|
||||||
|
? AcademyContentMetricDaily::query()->whereBetween('date', [$now->copy()->subDays(6)->toDateString(), $now->toDateString()])->count()
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
$report = [
|
||||||
|
'events_last_24h' => $eventsTableExists ? AcademyEvent::query()->where('occurred_at', '>=', $last24Hours)->count() : 0,
|
||||||
|
'events_last_7d' => $eventsTableExists ? AcademyEvent::query()->where('occurred_at', '>=', $last7Days)->count() : 0,
|
||||||
|
'latest_event_at' => $latestEvent ? Carbon::parse((string) $latestEvent)->toDateTimeString() : null,
|
||||||
|
'latest_rollup_date' => $latestRollup ? Carbon::parse((string) $latestRollup)->toDateString() : null,
|
||||||
|
'search_logs' => $searchLogCount,
|
||||||
|
'search_clicks' => $searchClickCount,
|
||||||
|
'likes' => $likesTableExists ? AcademyLike::query()->count() : 0,
|
||||||
|
'saves' => $savesTableExists ? AcademySave::query()->count() : 0,
|
||||||
|
'progress_records' => $progressTableExists ? AcademyUserProgress::query()->count() : 0,
|
||||||
|
'prompt_copies' => $eventsTableExists ? AcademyEvent::query()->where('event_type', AcademyAnalyticsEventType::PROMPT_COPY)->count() : 0,
|
||||||
|
'upgrade_clicks' => $eventsTableExists ? AcademyEvent::query()->where('event_type', AcademyAnalyticsEventType::UPGRADE_CLICK)->count() : 0,
|
||||||
|
'human_events' => $eventsTableExists ? AcademyEvent::query()->where('is_bot', false)->where('is_admin', false)->where('is_suspicious', false)->count() : 0,
|
||||||
|
'bot_admin_events' => $eventsTableExists ? AcademyEvent::query()->where(function ($query): void {
|
||||||
|
$query->where('is_bot', true)->orWhere('is_admin', true)->orWhere('is_suspicious', true);
|
||||||
|
})->count() : 0,
|
||||||
|
'raw_ip_storage_detected' => $rawIpStorageDetected,
|
||||||
|
'events_older_than_retention' => $eventsOlderThanRetention,
|
||||||
|
'recent_daily_metric_rows' => $recentDailyMetricRows,
|
||||||
|
'retention_days' => self::RETENTION_DAYS,
|
||||||
|
'tables_present' => [
|
||||||
|
'academy_events' => $eventsTableExists,
|
||||||
|
'academy_content_metrics_daily' => $metricsTableExists,
|
||||||
|
'academy_search_logs' => $searchLogsTableExists,
|
||||||
|
'academy_likes' => $likesTableExists,
|
||||||
|
'academy_saves' => $savesTableExists,
|
||||||
|
'academy_user_progress' => $progressTableExists,
|
||||||
|
],
|
||||||
|
'warnings' => [],
|
||||||
|
'status' => 'OK',
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($report['tables_present'] as $table => $present) {
|
||||||
|
if (! $present) {
|
||||||
|
$warnings[] = sprintf('Analytics table %s is missing.', $table);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($report['events_last_24h'] === 0) {
|
||||||
|
$warnings[] = 'No events received in last 24 hours.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($report['events_last_7d'] === 0) {
|
||||||
|
$warnings[] = 'No events received in last 7 days.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($report['latest_rollup_date'] === null) {
|
||||||
|
$warnings[] = 'No rollup rows exist yet.';
|
||||||
|
} elseif ($report['latest_rollup_date'] !== $now->toDateString()) {
|
||||||
|
$warnings[] = 'Rollup has not run for today.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($searchLogCount > 0 && $searchClickCount === 0) {
|
||||||
|
$warnings[] = 'Search clicks are zero although search logs exist.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($eventsOlderThanRetention > 0) {
|
||||||
|
$warnings[] = 'Raw events older than configured retention period exist.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($recentDailyMetricRows === 0) {
|
||||||
|
$warnings[] = 'No daily metrics exist for recent days.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($rawIpStorageDetected) {
|
||||||
|
$warnings[] = 'Raw IP storage indicators were found in Academy analytics tables.';
|
||||||
|
}
|
||||||
|
|
||||||
|
$report['warnings'] = $warnings;
|
||||||
|
$report['status'] = $warnings === [] ? 'OK' : 'WARNING';
|
||||||
|
|
||||||
|
return $report;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function rawIpStorageDetected(): bool
|
||||||
|
{
|
||||||
|
foreach (['academy_events', 'academy_search_logs', 'academy_content_metrics_daily', 'academy_likes', 'academy_saves', 'academy_user_progress'] as $table) {
|
||||||
|
if (! Schema::hasTable($table)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (['ip', 'ip_address', 'visitor_ip', 'raw_ip', 'remote_addr'] as $column) {
|
||||||
|
if (Schema::hasColumn($table, $column)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
27
app/Console/Commands/AcademyAnalyticsPruneEventsCommand.php
Normal file
27
app/Console/Commands/AcademyAnalyticsPruneEventsCommand.php
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Models\AcademyEvent;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
|
final class AcademyAnalyticsPruneEventsCommand extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'academy:analytics-prune-events {--days=180}';
|
||||||
|
|
||||||
|
protected $description = 'Delete old raw Academy analytics events while keeping daily rollups';
|
||||||
|
|
||||||
|
public function handle(): int
|
||||||
|
{
|
||||||
|
$days = max(1, (int) $this->option('days'));
|
||||||
|
$deleted = AcademyEvent::query()
|
||||||
|
->where('occurred_at', '<', now()->subDays($days)->startOfDay())
|
||||||
|
->delete();
|
||||||
|
|
||||||
|
$this->info(sprintf('Pruned %d Academy analytics event(s).', $deleted));
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Models\AcademyContentMetricDaily;
|
||||||
|
use App\Services\Academy\AcademyPopularityService;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
|
final class AcademyAnalyticsRecalculatePopularityCommand extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'academy:analytics-recalculate-popularity {--days=30}';
|
||||||
|
|
||||||
|
protected $description = 'Recalculate Academy daily popularity and conversion scores';
|
||||||
|
|
||||||
|
public function __construct(private readonly AcademyPopularityService $popularity)
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handle(): int
|
||||||
|
{
|
||||||
|
$days = max(1, (int) $this->option('days'));
|
||||||
|
|
||||||
|
AcademyContentMetricDaily::query()
|
||||||
|
->where('date', '>=', now()->subDays($days - 1)->toDateString())
|
||||||
|
->chunkById(500, function ($rows): void {
|
||||||
|
foreach ($rows as $row) {
|
||||||
|
$row->forceFill([
|
||||||
|
'popularity_score' => $this->popularity->calculatePopularityScore($row->toArray()),
|
||||||
|
'conversion_score' => $this->popularity->calculateConversionScore($row->toArray()),
|
||||||
|
])->save();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->info(sprintf('Recalculated Academy popularity for the last %d day(s).', $days));
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
258
app/Console/Commands/AcademyAnalyticsRollupCommand.php
Normal file
258
app/Console/Commands/AcademyAnalyticsRollupCommand.php
Normal file
@@ -0,0 +1,258 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Models\AcademyContentMetricDaily;
|
||||||
|
use App\Models\AcademyEvent;
|
||||||
|
use App\Models\AcademyLike;
|
||||||
|
use App\Models\AcademySave;
|
||||||
|
use App\Models\AcademySearchLog;
|
||||||
|
use App\Services\Academy\AcademyPopularityService;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
|
||||||
|
use Carbon\CarbonPeriod;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
|
final class AcademyAnalyticsRollupCommand extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'academy:analytics-rollup {--date=} {--from=} {--to=}';
|
||||||
|
|
||||||
|
protected $description = 'Aggregate Academy analytics raw events and interaction records into daily content metrics';
|
||||||
|
|
||||||
|
public function __construct(private readonly AcademyPopularityService $popularity)
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handle(): int
|
||||||
|
{
|
||||||
|
[$from, $to] = $this->resolveRange();
|
||||||
|
|
||||||
|
foreach (CarbonPeriod::create($from, $to) as $date) {
|
||||||
|
$this->rollupDate(Carbon::parse($date));
|
||||||
|
$this->line(sprintf('Rolled up Academy analytics for %s.', Carbon::parse($date)->toDateString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function rollupDate(Carbon $date): void
|
||||||
|
{
|
||||||
|
$start = $date->copy()->startOfDay();
|
||||||
|
$end = $date->copy()->endOfDay();
|
||||||
|
$metrics = [];
|
||||||
|
$uniqueVisitors = [];
|
||||||
|
$engagedDurations = [];
|
||||||
|
|
||||||
|
AcademyEvent::query()
|
||||||
|
->whereBetween('occurred_at', [$start, $end])
|
||||||
|
->orderBy('id')
|
||||||
|
->chunkById(1000, function ($events) use (&$metrics, &$uniqueVisitors, &$engagedDurations): void {
|
||||||
|
foreach ($events as $event) {
|
||||||
|
if ($event->is_bot || $event->is_admin || $event->is_suspicious) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$key = $this->metricKey((string) ($event->content_type ?? ''), $event->content_id ? (int) $event->content_id : null);
|
||||||
|
$this->ensureMetric($metrics, (string) ($event->content_type ?? ''), $event->content_id ? (int) $event->content_id : null, $key);
|
||||||
|
|
||||||
|
$visitorKey = $event->user_id ? sprintf('user:%d', (int) $event->user_id) : trim((string) ($event->visitor_id ?? ''));
|
||||||
|
if ($visitorKey !== '') {
|
||||||
|
$uniqueVisitors[$key][$visitorKey] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$eventType = (string) $event->event_type;
|
||||||
|
if (in_array($eventType, ['academy_page_view', 'academy_content_view', 'academy_lesson_view', 'academy_course_view', 'academy_prompt_pack_view', 'academy_challenge_view'], true)) {
|
||||||
|
$metrics[$key]['views']++;
|
||||||
|
if ($event->is_logged_in) {
|
||||||
|
$metrics[$key]['user_views']++;
|
||||||
|
} else {
|
||||||
|
$metrics[$key]['guest_views']++;
|
||||||
|
}
|
||||||
|
if ($event->is_subscriber) {
|
||||||
|
$metrics[$key]['subscriber_views']++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($eventType === 'academy_engaged_view') {
|
||||||
|
$metrics[$key]['engaged_views']++;
|
||||||
|
$engagedDurations[$key][] = max(0, (int) ($event->metadata['engaged_seconds'] ?? 15));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($eventType === 'academy_scroll_50') {
|
||||||
|
$metrics[$key]['scroll_50']++;
|
||||||
|
}
|
||||||
|
if ($eventType === 'academy_scroll_75') {
|
||||||
|
$metrics[$key]['scroll_75']++;
|
||||||
|
}
|
||||||
|
if ($eventType === 'academy_scroll_100') {
|
||||||
|
$metrics[$key]['scroll_100']++;
|
||||||
|
}
|
||||||
|
if ($eventType === 'academy_prompt_copy') {
|
||||||
|
$metrics[$key]['prompt_copies']++;
|
||||||
|
}
|
||||||
|
if ($eventType === 'academy_prompt_negative_copy') {
|
||||||
|
$metrics[$key]['negative_prompt_copies']++;
|
||||||
|
}
|
||||||
|
if (in_array($eventType, ['academy_lesson_started', 'academy_course_started', 'academy_challenge_started'], true)) {
|
||||||
|
$metrics[$key]['starts']++;
|
||||||
|
}
|
||||||
|
if (in_array($eventType, ['academy_lesson_completed', 'academy_course_completed', 'academy_challenge_submitted'], true)) {
|
||||||
|
$metrics[$key]['completions']++;
|
||||||
|
}
|
||||||
|
if ($eventType === 'academy_upgrade_click') {
|
||||||
|
$metrics[$key]['upgrade_clicks']++;
|
||||||
|
}
|
||||||
|
if ($eventType === 'academy_premium_preview_view') {
|
||||||
|
$metrics[$key]['premium_preview_views']++;
|
||||||
|
}
|
||||||
|
if ($eventType === 'academy_search_result_click') {
|
||||||
|
$metrics[$key]['search_clicks']++;
|
||||||
|
|
||||||
|
$searchKey = $this->metricKey(AcademyAnalyticsContentType::SEARCH, null);
|
||||||
|
$this->ensureMetric($metrics, AcademyAnalyticsContentType::SEARCH, null, $searchKey);
|
||||||
|
$metrics[$searchKey]['search_clicks']++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
foreach (AcademyLike::query()->whereBetween('created_at', [$start, $end])->get() as $like) {
|
||||||
|
$key = $this->metricKey((string) $like->content_type, (int) $like->content_id);
|
||||||
|
$this->ensureMetric($metrics, (string) $like->content_type, (int) $like->content_id, $key);
|
||||||
|
$metrics[$key]['likes']++;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (AcademySave::query()->whereBetween('created_at', [$start, $end])->get() as $save) {
|
||||||
|
$key = $this->metricKey((string) $save->content_type, (int) $save->content_id);
|
||||||
|
$this->ensureMetric($metrics, (string) $save->content_type, (int) $save->content_id, $key);
|
||||||
|
$metrics[$key]['saves']++;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (AcademySearchLog::query()->whereBetween('created_at', [$start, $end])->get() as $searchLog) {
|
||||||
|
$key = $this->metricKey(AcademyAnalyticsContentType::SEARCH, null);
|
||||||
|
$this->ensureMetric($metrics, AcademyAnalyticsContentType::SEARCH, null, $key);
|
||||||
|
$metrics[$key]['search_impressions']++;
|
||||||
|
if ((int) $searchLog->results_count === 0) {
|
||||||
|
$metrics[$key]['bounce_count']++;
|
||||||
|
}
|
||||||
|
|
||||||
|
$visitorKey = $searchLog->user_id ? sprintf('user:%d', (int) $searchLog->user_id) : trim((string) ($searchLog->visitor_id ?? ''));
|
||||||
|
if ($visitorKey !== '') {
|
||||||
|
$uniqueVisitors[$key][$visitorKey] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($metrics as $key => $metric) {
|
||||||
|
$metric['unique_visitors'] = isset($uniqueVisitors[$key]) ? count($uniqueVisitors[$key]) : 0;
|
||||||
|
$metric['avg_engaged_seconds'] = isset($engagedDurations[$key]) && $engagedDurations[$key] !== []
|
||||||
|
? (int) round(array_sum($engagedDurations[$key]) / count($engagedDurations[$key]))
|
||||||
|
: null;
|
||||||
|
$metric['bounce_count'] = max((int) ($metric['bounce_count'] ?? 0), max(0, (int) $metric['views'] - (int) $metric['engaged_views']));
|
||||||
|
$metric['popularity_score'] = $this->popularity->calculatePopularityScore($metric);
|
||||||
|
$metric['conversion_score'] = $this->popularity->calculateConversionScore($metric);
|
||||||
|
|
||||||
|
AcademyContentMetricDaily::query()->upsert([
|
||||||
|
array_merge($metric, [
|
||||||
|
'date' => $date->copy()->startOfDay(),
|
||||||
|
'created_at' => now(),
|
||||||
|
'updated_at' => now(),
|
||||||
|
]),
|
||||||
|
], ['date', 'content_type', 'content_id'], [
|
||||||
|
'views',
|
||||||
|
'unique_visitors',
|
||||||
|
'guest_views',
|
||||||
|
'user_views',
|
||||||
|
'subscriber_views',
|
||||||
|
'engaged_views',
|
||||||
|
'scroll_50',
|
||||||
|
'scroll_75',
|
||||||
|
'scroll_100',
|
||||||
|
'likes',
|
||||||
|
'saves',
|
||||||
|
'prompt_copies',
|
||||||
|
'negative_prompt_copies',
|
||||||
|
'starts',
|
||||||
|
'completions',
|
||||||
|
'upgrade_clicks',
|
||||||
|
'premium_preview_views',
|
||||||
|
'search_impressions',
|
||||||
|
'search_clicks',
|
||||||
|
'bounce_count',
|
||||||
|
'avg_engaged_seconds',
|
||||||
|
'popularity_score',
|
||||||
|
'conversion_score',
|
||||||
|
'updated_at',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, array<string, mixed>> $metrics
|
||||||
|
*/
|
||||||
|
private function ensureMetric(array &$metrics, string $contentType, ?int $contentId, string $key): void
|
||||||
|
{
|
||||||
|
if (isset($metrics[$key])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$metrics[$key] = [
|
||||||
|
'content_type' => $contentType,
|
||||||
|
'content_id' => $contentId,
|
||||||
|
'views' => 0,
|
||||||
|
'unique_visitors' => 0,
|
||||||
|
'guest_views' => 0,
|
||||||
|
'user_views' => 0,
|
||||||
|
'subscriber_views' => 0,
|
||||||
|
'engaged_views' => 0,
|
||||||
|
'scroll_50' => 0,
|
||||||
|
'scroll_75' => 0,
|
||||||
|
'scroll_100' => 0,
|
||||||
|
'likes' => 0,
|
||||||
|
'saves' => 0,
|
||||||
|
'prompt_copies' => 0,
|
||||||
|
'negative_prompt_copies' => 0,
|
||||||
|
'starts' => 0,
|
||||||
|
'completions' => 0,
|
||||||
|
'upgrade_clicks' => 0,
|
||||||
|
'premium_preview_views' => 0,
|
||||||
|
'search_impressions' => 0,
|
||||||
|
'search_clicks' => 0,
|
||||||
|
'bounce_count' => 0,
|
||||||
|
'avg_engaged_seconds' => null,
|
||||||
|
'popularity_score' => 0,
|
||||||
|
'conversion_score' => 0,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function metricKey(string $contentType, ?int $contentId): string
|
||||||
|
{
|
||||||
|
return sprintf('%s:%s', $contentType, $contentId ?? 'none');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{0: Carbon, 1: Carbon}
|
||||||
|
*/
|
||||||
|
private function resolveRange(): array
|
||||||
|
{
|
||||||
|
$date = $this->option('date');
|
||||||
|
$from = $this->option('from');
|
||||||
|
$to = $this->option('to');
|
||||||
|
|
||||||
|
if (is_string($date) && trim($date) !== '') {
|
||||||
|
$resolved = Carbon::parse($date)->startOfDay();
|
||||||
|
|
||||||
|
return [$resolved, $resolved->copy()];
|
||||||
|
}
|
||||||
|
|
||||||
|
$resolvedFrom = is_string($from) && trim($from) !== ''
|
||||||
|
? Carbon::parse($from)->startOfDay()
|
||||||
|
: now()->subDay()->startOfDay();
|
||||||
|
$resolvedTo = is_string($to) && trim($to) !== ''
|
||||||
|
? Carbon::parse($to)->startOfDay()
|
||||||
|
: $resolvedFrom->copy();
|
||||||
|
|
||||||
|
return [$resolvedFrom, $resolvedTo];
|
||||||
|
}
|
||||||
|
}
|
||||||
288
app/Console/Commands/AcademyBillingHealthCommand.php
Normal file
288
app/Console/Commands/AcademyBillingHealthCommand.php
Normal file
@@ -0,0 +1,288 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Services\Academy\AcademyBillingPlanService;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
use Illuminate\Support\Facades\Route;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
final class AcademyBillingHealthCommand extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'academy:billing-health
|
||||||
|
{--json : Output machine-readable JSON}
|
||||||
|
{--strict : Exit non-zero when blocking issues are found}';
|
||||||
|
|
||||||
|
protected $description = 'Inspect Academy Stripe billing deployment readiness, config completeness, and Cashier route wiring';
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private readonly AcademyBillingPlanService $plans,
|
||||||
|
) {
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handle(): int
|
||||||
|
{
|
||||||
|
$report = $this->buildReport();
|
||||||
|
|
||||||
|
if ((bool) $this->option('json')) {
|
||||||
|
$this->line((string) json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
|
||||||
|
|
||||||
|
return $this->exitCodeFor($report);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->line('Academy Billing Health Check');
|
||||||
|
$this->line('============================');
|
||||||
|
$this->newLine();
|
||||||
|
$this->line(sprintf('Environment: %s', $report['environment']));
|
||||||
|
$this->line(sprintf('App URL: %s', $report['app_url'] ?? 'unset'));
|
||||||
|
$this->line(sprintf('Academy enabled: %s', $report['academy_enabled'] ? 'yes' : 'no'));
|
||||||
|
$this->line(sprintf('Academy billing enabled: %s', $report['academy_billing_enabled'] ? 'yes' : 'no'));
|
||||||
|
$this->line(sprintf('Subscription name: %s', $report['subscription_name']));
|
||||||
|
$this->line(sprintf('Cashier path: %s', $report['cashier_path']));
|
||||||
|
$this->line(sprintf('Cashier webhook route: %s', $report['routes']['cashier_webhook']['present'] ? ($report['routes']['cashier_webhook']['url'] ?? 'present') : 'missing'));
|
||||||
|
$this->line(sprintf('Academy pricing route: %s', $report['routes']['academy_pricing']['present'] ? ($report['routes']['academy_pricing']['url'] ?? 'present') : 'missing'));
|
||||||
|
$this->line(sprintf('Academy billing account route: %s', $report['routes']['academy_billing_account']['present'] ? ($report['routes']['academy_billing_account']['url'] ?? 'present') : 'missing'));
|
||||||
|
$this->line(sprintf('Stripe key configured: %s', $report['stripe']['publishable_key_configured'] ? 'yes' : 'no'));
|
||||||
|
$this->line(sprintf('Stripe secret configured: %s', $report['stripe']['secret_key_configured'] ? 'yes' : 'no'));
|
||||||
|
$this->line(sprintf('Webhook secret configured: %s', $report['stripe']['webhook_secret_configured'] ? 'yes' : 'no'));
|
||||||
|
$this->line(sprintf('Cashier currency: %s', $report['stripe']['currency'] ?: 'unset'));
|
||||||
|
$this->line(sprintf('Cashier locale: %s', $report['stripe']['currency_locale'] ?: 'unset'));
|
||||||
|
$this->line(sprintf('Configured plans: %d', $report['configured_plan_count']));
|
||||||
|
$this->line(sprintf('Plans missing Stripe price IDs: %d', count($report['missing_plan_keys'])));
|
||||||
|
$this->line(sprintf('Billing tables present: %s', $report['tables']['subscriptions'] && $report['tables']['subscription_items'] && $report['tables']['academy_billing_events'] ? 'yes' : 'no'));
|
||||||
|
$this->line(sprintf('User billing columns present: %s', $report['users_billing_columns_present'] ? 'yes' : 'no'));
|
||||||
|
$this->newLine();
|
||||||
|
|
||||||
|
foreach ($report['blockers'] as $blocker) {
|
||||||
|
$this->error(sprintf('BLOCKER: %s', $blocker));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($report['warnings'] as $warning) {
|
||||||
|
$this->warn(sprintf('WARNING: %s', $warning));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($report['plan_summaries'] !== []) {
|
||||||
|
$this->newLine();
|
||||||
|
$this->line('Plans');
|
||||||
|
$this->line('-----');
|
||||||
|
|
||||||
|
foreach ($report['plan_summaries'] as $plan) {
|
||||||
|
$this->line(sprintf(
|
||||||
|
'%s: tier=%s interval=%s price_id=%s',
|
||||||
|
$plan['key'],
|
||||||
|
$plan['tier'],
|
||||||
|
$plan['interval'],
|
||||||
|
$plan['configured'] ? 'configured' : 'missing'
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->newLine();
|
||||||
|
$this->info(sprintf('Status: %s', $report['status']));
|
||||||
|
|
||||||
|
return $this->exitCodeFor($report);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
private function buildReport(): array
|
||||||
|
{
|
||||||
|
$stripeKey = (string) config('cashier.key', '');
|
||||||
|
$stripeSecret = (string) config('cashier.secret', env('STRIPE_SECRET', ''));
|
||||||
|
$webhookSecret = (string) config('cashier.webhook.secret', env('STRIPE_WEBHOOK_SECRET', ''));
|
||||||
|
$currency = trim((string) config('cashier.currency', env('CASHIER_CURRENCY', '')));
|
||||||
|
$currencyLocale = trim((string) config('cashier.currency_locale', env('CASHIER_CURRENCY_LOCALE', '')));
|
||||||
|
$academyEnabled = (bool) config('academy.enabled', true);
|
||||||
|
$billingEnabled = $this->plans->enabled();
|
||||||
|
$missingPlanKeys = $this->plans->missingPriceIds();
|
||||||
|
$routes = [
|
||||||
|
'cashier_webhook' => $this->routeStatus('cashier.webhook'),
|
||||||
|
'academy_pricing' => $this->routeStatus('academy.pricing'),
|
||||||
|
'academy_billing_account' => $this->routeStatus('academy.billing.account'),
|
||||||
|
'academy_billing_portal' => $this->routeStatus('academy.billing.portal'),
|
||||||
|
'admin_academy_billing' => $this->routeStatus('admin.academy.billing'),
|
||||||
|
];
|
||||||
|
$tables = [
|
||||||
|
'users' => Schema::hasTable('users'),
|
||||||
|
'subscriptions' => Schema::hasTable('subscriptions'),
|
||||||
|
'subscription_items' => Schema::hasTable('subscription_items'),
|
||||||
|
'academy_billing_events' => Schema::hasTable('academy_billing_events'),
|
||||||
|
];
|
||||||
|
$userBillingColumns = [
|
||||||
|
'stripe_id' => $tables['users'] && Schema::hasColumn('users', 'stripe_id'),
|
||||||
|
'pm_type' => $tables['users'] && Schema::hasColumn('users', 'pm_type'),
|
||||||
|
'pm_last_four' => $tables['users'] && Schema::hasColumn('users', 'pm_last_four'),
|
||||||
|
'trial_ends_at' => $tables['users'] && Schema::hasColumn('users', 'trial_ends_at'),
|
||||||
|
];
|
||||||
|
$planSummaries = collect(array_keys($this->plans->plans()))
|
||||||
|
->map(function (string $key): array {
|
||||||
|
$plan = $this->plans->plan($key);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'key' => $key,
|
||||||
|
'tier' => (string) ($plan['tier'] ?? 'free'),
|
||||||
|
'interval' => (string) ($plan['interval'] ?? 'monthly'),
|
||||||
|
'configured' => (bool) ($plan['configured'] ?? false),
|
||||||
|
];
|
||||||
|
})
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
|
||||||
|
$blockers = [];
|
||||||
|
$warnings = [];
|
||||||
|
|
||||||
|
if (! $academyEnabled) {
|
||||||
|
$warnings[] = 'SKINBASE_ACADEMY_ENABLED is disabled, so billing cannot be reached by users.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $billingEnabled) {
|
||||||
|
$warnings[] = 'ACADEMY_BILLING_ENABLED is disabled. Checkout routes will stay unavailable until rollout is enabled.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $this->isConfiguredSecret($stripeKey, 'pk_')) {
|
||||||
|
$blockers[] = 'STRIPE_KEY is missing or still using a placeholder value.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $this->isConfiguredSecret($stripeSecret, 'sk_')) {
|
||||||
|
$blockers[] = 'STRIPE_SECRET is missing or still using a placeholder value.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $this->isConfiguredSecret($webhookSecret, 'whsec_')) {
|
||||||
|
$blockers[] = 'STRIPE_WEBHOOK_SECRET is missing or still using a placeholder value.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($currency === '') {
|
||||||
|
$blockers[] = 'CASHIER_CURRENCY is not configured.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($currencyLocale === '') {
|
||||||
|
$warnings[] = 'CASHIER_CURRENCY_LOCALE is not configured.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($missingPlanKeys !== []) {
|
||||||
|
$blockers[] = 'Stripe price IDs are missing for: '.implode(', ', $missingPlanKeys).'.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $routes['cashier_webhook']['present']) {
|
||||||
|
$blockers[] = 'Cashier webhook route is missing; Stripe cannot sync subscriptions.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $routes['academy_pricing']['present']) {
|
||||||
|
$blockers[] = 'Academy pricing route is missing.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $routes['academy_billing_account']['present']) {
|
||||||
|
$blockers[] = 'Academy billing account route is missing.';
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($tables as $table => $present) {
|
||||||
|
if (! $present) {
|
||||||
|
$blockers[] = sprintf('Required billing table %s is missing.', $table);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($userBillingColumns as $column => $present) {
|
||||||
|
if (! $present) {
|
||||||
|
$blockers[] = sprintf('Required users.%s billing column is missing.', $column);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $routes['admin_academy_billing']['present']) {
|
||||||
|
$warnings[] = 'Moderation Academy billing overview route is missing.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Arr::where($planSummaries, fn (array $plan): bool => $plan['configured'] === false) === []) {
|
||||||
|
$warnings[] = 'All configured Academy plans have Stripe price IDs. Verify they are live-mode IDs before production rollout.';
|
||||||
|
}
|
||||||
|
|
||||||
|
$invalidPlanKeys = collect(array_keys($this->plans->plans()))
|
||||||
|
->filter(function (string $key): bool {
|
||||||
|
$plan = $this->plans->plan($key);
|
||||||
|
|
||||||
|
return $plan !== null && ($plan['configured'] ?? false) && ! ($plan['price_id_valid'] ?? false);
|
||||||
|
})
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
|
||||||
|
if ($invalidPlanKeys !== []) {
|
||||||
|
$blockers[] = 'Stripe price IDs are malformed for: '.implode(', ', $invalidPlanKeys).'. Use real price object IDs that start with price_.';
|
||||||
|
}
|
||||||
|
|
||||||
|
$status = $blockers !== []
|
||||||
|
? 'BLOCKED'
|
||||||
|
: ($warnings !== [] ? 'WARNING' : 'OK');
|
||||||
|
|
||||||
|
return [
|
||||||
|
'environment' => app()->environment(),
|
||||||
|
'app_url' => config('app.url'),
|
||||||
|
'academy_enabled' => $academyEnabled,
|
||||||
|
'academy_billing_enabled' => $billingEnabled,
|
||||||
|
'subscription_name' => $this->plans->subscriptionName(),
|
||||||
|
'cashier_path' => (string) config('cashier.path', 'stripe'),
|
||||||
|
'stripe' => [
|
||||||
|
'publishable_key_configured' => $this->isConfiguredSecret($stripeKey, 'pk_'),
|
||||||
|
'secret_key_configured' => $this->isConfiguredSecret($stripeSecret, 'sk_'),
|
||||||
|
'webhook_secret_configured' => $this->isConfiguredSecret($webhookSecret, 'whsec_'),
|
||||||
|
'currency' => $currency,
|
||||||
|
'currency_locale' => $currencyLocale,
|
||||||
|
],
|
||||||
|
'configured_plan_count' => count($planSummaries),
|
||||||
|
'missing_plan_keys' => $missingPlanKeys,
|
||||||
|
'invalid_plan_keys' => $invalidPlanKeys,
|
||||||
|
'plan_summaries' => $planSummaries,
|
||||||
|
'routes' => $routes,
|
||||||
|
'tables' => $tables,
|
||||||
|
'user_billing_columns' => $userBillingColumns,
|
||||||
|
'users_billing_columns_present' => ! in_array(false, $userBillingColumns, true),
|
||||||
|
'blockers' => array_values(array_unique($blockers)),
|
||||||
|
'warnings' => array_values(array_unique($warnings)),
|
||||||
|
'status' => $status,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{present: bool, url: string|null}
|
||||||
|
*/
|
||||||
|
private function routeStatus(string $name): array
|
||||||
|
{
|
||||||
|
if (! Route::has($name)) {
|
||||||
|
return [
|
||||||
|
'present' => false,
|
||||||
|
'url' => null,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'present' => true,
|
||||||
|
'url' => route($name),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isConfiguredSecret(string $value, string $expectedPrefix): bool
|
||||||
|
{
|
||||||
|
$value = trim($value);
|
||||||
|
|
||||||
|
if ($value === '' || ! str_starts_with($value, $expectedPrefix)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ! str_contains(strtolower($value), 'xxx');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $report
|
||||||
|
*/
|
||||||
|
private function exitCodeFor(array $report): int
|
||||||
|
{
|
||||||
|
if ((bool) $this->option('strict') && $report['status'] === 'BLOCKED') {
|
||||||
|
return self::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
103
app/Console/Commands/BuildWorldWebStoryAssetsCommand.php
Normal file
103
app/Console/Commands/BuildWorldWebStoryAssetsCommand.php
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Models\WorldWebStory;
|
||||||
|
use App\Services\WebStories\WorldWebStoryAssetService;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
|
final class BuildWorldWebStoryAssetsCommand extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'skinbase:webstories:build-assets
|
||||||
|
{story? : Story ID or slug}
|
||||||
|
{--published : Limit batch mode to published stories}
|
||||||
|
{--visible : Limit batch mode to stories currently visible on the public site}
|
||||||
|
{--limit=100 : Maximum stories to process in batch mode}
|
||||||
|
{--force : Rebuild already populated asset paths}
|
||||||
|
{--dry-run : Report changes without saving them}';
|
||||||
|
|
||||||
|
protected $description = 'Backfill poster, logo, and page background assets for World Web Stories';
|
||||||
|
|
||||||
|
public function handle(WorldWebStoryAssetService $assets): int
|
||||||
|
{
|
||||||
|
$storyKey = $this->argument('story');
|
||||||
|
$force = (bool) $this->option('force');
|
||||||
|
$dryRun = (bool) $this->option('dry-run');
|
||||||
|
|
||||||
|
if ($storyKey !== null && trim((string) $storyKey) !== '') {
|
||||||
|
$story = $this->resolveStory((string) $storyKey);
|
||||||
|
|
||||||
|
if (! $story instanceof WorldWebStory) {
|
||||||
|
$this->error(sprintf('Web story [%s] was not found.', (string) $storyKey));
|
||||||
|
|
||||||
|
return self::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->buildOne($assets, $story, $force, $dryRun);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->buildBatch($assets, $force, $dryRun, max(1, (int) $this->option('limit')));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function buildOne(WorldWebStoryAssetService $assets, WorldWebStory $story, bool $force, bool $dryRun): int
|
||||||
|
{
|
||||||
|
$result = $assets->buildAssets($story, force: $force, dryRun: $dryRun);
|
||||||
|
|
||||||
|
$this->line(sprintf('Story [%d] %s', (int) $story->id, (string) $story->slug));
|
||||||
|
$this->line($result['updated'] ? 'Assets updated.' : 'No asset changes needed.');
|
||||||
|
|
||||||
|
foreach ((array) $result['story'] as $field => $value) {
|
||||||
|
$this->line(sprintf(' - story.%s = %s', (string) $field, (string) $value));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ((array) $result['pages'] as $pageId => $changes) {
|
||||||
|
foreach ((array) $changes as $field => $value) {
|
||||||
|
$this->line(sprintf(' - page.%d.%s = %s', (int) $pageId, (string) $field, (string) $value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function buildBatch(WorldWebStoryAssetService $assets, bool $force, bool $dryRun, int $limit): int
|
||||||
|
{
|
||||||
|
$processed = 0;
|
||||||
|
$updated = 0;
|
||||||
|
|
||||||
|
$this->storyQuery()
|
||||||
|
->limit($limit)
|
||||||
|
->get()
|
||||||
|
->each(function (WorldWebStory $story) use ($assets, $force, $dryRun, &$processed, &$updated): void {
|
||||||
|
$processed++;
|
||||||
|
$result = $assets->buildAssets($story, force: $force, dryRun: $dryRun);
|
||||||
|
|
||||||
|
if ($result['updated']) {
|
||||||
|
$updated++;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->line(sprintf('[%d] %s -> %s', (int) $story->id, (string) $story->slug, $result['updated'] ? 'updated' : 'unchanged'));
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->info(sprintf('Done. processed=%d updated=%d', $processed, $updated));
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function storyQuery()
|
||||||
|
{
|
||||||
|
return WorldWebStory::query()
|
||||||
|
->when((bool) $this->option('published'), fn ($query) => $query->published())
|
||||||
|
->when((bool) $this->option('visible'), fn ($query) => $query->visible())
|
||||||
|
->orderByDesc('published_at')
|
||||||
|
->orderByDesc('id');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function resolveStory(string $value): ?WorldWebStory
|
||||||
|
{
|
||||||
|
return WorldWebStory::query()
|
||||||
|
->when(is_numeric($value), fn ($query) => $query->where('id', (int) $value), fn ($query) => $query->where('slug', $value))
|
||||||
|
->first();
|
||||||
|
}
|
||||||
|
}
|
||||||
390
app/Console/Commands/GenerateAcademyPromptThumbnailsCommand.php
Normal file
390
app/Console/Commands/GenerateAcademyPromptThumbnailsCommand.php
Normal file
@@ -0,0 +1,390 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Models\AcademyPromptTemplate;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use RuntimeException;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
final class GenerateAcademyPromptThumbnailsCommand extends Command
|
||||||
|
{
|
||||||
|
private const PROMPT_PREVIEW_PREFIX = 'academy-prompts/previews';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array<string, int>
|
||||||
|
*/
|
||||||
|
private const VARIANT_WIDTHS = [
|
||||||
|
'thumb' => 480,
|
||||||
|
'md' => 960,
|
||||||
|
];
|
||||||
|
|
||||||
|
private const PREVIEW_WEBP_QUALITY = 84;
|
||||||
|
|
||||||
|
private const LESSON_MEDIA_WEBP_QUALITY = 85;
|
||||||
|
|
||||||
|
protected $signature = 'academy:prompts:generate-missing-thumbnails
|
||||||
|
{--id=* : Restrict to one or more prompt IDs}
|
||||||
|
{--slug=* : Restrict to one or more prompt slugs}
|
||||||
|
{--limit= : Stop after processing this many prompts}
|
||||||
|
{--force : Regenerate variants even when they already exist}
|
||||||
|
{--dry-run : Report planned thumbnail work without writing files or saving prompt JSON}';
|
||||||
|
|
||||||
|
protected $description = 'Generate missing prompt preview and comparison thumbnails for existing Academy prompts';
|
||||||
|
|
||||||
|
public function handle(): int
|
||||||
|
{
|
||||||
|
if (! function_exists('imagecreatefromstring') || ! function_exists('imagewebp')) {
|
||||||
|
$this->error('GD WebP support is required to generate prompt thumbnails.');
|
||||||
|
|
||||||
|
return self::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
$ids = collect((array) $this->option('id'))
|
||||||
|
->map(static fn (mixed $id): int => (int) $id)
|
||||||
|
->filter(static fn (int $id): bool => $id > 0)
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
|
||||||
|
$slugs = collect((array) $this->option('slug'))
|
||||||
|
->map(static fn (mixed $slug): string => trim((string) $slug))
|
||||||
|
->filter(static fn (string $slug): bool => $slug !== '')
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
|
||||||
|
$limit = $this->option('limit') !== null ? max(1, (int) $this->option('limit')) : null;
|
||||||
|
$force = (bool) $this->option('force');
|
||||||
|
$dryRun = (bool) $this->option('dry-run');
|
||||||
|
|
||||||
|
$query = AcademyPromptTemplate::query()
|
||||||
|
->select(['id', 'slug', 'title', 'preview_image', 'tool_notes'])
|
||||||
|
->orderBy('id');
|
||||||
|
|
||||||
|
if ($ids !== []) {
|
||||||
|
$query->whereIn('id', $ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($slugs !== []) {
|
||||||
|
$query->whereIn('slug', $slugs);
|
||||||
|
}
|
||||||
|
|
||||||
|
$processed = 0;
|
||||||
|
$changed = 0;
|
||||||
|
$generatedVariants = 0;
|
||||||
|
$plannedVariants = 0;
|
||||||
|
$skipped = 0;
|
||||||
|
$failed = 0;
|
||||||
|
|
||||||
|
$query->chunkById(100, function ($prompts) use ($limit, $force, $dryRun, &$processed, &$changed, &$generatedVariants, &$plannedVariants, &$skipped, &$failed) {
|
||||||
|
foreach ($prompts as $prompt) {
|
||||||
|
if ($limit !== null && $processed >= $limit) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$result = $this->backfillPrompt($prompt, $force, $dryRun);
|
||||||
|
|
||||||
|
$generatedVariants += (int) ($result['generated_variants'] ?? 0);
|
||||||
|
$plannedVariants += (int) ($result['planned_variants'] ?? 0);
|
||||||
|
|
||||||
|
if (($result['changed'] ?? false) === true) {
|
||||||
|
$changed++;
|
||||||
|
} else {
|
||||||
|
$skipped++;
|
||||||
|
}
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
$failed++;
|
||||||
|
$this->warn(sprintf('Prompt %d (%s) failed: %s', (int) $prompt->id, (string) $prompt->slug, $e->getMessage()));
|
||||||
|
}
|
||||||
|
|
||||||
|
$processed++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->info(sprintf(
|
||||||
|
'Prompt thumbnail backfill complete. processed=%d changed=%d generated_variants=%d planned_variants=%d skipped=%d failed=%d',
|
||||||
|
$processed,
|
||||||
|
$changed,
|
||||||
|
$generatedVariants,
|
||||||
|
$plannedVariants,
|
||||||
|
$skipped,
|
||||||
|
$failed,
|
||||||
|
));
|
||||||
|
|
||||||
|
return $failed > 0 ? self::FAILURE : self::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{changed:bool,generated_variants:int,planned_variants:int}
|
||||||
|
*/
|
||||||
|
private function backfillPrompt(AcademyPromptTemplate $prompt, bool $force, bool $dryRun): array
|
||||||
|
{
|
||||||
|
$generatedVariants = 0;
|
||||||
|
$plannedVariants = 0;
|
||||||
|
$changed = false;
|
||||||
|
|
||||||
|
$previewResult = $this->ensureManagedImageVariants((string) ($prompt->preview_image ?? ''), $force, $dryRun);
|
||||||
|
$generatedVariants += $previewResult['generated_variants'];
|
||||||
|
$plannedVariants += $previewResult['planned_variants'];
|
||||||
|
$changed = $changed || $previewResult['changed'];
|
||||||
|
|
||||||
|
$notes = is_array($prompt->tool_notes) ? $prompt->tool_notes : [];
|
||||||
|
$nextNotes = [];
|
||||||
|
|
||||||
|
foreach ($notes as $note) {
|
||||||
|
if (! is_array($note)) {
|
||||||
|
$nextNotes[] = $note;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$noteResult = $this->ensurePromptComparisonNoteVariants($note, $force, $dryRun);
|
||||||
|
$generatedVariants += $noteResult['generated_variants'];
|
||||||
|
$plannedVariants += $noteResult['planned_variants'];
|
||||||
|
$changed = $changed || $noteResult['changed'];
|
||||||
|
$nextNotes[] = $noteResult['note'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($changed && ! $dryRun && $nextNotes !== $notes) {
|
||||||
|
$prompt->forceFill([
|
||||||
|
'tool_notes' => $nextNotes,
|
||||||
|
])->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'changed' => $changed,
|
||||||
|
'generated_variants' => $generatedVariants,
|
||||||
|
'planned_variants' => $plannedVariants,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $note
|
||||||
|
* @return array{note:array<string, mixed>,changed:bool,generated_variants:int,planned_variants:int}
|
||||||
|
*/
|
||||||
|
private function ensurePromptComparisonNoteVariants(array $note, bool $force, bool $dryRun): array
|
||||||
|
{
|
||||||
|
$imagePath = trim((string) ($note['image_path'] ?? ''));
|
||||||
|
|
||||||
|
if (! $this->isManagedLessonMediaPath($imagePath)) {
|
||||||
|
return [
|
||||||
|
'note' => $note,
|
||||||
|
'changed' => false,
|
||||||
|
'generated_variants' => 0,
|
||||||
|
'planned_variants' => 0,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$variants = $this->ensureManagedImageVariants($imagePath, $force, $dryRun);
|
||||||
|
$thumbPath = $variants['thumb_path'] ?? '';
|
||||||
|
|
||||||
|
if ($thumbPath === '') {
|
||||||
|
$thumbPath = $imagePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
$nextNote = $note;
|
||||||
|
$currentThumbPath = trim((string) ($note['thumb_path'] ?? ''));
|
||||||
|
|
||||||
|
if ($currentThumbPath !== $thumbPath) {
|
||||||
|
$nextNote['thumb_path'] = $thumbPath;
|
||||||
|
$variants['changed'] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'note' => $nextNote,
|
||||||
|
'changed' => (bool) $variants['changed'],
|
||||||
|
'generated_variants' => (int) $variants['generated_variants'],
|
||||||
|
'planned_variants' => (int) $variants['planned_variants'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{thumb_path:string,changed:bool,generated_variants:int,planned_variants:int}
|
||||||
|
*/
|
||||||
|
private function ensureManagedImageVariants(string $path, bool $force, bool $dryRun): array
|
||||||
|
{
|
||||||
|
$path = trim($path);
|
||||||
|
|
||||||
|
if (! $this->isManagedPromptPreviewPath($path) && ! $this->isManagedLessonMediaPath($path)) {
|
||||||
|
return [
|
||||||
|
'thumb_path' => '',
|
||||||
|
'changed' => false,
|
||||||
|
'generated_variants' => 0,
|
||||||
|
'planned_variants' => 0,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$source = $this->openManagedImage($path);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$generatedVariants = 0;
|
||||||
|
$plannedVariants = 0;
|
||||||
|
$changed = false;
|
||||||
|
$thumbPath = $path;
|
||||||
|
|
||||||
|
foreach (self::VARIANT_WIDTHS as $variant => $targetWidth) {
|
||||||
|
$status = $this->ensureVariantForWidth(
|
||||||
|
$source['image'],
|
||||||
|
$source['width'],
|
||||||
|
$source['height'],
|
||||||
|
$path,
|
||||||
|
$variant,
|
||||||
|
$targetWidth,
|
||||||
|
$force,
|
||||||
|
$dryRun,
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($variant === 'thumb' && $source['width'] > $targetWidth) {
|
||||||
|
$thumbPath = $this->variantPath($path, 'thumb');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($status === 'generated') {
|
||||||
|
$generatedVariants++;
|
||||||
|
$changed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($status === 'planned') {
|
||||||
|
$plannedVariants++;
|
||||||
|
$changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'thumb_path' => $thumbPath,
|
||||||
|
'changed' => $changed,
|
||||||
|
'generated_variants' => $generatedVariants,
|
||||||
|
'planned_variants' => $plannedVariants,
|
||||||
|
];
|
||||||
|
} finally {
|
||||||
|
imagedestroy($source['image']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{image:\GdImage,width:int,height:int}
|
||||||
|
*/
|
||||||
|
private function openManagedImage(string $path): array
|
||||||
|
{
|
||||||
|
$disk = Storage::disk($this->storageDisk());
|
||||||
|
|
||||||
|
if (! $disk->exists($path)) {
|
||||||
|
throw new RuntimeException(sprintf('Source image is missing: %s', $path));
|
||||||
|
}
|
||||||
|
|
||||||
|
$binary = $disk->get($path);
|
||||||
|
|
||||||
|
if (! is_string($binary) || $binary === '') {
|
||||||
|
throw new RuntimeException(sprintf('Source image could not be read: %s', $path));
|
||||||
|
}
|
||||||
|
|
||||||
|
$image = @imagecreatefromstring($binary);
|
||||||
|
|
||||||
|
if (! $image instanceof \GdImage) {
|
||||||
|
throw new RuntimeException(sprintf('Source image is not a supported raster image: %s', $path));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! imageistruecolor($image)) {
|
||||||
|
imagepalettetotruecolor($image);
|
||||||
|
}
|
||||||
|
|
||||||
|
imagealphablending($image, true);
|
||||||
|
imagesavealpha($image, true);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'image' => $image,
|
||||||
|
'width' => imagesx($image),
|
||||||
|
'height' => imagesy($image),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function ensureVariantForWidth(\GdImage $source, int $sourceWidth, int $sourceHeight, string $sourcePath, string $variant, int $targetWidth, bool $force, bool $dryRun): string
|
||||||
|
{
|
||||||
|
if ($sourceWidth <= $targetWidth || $sourceWidth < 1 || $sourceHeight < 1) {
|
||||||
|
return 'skipped';
|
||||||
|
}
|
||||||
|
|
||||||
|
$variantPath = $this->variantPath($sourcePath, $variant);
|
||||||
|
$disk = Storage::disk($this->storageDisk());
|
||||||
|
|
||||||
|
if (! $force && $disk->exists($variantPath)) {
|
||||||
|
return 'skipped';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($dryRun) {
|
||||||
|
return 'planned';
|
||||||
|
}
|
||||||
|
|
||||||
|
$targetHeight = max(1, (int) round(($sourceHeight / $sourceWidth) * $targetWidth));
|
||||||
|
$canvas = imagecreatetruecolor($targetWidth, $targetHeight);
|
||||||
|
|
||||||
|
if (! $canvas instanceof \GdImage) {
|
||||||
|
throw new RuntimeException(sprintf('Could not allocate variant canvas for %s', $sourcePath));
|
||||||
|
}
|
||||||
|
|
||||||
|
imagealphablending($canvas, false);
|
||||||
|
imagesavealpha($canvas, true);
|
||||||
|
$transparent = imagecolorallocatealpha($canvas, 0, 0, 0, 127);
|
||||||
|
imagefilledrectangle($canvas, 0, 0, $targetWidth, $targetHeight, $transparent);
|
||||||
|
imagecopyresampled($canvas, $source, 0, 0, 0, 0, $targetWidth, $targetHeight, $sourceWidth, $sourceHeight);
|
||||||
|
|
||||||
|
try {
|
||||||
|
ob_start();
|
||||||
|
$converted = imagewebp($canvas, null, $this->qualityForPath($sourcePath));
|
||||||
|
$webpBinary = ob_get_clean();
|
||||||
|
|
||||||
|
if (! $converted || ! is_string($webpBinary) || $webpBinary === '') {
|
||||||
|
throw new RuntimeException(sprintf('Could not encode %s variant for %s', $variant, $sourcePath));
|
||||||
|
}
|
||||||
|
|
||||||
|
$disk->put($variantPath, $webpBinary, ['visibility' => 'public']);
|
||||||
|
} finally {
|
||||||
|
imagedestroy($canvas);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'generated';
|
||||||
|
}
|
||||||
|
|
||||||
|
private function variantPath(string $path, string $variant): string
|
||||||
|
{
|
||||||
|
$directory = pathinfo($path, PATHINFO_DIRNAME);
|
||||||
|
$filename = pathinfo($path, PATHINFO_FILENAME);
|
||||||
|
$baseFilename = preg_replace('/-(thumb|md)$/', '', $filename) ?? $filename;
|
||||||
|
|
||||||
|
return sprintf('%s/%s-%s.webp', $directory, $baseFilename, $variant);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isManagedPromptPreviewPath(string $path): bool
|
||||||
|
{
|
||||||
|
return $this->isLocalPath($path) && str_starts_with($path, self::PROMPT_PREVIEW_PREFIX . '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isManagedLessonMediaPath(string $path): bool
|
||||||
|
{
|
||||||
|
return $this->isLocalPath($path)
|
||||||
|
&& (str_starts_with($path, 'academy/lessons/body/') || str_starts_with($path, 'academy/lessons/covers/'));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isLocalPath(string $path): bool
|
||||||
|
{
|
||||||
|
return $path !== ''
|
||||||
|
&& ! str_starts_with($path, 'http://')
|
||||||
|
&& ! str_starts_with($path, 'https://')
|
||||||
|
&& ! str_starts_with($path, '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function storageDisk(): string
|
||||||
|
{
|
||||||
|
return (string) config('uploads.object_storage.disk', 's3');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function qualityForPath(string $path): int
|
||||||
|
{
|
||||||
|
return $this->isManagedPromptPreviewPath($path)
|
||||||
|
? self::PREVIEW_WEBP_QUALITY
|
||||||
|
: self::LESSON_MEDIA_WEBP_QUALITY;
|
||||||
|
}
|
||||||
|
}
|
||||||
131
app/Console/Commands/GenerateWorldWebStoriesCommand.php
Normal file
131
app/Console/Commands/GenerateWorldWebStoriesCommand.php
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Models\World;
|
||||||
|
use App\Services\WebStories\WorldWebStoryGenerator;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
|
|
||||||
|
final class GenerateWorldWebStoriesCommand extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'skinbase:webstories:generate
|
||||||
|
{world? : World ID or slug}
|
||||||
|
{--all : Generate stories in batch mode}
|
||||||
|
{--pages=7 : Number of pages to generate (5-10)}
|
||||||
|
{--limit=25 : Maximum worlds to process in batch mode}
|
||||||
|
{--force : Rebuild an existing story for the target world}
|
||||||
|
{--publish : Publish immediately after generation if validation passes}
|
||||||
|
{--dry-run : Preview generation without saving anything}';
|
||||||
|
|
||||||
|
protected $description = 'Generate standalone AMP Web Stories from Skinbase Worlds';
|
||||||
|
|
||||||
|
public function handle(WorldWebStoryGenerator $generator): int
|
||||||
|
{
|
||||||
|
$worldKey = $this->argument('world');
|
||||||
|
$force = (bool) $this->option('force');
|
||||||
|
$publish = (bool) $this->option('publish');
|
||||||
|
$dryRun = (bool) $this->option('dry-run');
|
||||||
|
$pages = max(5, min(10, (int) $this->option('pages')));
|
||||||
|
|
||||||
|
if ($worldKey !== null && trim((string) $worldKey) !== '') {
|
||||||
|
$world = $this->resolveWorld((string) $worldKey);
|
||||||
|
|
||||||
|
if (! $world instanceof World) {
|
||||||
|
$this->error(sprintf('World [%s] was not found.', (string) $worldKey));
|
||||||
|
|
||||||
|
return self::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->generateOne($generator, $world, $pages, $force, $publish, $dryRun);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! (bool) $this->option('all')) {
|
||||||
|
$this->error('Provide a world ID/slug or pass --all for batch generation.');
|
||||||
|
|
||||||
|
return self::INVALID;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->generateBatch($generator, $pages, $force, $publish, $dryRun, max(1, (int) $this->option('limit')));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function generateOne(WorldWebStoryGenerator $generator, World $world, int $pages, bool $force, bool $publish, bool $dryRun): int
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$result = $generator->generateFromWorld($world, null, $pages, $force, $publish, $dryRun);
|
||||||
|
} catch (ValidationException $exception) {
|
||||||
|
foreach ($exception->errors() as $messages) {
|
||||||
|
foreach ($messages as $message) {
|
||||||
|
$this->error((string) $message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
$story = $result['story'];
|
||||||
|
$validation = $result['validation'];
|
||||||
|
|
||||||
|
$this->info(sprintf(
|
||||||
|
'%s story for world [%s] -> /web-stories/%s (%d pages)',
|
||||||
|
$result['created'] ? 'Created' : 'Updated',
|
||||||
|
(string) $world->slug,
|
||||||
|
(string) $story->slug,
|
||||||
|
(int) $validation['page_count'],
|
||||||
|
));
|
||||||
|
|
||||||
|
foreach ((array) $validation['warnings'] as $warning) {
|
||||||
|
$this->warn(' - ' . $warning);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ((array) $validation['errors'] as $error) {
|
||||||
|
$this->error(' - ' . $error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $validation['valid'] || ! $publish ? self::SUCCESS : self::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function generateBatch(WorldWebStoryGenerator $generator, int $pages, bool $force, bool $publish, bool $dryRun, int $limit): int
|
||||||
|
{
|
||||||
|
$processed = 0;
|
||||||
|
$created = 0;
|
||||||
|
$updated = 0;
|
||||||
|
$failed = 0;
|
||||||
|
|
||||||
|
$query = World::query()
|
||||||
|
->published()
|
||||||
|
->orderByDesc('published_at')
|
||||||
|
->orderByDesc('id');
|
||||||
|
|
||||||
|
if (! $force) {
|
||||||
|
$query->whereDoesntHave('webStories');
|
||||||
|
}
|
||||||
|
|
||||||
|
$query->limit($limit)->get()->each(function (World $world) use ($generator, $pages, $force, $publish, $dryRun, &$processed, &$created, &$updated, &$failed): void {
|
||||||
|
$processed++;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$result = $generator->generateFromWorld($world, null, $pages, $force, $publish, $dryRun);
|
||||||
|
$result['created'] ? $created++ : $updated++;
|
||||||
|
$this->line(sprintf('[%d] %s -> %s', (int) $world->id, (string) $world->slug, (string) $result['story']->slug));
|
||||||
|
} catch (ValidationException $exception) {
|
||||||
|
$failed++;
|
||||||
|
$first = collect($exception->errors())->flatten()->first();
|
||||||
|
$this->error(sprintf('[%d] %s failed: %s', (int) $world->id, (string) $world->slug, (string) $first));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->info(sprintf('Done. processed=%d created=%d updated=%d failed=%d', $processed, $created, $updated, $failed));
|
||||||
|
|
||||||
|
return $failed === 0 ? self::SUCCESS : self::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function resolveWorld(string $value): ?World
|
||||||
|
{
|
||||||
|
return World::query()
|
||||||
|
->when(is_numeric($value), fn ($query) => $query->where('id', (int) $value), fn ($query) => $query->where('slug', $value))
|
||||||
|
->first();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,8 +5,10 @@ declare(strict_types=1);
|
|||||||
namespace App\Console\Commands;
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
use App\Models\Artwork;
|
use App\Models\Artwork;
|
||||||
|
use App\Services\Sitemaps\SitemapReleaseManager;
|
||||||
use App\Services\Vision\ArtworkVisionImageUrl;
|
use App\Services\Vision\ArtworkVisionImageUrl;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Redis;
|
use Illuminate\Support\Facades\Redis;
|
||||||
@@ -25,10 +27,10 @@ use Throwable;
|
|||||||
class HealthCheckCommand extends Command
|
class HealthCheckCommand extends Command
|
||||||
{
|
{
|
||||||
protected $signature = 'health:check
|
protected $signature = 'health:check
|
||||||
{--only= : Run only a named check (mysql|redis|cache|meilisearch|qdrant|reverb|vision|horizon|webserver|phpfpm|paths|ram|disk|load|s3|failed_jobs|queue_backlog|ssl|scheduler|log_errors|app)}
|
{--only= : Run only a named check (mysql|redis|cache|meilisearch|qdrant|reverb|vision|horizon|webserver|phpfpm|paths|ram|disk|load|s3|failed_jobs|queue_backlog|ssl|scheduler|sitemap|log_errors|app)}
|
||||||
{--json : Output results as JSON}';
|
{--json : Output results as JSON}';
|
||||||
|
|
||||||
protected $description = 'Check health of all critical services (MySQL, Redis, Cache, Meilisearch, Qdrant, Reverb, Vision, Horizon, Nginx, PHP-FPM, writable paths, RAM, disk, load, S3/Contabo, failed jobs, queue backlog, SSL, scheduler, log errors, App).';
|
protected $description = 'Check health of all critical services (MySQL, Redis, Cache, Meilisearch, Qdrant, Reverb, Vision, Horizon, Nginx, PHP-FPM, writable paths, RAM, disk, load, S3/Contabo, failed jobs, queue backlog, SSL, scheduler, sitemap, log errors, App).';
|
||||||
|
|
||||||
/** Collected results: [name => [status, message, details]] */
|
/** Collected results: [name => [status, message, details]] */
|
||||||
private array $results = [];
|
private array $results = [];
|
||||||
@@ -57,6 +59,7 @@ class HealthCheckCommand extends Command
|
|||||||
'queue_backlog' => fn () => $this->checkQueueBacklog(),
|
'queue_backlog' => fn () => $this->checkQueueBacklog(),
|
||||||
'ssl' => fn () => $this->checkSsl(),
|
'ssl' => fn () => $this->checkSsl(),
|
||||||
'scheduler' => fn () => $this->checkScheduler(),
|
'scheduler' => fn () => $this->checkScheduler(),
|
||||||
|
'sitemap' => fn () => $this->checkSitemap(),
|
||||||
'log_errors' => fn () => $this->checkLogErrors(),
|
'log_errors' => fn () => $this->checkLogErrors(),
|
||||||
'app' => fn () => $this->checkApp(),
|
'app' => fn () => $this->checkApp(),
|
||||||
];
|
];
|
||||||
@@ -1041,6 +1044,51 @@ class HealthCheckCommand extends Command
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function checkSitemap(): void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$releases = app(SitemapReleaseManager::class)->listReleases();
|
||||||
|
|
||||||
|
if ($releases === []) {
|
||||||
|
$this->failCheck('sitemap', 'No sitemap releases found. Run `php artisan skinbase:sitemaps:publish` to build one.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$latest = $releases[0];
|
||||||
|
$releaseId = (string) ($latest['release_id'] ?? 'unknown');
|
||||||
|
$builtAtRaw = (string) ($latest['built_at'] ?? $latest['published_at'] ?? '');
|
||||||
|
|
||||||
|
if ($builtAtRaw === '') {
|
||||||
|
$this->warn_check('sitemap', "Latest sitemap release [{$releaseId}] is missing a build timestamp.", [
|
||||||
|
'release_id' => $releaseId,
|
||||||
|
'status' => (string) ($latest['status'] ?? 'unknown'),
|
||||||
|
]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$builtAt = Carbon::parse($builtAtRaw);
|
||||||
|
$ageSeconds = max(0, $builtAt->diffInSeconds(now()));
|
||||||
|
$builtAtLabel = $builtAt->toAtomString();
|
||||||
|
$details = [
|
||||||
|
'release_id' => $releaseId,
|
||||||
|
'built_at' => $builtAtLabel,
|
||||||
|
'age_seconds' => $ageSeconds,
|
||||||
|
'status' => (string) ($latest['status'] ?? 'unknown'),
|
||||||
|
];
|
||||||
|
$message = "Latest sitemap release [{$releaseId}] built at {$builtAtLabel} ({$ageSeconds}s ago).";
|
||||||
|
|
||||||
|
if ($ageSeconds > 72 * 3600) {
|
||||||
|
$this->failCheck('sitemap', 'Sitemap build is stale — ' . $message, $details);
|
||||||
|
} elseif ($ageSeconds > 36 * 3600) {
|
||||||
|
$this->warn_check('sitemap', 'Sitemap build is getting old — ' . $message, $details);
|
||||||
|
} else {
|
||||||
|
$this->pass('sitemap', $message, $details);
|
||||||
|
}
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
$this->warn_check('sitemap', 'Could not inspect sitemap releases: ' . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private function checkLogErrors(): void
|
private function checkLogErrors(): void
|
||||||
{
|
{
|
||||||
$logFile = storage_path('logs/laravel.log');
|
$logFile = storage_path('logs/laravel.log');
|
||||||
|
|||||||
163
app/Console/Commands/ValidateWorldWebStoriesCommand.php
Normal file
163
app/Console/Commands/ValidateWorldWebStoriesCommand.php
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Models\WorldWebStory;
|
||||||
|
use App\Services\WebStories\WorldWebStoryValidationService;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Symfony\Component\Process\Process;
|
||||||
|
|
||||||
|
final class ValidateWorldWebStoriesCommand extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'skinbase:webstories:validate
|
||||||
|
{story? : Story ID or slug}
|
||||||
|
{--published : Limit batch mode to published stories}
|
||||||
|
{--visible : Limit batch mode to publicly visible stories}
|
||||||
|
{--limit=100 : Maximum stories to validate in batch mode}
|
||||||
|
{--amp : Also run amphtml-validator against the public story URL}
|
||||||
|
{--fail-warnings : Treat validation warnings as failures}';
|
||||||
|
|
||||||
|
protected $description = 'Validate World Web Stories for publish safety and optional AMP validity';
|
||||||
|
|
||||||
|
public function handle(WorldWebStoryValidationService $validation): int
|
||||||
|
{
|
||||||
|
$storyKey = $this->argument('story');
|
||||||
|
|
||||||
|
if ($storyKey !== null && trim((string) $storyKey) !== '') {
|
||||||
|
$story = $this->resolveStory((string) $storyKey);
|
||||||
|
|
||||||
|
if (! $story instanceof WorldWebStory) {
|
||||||
|
$this->error(sprintf('Web story [%s] was not found.', (string) $storyKey));
|
||||||
|
|
||||||
|
return self::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->validateOne($validation, $story);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->validateBatch($validation, max(1, (int) $this->option('limit')));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function validateOne(WorldWebStoryValidationService $validation, WorldWebStory $story): int
|
||||||
|
{
|
||||||
|
$result = $validation->validate($story);
|
||||||
|
$ampErrors = $this->ampErrors($story);
|
||||||
|
|
||||||
|
$this->line(sprintf('Story [%d] %s', (int) $story->id, (string) $story->slug));
|
||||||
|
|
||||||
|
foreach ((array) $result['warnings'] as $warning) {
|
||||||
|
$this->warn(' - ' . $warning);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ((array) $result['errors'] as $error) {
|
||||||
|
$this->error(' - ' . $error);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($ampErrors as $ampError) {
|
||||||
|
$this->error(' - AMP: ' . $ampError);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($result['valid'] && $ampErrors === []) {
|
||||||
|
$this->info('Validation passed.');
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function validateBatch(WorldWebStoryValidationService $validation, int $limit): int
|
||||||
|
{
|
||||||
|
$processed = 0;
|
||||||
|
$failed = 0;
|
||||||
|
|
||||||
|
$this->storyQuery()
|
||||||
|
->limit($limit)
|
||||||
|
->get()
|
||||||
|
->each(function (WorldWebStory $story) use ($validation, &$processed, &$failed): void {
|
||||||
|
$processed++;
|
||||||
|
$result = $validation->validate($story);
|
||||||
|
$ampErrors = $this->ampErrors($story);
|
||||||
|
$warningsFail = (bool) $this->option('fail-warnings') && count((array) $result['warnings']) > 0;
|
||||||
|
$hasFailure = ! $result['valid'] || $warningsFail || $ampErrors !== [];
|
||||||
|
|
||||||
|
if ($hasFailure) {
|
||||||
|
$failed++;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->line(sprintf('[%d] %s -> %s', (int) $story->id, (string) $story->slug, $hasFailure ? 'invalid' : 'valid'));
|
||||||
|
|
||||||
|
foreach ((array) $result['warnings'] as $warning) {
|
||||||
|
$this->warn(' - ' . $warning);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ((array) $result['errors'] as $error) {
|
||||||
|
$this->error(' - ' . $error);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($ampErrors as $ampError) {
|
||||||
|
$this->error(' - AMP: ' . $ampError);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->info(sprintf('Done. processed=%d failed=%d', $processed, $failed));
|
||||||
|
|
||||||
|
return $failed === 0 ? self::SUCCESS : self::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function storyQuery()
|
||||||
|
{
|
||||||
|
return WorldWebStory::query()
|
||||||
|
->when((bool) $this->option('published'), fn ($query) => $query->published())
|
||||||
|
->when((bool) $this->option('visible'), fn ($query) => $query->visible())
|
||||||
|
->orderByDesc('published_at')
|
||||||
|
->orderByDesc('id');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list<string>
|
||||||
|
*/
|
||||||
|
private function ampErrors(WorldWebStory $story): array
|
||||||
|
{
|
||||||
|
if (! (bool) $this->option('amp')) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $story->exists || ! $story->publicUrl()) {
|
||||||
|
return ['Story has no public URL to validate.'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$probe = new Process(['npx', 'amphtml-validator', '--version'], base_path(), null, null, 60);
|
||||||
|
$probe->run();
|
||||||
|
|
||||||
|
if (! $probe->isSuccessful()) {
|
||||||
|
return ['amphtml-validator is not available via npx.'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$process = new Process(['npx', 'amphtml-validator', $story->publicUrl()], base_path(), null, null, 120);
|
||||||
|
$process->run();
|
||||||
|
|
||||||
|
if ($process->isSuccessful()) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$output = trim($process->getErrorOutput() ?: $process->getOutput());
|
||||||
|
|
||||||
|
if ($output === '') {
|
||||||
|
return ['AMP validator failed without output.'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$lines = preg_split('/\r\n|\r|\n/', $output);
|
||||||
|
|
||||||
|
return $lines === false || $lines === [] ? ['AMP validator failed.'] : $lines;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function resolveStory(string $value): ?WorldWebStory
|
||||||
|
{
|
||||||
|
return WorldWebStory::query()
|
||||||
|
->when(is_numeric($value), fn ($query) => $query->where('id', (int) $value), fn ($query) => $query->where('slug', $value))
|
||||||
|
->first();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,10 +31,13 @@ use App\Console\Commands\PublishScheduledArtworksCommand;
|
|||||||
use App\Console\Commands\PublishScheduledNewsCommand;
|
use App\Console\Commands\PublishScheduledNewsCommand;
|
||||||
use App\Console\Commands\PublishScheduledNovaCardsCommand;
|
use App\Console\Commands\PublishScheduledNovaCardsCommand;
|
||||||
use App\Console\Commands\BuildSitemapsCommand;
|
use App\Console\Commands\BuildSitemapsCommand;
|
||||||
|
use App\Console\Commands\BuildWorldWebStoryAssetsCommand;
|
||||||
use App\Console\Commands\ListSitemapReleasesCommand;
|
use App\Console\Commands\ListSitemapReleasesCommand;
|
||||||
|
use App\Console\Commands\GenerateWorldWebStoriesCommand;
|
||||||
use App\Console\Commands\PublishSitemapsCommand;
|
use App\Console\Commands\PublishSitemapsCommand;
|
||||||
use App\Console\Commands\RollbackSitemapReleaseCommand;
|
use App\Console\Commands\RollbackSitemapReleaseCommand;
|
||||||
use App\Console\Commands\SyncCollectionLifecycleCommand;
|
use App\Console\Commands\SyncCollectionLifecycleCommand;
|
||||||
|
use App\Console\Commands\ValidateWorldWebStoriesCommand;
|
||||||
use App\Console\Commands\ValidateSitemapsCommand;
|
use App\Console\Commands\ValidateSitemapsCommand;
|
||||||
use App\Console\Commands\AuditArtworkDownloadFilesCommand;
|
use App\Console\Commands\AuditArtworkDownloadFilesCommand;
|
||||||
use App\Console\Commands\InspectArtworkOriginalCommand;
|
use App\Console\Commands\InspectArtworkOriginalCommand;
|
||||||
@@ -58,6 +61,9 @@ class Kernel extends ConsoleKernel
|
|||||||
\App\Console\Commands\ResetAllUserPasswords::class,
|
\App\Console\Commands\ResetAllUserPasswords::class,
|
||||||
CleanupUploadsCommand::class,
|
CleanupUploadsCommand::class,
|
||||||
BuildSitemapsCommand::class,
|
BuildSitemapsCommand::class,
|
||||||
|
GenerateWorldWebStoriesCommand::class,
|
||||||
|
BuildWorldWebStoryAssetsCommand::class,
|
||||||
|
ValidateWorldWebStoriesCommand::class,
|
||||||
PublishSitemapsCommand::class,
|
PublishSitemapsCommand::class,
|
||||||
ListSitemapReleasesCommand::class,
|
ListSitemapReleasesCommand::class,
|
||||||
RollbackSitemapReleaseCommand::class,
|
RollbackSitemapReleaseCommand::class,
|
||||||
|
|||||||
@@ -0,0 +1,94 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Academy;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Services\Academy\AcademyAnalyticsContentResolver;
|
||||||
|
use App\Services\Academy\AcademyAnalyticsService;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsEventType;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
final class AcademyAnalyticsEventController extends Controller
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly AcademyAnalyticsService $analytics,
|
||||||
|
private readonly AcademyAnalyticsContentResolver $resolver,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(Request $request): JsonResponse
|
||||||
|
{
|
||||||
|
abort_unless((bool) config('academy.enabled', true), 404);
|
||||||
|
abort_unless($request->expectsJson() || $request->isJson(), 422);
|
||||||
|
|
||||||
|
$validated = $request->validate([
|
||||||
|
'event_type' => ['required', 'string', Rule::in(AcademyAnalyticsEventType::values())],
|
||||||
|
'content_type' => ['nullable', 'string', Rule::in(AcademyAnalyticsContentType::values())],
|
||||||
|
'content_id' => ['nullable', 'integer', 'min:1'],
|
||||||
|
'metadata' => ['nullable', 'array'],
|
||||||
|
'visitor_id' => ['nullable', 'string', 'max:120'],
|
||||||
|
'session_id' => ['nullable', 'string', 'max:120'],
|
||||||
|
'url' => ['nullable', 'string', 'max:4000'],
|
||||||
|
'route_name' => ['nullable', 'string', 'max:255'],
|
||||||
|
'referrer' => ['nullable', 'string', 'max:4000'],
|
||||||
|
'utm_source' => ['nullable', 'string', 'max:255'],
|
||||||
|
'utm_medium' => ['nullable', 'string', 'max:255'],
|
||||||
|
'utm_campaign' => ['nullable', 'string', 'max:255'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (isset($validated['metadata']) && strlen((string) json_encode($validated['metadata'])) > 8192) {
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Metadata payload is too large.',
|
||||||
|
], 422);
|
||||||
|
}
|
||||||
|
|
||||||
|
$contentType = $validated['content_type'] ?? null;
|
||||||
|
$contentId = $validated['content_id'] ?? null;
|
||||||
|
|
||||||
|
if ($contentType !== null && AcademyAnalyticsContentType::requiresContentId($contentType) && $contentId === null) {
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'content_id is required for this content type.',
|
||||||
|
], 422);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($contentType !== null && $contentId !== null && ! $this->resolver->exists($contentType, (int) $contentId)) {
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Unknown Academy analytics content target.',
|
||||||
|
], 422);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (($validated['event_type'] ?? null) === AcademyAnalyticsEventType::SEARCH_RESULT_CLICK) {
|
||||||
|
validator([
|
||||||
|
'content_type' => $contentType,
|
||||||
|
'content_id' => $contentId,
|
||||||
|
'metadata' => $validated['metadata'] ?? [],
|
||||||
|
], [
|
||||||
|
'content_type' => ['required', 'string', Rule::in([
|
||||||
|
AcademyAnalyticsContentType::PROMPT,
|
||||||
|
AcademyAnalyticsContentType::LESSON,
|
||||||
|
AcademyAnalyticsContentType::COURSE,
|
||||||
|
AcademyAnalyticsContentType::PROMPT_PACK,
|
||||||
|
AcademyAnalyticsContentType::CHALLENGE,
|
||||||
|
])],
|
||||||
|
'content_id' => ['required', 'integer', 'min:1'],
|
||||||
|
'metadata.query' => ['required', 'string', 'max:120'],
|
||||||
|
'metadata.normalized_query' => ['required', 'string', 'max:120'],
|
||||||
|
'metadata.results_count' => ['required', 'integer', 'min:0'],
|
||||||
|
'metadata.position' => ['nullable', 'integer', 'min:1'],
|
||||||
|
'metadata.source' => ['nullable', 'string', 'max:120'],
|
||||||
|
'metadata.filters' => ['nullable', 'array'],
|
||||||
|
])->validate();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->analytics->track($validated, $request->user(), $request);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'ok' => true,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
422
app/Http/Controllers/Academy/AcademyBillingController.php
Normal file
422
app/Http/Controllers/Academy/AcademyBillingController.php
Normal file
@@ -0,0 +1,422 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Academy;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Services\Academy\AcademyAccessService;
|
||||||
|
use App\Services\Academy\AcademyBillingPlanService;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
|
||||||
|
use App\Support\Seo\SeoFactory;
|
||||||
|
use Laravel\Cashier\Checkout;
|
||||||
|
use Laravel\Cashier\Subscription;
|
||||||
|
|
||||||
|
final class AcademyBillingController extends Controller
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly AcademyAccessService $access,
|
||||||
|
private readonly AcademyBillingPlanService $plans,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function pricing(\Illuminate\Http\Request $request): \Inertia\Response
|
||||||
|
{
|
||||||
|
\abort_unless((bool) \config('academy.enabled', true), 404);
|
||||||
|
|
||||||
|
$this->plans->assertConfigured();
|
||||||
|
|
||||||
|
/** @var User|null $user */
|
||||||
|
$user = $request->user();
|
||||||
|
$canonical = \route('academy.pricing');
|
||||||
|
$seo = \app(SeoFactory::class)
|
||||||
|
->collectionPage(
|
||||||
|
'Skinbase AI Academy Pricing — Skinbase',
|
||||||
|
'Compare Skinbase AI Academy Creator and Pro tiers, start free, and manage premium access through Stripe billing.',
|
||||||
|
$canonical,
|
||||||
|
)
|
||||||
|
->toArray();
|
||||||
|
|
||||||
|
$seo['og_type'] = 'website';
|
||||||
|
$activePlan = $user instanceof User ? $this->activePlan($user) : null;
|
||||||
|
|
||||||
|
return \Inertia\Inertia::render('Academy/Billing/Pricing', [
|
||||||
|
'seo' => $seo,
|
||||||
|
'billingEnabled' => $this->plans->enabled(),
|
||||||
|
'currentTier' => $this->access->currentTier($user),
|
||||||
|
'isSubscribed' => $user instanceof User ? $this->access->hasActiveAcademySubscription($user) : false,
|
||||||
|
'activePlanKey' => $activePlan['key'] ?? null,
|
||||||
|
'activePlanLabel' => $activePlan['label'] ?? null,
|
||||||
|
'catalog' => $this->catalog(),
|
||||||
|
'links' => [
|
||||||
|
'login' => \route('login'),
|
||||||
|
'pricing' => \route('academy.pricing'),
|
||||||
|
'billingAccount' => $user ? \route('academy.billing.account') : null,
|
||||||
|
'checkout' => $user ? \route('academy.billing.checkout') : null,
|
||||||
|
],
|
||||||
|
'analytics' => [
|
||||||
|
'enabled' => true,
|
||||||
|
'contentType' => AcademyAnalyticsContentType::UPGRADE,
|
||||||
|
'contentId' => null,
|
||||||
|
'eventUrl' => \route('academy.analytics.events.store'),
|
||||||
|
'pageName' => 'academy_billing_pricing',
|
||||||
|
'isPremium' => false,
|
||||||
|
'isGuest' => $user === null,
|
||||||
|
'isSubscriber' => $user?->hasAcademyCreatorAccess() || $user?->hasAcademyProAccess(),
|
||||||
|
],
|
||||||
|
])->rootView('collections');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function checkout(\Illuminate\Http\Request $request): Checkout|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
|
||||||
|
{
|
||||||
|
\abort_unless((bool) \config('academy.enabled', true), 404);
|
||||||
|
|
||||||
|
if (! $this->plans->enabled()) {
|
||||||
|
return $this->billingDisabledResponse($request, 'Academy billing is not enabled yet.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->plans->assertConfigured();
|
||||||
|
|
||||||
|
$user = $request->user();
|
||||||
|
|
||||||
|
if (! $user instanceof User) {
|
||||||
|
return \redirect()->route('login');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($user->email_verified_at === null) {
|
||||||
|
throw \Illuminate\Validation\ValidationException::withMessages([
|
||||||
|
'plan' => 'Verify your email address before starting Academy billing.',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$validated = $request->validate([
|
||||||
|
'plan' => ['required', 'string'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$plan = $this->plans->plan((string) $validated['plan']);
|
||||||
|
|
||||||
|
if ($plan === null) {
|
||||||
|
throw \Illuminate\Validation\ValidationException::withMessages([
|
||||||
|
'plan' => 'Select a valid Academy billing plan.',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! ($plan['configured'] ?? false)) {
|
||||||
|
return $this->missingPriceIdResponse($request, (string) $plan['key']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! ($plan['price_id_valid'] ?? false)) {
|
||||||
|
return $this->invalidPriceIdResponse($request, (string) $plan['key']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->access->hasActiveAcademySubscription($user)) {
|
||||||
|
return \redirect()->route('academy.billing.portal');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return $user
|
||||||
|
->newSubscription($this->plans->subscriptionName(), (string) $plan['stripe_price_id'])
|
||||||
|
->withMetadata([
|
||||||
|
'skinbase_module' => 'academy',
|
||||||
|
'user_id' => (string) $user->id,
|
||||||
|
'academy_plan' => (string) $plan['key'],
|
||||||
|
'academy_tier' => (string) $plan['tier'],
|
||||||
|
])
|
||||||
|
->checkout([
|
||||||
|
'success_url' => \route('academy.billing.success').'?session_id={CHECKOUT_SESSION_ID}',
|
||||||
|
'cancel_url' => \route('academy.billing.cancel'),
|
||||||
|
'allow_promotion_codes' => true,
|
||||||
|
'metadata' => [
|
||||||
|
'skinbase_module' => 'academy',
|
||||||
|
'user_id' => (string) $user->id,
|
||||||
|
'academy_plan' => (string) $plan['key'],
|
||||||
|
'academy_tier' => (string) $plan['tier'],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
} catch (\Throwable $exception) {
|
||||||
|
\report($exception);
|
||||||
|
|
||||||
|
return $this->checkoutErrorResponse($request, $exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function checkoutLegacy(\Illuminate\Http\Request $request, string $plan): Checkout|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
|
||||||
|
{
|
||||||
|
$request->merge([
|
||||||
|
'plan' => $this->plans->normalizePlanKey($plan),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $this->checkout($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function portal(\Illuminate\Http\Request $request): \Illuminate\Http\RedirectResponse
|
||||||
|
{
|
||||||
|
\abort_unless((bool) \config('academy.enabled', true), 404);
|
||||||
|
\abort_unless($this->plans->enabled(), 404);
|
||||||
|
|
||||||
|
/** @var User|null $user */
|
||||||
|
$user = $request->user();
|
||||||
|
|
||||||
|
if (! $user instanceof User || \blank($user->stripe_id)) {
|
||||||
|
return \redirect()->route('academy.billing.account')->with('error', 'No Stripe billing profile is connected to this account yet.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $user->redirectToBillingPortal(\route('academy.billing.account'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function success(\Illuminate\Http\Request $request): \Inertia\Response
|
||||||
|
{
|
||||||
|
\abort_unless((bool) \config('academy.enabled', true), 404);
|
||||||
|
|
||||||
|
/** @var User|null $user */
|
||||||
|
$user = $request->user();
|
||||||
|
$currentTier = $this->access->currentTier($user);
|
||||||
|
|
||||||
|
return \Inertia\Inertia::render('Academy/Billing/Success', [
|
||||||
|
'message' => 'Payment is being confirmed. Your access will update automatically.',
|
||||||
|
'currentTier' => $currentTier,
|
||||||
|
'isSubscribed' => $user instanceof User ? $this->access->hasActiveAcademySubscription($user) : false,
|
||||||
|
'links' => [
|
||||||
|
'pricing' => \route('academy.pricing'),
|
||||||
|
'account' => $user ? \route('academy.billing.account') : null,
|
||||||
|
'academy' => \route('academy.index'),
|
||||||
|
],
|
||||||
|
'sessionId' => $request->query('session_id'),
|
||||||
|
])->rootView('collections');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function cancel(): \Inertia\Response
|
||||||
|
{
|
||||||
|
\abort_unless((bool) \config('academy.enabled', true), 404);
|
||||||
|
|
||||||
|
return \Inertia\Inertia::render('Academy/Billing/Cancel', [
|
||||||
|
'message' => 'Checkout was canceled. No payment was made.',
|
||||||
|
'links' => [
|
||||||
|
'pricing' => \route('academy.pricing'),
|
||||||
|
'academy' => \route('academy.index'),
|
||||||
|
],
|
||||||
|
])->rootView('collections');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function account(\Illuminate\Http\Request $request): \Inertia\Response
|
||||||
|
{
|
||||||
|
\abort_unless((bool) \config('academy.enabled', true), 404);
|
||||||
|
\abort_unless($this->plans->enabled(), 404);
|
||||||
|
|
||||||
|
/** @var User $user */
|
||||||
|
$user = $request->user();
|
||||||
|
$subscription = $this->academySubscription($user);
|
||||||
|
|
||||||
|
$activePlan = $this->activePlan($user);
|
||||||
|
|
||||||
|
return \Inertia\Inertia::render('Academy/Billing/Account', [
|
||||||
|
'currentTier' => $this->access->currentTier($user),
|
||||||
|
'isSubscribed' => $this->access->hasActiveAcademySubscription($user),
|
||||||
|
'activePlan' => $activePlan ? [
|
||||||
|
'key' => $activePlan['key'],
|
||||||
|
'label' => $activePlan['label'],
|
||||||
|
'price_display' => $activePlan['price_display'] ?? null,
|
||||||
|
'tier' => $activePlan['tier'],
|
||||||
|
] : null,
|
||||||
|
'subscription' => $subscription ? [
|
||||||
|
'name' => $subscription->type,
|
||||||
|
'status' => $subscription->stripe_status,
|
||||||
|
'active' => $subscription->active(),
|
||||||
|
'onGracePeriod' => $subscription->onGracePeriod(),
|
||||||
|
'endsAt' => $subscription->ends_at?->toISOString(),
|
||||||
|
'priceIds' => $subscription->items->pluck('stripe_price')->filter()->values()->all(),
|
||||||
|
] : null,
|
||||||
|
'links' => [
|
||||||
|
'portal' => \route('academy.billing.portal'),
|
||||||
|
'pricing' => \route('academy.pricing'),
|
||||||
|
'academy' => \route('academy.index'),
|
||||||
|
],
|
||||||
|
])->rootView('collections');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, array<string, mixed>>
|
||||||
|
*/
|
||||||
|
private function catalog(): array
|
||||||
|
{
|
||||||
|
$definitions = [
|
||||||
|
'creator' => [
|
||||||
|
'name' => 'Creator',
|
||||||
|
'description' => 'Entry premium access for prompt systems, creator lessons, and saved Academy workflows.',
|
||||||
|
'badge' => 'Paid',
|
||||||
|
'featured' => false,
|
||||||
|
'features' => [
|
||||||
|
'Creator lessons and walkthroughs',
|
||||||
|
'Full Creator prompt templates',
|
||||||
|
'Prompt save and reuse flows',
|
||||||
|
'Upgrade path into Pro later',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'pro' => [
|
||||||
|
'name' => 'Pro',
|
||||||
|
'description' => 'Full Academy access across Creator and Pro lessons, prompts, and future premium drops.',
|
||||||
|
'badge' => 'Recommended',
|
||||||
|
'featured' => true,
|
||||||
|
'features' => [
|
||||||
|
'Everything in Creator',
|
||||||
|
'Advanced Pro lessons and prompt systems',
|
||||||
|
'Priority access to future Academy premium features',
|
||||||
|
'Stripe billing portal for upgrades and invoices',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
return collect($definitions)
|
||||||
|
->map(function (array $definition, string $tier): array {
|
||||||
|
$plan = $this->plans->plan($tier.'_monthly');
|
||||||
|
|
||||||
|
$plans = $plan !== null ? [[
|
||||||
|
'key' => $plan['key'],
|
||||||
|
'label' => $plan['label'],
|
||||||
|
'interval' => $plan['interval'],
|
||||||
|
'amount' => $plan['amount'],
|
||||||
|
'currency' => $plan['currency'],
|
||||||
|
'price_display' => $plan['price_display'],
|
||||||
|
'configured' => $plan['configured'],
|
||||||
|
'price_id_valid' => $plan['price_id_valid'],
|
||||||
|
]] : [];
|
||||||
|
|
||||||
|
return [
|
||||||
|
'tier' => $tier,
|
||||||
|
'name' => $definition['name'],
|
||||||
|
'description' => $definition['description'],
|
||||||
|
'badge' => $definition['badge'],
|
||||||
|
'featured' => $definition['featured'],
|
||||||
|
'features' => $definition['features'],
|
||||||
|
'plans' => $plans,
|
||||||
|
];
|
||||||
|
})
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function academySubscription(User $user): ?Subscription
|
||||||
|
{
|
||||||
|
$subscription = $user->subscription($this->plans->subscriptionName());
|
||||||
|
|
||||||
|
return $subscription instanceof Subscription
|
||||||
|
? $subscription->loadMissing('items')
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>|null
|
||||||
|
*/
|
||||||
|
private function activePlan(User $user): ?array
|
||||||
|
{
|
||||||
|
$subscription = $this->academySubscription($user);
|
||||||
|
|
||||||
|
if (! $subscription instanceof Subscription || (! $subscription->active() && ! $subscription->onGracePeriod())) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$matchedPlan = null;
|
||||||
|
|
||||||
|
foreach ($subscription->items as $item) {
|
||||||
|
$priceId = trim((string) $item->stripe_price);
|
||||||
|
|
||||||
|
if ($priceId === '') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$plan = $this->plans->planForPriceId($priceId);
|
||||||
|
|
||||||
|
if ($plan === null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($matchedPlan === null || $this->planRank((string) $plan['tier']) > $this->planRank((string) $matchedPlan['tier'])) {
|
||||||
|
$matchedPlan = $plan;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($matchedPlan !== null) {
|
||||||
|
return $matchedPlan;
|
||||||
|
}
|
||||||
|
|
||||||
|
$fallbackPriceId = trim((string) $subscription->stripe_price);
|
||||||
|
|
||||||
|
return $fallbackPriceId !== '' ? $this->plans->planForPriceId($fallbackPriceId) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function planRank(string $tier): int
|
||||||
|
{
|
||||||
|
return match (strtolower(trim($tier))) {
|
||||||
|
'admin' => 40,
|
||||||
|
'pro' => 30,
|
||||||
|
'creator' => 20,
|
||||||
|
default => 10,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private function billingDisabledResponse(\Illuminate\Http\Request $request, string $message): \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
|
||||||
|
{
|
||||||
|
$payload = [
|
||||||
|
'ok' => false,
|
||||||
|
'code' => 'academy_payments_disabled',
|
||||||
|
'message' => $message,
|
||||||
|
];
|
||||||
|
|
||||||
|
if ($request->expectsJson()) {
|
||||||
|
return \response()->json($payload, 423);
|
||||||
|
}
|
||||||
|
|
||||||
|
return \redirect()->route('academy.pricing')->with('error', $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function missingPriceIdResponse(\Illuminate\Http\Request $request, string $planKey): \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
|
||||||
|
{
|
||||||
|
$message = 'The selected Academy plan is not configured yet. Please try again later.';
|
||||||
|
|
||||||
|
if ($request->expectsJson()) {
|
||||||
|
return \response()->json([
|
||||||
|
'ok' => false,
|
||||||
|
'code' => 'academy_billing_price_missing',
|
||||||
|
'message' => $message,
|
||||||
|
'plan' => $planKey,
|
||||||
|
], 422);
|
||||||
|
}
|
||||||
|
|
||||||
|
return \redirect()->route('academy.pricing')->with('error', $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function invalidPriceIdResponse(\Illuminate\Http\Request $request, string $planKey): \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
|
||||||
|
{
|
||||||
|
$message = 'The selected Academy plan is misconfigured. Please contact support before continuing.';
|
||||||
|
|
||||||
|
if ($request->expectsJson()) {
|
||||||
|
return \response()->json([
|
||||||
|
'ok' => false,
|
||||||
|
'code' => 'academy_billing_price_invalid',
|
||||||
|
'message' => $message,
|
||||||
|
'plan' => $planKey,
|
||||||
|
], 422);
|
||||||
|
}
|
||||||
|
|
||||||
|
return \redirect()->route('academy.pricing')->with('error', $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function checkoutErrorResponse(\Illuminate\Http\Request $request, \Throwable $exception): \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
|
||||||
|
{
|
||||||
|
$message = 'Academy checkout could not be started right now.';
|
||||||
|
|
||||||
|
if (app()->hasDebugModeEnabled() && trim($exception->getMessage()) !== '') {
|
||||||
|
$message .= ' '.$exception->getMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->expectsJson()) {
|
||||||
|
return \response()->json([
|
||||||
|
'ok' => false,
|
||||||
|
'code' => 'academy_billing_checkout_failed',
|
||||||
|
'message' => $message,
|
||||||
|
], 422);
|
||||||
|
}
|
||||||
|
|
||||||
|
return \redirect()->route('academy.pricing')->with('error', $message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,8 @@ namespace App\Http\Controllers\Academy;
|
|||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Models\AcademyChallenge;
|
use App\Models\AcademyChallenge;
|
||||||
use App\Services\Academy\AcademyAccessService;
|
use App\Services\Academy\AcademyAccessService;
|
||||||
|
use App\Services\Academy\AcademyInteractionService;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
|
||||||
use App\Support\Seo\SeoFactory;
|
use App\Support\Seo\SeoFactory;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
@@ -15,7 +17,10 @@ use Inertia\Response;
|
|||||||
|
|
||||||
final class AcademyChallengeController extends Controller
|
final class AcademyChallengeController extends Controller
|
||||||
{
|
{
|
||||||
public function __construct(private readonly AcademyAccessService $access)
|
public function __construct(
|
||||||
|
private readonly AcademyAccessService $access,
|
||||||
|
private readonly AcademyInteractionService $interactions,
|
||||||
|
)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,6 +54,16 @@ final class AcademyChallengeController extends Controller
|
|||||||
'filters' => [],
|
'filters' => [],
|
||||||
'categories' => [],
|
'categories' => [],
|
||||||
'pricingUrl' => route('academy.pricing'),
|
'pricingUrl' => route('academy.pricing'),
|
||||||
|
'analytics' => [
|
||||||
|
'enabled' => true,
|
||||||
|
'contentType' => null,
|
||||||
|
'contentId' => null,
|
||||||
|
'eventUrl' => route('academy.analytics.events.store'),
|
||||||
|
'pageName' => 'academy_challenges_index',
|
||||||
|
'isPremium' => false,
|
||||||
|
'isGuest' => $request->user() === null,
|
||||||
|
'isSubscriber' => $request->user()?->hasAcademyCreatorAccess() || $request->user()?->hasAcademyProAccess(),
|
||||||
|
],
|
||||||
])->rootView('collections');
|
])->rootView('collections');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,12 +101,31 @@ final class AcademyChallengeController extends Controller
|
|||||||
$challenge->cover_image,
|
$challenge->cover_image,
|
||||||
)->toArray();
|
)->toArray();
|
||||||
|
|
||||||
|
$interaction = $this->interactions->getInteractionState($request->user(), AcademyAnalyticsContentType::CHALLENGE, (int) $challenge->id);
|
||||||
|
|
||||||
return Inertia::render('Academy/Show', [
|
return Inertia::render('Academy/Show', [
|
||||||
'pageType' => 'challenge',
|
'pageType' => 'challenge',
|
||||||
'item' => $payload,
|
'item' => $payload,
|
||||||
'seo' => $seo,
|
'seo' => $seo,
|
||||||
'pricingUrl' => route('academy.pricing'),
|
'pricingUrl' => route('academy.pricing'),
|
||||||
'submitUrl' => $request->user() ? route('academy.challenges.submit', ['slug' => $challenge->slug]) : null,
|
'submitUrl' => $request->user() ? route('academy.challenges.submit', ['slug' => $challenge->slug]) : null,
|
||||||
|
'interaction' => $interaction,
|
||||||
|
'interactionRoutes' => [
|
||||||
|
'like' => route('academy.interactions.like'),
|
||||||
|
'save' => route('academy.interactions.save'),
|
||||||
|
],
|
||||||
|
'loginUrl' => route('login'),
|
||||||
|
'analytics' => [
|
||||||
|
'enabled' => true,
|
||||||
|
'contentType' => AcademyAnalyticsContentType::CHALLENGE,
|
||||||
|
'contentId' => (int) $challenge->id,
|
||||||
|
'eventUrl' => route('academy.analytics.events.store'),
|
||||||
|
'pageName' => 'academy_challenge_show',
|
||||||
|
'isPremium' => (string) ($challenge->access_level ?? 'free') !== 'free',
|
||||||
|
'isGuest' => $request->user() === null,
|
||||||
|
'isSubscriber' => $request->user()?->hasAcademyCreatorAccess() || $request->user()?->hasAcademyProAccess(),
|
||||||
|
'isLocked' => (bool) ($payload['locked'] ?? false),
|
||||||
|
],
|
||||||
])->rootView('collections');
|
])->rootView('collections');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,9 +8,11 @@ use App\Http\Controllers\Controller;
|
|||||||
use App\Models\AcademyCourse;
|
use App\Models\AcademyCourse;
|
||||||
use App\Models\AcademyCourseLesson;
|
use App\Models\AcademyCourseLesson;
|
||||||
use App\Services\Academy\AcademyAccessService;
|
use App\Services\Academy\AcademyAccessService;
|
||||||
|
use App\Services\Academy\AcademyInteractionService;
|
||||||
use App\Services\Academy\AcademyCacheService;
|
use App\Services\Academy\AcademyCacheService;
|
||||||
use App\Services\Academy\AcademyCourseNavigationService;
|
use App\Services\Academy\AcademyCourseNavigationService;
|
||||||
use App\Services\Academy\AcademyCourseProgressService;
|
use App\Services\Academy\AcademyCourseProgressService;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
|
||||||
use App\Support\Seo\SeoFactory;
|
use App\Support\Seo\SeoFactory;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Inertia\Inertia;
|
use Inertia\Inertia;
|
||||||
@@ -23,6 +25,7 @@ final class AcademyCourseController extends Controller
|
|||||||
private readonly AcademyCacheService $cache,
|
private readonly AcademyCacheService $cache,
|
||||||
private readonly AcademyCourseNavigationService $navigation,
|
private readonly AcademyCourseNavigationService $navigation,
|
||||||
private readonly AcademyCourseProgressService $progress,
|
private readonly AcademyCourseProgressService $progress,
|
||||||
|
private readonly AcademyInteractionService $interactions,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,6 +85,16 @@ final class AcademyCourseController extends Controller
|
|||||||
'featuredCourses' => $featuredCourses->all(),
|
'featuredCourses' => $featuredCourses->all(),
|
||||||
'filters' => $filters,
|
'filters' => $filters,
|
||||||
'pricingUrl' => route('academy.pricing'),
|
'pricingUrl' => route('academy.pricing'),
|
||||||
|
'analytics' => [
|
||||||
|
'enabled' => true,
|
||||||
|
'contentType' => null,
|
||||||
|
'contentId' => null,
|
||||||
|
'eventUrl' => route('academy.analytics.events.store'),
|
||||||
|
'pageName' => 'academy_courses_index',
|
||||||
|
'isPremium' => false,
|
||||||
|
'isGuest' => $request->user() === null,
|
||||||
|
'isSubscriber' => $request->user()?->hasAcademyCreatorAccess() || $request->user()?->hasAcademyProAccess(),
|
||||||
|
],
|
||||||
])->rootView('collections');
|
])->rootView('collections');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,6 +185,8 @@ final class AcademyCourseController extends Controller
|
|||||||
)
|
)
|
||||||
->toArray();
|
->toArray();
|
||||||
|
|
||||||
|
$interaction = $this->interactions->getInteractionState($request->user(), AcademyAnalyticsContentType::COURSE, (int) $course->id);
|
||||||
|
|
||||||
return Inertia::render('Academy/CoursesShow', [
|
return Inertia::render('Academy/CoursesShow', [
|
||||||
'seo' => $seo,
|
'seo' => $seo,
|
||||||
'course' => $coursePayload,
|
'course' => $coursePayload,
|
||||||
@@ -179,6 +194,23 @@ final class AcademyCourseController extends Controller
|
|||||||
'unsectionedLessons' => $unsectionedLessons,
|
'unsectionedLessons' => $unsectionedLessons,
|
||||||
'pricingUrl' => route('academy.pricing'),
|
'pricingUrl' => route('academy.pricing'),
|
||||||
'startUrl' => $request->user() ? route('academy.courses.start', ['course' => $course->slug]) : null,
|
'startUrl' => $request->user() ? route('academy.courses.start', ['course' => $course->slug]) : null,
|
||||||
|
'interaction' => $interaction,
|
||||||
|
'interactionRoutes' => [
|
||||||
|
'like' => route('academy.interactions.like'),
|
||||||
|
'save' => route('academy.interactions.save'),
|
||||||
|
],
|
||||||
|
'loginUrl' => route('login'),
|
||||||
|
'analytics' => [
|
||||||
|
'enabled' => true,
|
||||||
|
'contentType' => AcademyAnalyticsContentType::COURSE,
|
||||||
|
'contentId' => (int) $course->id,
|
||||||
|
'eventUrl' => route('academy.analytics.events.store'),
|
||||||
|
'pageName' => 'academy_course_show',
|
||||||
|
'isPremium' => (string) ($course->access_level ?? 'free') !== 'free',
|
||||||
|
'isGuest' => $request->user() === null,
|
||||||
|
'isSubscriber' => $request->user()?->hasAcademyCreatorAccess() || $request->user()?->hasAcademyProAccess(),
|
||||||
|
'isLocked' => false,
|
||||||
|
],
|
||||||
])->rootView('collections');
|
])->rootView('collections');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,14 +6,17 @@ namespace App\Http\Controllers\Academy;
|
|||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Models\AcademyCourse;
|
use App\Models\AcademyCourse;
|
||||||
|
use App\Services\Academy\AcademyProgressService;
|
||||||
use App\Services\Academy\AcademyCourseProgressService;
|
use App\Services\Academy\AcademyCourseProgressService;
|
||||||
use Illuminate\Http\RedirectResponse;
|
use Illuminate\Http\RedirectResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
final class AcademyCourseEnrollmentController extends Controller
|
final class AcademyCourseEnrollmentController extends Controller
|
||||||
{
|
{
|
||||||
public function __construct(private readonly AcademyCourseProgressService $progress)
|
public function __construct(
|
||||||
{
|
private readonly AcademyCourseProgressService $progress,
|
||||||
|
private readonly AcademyProgressService $academyProgress,
|
||||||
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function start(Request $request, AcademyCourse $course): RedirectResponse
|
public function start(Request $request, AcademyCourse $course): RedirectResponse
|
||||||
@@ -21,7 +24,7 @@ final class AcademyCourseEnrollmentController extends Controller
|
|||||||
abort_unless((bool) config('academy.enabled', true), 404);
|
abort_unless((bool) config('academy.enabled', true), 404);
|
||||||
abort_unless($course->isPublished(), 404);
|
abort_unless($course->isPublished(), 404);
|
||||||
|
|
||||||
$this->progress->markEnrollmentStarted($request->user(), $course);
|
$this->academyProgress->startCourse($request->user(), (int) $course->id, $request);
|
||||||
$continueLesson = $this->progress->getContinueLesson($request->user(), $course);
|
$continueLesson = $this->progress->getContinueLesson($request->user(), $course);
|
||||||
|
|
||||||
if ($continueLesson?->lesson) {
|
if ($continueLesson?->lesson) {
|
||||||
|
|||||||
@@ -8,8 +8,10 @@ use App\Http\Controllers\Controller;
|
|||||||
use App\Models\AcademyCourse;
|
use App\Models\AcademyCourse;
|
||||||
use App\Models\AcademyLesson;
|
use App\Models\AcademyLesson;
|
||||||
use App\Services\Academy\AcademyAccessService;
|
use App\Services\Academy\AcademyAccessService;
|
||||||
|
use App\Services\Academy\AcademyInteractionService;
|
||||||
use App\Services\Academy\AcademyCourseNavigationService;
|
use App\Services\Academy\AcademyCourseNavigationService;
|
||||||
use App\Services\Academy\AcademyCourseProgressService;
|
use App\Services\Academy\AcademyCourseProgressService;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
|
||||||
use App\Support\Seo\SeoFactory;
|
use App\Support\Seo\SeoFactory;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
@@ -22,6 +24,7 @@ final class AcademyCourseLessonController extends Controller
|
|||||||
private readonly AcademyAccessService $access,
|
private readonly AcademyAccessService $access,
|
||||||
private readonly AcademyCourseNavigationService $navigation,
|
private readonly AcademyCourseNavigationService $navigation,
|
||||||
private readonly AcademyCourseProgressService $progress,
|
private readonly AcademyCourseProgressService $progress,
|
||||||
|
private readonly AcademyInteractionService $interactions,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,6 +71,8 @@ final class AcademyCourseLessonController extends Controller
|
|||||||
(string) $course->title,
|
(string) $course->title,
|
||||||
)->toArray();
|
)->toArray();
|
||||||
|
|
||||||
|
$interaction = $this->interactions->getInteractionState($request->user(), AcademyAnalyticsContentType::LESSON, (int) $lesson->id);
|
||||||
|
|
||||||
return Inertia::render('Academy/Show', [
|
return Inertia::render('Academy/Show', [
|
||||||
'pageType' => 'lesson',
|
'pageType' => 'lesson',
|
||||||
'item' => $payload,
|
'item' => $payload,
|
||||||
@@ -79,6 +84,26 @@ final class AcademyCourseLessonController extends Controller
|
|||||||
'pricingUrl' => route('academy.pricing'),
|
'pricingUrl' => route('academy.pricing'),
|
||||||
'completeUrl' => $request->user() ? route('academy.lessons.complete', ['lesson' => $lesson->id]) : null,
|
'completeUrl' => $request->user() ? route('academy.lessons.complete', ['lesson' => $lesson->id]) : null,
|
||||||
'completed' => $request->user()?->academyLessonProgress()->where('lesson_id', $lesson->id)->whereNotNull('completed_at')->exists() ?? false,
|
'completed' => $request->user()?->academyLessonProgress()->where('lesson_id', $lesson->id)->whereNotNull('completed_at')->exists() ?? false,
|
||||||
|
'interaction' => $interaction,
|
||||||
|
'interactionRoutes' => [
|
||||||
|
'like' => route('academy.interactions.like'),
|
||||||
|
'save' => route('academy.interactions.save'),
|
||||||
|
],
|
||||||
|
'loginUrl' => route('login'),
|
||||||
|
'progressRoutes' => [
|
||||||
|
'startLesson' => $request->user() ? route('academy.progress.lesson.start') : null,
|
||||||
|
],
|
||||||
|
'analytics' => [
|
||||||
|
'enabled' => true,
|
||||||
|
'contentType' => AcademyAnalyticsContentType::LESSON,
|
||||||
|
'contentId' => (int) $lesson->id,
|
||||||
|
'eventUrl' => route('academy.analytics.events.store'),
|
||||||
|
'pageName' => 'academy_course_lesson_show',
|
||||||
|
'isPremium' => (string) ($payload['access_level'] ?? 'free') !== 'free',
|
||||||
|
'isGuest' => $request->user() === null,
|
||||||
|
'isSubscriber' => $request->user()?->hasAcademyCreatorAccess() || $request->user()?->hasAcademyProAccess(),
|
||||||
|
'isLocked' => (bool) ($payload['locked'] ?? false),
|
||||||
|
],
|
||||||
'courseContext' => [
|
'courseContext' => [
|
||||||
'id' => (int) $course->id,
|
'id' => (int) $course->id,
|
||||||
'title' => (string) $course->title,
|
'title' => (string) $course->title,
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ use App\Models\AcademyLesson;
|
|||||||
use App\Models\AcademyPromptTemplate;
|
use App\Models\AcademyPromptTemplate;
|
||||||
use App\Services\Academy\AcademyAccessService;
|
use App\Services\Academy\AcademyAccessService;
|
||||||
use App\Services\Academy\AcademyCacheService;
|
use App\Services\Academy\AcademyCacheService;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
|
||||||
use App\Support\Seo\SeoFactory;
|
use App\Support\Seo\SeoFactory;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Inertia\Inertia;
|
use Inertia\Inertia;
|
||||||
@@ -81,6 +82,16 @@ final class AcademyHomeController extends Controller
|
|||||||
'featuredLessons' => collect($home['featuredLessons'])->map(fn (AcademyLesson $lesson): array => $this->access->lessonPayload($lesson, $request->user()))->values()->all(),
|
'featuredLessons' => collect($home['featuredLessons'])->map(fn (AcademyLesson $lesson): array => $this->access->lessonPayload($lesson, $request->user()))->values()->all(),
|
||||||
'featuredPrompts' => collect($home['featuredPrompts'])->map(fn (AcademyPromptTemplate $prompt): array => $this->access->promptPayload($prompt, $request->user()))->values()->all(),
|
'featuredPrompts' => collect($home['featuredPrompts'])->map(fn (AcademyPromptTemplate $prompt): array => $this->access->promptPayload($prompt, $request->user()))->values()->all(),
|
||||||
'featuredChallenges' => collect($home['featuredChallenges'])->map(fn (AcademyChallenge $challenge): array => $this->access->challengePayload($challenge, $request->user(), true))->values()->all(),
|
'featuredChallenges' => collect($home['featuredChallenges'])->map(fn (AcademyChallenge $challenge): array => $this->access->challengePayload($challenge, $request->user(), true))->values()->all(),
|
||||||
|
'analytics' => [
|
||||||
|
'enabled' => true,
|
||||||
|
'contentType' => AcademyAnalyticsContentType::HOME,
|
||||||
|
'contentId' => null,
|
||||||
|
'eventUrl' => route('academy.analytics.events.store'),
|
||||||
|
'pageName' => 'academy_home',
|
||||||
|
'isPremium' => false,
|
||||||
|
'isGuest' => $request->user() === null,
|
||||||
|
'isSubscriber' => $request->user()?->hasAcademyCreatorAccess() || $request->user()?->hasAcademyProAccess(),
|
||||||
|
],
|
||||||
])->rootView('collections');
|
])->rootView('collections');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Academy;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Services\Academy\AcademyInteractionService;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
|
final class AcademyInteractionController extends Controller
|
||||||
|
{
|
||||||
|
public function __construct(private readonly AcademyInteractionService $interactions)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function like(Request $request): JsonResponse
|
||||||
|
{
|
||||||
|
abort_unless((bool) config('academy.enabled', true), 404);
|
||||||
|
|
||||||
|
$validated = $this->validatePayload($request);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$payload = $this->interactions->toggleLike($request->user(), (string) $validated['content_type'], (int) $validated['content_id'], $request);
|
||||||
|
} catch (InvalidArgumentException $exception) {
|
||||||
|
return response()->json(['message' => $exception->getMessage()], 422);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json($payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function save(Request $request): JsonResponse
|
||||||
|
{
|
||||||
|
abort_unless((bool) config('academy.enabled', true), 404);
|
||||||
|
|
||||||
|
$validated = $this->validatePayload($request);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$payload = $this->interactions->toggleSave($request->user(), (string) $validated['content_type'], (int) $validated['content_id'], $request);
|
||||||
|
} catch (InvalidArgumentException $exception) {
|
||||||
|
return response()->json(['message' => $exception->getMessage()], 422);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json($payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
private function validatePayload(Request $request): array
|
||||||
|
{
|
||||||
|
return $request->validate([
|
||||||
|
'content_type' => ['required', 'string', Rule::in([
|
||||||
|
AcademyAnalyticsContentType::PROMPT,
|
||||||
|
AcademyAnalyticsContentType::LESSON,
|
||||||
|
AcademyAnalyticsContentType::COURSE,
|
||||||
|
AcademyAnalyticsContentType::PROMPT_PACK,
|
||||||
|
AcademyAnalyticsContentType::CHALLENGE,
|
||||||
|
])],
|
||||||
|
'content_id' => ['required', 'integer', 'min:1'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,7 +8,10 @@ use App\Http\Controllers\Controller;
|
|||||||
use App\Models\AcademyCourse;
|
use App\Models\AcademyCourse;
|
||||||
use App\Models\AcademyLesson;
|
use App\Models\AcademyLesson;
|
||||||
use App\Services\Academy\AcademyAccessService;
|
use App\Services\Academy\AcademyAccessService;
|
||||||
|
use App\Services\Academy\AcademyAnalyticsService;
|
||||||
use App\Services\Academy\AcademyCacheService;
|
use App\Services\Academy\AcademyCacheService;
|
||||||
|
use App\Services\Academy\AcademyInteractionService;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
|
||||||
use App\Support\Seo\SeoFactory;
|
use App\Support\Seo\SeoFactory;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
@@ -20,6 +23,8 @@ final class AcademyLessonController extends Controller
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly AcademyAccessService $access,
|
private readonly AcademyAccessService $access,
|
||||||
private readonly AcademyCacheService $cache,
|
private readonly AcademyCacheService $cache,
|
||||||
|
private readonly AcademyAnalyticsService $analytics,
|
||||||
|
private readonly AcademyInteractionService $interactions,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function index(Request $request): Response
|
public function index(Request $request): Response
|
||||||
@@ -56,6 +61,10 @@ final class AcademyLessonController extends Controller
|
|||||||
$lessons = $query->paginate(12)->withQueryString();
|
$lessons = $query->paginate(12)->withQueryString();
|
||||||
$lessons->getCollection()->transform(fn (AcademyLesson $lesson): array => $this->access->lessonPayload($lesson, $request->user()));
|
$lessons->getCollection()->transform(fn (AcademyLesson $lesson): array => $this->access->lessonPayload($lesson, $request->user()));
|
||||||
|
|
||||||
|
if (filled($filters['q'] ?? null)) {
|
||||||
|
$this->analytics->trackSearch((string) $filters['q'], (int) $lessons->total(), array_filter($filters), $request);
|
||||||
|
}
|
||||||
|
|
||||||
$seo = app(SeoFactory::class)
|
$seo = app(SeoFactory::class)
|
||||||
->collectionListing(
|
->collectionListing(
|
||||||
'Academy Lessons — Skinbase',
|
'Academy Lessons — Skinbase',
|
||||||
@@ -73,6 +82,20 @@ final class AcademyLessonController extends Controller
|
|||||||
'filters' => $filters,
|
'filters' => $filters,
|
||||||
'categories' => $this->cache->categoriesByType('lesson'),
|
'categories' => $this->cache->categoriesByType('lesson'),
|
||||||
'pricingUrl' => route('academy.pricing'),
|
'pricingUrl' => route('academy.pricing'),
|
||||||
|
'analytics' => [
|
||||||
|
'enabled' => true,
|
||||||
|
'contentType' => filled($filters['q'] ?? null) ? AcademyAnalyticsContentType::SEARCH : null,
|
||||||
|
'contentId' => null,
|
||||||
|
'eventUrl' => route('academy.analytics.events.store'),
|
||||||
|
'pageName' => 'academy_lessons_index',
|
||||||
|
'search' => filled($filters['q'] ?? null) ? [
|
||||||
|
'query' => (string) $filters['q'],
|
||||||
|
'resultsCount' => (int) $lessons->total(),
|
||||||
|
] : null,
|
||||||
|
'isPremium' => false,
|
||||||
|
'isGuest' => $request->user() === null,
|
||||||
|
'isSubscriber' => $request->user()?->hasAcademyCreatorAccess() || $request->user()?->hasAcademyProAccess(),
|
||||||
|
],
|
||||||
])->rootView('collections');
|
])->rootView('collections');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,6 +171,8 @@ final class AcademyLessonController extends Controller
|
|||||||
(string) ($lesson->series_name ?: $lesson->category?->name ?: 'Academy'),
|
(string) ($lesson->series_name ?: $lesson->category?->name ?: 'Academy'),
|
||||||
)->toArray();
|
)->toArray();
|
||||||
|
|
||||||
|
$interaction = $this->interactions->getInteractionState($request->user(), AcademyAnalyticsContentType::LESSON, (int) $lesson->id);
|
||||||
|
|
||||||
return Inertia::render('Academy/Show', [
|
return Inertia::render('Academy/Show', [
|
||||||
'pageType' => 'lesson',
|
'pageType' => 'lesson',
|
||||||
'item' => $payload,
|
'item' => $payload,
|
||||||
@@ -159,6 +184,26 @@ final class AcademyLessonController extends Controller
|
|||||||
'pricingUrl' => route('academy.pricing'),
|
'pricingUrl' => route('academy.pricing'),
|
||||||
'completeUrl' => $request->user() ? route('academy.lessons.complete', ['lesson' => $lesson->id]) : null,
|
'completeUrl' => $request->user() ? route('academy.lessons.complete', ['lesson' => $lesson->id]) : null,
|
||||||
'completed' => $request->user()?->academyLessonProgress()->where('lesson_id', $lesson->id)->whereNotNull('completed_at')->exists() ?? false,
|
'completed' => $request->user()?->academyLessonProgress()->where('lesson_id', $lesson->id)->whereNotNull('completed_at')->exists() ?? false,
|
||||||
|
'interaction' => $interaction,
|
||||||
|
'interactionRoutes' => [
|
||||||
|
'like' => route('academy.interactions.like'),
|
||||||
|
'save' => route('academy.interactions.save'),
|
||||||
|
],
|
||||||
|
'loginUrl' => route('login'),
|
||||||
|
'progressRoutes' => [
|
||||||
|
'startLesson' => $request->user() ? route('academy.progress.lesson.start') : null,
|
||||||
|
],
|
||||||
|
'analytics' => [
|
||||||
|
'enabled' => true,
|
||||||
|
'contentType' => AcademyAnalyticsContentType::LESSON,
|
||||||
|
'contentId' => (int) $lesson->id,
|
||||||
|
'eventUrl' => route('academy.analytics.events.store'),
|
||||||
|
'pageName' => 'academy_lesson_show',
|
||||||
|
'isPremium' => (string) ($lesson->access_level ?? 'free') !== 'free',
|
||||||
|
'isGuest' => $request->user() === null,
|
||||||
|
'isSubscriber' => $request->user()?->hasAcademyCreatorAccess() || $request->user()?->hasAcademyProAccess(),
|
||||||
|
'isLocked' => (bool) ($payload['locked'] ?? false),
|
||||||
|
],
|
||||||
])->rootView('collections');
|
])->rootView('collections');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,13 +5,15 @@ declare(strict_types=1);
|
|||||||
namespace App\Http\Controllers\Academy;
|
namespace App\Http\Controllers\Academy;
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
|
||||||
use App\Support\Seo\SeoFactory;
|
use App\Support\Seo\SeoFactory;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
use Inertia\Inertia;
|
use Inertia\Inertia;
|
||||||
use Inertia\Response;
|
use Inertia\Response;
|
||||||
|
|
||||||
final class AcademyPricingController extends Controller
|
final class AcademyPricingController extends Controller
|
||||||
{
|
{
|
||||||
public function index(): Response
|
public function index(Request $request): Response
|
||||||
{
|
{
|
||||||
abort_unless((bool) config('academy.enabled', true), 404);
|
abort_unless((bool) config('academy.enabled', true), 404);
|
||||||
|
|
||||||
@@ -67,6 +69,16 @@ final class AcademyPricingController extends Controller
|
|||||||
],
|
],
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
'analytics' => [
|
||||||
|
'enabled' => true,
|
||||||
|
'contentType' => AcademyAnalyticsContentType::UPGRADE,
|
||||||
|
'contentId' => null,
|
||||||
|
'eventUrl' => route('academy.analytics.events.store'),
|
||||||
|
'pageName' => 'academy_pricing',
|
||||||
|
'isPremium' => false,
|
||||||
|
'isGuest' => $request->user() === null,
|
||||||
|
'isSubscriber' => $request->user()?->hasAcademyCreatorAccess() || $request->user()?->hasAcademyProAccess(),
|
||||||
|
],
|
||||||
])->rootView('collections');
|
])->rootView('collections');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,6 +20,32 @@ final class AcademyProgressController extends Controller
|
|||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function startLesson(Request $request): JsonResponse
|
||||||
|
{
|
||||||
|
abort_unless((bool) config('academy.enabled', true), 404);
|
||||||
|
|
||||||
|
$validated = $request->validate([
|
||||||
|
'lesson_id' => ['required', 'integer', 'min:1'],
|
||||||
|
'course_id' => ['nullable', 'integer', 'min:1'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$lesson = AcademyLesson::query()->findOrFail((int) $validated['lesson_id']);
|
||||||
|
abort_unless($this->access->canAccessLesson($request->user(), $lesson), 403);
|
||||||
|
|
||||||
|
$courseId = $request->filled('course_id') ? (int) $validated['course_id'] : null;
|
||||||
|
if ($courseId !== null) {
|
||||||
|
$course = AcademyCourse::query()->published()->findOrFail($courseId);
|
||||||
|
abort_unless($course->courseLessons()->where('lesson_id', $lesson->id)->exists(), 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
$record = $this->progress->startLesson($request->user(), (int) $lesson->id, $courseId, $request);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'ok' => true,
|
||||||
|
'status' => (string) $record->status,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
public function complete(Request $request, AcademyLesson $lesson): JsonResponse
|
public function complete(Request $request, AcademyLesson $lesson): JsonResponse
|
||||||
{
|
{
|
||||||
abort_unless((bool) config('academy.enabled', true), 404);
|
abort_unless((bool) config('academy.enabled', true), 404);
|
||||||
@@ -31,7 +57,7 @@ final class AcademyProgressController extends Controller
|
|||||||
$course = AcademyCourse::query()->published()->find($request->integer('course_id'));
|
$course = AcademyCourse::query()->published()->find($request->integer('course_id'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$record = $this->progress->markLessonComplete($request->user(), $lesson, $course);
|
$record = $this->progress->markLessonComplete($request->user(), $lesson, $course, $request);
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'ok' => true,
|
'ok' => true,
|
||||||
@@ -39,4 +65,55 @@ final class AcademyProgressController extends Controller
|
|||||||
'completed_at' => $record->completed_at?->toISOString(),
|
'completed_at' => $record->completed_at?->toISOString(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function completeLesson(Request $request): JsonResponse
|
||||||
|
{
|
||||||
|
abort_unless((bool) config('academy.enabled', true), 404);
|
||||||
|
|
||||||
|
$validated = $request->validate([
|
||||||
|
'lesson_id' => ['required', 'integer', 'min:1'],
|
||||||
|
'course_id' => ['nullable', 'integer', 'min:1'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$lesson = AcademyLesson::query()->findOrFail((int) $validated['lesson_id']);
|
||||||
|
|
||||||
|
return $this->complete($request, $lesson);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function startCourse(Request $request): JsonResponse
|
||||||
|
{
|
||||||
|
abort_unless((bool) config('academy.enabled', true), 404);
|
||||||
|
|
||||||
|
$validated = $request->validate([
|
||||||
|
'course_id' => ['required', 'integer', 'min:1'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$course = AcademyCourse::query()->published()->findOrFail((int) $validated['course_id']);
|
||||||
|
$record = $this->progress->startCourse($request->user(), (int) $course->id, $request);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'ok' => true,
|
||||||
|
'status' => (string) $record->status,
|
||||||
|
'progress_percent' => (int) $record->progress_percent,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function completeCourse(Request $request): JsonResponse
|
||||||
|
{
|
||||||
|
abort_unless((bool) config('academy.enabled', true), 404);
|
||||||
|
|
||||||
|
$validated = $request->validate([
|
||||||
|
'course_id' => ['required', 'integer', 'min:1'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$course = AcademyCourse::query()->published()->findOrFail((int) $validated['course_id']);
|
||||||
|
$record = $this->progress->completeCourse($request->user(), (int) $course->id, $request);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'ok' => true,
|
||||||
|
'status' => (string) $record->status,
|
||||||
|
'progress_percent' => (int) $record->progress_percent,
|
||||||
|
'completed' => (string) $record->status === 'completed',
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -7,9 +7,13 @@ namespace App\Http\Controllers\Academy;
|
|||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Models\AcademyPromptTemplate;
|
use App\Models\AcademyPromptTemplate;
|
||||||
use App\Services\Academy\AcademyAccessService;
|
use App\Services\Academy\AcademyAccessService;
|
||||||
|
use App\Services\Academy\AcademyAnalyticsService;
|
||||||
use App\Services\Academy\AcademyCacheService;
|
use App\Services\Academy\AcademyCacheService;
|
||||||
|
use App\Services\Academy\AcademyInteractionService;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
|
||||||
use App\Support\Seo\SeoFactory;
|
use App\Support\Seo\SeoFactory;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Inertia\Inertia;
|
use Inertia\Inertia;
|
||||||
use Inertia\Response;
|
use Inertia\Response;
|
||||||
@@ -19,10 +23,12 @@ final class AcademyPromptController extends Controller
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly AcademyAccessService $access,
|
private readonly AcademyAccessService $access,
|
||||||
private readonly AcademyCacheService $cache,
|
private readonly AcademyCacheService $cache,
|
||||||
|
private readonly AcademyAnalyticsService $analytics,
|
||||||
|
private readonly AcademyInteractionService $interactions,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function index(Request $request): Response
|
public function index(Request $request): Response|JsonResponse
|
||||||
{
|
{
|
||||||
abort_unless((bool) config('academy.enabled', true), 404);
|
abort_unless((bool) config('academy.enabled', true), 404);
|
||||||
|
|
||||||
@@ -62,6 +68,14 @@ final class AcademyPromptController extends Controller
|
|||||||
$prompts = $query->paginate(12)->withQueryString();
|
$prompts = $query->paginate(12)->withQueryString();
|
||||||
$prompts->getCollection()->transform(fn (AcademyPromptTemplate $prompt): array => $this->access->promptPayload($prompt, $request->user()));
|
$prompts->getCollection()->transform(fn (AcademyPromptTemplate $prompt): array => $this->access->promptPayload($prompt, $request->user()));
|
||||||
|
|
||||||
|
if (filled($filters['q'] ?? null)) {
|
||||||
|
$this->analytics->trackSearch((string) $filters['q'], (int) $prompts->total(), array_filter($filters), $request);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->expectsJson()) {
|
||||||
|
return response()->json($prompts);
|
||||||
|
}
|
||||||
|
|
||||||
$seo = app(SeoFactory::class)
|
$seo = app(SeoFactory::class)
|
||||||
->collectionListing(
|
->collectionListing(
|
||||||
'Academy Prompts — Skinbase',
|
'Academy Prompts — Skinbase',
|
||||||
@@ -79,6 +93,20 @@ final class AcademyPromptController extends Controller
|
|||||||
'filters' => $filters,
|
'filters' => $filters,
|
||||||
'categories' => $this->cache->categoriesByType('prompt'),
|
'categories' => $this->cache->categoriesByType('prompt'),
|
||||||
'pricingUrl' => route('academy.pricing'),
|
'pricingUrl' => route('academy.pricing'),
|
||||||
|
'analytics' => [
|
||||||
|
'enabled' => true,
|
||||||
|
'contentType' => filled($filters['q'] ?? null) ? AcademyAnalyticsContentType::SEARCH : null,
|
||||||
|
'contentId' => null,
|
||||||
|
'eventUrl' => route('academy.analytics.events.store'),
|
||||||
|
'pageName' => 'academy_prompts_index',
|
||||||
|
'search' => filled($filters['q'] ?? null) ? [
|
||||||
|
'query' => (string) $filters['q'],
|
||||||
|
'resultsCount' => (int) $prompts->total(),
|
||||||
|
] : null,
|
||||||
|
'isPremium' => false,
|
||||||
|
'isGuest' => $request->user() === null,
|
||||||
|
'isSubscriber' => $request->user()?->hasAcademyCreatorAccess() || $request->user()?->hasAcademyProAccess(),
|
||||||
|
],
|
||||||
])->rootView('collections');
|
])->rootView('collections');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,15 +130,75 @@ final class AcademyPromptController extends Controller
|
|||||||
$canonical,
|
$canonical,
|
||||||
$payload['preview_image'] ?? null,
|
$payload['preview_image'] ?? null,
|
||||||
)->toArray();
|
)->toArray();
|
||||||
|
$existingSchemas = $seo['json_ld'] ?? [];
|
||||||
|
if (! is_array($existingSchemas) || ! array_is_list($existingSchemas)) {
|
||||||
|
$existingSchemas = [$existingSchemas];
|
||||||
|
}
|
||||||
|
$seo['json_ld'] = [
|
||||||
|
...$existingSchemas,
|
||||||
|
$this->promptStructuredData($payload, $canonical, $description),
|
||||||
|
];
|
||||||
|
|
||||||
|
$canSavePrompt = $request->user() !== null && $this->access->canAccessPrompt($request->user(), $prompt);
|
||||||
|
$interaction = $this->interactions->getInteractionState($request->user(), AcademyAnalyticsContentType::PROMPT, (int) $prompt->id);
|
||||||
|
|
||||||
return Inertia::render('Academy/Show', [
|
return Inertia::render('Academy/Show', [
|
||||||
'pageType' => 'prompt',
|
'pageType' => 'prompt',
|
||||||
'item' => $payload,
|
'item' => $payload,
|
||||||
'seo' => $seo,
|
'seo' => $seo,
|
||||||
'pricingUrl' => route('academy.pricing'),
|
'pricingUrl' => route('academy.pricing'),
|
||||||
'saveUrl' => $request->user() ? route('academy.prompts.save', ['prompt' => $prompt->id]) : null,
|
'saveUrl' => $canSavePrompt ? route('academy.prompts.save', ['prompt' => $prompt->id]) : null,
|
||||||
'unsaveUrl' => $request->user() ? route('academy.prompts.unsave', ['prompt' => $prompt->id]) : null,
|
'unsaveUrl' => $canSavePrompt ? route('academy.prompts.unsave', ['prompt' => $prompt->id]) : null,
|
||||||
'saved' => $request->user()?->academySavedPrompts()->where('prompt_template_id', $prompt->id)->exists() ?? false,
|
'saved' => $canSavePrompt ? ($request->user()?->academySavedPrompts()->where('prompt_template_id', $prompt->id)->exists() ?? false) : false,
|
||||||
|
'interaction' => $interaction,
|
||||||
|
'interactionRoutes' => [
|
||||||
|
'like' => route('academy.interactions.like'),
|
||||||
|
'save' => route('academy.interactions.save'),
|
||||||
|
],
|
||||||
|
'loginUrl' => route('login'),
|
||||||
|
'analytics' => [
|
||||||
|
'enabled' => true,
|
||||||
|
'contentType' => AcademyAnalyticsContentType::PROMPT,
|
||||||
|
'contentId' => (int) $prompt->id,
|
||||||
|
'eventUrl' => route('academy.analytics.events.store'),
|
||||||
|
'pageName' => 'academy_prompt_show',
|
||||||
|
'isPremium' => (string) ($prompt->access_level ?? 'free') !== 'free',
|
||||||
|
'isGuest' => $request->user() === null,
|
||||||
|
'isSubscriber' => $request->user()?->hasAcademyCreatorAccess() || $request->user()?->hasAcademyProAccess(),
|
||||||
|
'isLocked' => (bool) ($payload['locked'] ?? false),
|
||||||
|
],
|
||||||
])->rootView('collections');
|
])->rootView('collections');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $payload
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
private function promptStructuredData(array $payload, string $canonical, string $description): array
|
||||||
|
{
|
||||||
|
$imageUrls = array_values(array_unique(array_filter([
|
||||||
|
$payload['preview_image'] ?? null,
|
||||||
|
...collect((array) ($payload['public_examples'] ?? []))
|
||||||
|
->map(fn (array $example): ?string => $example['image_url'] ?? $example['thumb_url'] ?? null)
|
||||||
|
->filter()
|
||||||
|
->values()
|
||||||
|
->all(),
|
||||||
|
], fn (mixed $value): bool => is_string($value) && $value !== '')));
|
||||||
|
$isFree = (string) ($payload['access_level'] ?? 'free') === 'free';
|
||||||
|
|
||||||
|
return array_filter([
|
||||||
|
'@context' => 'https://schema.org',
|
||||||
|
'@type' => ['CreativeWork', 'LearningResource'],
|
||||||
|
'name' => (string) ($payload['title'] ?? 'Skinbase Academy prompt'),
|
||||||
|
'description' => $description,
|
||||||
|
'url' => $canonical,
|
||||||
|
'image' => $imageUrls !== [] ? $imageUrls : null,
|
||||||
|
'isAccessibleForFree' => $isFree,
|
||||||
|
'hasPart' => $isFree ? null : [
|
||||||
|
'@type' => 'WebPageElement',
|
||||||
|
'isAccessibleForFree' => false,
|
||||||
|
'cssSelector' => '.academy-paywalled-content',
|
||||||
|
],
|
||||||
|
], fn (mixed $value): bool => $value !== null && $value !== '' && $value !== []);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -7,6 +7,8 @@ namespace App\Http\Controllers\Academy;
|
|||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Models\AcademyPromptPack;
|
use App\Models\AcademyPromptPack;
|
||||||
use App\Services\Academy\AcademyAccessService;
|
use App\Services\Academy\AcademyAccessService;
|
||||||
|
use App\Services\Academy\AcademyInteractionService;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
|
||||||
use App\Support\Seo\SeoFactory;
|
use App\Support\Seo\SeoFactory;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
@@ -15,7 +17,10 @@ use Inertia\Response;
|
|||||||
|
|
||||||
final class AcademyPromptPackController extends Controller
|
final class AcademyPromptPackController extends Controller
|
||||||
{
|
{
|
||||||
public function __construct(private readonly AcademyAccessService $access)
|
public function __construct(
|
||||||
|
private readonly AcademyAccessService $access,
|
||||||
|
private readonly AcademyInteractionService $interactions,
|
||||||
|
)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,6 +55,16 @@ final class AcademyPromptPackController extends Controller
|
|||||||
'filters' => [],
|
'filters' => [],
|
||||||
'categories' => [],
|
'categories' => [],
|
||||||
'pricingUrl' => route('academy.pricing'),
|
'pricingUrl' => route('academy.pricing'),
|
||||||
|
'analytics' => [
|
||||||
|
'enabled' => true,
|
||||||
|
'contentType' => null,
|
||||||
|
'contentId' => null,
|
||||||
|
'eventUrl' => route('academy.analytics.events.store'),
|
||||||
|
'pageName' => 'academy_packs_index',
|
||||||
|
'isPremium' => false,
|
||||||
|
'isGuest' => $request->user() === null,
|
||||||
|
'isSubscriber' => $request->user()?->hasAcademyCreatorAccess() || $request->user()?->hasAcademyProAccess(),
|
||||||
|
],
|
||||||
])->rootView('collections');
|
])->rootView('collections');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,11 +87,30 @@ final class AcademyPromptPackController extends Controller
|
|||||||
$pack->cover_image,
|
$pack->cover_image,
|
||||||
)->toArray();
|
)->toArray();
|
||||||
|
|
||||||
|
$interaction = $this->interactions->getInteractionState($request->user(), AcademyAnalyticsContentType::PROMPT_PACK, (int) $pack->id);
|
||||||
|
|
||||||
return Inertia::render('Academy/Show', [
|
return Inertia::render('Academy/Show', [
|
||||||
'pageType' => 'pack',
|
'pageType' => 'pack',
|
||||||
'item' => $payload,
|
'item' => $payload,
|
||||||
'seo' => $seo,
|
'seo' => $seo,
|
||||||
'pricingUrl' => route('academy.pricing'),
|
'pricingUrl' => route('academy.pricing'),
|
||||||
|
'interaction' => $interaction,
|
||||||
|
'interactionRoutes' => [
|
||||||
|
'like' => route('academy.interactions.like'),
|
||||||
|
'save' => route('academy.interactions.save'),
|
||||||
|
],
|
||||||
|
'loginUrl' => route('login'),
|
||||||
|
'analytics' => [
|
||||||
|
'enabled' => true,
|
||||||
|
'contentType' => AcademyAnalyticsContentType::PROMPT_PACK,
|
||||||
|
'contentId' => (int) $pack->id,
|
||||||
|
'eventUrl' => route('academy.analytics.events.store'),
|
||||||
|
'pageName' => 'academy_pack_show',
|
||||||
|
'isPremium' => (string) ($pack->access_level ?? 'free') !== 'free',
|
||||||
|
'isGuest' => $request->user() === null,
|
||||||
|
'isSubscriber' => $request->user()?->hasAcademyCreatorAccess() || $request->user()?->hasAcademyProAccess(),
|
||||||
|
'isLocked' => (bool) ($payload['locked'] ?? false),
|
||||||
|
],
|
||||||
])->rootView('collections');
|
])->rootView('collections');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -11,11 +11,21 @@ use App\Services\ThumbnailPresenter;
|
|||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Contracts\Pagination\Paginator;
|
||||||
|
|
||||||
class LatestCommentsApiController extends Controller
|
class LatestCommentsApiController extends Controller
|
||||||
{
|
{
|
||||||
private const PER_PAGE = 20;
|
private const PER_PAGE = 20;
|
||||||
|
|
||||||
|
private function paginationMeta(Paginator $paginator): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'current_page' => $paginator->currentPage(),
|
||||||
|
'per_page' => $paginator->perPage(),
|
||||||
|
'has_more' => $paginator->hasMorePages(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
public function index(Request $request): JsonResponse
|
public function index(Request $request): JsonResponse
|
||||||
{
|
{
|
||||||
$type = $request->query('type', 'all');
|
$type = $request->query('type', 'all');
|
||||||
@@ -66,15 +76,21 @@ class LatestCommentsApiController extends Controller
|
|||||||
$cacheKey = 'comments.latest.all.page1';
|
$cacheKey = 'comments.latest.all.page1';
|
||||||
$ttl = 120; // 2 minutes
|
$ttl = 120; // 2 minutes
|
||||||
|
|
||||||
$paginator = Cache::remember($cacheKey, $ttl, fn () => $query->paginate(self::PER_PAGE));
|
$paginator = Cache::remember($cacheKey, $ttl, fn () => $query
|
||||||
|
->orderByDesc('artwork_comments.id')
|
||||||
|
->simplePaginate(self::PER_PAGE));
|
||||||
} else {
|
} else {
|
||||||
$paginator = $query->paginate(self::PER_PAGE);
|
$paginator = $query
|
||||||
|
->orderByDesc('artwork_comments.id')
|
||||||
|
->simplePaginate(self::PER_PAGE);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! isset($paginator)) {
|
if (! isset($paginator)) {
|
||||||
$paginator = $query->paginate(self::PER_PAGE);
|
$paginator = $query
|
||||||
|
->orderByDesc('artwork_comments.id')
|
||||||
|
->simplePaginate(self::PER_PAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
$items = $paginator->getCollection()->map(function (ArtworkComment $c) {
|
$items = $paginator->getCollection()->map(function (ArtworkComment $c) {
|
||||||
@@ -113,13 +129,7 @@ class LatestCommentsApiController extends Controller
|
|||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'data' => $items,
|
'data' => $items,
|
||||||
'meta' => [
|
'meta' => $this->paginationMeta($paginator),
|
||||||
'current_page' => $paginator->currentPage(),
|
|
||||||
'last_page' => $paginator->lastPage(),
|
|
||||||
'per_page' => $paginator->perPage(),
|
|
||||||
'total' => $paginator->total(),
|
|
||||||
'has_more' => $paginator->hasMorePages(),
|
|
||||||
],
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,11 +10,21 @@ use App\Services\ThumbnailPresenter;
|
|||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Contracts\Pagination\Paginator;
|
||||||
|
|
||||||
class LatestCommentsController extends Controller
|
class LatestCommentsController extends Controller
|
||||||
{
|
{
|
||||||
private const PER_PAGE = 20;
|
private const PER_PAGE = 20;
|
||||||
|
|
||||||
|
private function paginationMeta(Paginator $paginator): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'current_page' => $paginator->currentPage(),
|
||||||
|
'per_page' => $paginator->perPage(),
|
||||||
|
'has_more' => $paginator->hasMorePages(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
public function index(Request $request)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
$page_title = 'Latest Comments';
|
$page_title = 'Latest Comments';
|
||||||
@@ -38,7 +48,8 @@ class LatestCommentsController extends Controller
|
|||||||
$q->public()->published()->whereNull('deleted_at');
|
$q->public()->published()->whereNull('deleted_at');
|
||||||
})
|
})
|
||||||
->orderByDesc('artwork_comments.created_at')
|
->orderByDesc('artwork_comments.created_at')
|
||||||
->paginate(self::PER_PAGE);
|
->orderByDesc('artwork_comments.id')
|
||||||
|
->simplePaginate(self::PER_PAGE);
|
||||||
});
|
});
|
||||||
|
|
||||||
$items = $initialData->getCollection()->map(function (ArtworkComment $c) {
|
$items = $initialData->getCollection()->map(function (ArtworkComment $c) {
|
||||||
@@ -76,13 +87,7 @@ class LatestCommentsController extends Controller
|
|||||||
|
|
||||||
$props = [
|
$props = [
|
||||||
'initialComments' => $items->values()->all(),
|
'initialComments' => $items->values()->all(),
|
||||||
'initialMeta' => [
|
'initialMeta' => $this->paginationMeta($initialData),
|
||||||
'current_page' => $initialData->currentPage(),
|
|
||||||
'last_page' => $initialData->lastPage(),
|
|
||||||
'per_page' => $initialData->perPage(),
|
|
||||||
'total' => $initialData->total(),
|
|
||||||
'has_more' => $initialData->hasMorePages(),
|
|
||||||
],
|
|
||||||
'isAuthenticated' => (bool) auth()->user(),
|
'isAuthenticated' => (bool) auth()->user(),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,470 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Settings;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Models\AcademyContentMetricDaily;
|
||||||
|
use App\Models\AcademySearchLog;
|
||||||
|
use App\Services\Academy\AcademyAnalyticsContentResolver;
|
||||||
|
use App\Services\Academy\AcademyContentIntelligenceService;
|
||||||
|
use App\Services\Academy\AcademyPopularityService;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Inertia\Inertia;
|
||||||
|
use Inertia\Response;
|
||||||
|
|
||||||
|
final class AcademyAdminAnalyticsController extends Controller
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly AcademyPopularityService $popularity,
|
||||||
|
private readonly AcademyAnalyticsContentResolver $resolver,
|
||||||
|
private readonly AcademyContentIntelligenceService $intelligence,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function overview(Request $request): Response
|
||||||
|
{
|
||||||
|
[$from, $to, $range] = $this->resolveDateRange($request);
|
||||||
|
|
||||||
|
$summary = $this->metricsQuery($from, $to)
|
||||||
|
->selectRaw('sum(views) as views, sum(unique_visitors) as unique_visitors, sum(user_views) as user_views, sum(guest_views) as guest_views, sum(subscriber_views) as subscriber_views, sum(prompt_copies) as prompt_copies, sum(likes) as likes, sum(saves) as saves, sum(completions) as completions, sum(starts) as starts, sum(upgrade_clicks) as upgrade_clicks')
|
||||||
|
->first();
|
||||||
|
|
||||||
|
return Inertia::render('Admin/Academy/AnalyticsOverview', [
|
||||||
|
'nav' => $this->nav(),
|
||||||
|
'range' => $this->rangePayload($range, $from, $to),
|
||||||
|
'stats' => [
|
||||||
|
'views' => (int) ($summary?->views ?? 0),
|
||||||
|
'uniqueVisitors' => (int) ($summary?->unique_visitors ?? 0),
|
||||||
|
'userViews' => (int) ($summary?->user_views ?? 0),
|
||||||
|
'guestViews' => (int) ($summary?->guest_views ?? 0),
|
||||||
|
'subscriberViews' => (int) ($summary?->subscriber_views ?? 0),
|
||||||
|
'promptCopies' => (int) ($summary?->prompt_copies ?? 0),
|
||||||
|
'likes' => (int) ($summary?->likes ?? 0),
|
||||||
|
'saves' => (int) ($summary?->saves ?? 0),
|
||||||
|
'lessonCompletions' => (int) ($summary?->completions ?? 0),
|
||||||
|
'courseStarts' => (int) ($summary?->starts ?? 0),
|
||||||
|
'upgradeClicks' => (int) ($summary?->upgrade_clicks ?? 0),
|
||||||
|
],
|
||||||
|
'topContent' => $this->serializeContentRows($this->popularity->topContent($from, $to, 8)),
|
||||||
|
'topWeek' => $this->serializeContentRows($this->popularity->topContent(now()->subDays(6)->startOfDay(), now()->endOfDay(), 8)),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function content(Request $request): Response
|
||||||
|
{
|
||||||
|
return $this->renderContentPage($request, null, 'Content performance', 'Cross-module performance across prompts, lessons, courses, packs, and challenges.');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function prompts(Request $request): Response
|
||||||
|
{
|
||||||
|
return $this->renderContentPage($request, AcademyAnalyticsContentType::PROMPT, 'Prompt analytics', 'Copy-heavy prompt performance, save rates, and upgrade interest.');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function lessons(Request $request): Response
|
||||||
|
{
|
||||||
|
return $this->renderContentPage($request, AcademyAnalyticsContentType::LESSON, 'Lesson analytics', 'Lesson engagement, starts, completions, and drop-off signals.');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function courses(Request $request): Response
|
||||||
|
{
|
||||||
|
return $this->renderContentPage($request, AcademyAnalyticsContentType::COURSE, 'Course analytics', 'Course views, starts, completion progress, and upgrade intent.');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function search(Request $request): Response
|
||||||
|
{
|
||||||
|
[$from, $to, $range] = $this->resolveDateRange($request);
|
||||||
|
|
||||||
|
$searchQuery = AcademySearchLog::query()->whereBetween('created_at', [$from, $to]);
|
||||||
|
$searchLogs = (clone $searchQuery)->latest('created_at')->limit(500)->get();
|
||||||
|
|
||||||
|
return Inertia::render('Admin/Academy/AnalyticsSearch', [
|
||||||
|
'nav' => $this->nav(),
|
||||||
|
'range' => $this->rangePayload($range, $from, $to),
|
||||||
|
'summary' => [
|
||||||
|
'searches' => (int) (clone $searchQuery)->count(),
|
||||||
|
'zeroResultSearches' => (int) (clone $searchQuery)->where('results_count', 0)->count(),
|
||||||
|
'loggedInSearches' => (int) (clone $searchQuery)->where('is_logged_in', true)->count(),
|
||||||
|
'subscriberSearches' => (int) (clone $searchQuery)->where('is_subscriber', true)->count(),
|
||||||
|
'searchesWithClicks' => (int) (clone $searchQuery)->whereNotNull('clicked_content_id')->count(),
|
||||||
|
],
|
||||||
|
'topSearches' => (clone $searchQuery)
|
||||||
|
->selectRaw('normalized_query, max(query) as query, count(*) as searches, sum(results_count = 0) as zero_result_hits, avg(results_count) as avg_results, sum(case when clicked_content_id is not null then 1 else 0 end) as clicks')
|
||||||
|
->groupBy('normalized_query')
|
||||||
|
->orderByDesc('searches')
|
||||||
|
->limit(20)
|
||||||
|
->get()
|
||||||
|
->map(fn ($row): array => [
|
||||||
|
'query' => (string) ($row->query ?: $row->normalized_query),
|
||||||
|
'normalized_query' => (string) $row->normalized_query,
|
||||||
|
'searches' => (int) $row->searches,
|
||||||
|
'zero_result_hits' => (int) $row->zero_result_hits,
|
||||||
|
'avg_results' => round((float) $row->avg_results, 1),
|
||||||
|
'clicks' => (int) ($row->clicks ?? 0),
|
||||||
|
'click_through_rate' => (int) $row->searches > 0 ? round((((int) ($row->clicks ?? 0)) / (int) $row->searches) * 100, 1) : 0,
|
||||||
|
])
|
||||||
|
->all(),
|
||||||
|
'zeroResults' => (clone $searchQuery)
|
||||||
|
->selectRaw('normalized_query, max(query) as query, count(*) as searches')
|
||||||
|
->where('results_count', 0)
|
||||||
|
->groupBy('normalized_query')
|
||||||
|
->orderByDesc('searches')
|
||||||
|
->limit(20)
|
||||||
|
->get()
|
||||||
|
->map(fn ($row): array => [
|
||||||
|
'query' => (string) ($row->query ?: $row->normalized_query),
|
||||||
|
'searches' => (int) $row->searches,
|
||||||
|
])
|
||||||
|
->all(),
|
||||||
|
'lowClickThroughSearches' => (clone $searchQuery)
|
||||||
|
->selectRaw('normalized_query, max(query) as query, count(*) as searches, sum(case when clicked_content_id is not null then 1 else 0 end) as clicks, avg(results_count) as avg_results')
|
||||||
|
->groupBy('normalized_query')
|
||||||
|
->havingRaw('count(*) >= 2')
|
||||||
|
->orderByRaw('case when count(*) = 0 then 1 else (sum(case when clicked_content_id is not null then 1 else 0 end) * 1.0 / count(*)) end asc')
|
||||||
|
->limit(20)
|
||||||
|
->get()
|
||||||
|
->map(fn ($row): array => [
|
||||||
|
'query' => (string) ($row->query ?: $row->normalized_query),
|
||||||
|
'searches' => (int) $row->searches,
|
||||||
|
'clicks' => (int) ($row->clicks ?? 0),
|
||||||
|
'avg_results' => round((float) $row->avg_results, 1),
|
||||||
|
'click_through_rate' => (int) $row->searches > 0 ? round((((int) ($row->clicks ?? 0)) / (int) $row->searches) * 100, 1) : 0,
|
||||||
|
])
|
||||||
|
->all(),
|
||||||
|
'highestClickThroughSearches' => (clone $searchQuery)
|
||||||
|
->selectRaw('normalized_query, max(query) as query, count(*) as searches, sum(case when clicked_content_id is not null then 1 else 0 end) as clicks, avg(results_count) as avg_results')
|
||||||
|
->groupBy('normalized_query')
|
||||||
|
->havingRaw('count(*) >= 2')
|
||||||
|
->orderByRaw('(sum(case when clicked_content_id is not null then 1 else 0 end) * 1.0 / count(*)) desc')
|
||||||
|
->limit(20)
|
||||||
|
->get()
|
||||||
|
->map(fn ($row): array => [
|
||||||
|
'query' => (string) ($row->query ?: $row->normalized_query),
|
||||||
|
'searches' => (int) $row->searches,
|
||||||
|
'clicks' => (int) ($row->clicks ?? 0),
|
||||||
|
'avg_results' => round((float) $row->avg_results, 1),
|
||||||
|
'click_through_rate' => (int) $row->searches > 0 ? round((((int) ($row->clicks ?? 0)) / (int) $row->searches) * 100, 1) : 0,
|
||||||
|
])
|
||||||
|
->all(),
|
||||||
|
'searchesWithResultsNoClicks' => (clone $searchQuery)
|
||||||
|
->selectRaw('normalized_query, max(query) as query, count(*) as searches, avg(results_count) as avg_results')
|
||||||
|
->where('results_count', '>', 0)
|
||||||
|
->whereNull('clicked_content_id')
|
||||||
|
->groupBy('normalized_query')
|
||||||
|
->orderByDesc('searches')
|
||||||
|
->limit(20)
|
||||||
|
->get()
|
||||||
|
->map(fn ($row): array => [
|
||||||
|
'query' => (string) ($row->query ?: $row->normalized_query),
|
||||||
|
'searches' => (int) $row->searches,
|
||||||
|
'avg_results' => round((float) $row->avg_results, 1),
|
||||||
|
'clicks' => 0,
|
||||||
|
'click_through_rate' => 0,
|
||||||
|
])
|
||||||
|
->all(),
|
||||||
|
'topClickedResults' => (clone $searchQuery)
|
||||||
|
->selectRaw('clicked_content_type, clicked_content_id, count(*) as clicks')
|
||||||
|
->whereNotNull('clicked_content_type')
|
||||||
|
->whereNotNull('clicked_content_id')
|
||||||
|
->groupBy('clicked_content_type', 'clicked_content_id')
|
||||||
|
->orderByDesc('clicks')
|
||||||
|
->limit(20)
|
||||||
|
->get()
|
||||||
|
->map(fn ($row): array => [
|
||||||
|
'title' => $this->resolver->title((string) $row->clicked_content_type, (int) $row->clicked_content_id),
|
||||||
|
'content_type' => (string) $row->clicked_content_type,
|
||||||
|
'content_id' => (int) $row->clicked_content_id,
|
||||||
|
'clicks' => (int) $row->clicks,
|
||||||
|
])
|
||||||
|
->all(),
|
||||||
|
'filterUsage' => $this->summarizeSearchFilters($searchLogs),
|
||||||
|
'recentSearches' => (clone $searchQuery)
|
||||||
|
->latest('created_at')
|
||||||
|
->limit(25)
|
||||||
|
->get()
|
||||||
|
->map(fn (AcademySearchLog $log): array => [
|
||||||
|
'query' => (string) $log->query,
|
||||||
|
'results_count' => (int) $log->results_count,
|
||||||
|
'logged_in' => (bool) $log->is_logged_in,
|
||||||
|
'subscriber' => (bool) $log->is_subscriber,
|
||||||
|
'clicked_content_type' => $log->clicked_content_type,
|
||||||
|
'has_click' => $log->clicked_content_id !== null,
|
||||||
|
'created_at' => $log->created_at?->toISOString(),
|
||||||
|
])
|
||||||
|
->all(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function intelligence(Request $request): Response
|
||||||
|
{
|
||||||
|
[$from, $to, $range] = $this->resolveDateRange($request, '30d');
|
||||||
|
$filters = [
|
||||||
|
'from' => $from,
|
||||||
|
'to' => $to,
|
||||||
|
'limit' => 25,
|
||||||
|
];
|
||||||
|
|
||||||
|
return Inertia::render('Admin/Academy/AnalyticsIntelligence', [
|
||||||
|
'nav' => $this->nav(),
|
||||||
|
'range' => $this->rangePayload($range, $from, $to),
|
||||||
|
'contentOpportunities' => $this->intelligence->getContentOpportunities($filters),
|
||||||
|
'searchGaps' => $this->intelligence->getSearchGaps($filters),
|
||||||
|
'promptInsights' => $this->intelligence->getPromptInsights($filters),
|
||||||
|
'lessonDropoffs' => $this->intelligence->getLessonDropoffs($filters),
|
||||||
|
'courseHealth' => $this->intelligence->getCourseHealth($filters),
|
||||||
|
'premiumInterest' => $this->intelligence->getPremiumInterest($filters),
|
||||||
|
'editorialRecommendations' => $this->intelligence->getEditorialRecommendations($filters),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Collection<int, AcademySearchLog> $logs
|
||||||
|
* @return list<array<string, int|string>>
|
||||||
|
*/
|
||||||
|
private function summarizeSearchFilters(Collection $logs): array
|
||||||
|
{
|
||||||
|
$counts = [];
|
||||||
|
|
||||||
|
foreach ($logs as $log) {
|
||||||
|
$filters = is_array($log->filters) ? $log->filters : [];
|
||||||
|
|
||||||
|
foreach ($filters as $key => $value) {
|
||||||
|
if ($value === null || $value === '' || $key === 'q') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$values = is_array($value) ? $value : [$value];
|
||||||
|
|
||||||
|
foreach ($values as $rawValue) {
|
||||||
|
$label = trim((string) $rawValue);
|
||||||
|
if ($label === '') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$bucket = sprintf('%s:%s', $key, $label);
|
||||||
|
$counts[$bucket] = [
|
||||||
|
'filter' => (string) $key,
|
||||||
|
'value' => $label,
|
||||||
|
'uses' => (int) (($counts[$bucket]['uses'] ?? 0) + 1),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
usort($counts, static fn (array $left, array $right): int => $right['uses'] <=> $left['uses']);
|
||||||
|
|
||||||
|
return array_slice(array_values($counts), 0, 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function funnel(Request $request): Response
|
||||||
|
{
|
||||||
|
[$from, $to, $range] = $this->resolveDateRange($request);
|
||||||
|
|
||||||
|
$summary = $this->metricsQuery($from, $to)
|
||||||
|
->selectRaw('sum(unique_visitors) as unique_visitors, sum(premium_preview_views) as premium_preview_views, sum(upgrade_clicks) as upgrade_clicks, sum(starts) as starts, sum(completions) as completions')
|
||||||
|
->first();
|
||||||
|
|
||||||
|
$bestConverters = $this->metricsQuery($from, $to)
|
||||||
|
->selectRaw('content_type, content_id, sum(unique_visitors) as unique_visitors, sum(premium_preview_views) as premium_preview_views, sum(upgrade_clicks) as upgrade_clicks, sum(conversion_score) as conversion_score')
|
||||||
|
->groupBy('content_type', 'content_id')
|
||||||
|
->havingRaw('sum(upgrade_clicks) > 0')
|
||||||
|
->orderByDesc('conversion_score')
|
||||||
|
->limit(12)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
return Inertia::render('Admin/Academy/AnalyticsFunnel', [
|
||||||
|
'nav' => $this->nav(),
|
||||||
|
'range' => $this->rangePayload($range, $from, $to),
|
||||||
|
'summary' => [
|
||||||
|
'academyVisitors' => (int) ($summary?->unique_visitors ?? 0),
|
||||||
|
'premiumPreviewViews' => (int) ($summary?->premium_preview_views ?? 0),
|
||||||
|
'upgradeClicks' => (int) ($summary?->upgrade_clicks ?? 0),
|
||||||
|
'starts' => (int) ($summary?->starts ?? 0),
|
||||||
|
'completions' => (int) ($summary?->completions ?? 0),
|
||||||
|
'checkoutStarts' => 0,
|
||||||
|
'subscriptions' => 0,
|
||||||
|
],
|
||||||
|
'bestConverters' => $this->serializeContentRows($bestConverters, includeConversion: true),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function renderContentPage(Request $request, ?string $forcedContentType, string $title, string $subtitle): Response
|
||||||
|
{
|
||||||
|
[$from, $to, $range] = $this->resolveDateRange($request);
|
||||||
|
$sort = (string) $request->query('sort', 'popularity_score');
|
||||||
|
$direction = strtolower((string) $request->query('direction', 'desc')) === 'asc' ? 'asc' : 'desc';
|
||||||
|
$access = trim((string) $request->query('access', ''));
|
||||||
|
$contentType = $forcedContentType ?: (trim((string) $request->query('content_type', '')) ?: null);
|
||||||
|
|
||||||
|
$query = $this->metricsQuery($from, $to)
|
||||||
|
->selectRaw('content_type, content_id, sum(views) as views, sum(unique_visitors) as unique_visitors, sum(engaged_views) as engaged_views, sum(likes) as likes, sum(saves) as saves, sum(prompt_copies) as prompt_copies, sum(starts) as starts, sum(completions) as completions, sum(upgrade_clicks) as upgrade_clicks, sum(popularity_score) as popularity_score, sum(conversion_score) as conversion_score')
|
||||||
|
->groupBy('content_type', 'content_id');
|
||||||
|
|
||||||
|
if ($contentType !== null) {
|
||||||
|
$query->where('content_type', $contentType);
|
||||||
|
}
|
||||||
|
|
||||||
|
$rows = $query->get();
|
||||||
|
|
||||||
|
$serializedRows = $this->serializeContentRows($rows, includeConversion: true)
|
||||||
|
->filter(function (array $row) use ($access): bool {
|
||||||
|
if ($access === '') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return strtolower((string) ($row['access_level'] ?? '')) === strtolower($access);
|
||||||
|
})
|
||||||
|
->sortBy($sort, SORT_REGULAR, $direction === 'desc')
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
|
||||||
|
return Inertia::render('Admin/Academy/AnalyticsContent', [
|
||||||
|
'nav' => $this->nav(),
|
||||||
|
'range' => $this->rangePayload($range, $from, $to),
|
||||||
|
'title' => $title,
|
||||||
|
'subtitle' => $subtitle,
|
||||||
|
'filters' => [
|
||||||
|
'sort' => $sort,
|
||||||
|
'direction' => $direction,
|
||||||
|
'access' => $access,
|
||||||
|
'content_type' => $contentType,
|
||||||
|
],
|
||||||
|
'rows' => $serializedRows,
|
||||||
|
'contentTypeOptions' => [
|
||||||
|
['value' => '', 'label' => 'All content'],
|
||||||
|
['value' => AcademyAnalyticsContentType::PROMPT, 'label' => 'Prompts'],
|
||||||
|
['value' => AcademyAnalyticsContentType::LESSON, 'label' => 'Lessons'],
|
||||||
|
['value' => AcademyAnalyticsContentType::COURSE, 'label' => 'Courses'],
|
||||||
|
['value' => AcademyAnalyticsContentType::PROMPT_PACK, 'label' => 'Prompt packs'],
|
||||||
|
['value' => AcademyAnalyticsContentType::CHALLENGE, 'label' => 'Challenges'],
|
||||||
|
],
|
||||||
|
'sortOptions' => [
|
||||||
|
['value' => 'views', 'label' => 'Views'],
|
||||||
|
['value' => 'unique_visitors', 'label' => 'Unique visitors'],
|
||||||
|
['value' => 'likes', 'label' => 'Likes'],
|
||||||
|
['value' => 'saves', 'label' => 'Saves'],
|
||||||
|
['value' => 'prompt_copies', 'label' => 'Copies'],
|
||||||
|
['value' => 'completions', 'label' => 'Completions'],
|
||||||
|
['value' => 'upgrade_clicks', 'label' => 'Upgrade clicks'],
|
||||||
|
['value' => 'popularity_score', 'label' => 'Popularity score'],
|
||||||
|
['value' => 'conversion_score', 'label' => 'Conversion score'],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function metricsQuery(Carbon $from, Carbon $to)
|
||||||
|
{
|
||||||
|
return AcademyContentMetricDaily::query()
|
||||||
|
->whereBetween('date', [$from->toDateString(), $to->toDateString()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Collection<int, mixed> $rows
|
||||||
|
* @return Collection<int, array<string, mixed>>
|
||||||
|
*/
|
||||||
|
private function serializeContentRows(Collection $rows, bool $includeConversion = false): Collection
|
||||||
|
{
|
||||||
|
return $rows->map(function ($row) use ($includeConversion): array {
|
||||||
|
$contentType = (string) $row->content_type;
|
||||||
|
$contentId = $row->content_id ? (int) $row->content_id : null;
|
||||||
|
$title = $this->resolver->title($contentType, $contentId);
|
||||||
|
$accessLevel = $this->resolver->accessLevel($contentType, $contentId);
|
||||||
|
$uniqueVisitors = max(0, (int) ($row->unique_visitors ?? 0));
|
||||||
|
$promptCopies = max(0, (int) ($row->prompt_copies ?? 0));
|
||||||
|
$likes = max(0, (int) ($row->likes ?? 0));
|
||||||
|
$saves = max(0, (int) ($row->saves ?? 0));
|
||||||
|
$starts = max(0, (int) ($row->starts ?? 0));
|
||||||
|
$completions = max(0, (int) ($row->completions ?? 0));
|
||||||
|
$premiumPreviewViews = max(0, (int) ($row->premium_preview_views ?? 0));
|
||||||
|
$upgradeClicks = max(0, (int) ($row->upgrade_clicks ?? 0));
|
||||||
|
|
||||||
|
return [
|
||||||
|
'content_type' => $contentType,
|
||||||
|
'content_type_label' => (string) Str::of(str_replace('academy_', '', $contentType))->replace('_', ' ')->headline(),
|
||||||
|
'content_id' => $contentId,
|
||||||
|
'title' => $title,
|
||||||
|
'access_level' => $accessLevel,
|
||||||
|
'views' => (int) ($row->views ?? 0),
|
||||||
|
'unique_visitors' => $uniqueVisitors,
|
||||||
|
'engaged_views' => (int) ($row->engaged_views ?? 0),
|
||||||
|
'likes' => $likes,
|
||||||
|
'saves' => $saves,
|
||||||
|
'prompt_copies' => $promptCopies,
|
||||||
|
'starts' => $starts,
|
||||||
|
'completions' => $completions,
|
||||||
|
'upgrade_clicks' => $upgradeClicks,
|
||||||
|
'popularity_score' => round((float) ($row->popularity_score ?? 0), 2),
|
||||||
|
'conversion_score' => round((float) ($row->conversion_score ?? 0), 2),
|
||||||
|
'copy_rate' => $uniqueVisitors > 0 ? round(($promptCopies / $uniqueVisitors) * 100, 1) : 0,
|
||||||
|
'save_rate' => $uniqueVisitors > 0 ? round(($saves / $uniqueVisitors) * 100, 1) : 0,
|
||||||
|
'like_rate' => $uniqueVisitors > 0 ? round(($likes / $uniqueVisitors) * 100, 1) : 0,
|
||||||
|
'completion_rate' => $starts > 0 ? round(($completions / $starts) * 100, 1) : 0,
|
||||||
|
'upgrade_rate' => max(1, $premiumPreviewViews) > 0 ? round(($upgradeClicks / max(1, $premiumPreviewViews)) * 100, 1) : 0,
|
||||||
|
'trend' => ((float) ($row->popularity_score ?? 0)) >= 100 ? 'High momentum' : (((float) ($row->popularity_score ?? 0)) >= 25 ? 'Building' : 'Early'),
|
||||||
|
'include_conversion' => $includeConversion,
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{0: Carbon, 1: Carbon, 2: string}
|
||||||
|
*/
|
||||||
|
private function resolveDateRange(Request $request, string $defaultRange = '7d'): array
|
||||||
|
{
|
||||||
|
$range = trim((string) $request->query('range', $defaultRange));
|
||||||
|
|
||||||
|
return match ($range) {
|
||||||
|
'today' => [now()->startOfDay(), now()->endOfDay(), 'today'],
|
||||||
|
'yesterday' => [now()->subDay()->startOfDay(), now()->subDay()->endOfDay(), 'yesterday'],
|
||||||
|
'30d' => [now()->subDays(29)->startOfDay(), now()->endOfDay(), '30d'],
|
||||||
|
'90d' => [now()->subDays(89)->startOfDay(), now()->endOfDay(), '90d'],
|
||||||
|
'custom' => [
|
||||||
|
Carbon::parse((string) $request->query('from', now()->subDays(6)->toDateString()))->startOfDay(),
|
||||||
|
Carbon::parse((string) $request->query('to', now()->toDateString()))->endOfDay(),
|
||||||
|
'custom',
|
||||||
|
],
|
||||||
|
default => [now()->subDays(6)->startOfDay(), now()->endOfDay(), '7d'],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list<array<string, string|bool>>
|
||||||
|
*/
|
||||||
|
private function nav(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
['label' => 'Overview', 'href' => route('admin.academy.analytics.overview')],
|
||||||
|
['label' => 'Intelligence', 'href' => route('admin.academy.analytics.intelligence')],
|
||||||
|
['label' => 'Content', 'href' => route('admin.academy.analytics.content')],
|
||||||
|
['label' => 'Prompts', 'href' => route('admin.academy.analytics.prompts')],
|
||||||
|
['label' => 'Lessons', 'href' => route('admin.academy.analytics.lessons')],
|
||||||
|
['label' => 'Courses', 'href' => route('admin.academy.analytics.courses')],
|
||||||
|
['label' => 'Search', 'href' => route('admin.academy.analytics.search')],
|
||||||
|
['label' => 'Funnel', 'href' => route('admin.academy.analytics.funnel')],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
private function rangePayload(string $activeRange, Carbon $from, Carbon $to): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'active' => $activeRange,
|
||||||
|
'from' => $from->toDateString(),
|
||||||
|
'to' => $to->toDateString(),
|
||||||
|
'options' => [
|
||||||
|
['value' => 'today', 'label' => 'Today'],
|
||||||
|
['value' => 'yesterday', 'label' => 'Yesterday'],
|
||||||
|
['value' => '7d', 'label' => 'Last 7 days'],
|
||||||
|
['value' => '30d', 'label' => 'Last 30 days'],
|
||||||
|
['value' => '90d', 'label' => 'Last 90 days'],
|
||||||
|
['value' => 'custom', 'label' => 'Custom range'],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@ use App\Models\AcademyChallenge;
|
|||||||
use App\Models\AcademyChallengeSubmission;
|
use App\Models\AcademyChallengeSubmission;
|
||||||
use App\Models\AcademyCourse;
|
use App\Models\AcademyCourse;
|
||||||
use App\Models\AcademyCourseLesson;
|
use App\Models\AcademyCourseLesson;
|
||||||
|
use App\Models\AcademyCourseSection;
|
||||||
use App\Models\AcademyLesson;
|
use App\Models\AcademyLesson;
|
||||||
use App\Models\AcademyLessonBlock;
|
use App\Models\AcademyLessonBlock;
|
||||||
use App\Models\AcademyLessonRevision;
|
use App\Models\AcademyLessonRevision;
|
||||||
@@ -26,6 +27,7 @@ use App\Models\AcademyPromptPack;
|
|||||||
use App\Models\AcademyPromptPackItem;
|
use App\Models\AcademyPromptPackItem;
|
||||||
use App\Models\AcademyPromptTemplate;
|
use App\Models\AcademyPromptTemplate;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use App\Services\Academy\AcademyAdminBillingOverviewService;
|
||||||
use App\Services\Academy\AcademyCacheService;
|
use App\Services\Academy\AcademyCacheService;
|
||||||
use App\Services\Academy\AcademyCourseLessonOrderingService;
|
use App\Services\Academy\AcademyCourseLessonOrderingService;
|
||||||
use App\Services\Academy\AcademyLessonMarkdownRenderer;
|
use App\Services\Academy\AcademyLessonMarkdownRenderer;
|
||||||
@@ -38,6 +40,7 @@ use Illuminate\Support\Carbon;
|
|||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
use Inertia\Inertia;
|
use Inertia\Inertia;
|
||||||
use Inertia\Response;
|
use Inertia\Response;
|
||||||
@@ -48,7 +51,13 @@ final class AcademyAdminController extends Controller
|
|||||||
|
|
||||||
private const PROMPT_PREVIEW_PREFIX = 'academy-prompts/previews';
|
private const PROMPT_PREVIEW_PREFIX = 'academy-prompts/previews';
|
||||||
|
|
||||||
|
private const PROMPT_PREVIEW_VARIANT_WIDTHS = [
|
||||||
|
'thumb' => 480,
|
||||||
|
'md' => 960,
|
||||||
|
];
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
private readonly AcademyAdminBillingOverviewService $billingOverview,
|
||||||
private readonly AcademyCacheService $cache,
|
private readonly AcademyCacheService $cache,
|
||||||
private readonly AcademyCourseLessonOrderingService $courseLessonOrdering,
|
private readonly AcademyCourseLessonOrderingService $courseLessonOrdering,
|
||||||
private readonly AcademyLessonMarkdownRenderer $lessonMarkdownRenderer,
|
private readonly AcademyLessonMarkdownRenderer $lessonMarkdownRenderer,
|
||||||
@@ -56,6 +65,8 @@ final class AcademyAdminController extends Controller
|
|||||||
|
|
||||||
public function dashboard(): Response
|
public function dashboard(): Response
|
||||||
{
|
{
|
||||||
|
$billingSummary = $this->billingOverview->summary();
|
||||||
|
|
||||||
return Inertia::render('Admin/Academy/Dashboard', [
|
return Inertia::render('Admin/Academy/Dashboard', [
|
||||||
'stats' => [
|
'stats' => [
|
||||||
'courses' => AcademyCourse::query()->count(),
|
'courses' => AcademyCourse::query()->count(),
|
||||||
@@ -65,11 +76,13 @@ final class AcademyAdminController extends Controller
|
|||||||
'challenges' => AcademyChallenge::query()->count(),
|
'challenges' => AcademyChallenge::query()->count(),
|
||||||
'submissions' => AcademyChallengeSubmission::query()->count(),
|
'submissions' => AcademyChallengeSubmission::query()->count(),
|
||||||
'badges' => AcademyBadge::query()->count(),
|
'badges' => AcademyBadge::query()->count(),
|
||||||
'creator_subscribers' => 0,
|
'active_subscribers' => (int) ($billingSummary['active_subscribers'] ?? 0),
|
||||||
'pro_subscribers' => 0,
|
'creator_subscribers' => (int) ($billingSummary['creator_subscribers'] ?? 0),
|
||||||
'mrr' => 0,
|
'pro_subscribers' => (int) ($billingSummary['pro_subscribers'] ?? 0),
|
||||||
|
'grace_period_subscribers' => (int) ($billingSummary['grace_period_subscribers'] ?? 0),
|
||||||
],
|
],
|
||||||
'links' => [
|
'links' => [
|
||||||
|
'billing' => route('admin.academy.billing'),
|
||||||
'courses' => route('admin.academy.courses.index'),
|
'courses' => route('admin.academy.courses.index'),
|
||||||
'categories' => route('admin.academy.categories.index'),
|
'categories' => route('admin.academy.categories.index'),
|
||||||
'lessons' => route('admin.academy.lessons.index'),
|
'lessons' => route('admin.academy.lessons.index'),
|
||||||
@@ -83,6 +96,22 @@ final class AcademyAdminController extends Controller
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function billing(): Response
|
||||||
|
{
|
||||||
|
$summary = $this->billingOverview->summary();
|
||||||
|
|
||||||
|
return Inertia::render('Admin/Academy/Billing', [
|
||||||
|
'summary' => $summary,
|
||||||
|
'planBreakdown' => $summary['plan_breakdown'] ?? [],
|
||||||
|
'recentEvents' => $this->billingOverview->recentEvents(),
|
||||||
|
'links' => [
|
||||||
|
'dashboard' => route('admin.academy.dashboard'),
|
||||||
|
'pricing' => route('academy.pricing'),
|
||||||
|
'account' => route('academy.billing.account'),
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
public function categoriesIndex(): Response
|
public function categoriesIndex(): Response
|
||||||
{
|
{
|
||||||
return $this->renderIndex('categories');
|
return $this->renderIndex('categories');
|
||||||
@@ -100,13 +129,20 @@ final class AcademyAdminController extends Controller
|
|||||||
|
|
||||||
public function coursesStore(UpsertAcademyCourseRequest $request): RedirectResponse
|
public function coursesStore(UpsertAcademyCourseRequest $request): RedirectResponse
|
||||||
{
|
{
|
||||||
$course = new AcademyCourse;
|
$course = $this->saveCourseFromRequest($request);
|
||||||
$course->fill($this->persistCourseAttributes($request))->save();
|
|
||||||
$this->cache->clearAll();
|
$this->cache->clearAll();
|
||||||
|
|
||||||
return redirect()->route('admin.academy.courses.edit', ['academyCourse' => $course])->with('success', 'Academy course created.');
|
return redirect()->route('admin.academy.courses.edit', ['academyCourse' => $course])->with('success', 'Academy course created.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function coursesStoreJson(UpsertAcademyCourseRequest $request): RedirectResponse
|
||||||
|
{
|
||||||
|
$course = $this->saveCourseFromRequest($request);
|
||||||
|
$this->cache->clearAll();
|
||||||
|
|
||||||
|
return redirect()->route('admin.academy.courses.edit', ['academyCourse' => $course])->with('success', 'Academy course created from JSON.');
|
||||||
|
}
|
||||||
|
|
||||||
public function coursesEdit(AcademyCourse $academyCourse): Response
|
public function coursesEdit(AcademyCourse $academyCourse): Response
|
||||||
{
|
{
|
||||||
return $this->renderForm('courses', $academyCourse);
|
return $this->renderForm('courses', $academyCourse);
|
||||||
@@ -114,12 +150,94 @@ final class AcademyAdminController extends Controller
|
|||||||
|
|
||||||
public function coursesUpdate(UpsertAcademyCourseRequest $request, AcademyCourse $academyCourse): RedirectResponse
|
public function coursesUpdate(UpsertAcademyCourseRequest $request, AcademyCourse $academyCourse): RedirectResponse
|
||||||
{
|
{
|
||||||
$academyCourse->fill($this->persistCourseAttributes($request, $academyCourse))->save();
|
$this->saveCourseFromRequest($request, $academyCourse);
|
||||||
$this->cache->clearAll();
|
$this->cache->clearAll();
|
||||||
|
|
||||||
return redirect()->route('admin.academy.courses.edit', ['academyCourse' => $academyCourse])->with('success', 'Academy course updated.');
|
return redirect()->route('admin.academy.courses.edit', ['academyCourse' => $academyCourse])->with('success', 'Academy course updated.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function coursesImportLessons(Request $request, AcademyCourse $academyCourse): RedirectResponse
|
||||||
|
{
|
||||||
|
$difficultyLevels = array_values(array_filter(array_map('strval', (array) config('academy.difficulty_levels', []))));
|
||||||
|
|
||||||
|
$validated = $request->validate([
|
||||||
|
'defaults' => ['nullable', 'array'],
|
||||||
|
'defaults.category_id' => ['nullable', 'integer', 'exists:academy_categories,id'],
|
||||||
|
'defaults.category_slug' => ['nullable', 'string', 'max:180'],
|
||||||
|
'defaults.category' => ['nullable', 'string', 'max:180'],
|
||||||
|
'defaults.difficulty' => ['nullable', 'string', Rule::in($difficultyLevels)],
|
||||||
|
'defaults.access_level' => ['nullable', 'string', Rule::in(['free', 'creator', 'pro'])],
|
||||||
|
'defaults.lesson_type' => ['nullable', 'string', 'max:80'],
|
||||||
|
'defaults.active' => ['nullable', 'boolean'],
|
||||||
|
'defaults.series_name' => ['nullable', 'string', 'max:120'],
|
||||||
|
'lessons' => ['required', 'array', 'min:1', 'max:250'],
|
||||||
|
'lessons.*.title' => ['required', 'string', 'max:180'],
|
||||||
|
'lessons.*.slug' => ['nullable', 'string', 'max:180'],
|
||||||
|
'lessons.*.goal' => ['nullable', 'string'],
|
||||||
|
'lessons.*.excerpt' => ['nullable', 'string'],
|
||||||
|
'lessons.*.category_id' => ['nullable', 'integer', 'exists:academy_categories,id'],
|
||||||
|
'lessons.*.category_slug' => ['nullable', 'string', 'max:180'],
|
||||||
|
'lessons.*.category' => ['nullable', 'string', 'max:180'],
|
||||||
|
'lessons.*.difficulty' => ['nullable', 'string', Rule::in($difficultyLevels)],
|
||||||
|
'lessons.*.access_level' => ['nullable', 'string', Rule::in(['free', 'creator', 'pro'])],
|
||||||
|
'lessons.*.lesson_type' => ['nullable', 'string', 'max:80'],
|
||||||
|
'lessons.*.active' => ['nullable', 'boolean'],
|
||||||
|
'lessons.*.series_name' => ['nullable', 'string', 'max:120'],
|
||||||
|
'lessons.*.tags' => ['nullable', 'array'],
|
||||||
|
'lessons.*.tags.*' => ['string', 'max:60'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$defaults = (array) ($validated['defaults'] ?? []);
|
||||||
|
$lessons = array_values((array) ($validated['lessons'] ?? []));
|
||||||
|
|
||||||
|
if ($lessons === []) {
|
||||||
|
throw ValidationException::withMessages([
|
||||||
|
'lessons' => 'Provide at least one lesson row to import.',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
DB::transaction(function () use ($academyCourse, $defaults, $lessons): void {
|
||||||
|
$reservedSlugs = AcademyLesson::query()
|
||||||
|
->pluck('slug')
|
||||||
|
->filter(fn ($slug): bool => is_string($slug) && trim($slug) !== '')
|
||||||
|
->map(fn ($slug): string => trim((string) $slug))
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
|
||||||
|
$nextOrder = (int) ((AcademyCourseLesson::query()->where('course_id', $academyCourse->id)->max('order_num') ?? -1) + 1);
|
||||||
|
|
||||||
|
foreach ($lessons as $lessonData) {
|
||||||
|
$attributes = $this->buildImportedCourseLessonAttributes($academyCourse, (array) $lessonData, $defaults, $reservedSlugs);
|
||||||
|
|
||||||
|
$lesson = new AcademyLesson;
|
||||||
|
$lesson->fill($attributes)->save();
|
||||||
|
|
||||||
|
AcademyCourseLesson::query()->create([
|
||||||
|
'course_id' => $academyCourse->id,
|
||||||
|
'lesson_id' => $lesson->id,
|
||||||
|
'section_id' => null,
|
||||||
|
'order_num' => $nextOrder,
|
||||||
|
'is_required' => true,
|
||||||
|
'access_override' => null,
|
||||||
|
'unlock_after_lesson_id' => null,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$nextOrder++;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->courseLessonOrdering->syncCourse($academyCourse);
|
||||||
|
|
||||||
|
$academyCourse->forceFill([
|
||||||
|
'lessons_count_cache' => (int) AcademyCourseLesson::query()->where('course_id', $academyCourse->id)->count(),
|
||||||
|
])->save();
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->cache->clearAll();
|
||||||
|
|
||||||
|
return redirect()->route('admin.academy.courses.edit', ['academyCourse' => $academyCourse])
|
||||||
|
->with('success', sprintf('%d lesson%s imported into the course.', count($lessons), count($lessons) === 1 ? '' : 's'));
|
||||||
|
}
|
||||||
|
|
||||||
public function coursesDestroy(AcademyCourse $academyCourse): RedirectResponse
|
public function coursesDestroy(AcademyCourse $academyCourse): RedirectResponse
|
||||||
{
|
{
|
||||||
$this->deleteStoredLessonCoverIfLocal((string) $academyCourse->cover_image);
|
$this->deleteStoredLessonCoverIfLocal((string) $academyCourse->cover_image);
|
||||||
@@ -484,12 +602,40 @@ final class AcademyAdminController extends Controller
|
|||||||
private function renderIndex(string $resource): Response
|
private function renderIndex(string $resource): Response
|
||||||
{
|
{
|
||||||
$meta = $this->resourceMeta($resource);
|
$meta = $this->resourceMeta($resource);
|
||||||
$query = $meta['model']::query()->latest('updated_at');
|
$search = trim((string) request()->query('search', ''));
|
||||||
|
$query = $meta['model']::query();
|
||||||
|
|
||||||
|
if ($resource === 'courses') {
|
||||||
|
$query->withCount('courseLessons');
|
||||||
|
|
||||||
|
if ($search !== '') {
|
||||||
|
$query->where(function ($builder) use ($search): void {
|
||||||
|
$like = '%'.str_replace(['%', '_'], ['\\%', '\\_'], $search).'%';
|
||||||
|
|
||||||
|
$builder->where('title', 'like', $like)
|
||||||
|
->orWhere('slug', 'like', $like)
|
||||||
|
->orWhere('subtitle', 'like', $like)
|
||||||
|
->orWhere('excerpt', 'like', $like)
|
||||||
|
->orWhere('description', 'like', $like);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
$query->orderByDesc('is_featured')
|
||||||
|
->orderBy('order_num')
|
||||||
|
->orderByDesc('updated_at')
|
||||||
|
->orderByDesc('id');
|
||||||
|
} else {
|
||||||
|
$query->latest('updated_at');
|
||||||
|
}
|
||||||
|
|
||||||
if ($resource === 'prompts') {
|
if ($resource === 'prompts') {
|
||||||
$query->with('category');
|
$query->with('category');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($resource === 'lessons') {
|
||||||
|
$query->with('courses:id,title');
|
||||||
|
}
|
||||||
|
|
||||||
$items = $query->paginate(25)->withQueryString();
|
$items = $query->paginate(25)->withQueryString();
|
||||||
$items->getCollection()->transform(fn (Model $model): array => $this->serializeIndexItem($resource, $model));
|
$items->getCollection()->transform(fn (Model $model): array => $this->serializeIndexItem($resource, $model));
|
||||||
|
|
||||||
@@ -500,6 +646,45 @@ final class AcademyAdminController extends Controller
|
|||||||
'items' => $items,
|
'items' => $items,
|
||||||
'columns' => $meta['columns'],
|
'columns' => $meta['columns'],
|
||||||
'createUrl' => route($meta['route_base'].'.create'),
|
'createUrl' => route($meta['route_base'].'.create'),
|
||||||
|
'filters' => [
|
||||||
|
'search' => $search,
|
||||||
|
],
|
||||||
|
'summary' => $resource === 'courses' ? [
|
||||||
|
'total' => (int) $items->total(),
|
||||||
|
'published' => (int) (clone $meta['model']::query())->when($search !== '', function ($builder) use ($search): void {
|
||||||
|
$like = '%'.str_replace(['%', '_'], ['\\%', '\\_'], $search).'%';
|
||||||
|
|
||||||
|
$builder->where(function ($inner) use ($like): void {
|
||||||
|
$inner->where('title', 'like', $like)
|
||||||
|
->orWhere('slug', 'like', $like)
|
||||||
|
->orWhere('subtitle', 'like', $like)
|
||||||
|
->orWhere('excerpt', 'like', $like)
|
||||||
|
->orWhere('description', 'like', $like);
|
||||||
|
});
|
||||||
|
})->where('status', AcademyCourse::STATUS_PUBLISHED)->count(),
|
||||||
|
'featured' => (int) (clone $meta['model']::query())->when($search !== '', function ($builder) use ($search): void {
|
||||||
|
$like = '%'.str_replace(['%', '_'], ['\\%', '\\_'], $search).'%';
|
||||||
|
|
||||||
|
$builder->where(function ($inner) use ($like): void {
|
||||||
|
$inner->where('title', 'like', $like)
|
||||||
|
->orWhere('slug', 'like', $like)
|
||||||
|
->orWhere('subtitle', 'like', $like)
|
||||||
|
->orWhere('excerpt', 'like', $like)
|
||||||
|
->orWhere('description', 'like', $like);
|
||||||
|
});
|
||||||
|
})->where('is_featured', true)->count(),
|
||||||
|
'drafts' => (int) (clone $meta['model']::query())->when($search !== '', function ($builder) use ($search): void {
|
||||||
|
$like = '%'.str_replace(['%', '_'], ['\\%', '\\_'], $search).'%';
|
||||||
|
|
||||||
|
$builder->where(function ($inner) use ($like): void {
|
||||||
|
$inner->where('title', 'like', $like)
|
||||||
|
->orWhere('slug', 'like', $like)
|
||||||
|
->orWhere('subtitle', 'like', $like)
|
||||||
|
->orWhere('excerpt', 'like', $like)
|
||||||
|
->orWhere('description', 'like', $like);
|
||||||
|
});
|
||||||
|
})->where('status', AcademyCourse::STATUS_DRAFT)->count(),
|
||||||
|
] : null,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -538,6 +723,9 @@ final class AcademyAdminController extends Controller
|
|||||||
'outlineSummary' => $record instanceof AcademyCourse && $record->exists
|
'outlineSummary' => $record instanceof AcademyCourse && $record->exists
|
||||||
? $this->serializeCourseOutlineSummary($record)
|
? $this->serializeCourseOutlineSummary($record)
|
||||||
: null,
|
: null,
|
||||||
|
'courseSections' => $record instanceof AcademyCourse && $record->exists
|
||||||
|
? $this->serializeCourseEditorSections($record)
|
||||||
|
: [],
|
||||||
'courseLessons' => $record instanceof AcademyCourse && $record->exists
|
'courseLessons' => $record instanceof AcademyCourse && $record->exists
|
||||||
? $this->serializeCourseEditorLessons($record)
|
? $this->serializeCourseEditorLessons($record)
|
||||||
: [],
|
: [],
|
||||||
@@ -547,9 +735,19 @@ final class AcademyAdminController extends Controller
|
|||||||
'attachLessonUrl' => $record instanceof AcademyCourse && $record->exists
|
'attachLessonUrl' => $record instanceof AcademyCourse && $record->exists
|
||||||
? route('admin.academy.courses.lessons.attach', ['academyCourse' => $record])
|
? route('admin.academy.courses.lessons.attach', ['academyCourse' => $record])
|
||||||
: null,
|
: null,
|
||||||
|
'importLessonsUrl' => $record instanceof AcademyCourse && $record->exists
|
||||||
|
? route('admin.academy.courses.lessons.import', ['academyCourse' => $record])
|
||||||
|
: null,
|
||||||
|
'sectionStoreUrl' => $record instanceof AcademyCourse && $record->exists
|
||||||
|
? route('admin.academy.courses.sections.store', ['academyCourse' => $record])
|
||||||
|
: null,
|
||||||
'reorderUrl' => $record instanceof AcademyCourse && $record->exists
|
'reorderUrl' => $record instanceof AcademyCourse && $record->exists
|
||||||
? route('admin.academy.courses.reorder', ['academyCourse' => $record])
|
? route('admin.academy.courses.reorder', ['academyCourse' => $record])
|
||||||
: null,
|
: null,
|
||||||
|
'courseImportUrl' => $record instanceof AcademyCourse && ! $record->exists
|
||||||
|
? route('admin.academy.courses.import-json')
|
||||||
|
: null,
|
||||||
|
'lessonCategoryOptions' => $this->categoriesForEditor('lesson'),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -656,7 +854,7 @@ final class AcademyAdminController extends Controller
|
|||||||
'singular' => 'lesson',
|
'singular' => 'lesson',
|
||||||
'subtitle' => 'Create and publish Academy lessons.',
|
'subtitle' => 'Create and publish Academy lessons.',
|
||||||
'route_base' => 'admin.academy.lessons',
|
'route_base' => 'admin.academy.lessons',
|
||||||
'columns' => ['title', 'difficulty', 'access_level', 'featured', 'active'],
|
'columns' => ['title', 'course_names', 'course_order', 'difficulty', 'access_level', 'active'],
|
||||||
'fields' => [
|
'fields' => [
|
||||||
['name' => 'category_id', 'label' => 'Category', 'type' => 'select', 'options' => $this->categoryOptions('lesson')],
|
['name' => 'category_id', 'label' => 'Category', 'type' => 'select', 'options' => $this->categoryOptions('lesson')],
|
||||||
['name' => 'course_ids', 'label' => 'Courses', 'type' => 'multiselect', 'options' => $this->courseOptions()],
|
['name' => 'course_ids', 'label' => 'Courses', 'type' => 'multiselect', 'options' => $this->courseOptions()],
|
||||||
@@ -694,6 +892,10 @@ final class AcademyAdminController extends Controller
|
|||||||
['name' => 'negative_prompt', 'label' => 'Negative Prompt', 'type' => 'textarea'],
|
['name' => 'negative_prompt', 'label' => 'Negative Prompt', 'type' => 'textarea'],
|
||||||
['name' => 'usage_notes', 'label' => 'Usage Notes', 'type' => 'textarea'],
|
['name' => 'usage_notes', 'label' => 'Usage Notes', 'type' => 'textarea'],
|
||||||
['name' => 'workflow_notes', 'label' => 'Workflow Notes', 'type' => 'textarea'],
|
['name' => 'workflow_notes', 'label' => 'Workflow Notes', 'type' => 'textarea'],
|
||||||
|
['name' => 'documentation', 'label' => 'Documentation JSON', 'type' => 'json'],
|
||||||
|
['name' => 'placeholders', 'label' => 'Placeholders JSON', 'type' => 'json'],
|
||||||
|
['name' => 'helper_prompts', 'label' => 'Helper Prompts JSON', 'type' => 'json'],
|
||||||
|
['name' => 'prompt_variants', 'label' => 'Prompt Variants JSON', 'type' => 'json'],
|
||||||
['name' => 'difficulty', 'label' => 'Difficulty', 'type' => 'select', 'options' => $this->difficultyOptions()],
|
['name' => 'difficulty', 'label' => 'Difficulty', 'type' => 'select', 'options' => $this->difficultyOptions()],
|
||||||
['name' => 'access_level', 'label' => 'Access', 'type' => 'select', 'options' => $this->accessOptions()],
|
['name' => 'access_level', 'label' => 'Access', 'type' => 'select', 'options' => $this->accessOptions()],
|
||||||
['name' => 'aspect_ratio', 'label' => 'Aspect Ratio', 'type' => 'text'],
|
['name' => 'aspect_ratio', 'label' => 'Aspect Ratio', 'type' => 'text'],
|
||||||
@@ -785,10 +987,17 @@ final class AcademyAdminController extends Controller
|
|||||||
'courses' => [
|
'courses' => [
|
||||||
'id' => (int) $model->id,
|
'id' => (int) $model->id,
|
||||||
'title' => (string) $model->title,
|
'title' => (string) $model->title,
|
||||||
|
'slug' => (string) $model->slug,
|
||||||
|
'subtitle' => (string) ($model->subtitle ?? ''),
|
||||||
|
'excerpt' => (string) ($model->excerpt ?? ''),
|
||||||
|
'cover_image_url' => $this->resolveLessonCoverImageUrl((string) ($model->cover_image ?? '')),
|
||||||
|
'lessons_count' => (int) ($model->lessons_count_cache ?? $model->course_lessons_count ?? 0),
|
||||||
'difficulty' => (string) $model->difficulty,
|
'difficulty' => (string) $model->difficulty,
|
||||||
'access_level' => (string) $model->access_level,
|
'access_level' => (string) $model->access_level,
|
||||||
'status' => (string) $model->status,
|
'status' => (string) $model->status,
|
||||||
'is_featured' => (bool) $model->is_featured,
|
'is_featured' => (bool) $model->is_featured,
|
||||||
|
'published_at' => optional($model->published_at)->toIso8601String(),
|
||||||
|
'updated_at' => optional($model->updated_at)->toIso8601String(),
|
||||||
'edit_url' => route('admin.academy.courses.edit', ['academyCourse' => $model]),
|
'edit_url' => route('admin.academy.courses.edit', ['academyCourse' => $model]),
|
||||||
'destroy_url' => route('admin.academy.courses.destroy', ['academyCourse' => $model]),
|
'destroy_url' => route('admin.academy.courses.destroy', ['academyCourse' => $model]),
|
||||||
'builder_url' => route('admin.academy.courses.builder.edit', ['academyCourse' => $model]),
|
'builder_url' => route('admin.academy.courses.builder.edit', ['academyCourse' => $model]),
|
||||||
@@ -805,6 +1014,8 @@ final class AcademyAdminController extends Controller
|
|||||||
'lessons' => [
|
'lessons' => [
|
||||||
'id' => (int) $model->id,
|
'id' => (int) $model->id,
|
||||||
'title' => (string) $model->title,
|
'title' => (string) $model->title,
|
||||||
|
'course_names' => $model->courses->pluck('title')->filter()->values()->all(),
|
||||||
|
'course_order' => $model->course_order,
|
||||||
'difficulty' => (string) $model->difficulty,
|
'difficulty' => (string) $model->difficulty,
|
||||||
'access_level' => (string) $model->access_level,
|
'access_level' => (string) $model->access_level,
|
||||||
'featured' => (bool) $model->featured,
|
'featured' => (bool) $model->featured,
|
||||||
@@ -941,6 +1152,10 @@ final class AcademyAdminController extends Controller
|
|||||||
'negative_prompt' => (string) ($record->negative_prompt ?? ''),
|
'negative_prompt' => (string) ($record->negative_prompt ?? ''),
|
||||||
'usage_notes' => (string) ($record->usage_notes ?? ''),
|
'usage_notes' => (string) ($record->usage_notes ?? ''),
|
||||||
'workflow_notes' => (string) ($record->workflow_notes ?? ''),
|
'workflow_notes' => (string) ($record->workflow_notes ?? ''),
|
||||||
|
'documentation' => $this->encodePrettyJsonForForm($record->documentation),
|
||||||
|
'placeholders' => $this->encodePrettyJsonForForm($record->placeholders),
|
||||||
|
'helper_prompts' => $this->encodePrettyJsonForForm($record->helper_prompts),
|
||||||
|
'prompt_variants' => $this->encodePrettyJsonForForm($record->prompt_variants),
|
||||||
'difficulty' => (string) ($record->difficulty ?? 'beginner'),
|
'difficulty' => (string) ($record->difficulty ?? 'beginner'),
|
||||||
'access_level' => (string) ($record->access_level ?? 'free'),
|
'access_level' => (string) ($record->access_level ?? 'free'),
|
||||||
'aspect_ratio' => (string) ($record->aspect_ratio ?? ''),
|
'aspect_ratio' => (string) ($record->aspect_ratio ?? ''),
|
||||||
@@ -1464,9 +1679,46 @@ final class AcademyAdminController extends Controller
|
|||||||
return $validated;
|
return $validated;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function saveCourseFromRequest(UpsertAcademyCourseRequest $request, ?AcademyCourse $course = null): AcademyCourse
|
||||||
|
{
|
||||||
|
$course ??= new AcademyCourse;
|
||||||
|
$course->fill($this->persistCourseAttributes($request, $course))->save();
|
||||||
|
|
||||||
|
return $course;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array<string, mixed>
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
|
/**
|
||||||
|
* @return array<int, array<string, mixed>>
|
||||||
|
*/
|
||||||
|
private function serializeCourseEditorSections(AcademyCourse $course): array
|
||||||
|
{
|
||||||
|
$course->loadMissing(['sections']);
|
||||||
|
|
||||||
|
return $course->sections
|
||||||
|
->sortBy([['order_num', 'asc'], ['id', 'asc']])
|
||||||
|
->values()
|
||||||
|
->map(fn (AcademyCourseSection $section): array => [
|
||||||
|
'id' => (int) $section->id,
|
||||||
|
'title' => (string) $section->title,
|
||||||
|
'slug' => (string) ($section->slug ?? ''),
|
||||||
|
'description' => (string) ($section->description ?? ''),
|
||||||
|
'order_num' => (int) ($section->order_num ?? 0),
|
||||||
|
'is_visible' => (bool) ($section->is_visible ?? true),
|
||||||
|
'update_url' => route('admin.academy.courses.sections.update', [
|
||||||
|
'academyCourse' => $course,
|
||||||
|
'academyCourseSection' => $section,
|
||||||
|
]),
|
||||||
|
'destroy_url' => route('admin.academy.courses.sections.destroy', [
|
||||||
|
'academyCourse' => $course,
|
||||||
|
'academyCourseSection' => $section,
|
||||||
|
]),
|
||||||
|
])
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array<int, array<string, mixed>>
|
* @return array<int, array<string, mixed>>
|
||||||
*/
|
*/
|
||||||
@@ -1479,12 +1731,17 @@ final class AcademyAdminController extends Controller
|
|||||||
->values()
|
->values()
|
||||||
->map(function (AcademyCourseLesson $courseLesson) use ($course): array {
|
->map(function (AcademyCourseLesson $courseLesson) use ($course): array {
|
||||||
$lesson = $courseLesson->lesson;
|
$lesson = $courseLesson->lesson;
|
||||||
|
$publicationMeta = $this->serializeLessonPublicationMeta($lesson instanceof AcademyLesson ? $lesson : null);
|
||||||
|
|
||||||
return [
|
return array_merge([
|
||||||
'id' => (int) $courseLesson->id,
|
'id' => (int) $courseLesson->id,
|
||||||
'lesson_id' => (int) $courseLesson->lesson_id,
|
'lesson_id' => (int) $courseLesson->lesson_id,
|
||||||
'title' => (string) ($lesson?->title ?? 'Untitled lesson'),
|
'title' => (string) ($lesson?->title ?? 'Untitled lesson'),
|
||||||
'slug' => (string) ($lesson?->slug ?? ''),
|
'slug' => (string) ($lesson?->slug ?? ''),
|
||||||
|
'cover_image' => (string) ($lesson?->cover_image ?? ''),
|
||||||
|
'cover_image_url' => $lesson instanceof AcademyLesson
|
||||||
|
? $this->resolveLessonCoverImageUrl((string) ($lesson->cover_image ?: $lesson->article_cover_image ?? ''))
|
||||||
|
: null,
|
||||||
'section_id' => $courseLesson->section_id ? (int) $courseLesson->section_id : null,
|
'section_id' => $courseLesson->section_id ? (int) $courseLesson->section_id : null,
|
||||||
'section_title' => (string) ($courseLesson->section?->title ?? ''),
|
'section_title' => (string) ($courseLesson->section?->title ?? ''),
|
||||||
'order_num' => (int) ($courseLesson->order_num ?? 0),
|
'order_num' => (int) ($courseLesson->order_num ?? 0),
|
||||||
@@ -1493,6 +1750,7 @@ final class AcademyAdminController extends Controller
|
|||||||
'is_required' => (bool) $courseLesson->is_required,
|
'is_required' => (bool) $courseLesson->is_required,
|
||||||
'difficulty' => (string) ($lesson?->difficulty ?? ''),
|
'difficulty' => (string) ($lesson?->difficulty ?? ''),
|
||||||
'access_level' => (string) ($lesson?->access_level ?? ''),
|
'access_level' => (string) ($lesson?->access_level ?? ''),
|
||||||
|
'active' => (bool) ($lesson?->active ?? false),
|
||||||
'destroy_url' => route('admin.academy.courses.lessons.destroy', [
|
'destroy_url' => route('admin.academy.courses.lessons.destroy', [
|
||||||
'academyCourse' => $course,
|
'academyCourse' => $course,
|
||||||
'academyCourseLesson' => $courseLesson,
|
'academyCourseLesson' => $courseLesson,
|
||||||
@@ -1500,42 +1758,208 @@ final class AcademyAdminController extends Controller
|
|||||||
'edit_url' => $lesson instanceof AcademyLesson
|
'edit_url' => $lesson instanceof AcademyLesson
|
||||||
? route('admin.academy.lessons.edit', ['academyLesson' => $lesson])
|
? route('admin.academy.lessons.edit', ['academyLesson' => $lesson])
|
||||||
: null,
|
: null,
|
||||||
];
|
], $publicationMeta);
|
||||||
})
|
})
|
||||||
->all();
|
->all();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $lessonData
|
||||||
|
* @param array<string, mixed> $defaults
|
||||||
|
* @param array<int, string> $reservedSlugs
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
private function buildImportedCourseLessonAttributes(AcademyCourse $course, array $lessonData, array $defaults, array &$reservedSlugs): array
|
||||||
|
{
|
||||||
|
$title = trim((string) ($lessonData['title'] ?? ''));
|
||||||
|
$slugSource = $this->nullableTrimmedString($lessonData['slug'] ?? null) ?? $title;
|
||||||
|
$excerpt = $this->nullableTrimmedString($lessonData['excerpt'] ?? null)
|
||||||
|
?? $this->nullableTrimmedString($lessonData['goal'] ?? null);
|
||||||
|
$difficulty = $this->nullableTrimmedString($lessonData['difficulty'] ?? null)
|
||||||
|
?? $this->nullableTrimmedString($defaults['difficulty'] ?? null)
|
||||||
|
?? $this->nullableTrimmedString($course->difficulty)
|
||||||
|
?? 'beginner';
|
||||||
|
$accessLevel = $this->nullableTrimmedString($lessonData['access_level'] ?? null)
|
||||||
|
?? $this->nullableTrimmedString($defaults['access_level'] ?? null)
|
||||||
|
?? 'free';
|
||||||
|
$lessonType = $this->nullableTrimmedString($lessonData['lesson_type'] ?? null)
|
||||||
|
?? $this->nullableTrimmedString($defaults['lesson_type'] ?? null)
|
||||||
|
?? 'article';
|
||||||
|
$seriesName = $this->nullableTrimmedString($lessonData['series_name'] ?? null)
|
||||||
|
?? $this->nullableTrimmedString($defaults['series_name'] ?? null)
|
||||||
|
?? $this->nullableTrimmedString($course->title);
|
||||||
|
$active = array_key_exists('active', $lessonData)
|
||||||
|
? (bool) $lessonData['active']
|
||||||
|
: (array_key_exists('active', $defaults) ? (bool) $defaults['active'] : false);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'category_id' => $this->resolveImportedLessonCategoryId($lessonData, $defaults),
|
||||||
|
'title' => $title,
|
||||||
|
'slug' => $this->reserveImportedLessonSlug($slugSource, $reservedSlugs),
|
||||||
|
'lesson_number' => null,
|
||||||
|
'course_order' => null,
|
||||||
|
'series_name' => $seriesName,
|
||||||
|
'excerpt' => $excerpt,
|
||||||
|
'content' => null,
|
||||||
|
'content_markdown' => null,
|
||||||
|
'difficulty' => $difficulty,
|
||||||
|
'access_level' => $accessLevel,
|
||||||
|
'lesson_type' => $lessonType,
|
||||||
|
'cover_image' => null,
|
||||||
|
'article_cover_image' => null,
|
||||||
|
'tags' => collect((array) ($lessonData['tags'] ?? []))
|
||||||
|
->map(fn ($tag): string => trim((string) $tag))
|
||||||
|
->filter(fn (string $tag): bool => $tag !== '')
|
||||||
|
->values()
|
||||||
|
->all(),
|
||||||
|
'video_url' => null,
|
||||||
|
'reading_minutes' => 5,
|
||||||
|
'featured' => false,
|
||||||
|
'active' => $active,
|
||||||
|
'published_at' => null,
|
||||||
|
'seo_title' => null,
|
||||||
|
'seo_description' => $excerpt,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $lessonData
|
||||||
|
* @param array<string, mixed> $defaults
|
||||||
|
*/
|
||||||
|
private function resolveImportedLessonCategoryId(array $lessonData, array $defaults): ?int
|
||||||
|
{
|
||||||
|
foreach ([$lessonData, $defaults] as $source) {
|
||||||
|
if ($source === []) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$categoryId = $source['category_id'] ?? null;
|
||||||
|
if ($categoryId !== null && AcademyCategory::query()->where('type', 'lesson')->whereKey((int) $categoryId)->exists()) {
|
||||||
|
return (int) $categoryId;
|
||||||
|
}
|
||||||
|
|
||||||
|
$categorySlug = $this->nullableTrimmedString($source['category_slug'] ?? null);
|
||||||
|
if ($categorySlug !== null) {
|
||||||
|
$category = AcademyCategory::query()->where('type', 'lesson')->where('slug', $categorySlug)->first();
|
||||||
|
if ($category instanceof AcademyCategory) {
|
||||||
|
return (int) $category->id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$categoryName = $this->nullableTrimmedString($source['category'] ?? null);
|
||||||
|
if ($categoryName !== null) {
|
||||||
|
$category = AcademyCategory::query()->where('type', 'lesson')->whereRaw('lower(name) = ?', [Str::lower($categoryName)])->first();
|
||||||
|
if ($category instanceof AcademyCategory) {
|
||||||
|
return (int) $category->id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<int, string> $reservedSlugs
|
||||||
|
*/
|
||||||
|
private function reserveImportedLessonSlug(string $source, array &$reservedSlugs): string
|
||||||
|
{
|
||||||
|
$base = Str::slug($source);
|
||||||
|
|
||||||
|
if ($base === '') {
|
||||||
|
$base = 'academy-lesson';
|
||||||
|
}
|
||||||
|
|
||||||
|
$candidate = $base;
|
||||||
|
$suffix = 2;
|
||||||
|
|
||||||
|
while (in_array($candidate, $reservedSlugs, true)) {
|
||||||
|
$candidate = $base.'-'.$suffix;
|
||||||
|
$suffix++;
|
||||||
|
}
|
||||||
|
|
||||||
|
$reservedSlugs[] = $candidate;
|
||||||
|
|
||||||
|
return $candidate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, array<string, mixed>>
|
||||||
|
*/
|
||||||
|
private function categoriesForEditor(string $type): array
|
||||||
|
{
|
||||||
|
return AcademyCategory::query()
|
||||||
|
->where('type', $type)
|
||||||
|
->orderBy('order_num')
|
||||||
|
->orderBy('name')
|
||||||
|
->get()
|
||||||
|
->map(fn (AcademyCategory $category): array => $this->serializeCategoryOption($category))
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array<int, array<string, mixed>>
|
* @return array<int, array<string, mixed>>
|
||||||
*/
|
*/
|
||||||
private function serializeCourseAvailableLessons(AcademyCourse $course): array
|
private function serializeCourseAvailableLessons(AcademyCourse $course): array
|
||||||
{
|
{
|
||||||
$course->loadMissing(['courseLessons']);
|
|
||||||
|
|
||||||
$attachedLessonIds = $course->courseLessons
|
|
||||||
->pluck('lesson_id')
|
|
||||||
->map(fn ($id): int => (int) $id)
|
|
||||||
->flip()
|
|
||||||
->all();
|
|
||||||
|
|
||||||
return AcademyLesson::query()
|
return AcademyLesson::query()
|
||||||
|
->whereDoesntHave('courseLessons')
|
||||||
->with('category')
|
->with('category')
|
||||||
->orderBy('title')
|
->orderBy('title')
|
||||||
->get()
|
->get()
|
||||||
->map(fn (AcademyLesson $lesson): array => [
|
->map(function (AcademyLesson $lesson): array {
|
||||||
|
$publicationMeta = $this->serializeLessonPublicationMeta($lesson);
|
||||||
|
|
||||||
|
return array_merge([
|
||||||
'id' => (int) $lesson->id,
|
'id' => (int) $lesson->id,
|
||||||
'title' => (string) $lesson->title,
|
'title' => (string) $lesson->title,
|
||||||
'slug' => (string) $lesson->slug,
|
'slug' => (string) $lesson->slug,
|
||||||
|
'cover_image' => (string) ($lesson->cover_image ?? ''),
|
||||||
|
'cover_image_url' => $this->resolveLessonCoverImageUrl((string) ($lesson->cover_image ?: $lesson->article_cover_image ?? '')),
|
||||||
'difficulty' => (string) $lesson->difficulty,
|
'difficulty' => (string) $lesson->difficulty,
|
||||||
'access_level' => (string) $lesson->access_level,
|
'access_level' => (string) $lesson->access_level,
|
||||||
'active' => (bool) $lesson->active,
|
'active' => (bool) $lesson->active,
|
||||||
'category' => $lesson->category ? (string) $lesson->category->name : '',
|
'category' => $lesson->category ? (string) $lesson->category->name : '',
|
||||||
'attached' => isset($attachedLessonIds[(int) $lesson->id]),
|
'edit_url' => route('admin.academy.lessons.edit', ['academyLesson' => $lesson]),
|
||||||
])
|
'attached' => false,
|
||||||
|
], $publicationMeta);
|
||||||
|
})
|
||||||
->values()
|
->values()
|
||||||
->all();
|
->all();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, string|null>
|
||||||
|
*/
|
||||||
|
private function serializeLessonPublicationMeta(?AcademyLesson $lesson): array
|
||||||
|
{
|
||||||
|
$publishedAt = $lesson?->published_at instanceof Carbon
|
||||||
|
? $lesson->published_at->copy()
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (! $publishedAt) {
|
||||||
|
return [
|
||||||
|
'published_at' => null,
|
||||||
|
'publication_state' => 'draft',
|
||||||
|
'publication_label' => 'Unscheduled',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($publishedAt->isFuture()) {
|
||||||
|
return [
|
||||||
|
'published_at' => $publishedAt->toIso8601String(),
|
||||||
|
'publication_state' => 'scheduled',
|
||||||
|
'publication_label' => 'Publishes '.$publishedAt->format('Y-m-d H:i'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'published_at' => $publishedAt->toIso8601String(),
|
||||||
|
'publication_state' => 'published',
|
||||||
|
'publication_label' => 'Published',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
private function serializeCourseOutlineSummary(AcademyCourse $course): array
|
private function serializeCourseOutlineSummary(AcademyCourse $course): array
|
||||||
{
|
{
|
||||||
$course->loadMissing(['sections', 'courseLessons']);
|
$course->loadMissing(['sections', 'courseLessons']);
|
||||||
@@ -1734,6 +2158,10 @@ final class AcademyAdminController extends Controller
|
|||||||
$validated['category_id'] = $this->resolveOrCreatePromptCategoryId($newCategoryName);
|
$validated['category_id'] = $this->resolveOrCreatePromptCategoryId($newCategoryName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$validated['documentation'] = $this->normalizePromptDocumentation($validated['documentation'] ?? null);
|
||||||
|
$validated['placeholders'] = $this->normalizePromptPlaceholders($validated['placeholders'] ?? null);
|
||||||
|
$validated['helper_prompts'] = $this->normalizePromptHelperPrompts($validated['helper_prompts'] ?? null);
|
||||||
|
$validated['prompt_variants'] = $this->normalizePromptVariants($validated['prompt_variants'] ?? null);
|
||||||
$validated['tool_notes'] = $this->normalizePromptToolNotes((array) ($validated['tool_notes'] ?? []));
|
$validated['tool_notes'] = $this->normalizePromptToolNotes((array) ($validated['tool_notes'] ?? []));
|
||||||
$previousToolNotes = $this->normalizePromptToolNotes((array) ($prompt?->tool_notes ?? []));
|
$previousToolNotes = $this->normalizePromptToolNotes((array) ($prompt?->tool_notes ?? []));
|
||||||
|
|
||||||
@@ -1803,6 +2231,172 @@ final class AcademyAdminController extends Controller
|
|||||||
->all();
|
->all();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function encodePrettyJsonForForm(mixed $value): string
|
||||||
|
{
|
||||||
|
if ($value === null || $value === [] || $value === '') {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return (string) json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>|null
|
||||||
|
*/
|
||||||
|
private function normalizePromptDocumentation(mixed $documentation): ?array
|
||||||
|
{
|
||||||
|
if (! is_array($documentation)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$listFields = ['best_for', 'how_to_use', 'required_inputs', 'workflow', 'tips', 'common_mistakes', 'data_accuracy_notes'];
|
||||||
|
$normalized = [
|
||||||
|
'summary' => $this->nullableTrimmedString($documentation['summary'] ?? null),
|
||||||
|
'display_notes' => $this->nullableTrimmedString($documentation['display_notes'] ?? null),
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($listFields as $field) {
|
||||||
|
$normalized[$field] = $this->normalizePromptStringList($documentation[$field] ?? []);
|
||||||
|
}
|
||||||
|
|
||||||
|
$hasContent = $normalized['summary'] !== null
|
||||||
|
|| $normalized['display_notes'] !== null
|
||||||
|
|| collect($listFields)->contains(fn (string $field): bool => $normalized[$field] !== []);
|
||||||
|
|
||||||
|
return $hasContent ? $normalized : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, array<string, mixed>>
|
||||||
|
*/
|
||||||
|
private function normalizePromptPlaceholders(mixed $placeholders): array
|
||||||
|
{
|
||||||
|
if (! is_array($placeholders)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return collect($placeholders)
|
||||||
|
->filter(static fn ($placeholder): bool => is_array($placeholder))
|
||||||
|
->map(function (array $placeholder): array {
|
||||||
|
return [
|
||||||
|
'key' => $this->nullableTrimmedString($placeholder['key'] ?? null),
|
||||||
|
'label' => $this->nullableTrimmedString($placeholder['label'] ?? null),
|
||||||
|
'description' => $this->nullableTrimmedString($placeholder['description'] ?? null),
|
||||||
|
'required' => filter_var($placeholder['required'] ?? false, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE) ?? false,
|
||||||
|
'example' => $this->normalizePromptJsonValue($placeholder['example'] ?? null),
|
||||||
|
'default' => $this->normalizePromptJsonValue($placeholder['default'] ?? null),
|
||||||
|
'type' => $this->nullableTrimmedString($placeholder['type'] ?? null),
|
||||||
|
];
|
||||||
|
})
|
||||||
|
->filter(function (array $placeholder): bool {
|
||||||
|
return collect([
|
||||||
|
$placeholder['key'] ?? null,
|
||||||
|
$placeholder['label'] ?? null,
|
||||||
|
$placeholder['description'] ?? null,
|
||||||
|
$placeholder['example'] ?? null,
|
||||||
|
$placeholder['default'] ?? null,
|
||||||
|
$placeholder['type'] ?? null,
|
||||||
|
])->contains(fn ($item): bool => $item !== null && $item !== '' && $item !== []);
|
||||||
|
})
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, array<string, mixed>>
|
||||||
|
*/
|
||||||
|
private function normalizePromptHelperPrompts(mixed $helperPrompts): array
|
||||||
|
{
|
||||||
|
if (! is_array($helperPrompts)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return collect($helperPrompts)
|
||||||
|
->filter(static fn ($helperPrompt): bool => is_array($helperPrompt))
|
||||||
|
->map(function (array $helperPrompt): array {
|
||||||
|
return [
|
||||||
|
'title' => $this->nullableTrimmedString($helperPrompt['title'] ?? null),
|
||||||
|
'type' => $this->nullableTrimmedString($helperPrompt['type'] ?? null) ?? 'other',
|
||||||
|
'description' => $this->nullableTrimmedString($helperPrompt['description'] ?? null),
|
||||||
|
'prompt' => $this->nullableTrimmedString($helperPrompt['prompt'] ?? null),
|
||||||
|
'expected_output' => $this->nullableTrimmedString($helperPrompt['expected_output'] ?? null) ?? 'text',
|
||||||
|
'active' => filter_var($helperPrompt['active'] ?? true, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE) ?? true,
|
||||||
|
];
|
||||||
|
})
|
||||||
|
->filter(function (array $helperPrompt): bool {
|
||||||
|
return collect([
|
||||||
|
$helperPrompt['title'] ?? null,
|
||||||
|
$helperPrompt['description'] ?? null,
|
||||||
|
$helperPrompt['prompt'] ?? null,
|
||||||
|
])->contains(fn ($item): bool => $item !== null && $item !== '');
|
||||||
|
})
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, array<string, mixed>>
|
||||||
|
*/
|
||||||
|
private function normalizePromptVariants(mixed $variants): array
|
||||||
|
{
|
||||||
|
if (! is_array($variants)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return collect($variants)
|
||||||
|
->filter(static fn ($variant): bool => is_array($variant))
|
||||||
|
->map(function (array $variant): array {
|
||||||
|
return [
|
||||||
|
'title' => $this->nullableTrimmedString($variant['title'] ?? null),
|
||||||
|
'slug' => $this->nullableTrimmedString($variant['slug'] ?? null),
|
||||||
|
'description' => $this->nullableTrimmedString($variant['description'] ?? null),
|
||||||
|
'prompt' => $this->nullableTrimmedString($variant['prompt'] ?? null),
|
||||||
|
'negative_prompt' => $this->nullableTrimmedString($variant['negative_prompt'] ?? null),
|
||||||
|
'recommended' => filter_var($variant['recommended'] ?? false, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE) ?? false,
|
||||||
|
'recommended_for' => $this->normalizePromptStringList($variant['recommended_for'] ?? []),
|
||||||
|
'risk_notes' => $this->normalizePromptStringList($variant['risk_notes'] ?? []),
|
||||||
|
'active' => filter_var($variant['active'] ?? true, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE) ?? true,
|
||||||
|
];
|
||||||
|
})
|
||||||
|
->filter(function (array $variant): bool {
|
||||||
|
return collect([
|
||||||
|
$variant['title'] ?? null,
|
||||||
|
$variant['description'] ?? null,
|
||||||
|
$variant['prompt'] ?? null,
|
||||||
|
$variant['negative_prompt'] ?? null,
|
||||||
|
])->contains(fn ($item): bool => $item !== null && $item !== '');
|
||||||
|
})
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, string>
|
||||||
|
*/
|
||||||
|
private function normalizePromptStringList(mixed $value): array
|
||||||
|
{
|
||||||
|
if (! is_array($value)) {
|
||||||
|
$value = $value === null ? [] : [$value];
|
||||||
|
}
|
||||||
|
|
||||||
|
return collect($value)
|
||||||
|
->map(fn ($item): string => trim((string) $item))
|
||||||
|
->filter(static fn (string $item): bool => $item !== '')
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function normalizePromptJsonValue(mixed $value): mixed
|
||||||
|
{
|
||||||
|
if (! is_string($value)) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$trimmed = trim($value);
|
||||||
|
|
||||||
|
return $trimmed !== '' ? $trimmed : null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array<int, mixed> $notes
|
* @param array<int, mixed> $notes
|
||||||
* @return array<int, array<string, string>>
|
* @return array<int, array<string, string>>
|
||||||
@@ -1966,6 +2560,23 @@ final class AcademyAdminController extends Controller
|
|||||||
|
|
||||||
$storedPath = self::PROMPT_PREVIEW_PREFIX.'/'.pathinfo(Str::replace('\\', '/', $file->hashName()), PATHINFO_FILENAME).'.webp';
|
$storedPath = self::PROMPT_PREVIEW_PREFIX.'/'.pathinfo(Str::replace('\\', '/', $file->hashName()), PATHINFO_FILENAME).'.webp';
|
||||||
Storage::disk($this->promptPreviewImageDisk())->put($storedPath, $webpBinary, ['visibility' => 'public']);
|
Storage::disk($this->promptPreviewImageDisk())->put($storedPath, $webpBinary, ['visibility' => 'public']);
|
||||||
|
|
||||||
|
$sourceWidth = imagesx($image);
|
||||||
|
$sourceHeight = imagesy($image);
|
||||||
|
|
||||||
|
foreach (self::PROMPT_PREVIEW_VARIANT_WIDTHS as $variant => $targetWidth) {
|
||||||
|
$variantBinary = $this->encodePromptPreviewVariant($image, $targetWidth, $sourceWidth, $sourceHeight);
|
||||||
|
|
||||||
|
if ($variantBinary === null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Storage::disk($this->promptPreviewImageDisk())->put(
|
||||||
|
$this->promptPreviewVariantPath($storedPath, $variant),
|
||||||
|
$variantBinary,
|
||||||
|
['visibility' => 'public']
|
||||||
|
);
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
imagedestroy($image);
|
imagedestroy($image);
|
||||||
}
|
}
|
||||||
@@ -1973,6 +2584,62 @@ final class AcademyAdminController extends Controller
|
|||||||
return $storedPath;
|
return $storedPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function encodePromptPreviewVariant(\GdImage $source, int $targetWidth, int $sourceWidth, int $sourceHeight): ?string
|
||||||
|
{
|
||||||
|
if ($sourceWidth <= $targetWidth || $sourceWidth < 1 || $sourceHeight < 1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$targetHeight = max(1, (int) round(($sourceHeight / $sourceWidth) * $targetWidth));
|
||||||
|
$variant = imagecreatetruecolor($targetWidth, $targetHeight);
|
||||||
|
|
||||||
|
if (! $variant instanceof \GdImage) {
|
||||||
|
throw ValidationException::withMessages([
|
||||||
|
'preview_image_file' => 'The uploaded preview image could not be resized. Please try a different image.',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
imagealphablending($variant, false);
|
||||||
|
imagesavealpha($variant, true);
|
||||||
|
$transparent = imagecolorallocatealpha($variant, 0, 0, 0, 127);
|
||||||
|
imagefilledrectangle($variant, 0, 0, $targetWidth, $targetHeight, $transparent);
|
||||||
|
imagecopyresampled($variant, $source, 0, 0, 0, 0, $targetWidth, $targetHeight, $sourceWidth, $sourceHeight);
|
||||||
|
|
||||||
|
try {
|
||||||
|
ob_start();
|
||||||
|
$converted = imagewebp($variant, null, self::PROMPT_PREVIEW_WEBP_QUALITY);
|
||||||
|
$webpBinary = ob_get_clean();
|
||||||
|
|
||||||
|
if (! $converted || ! is_string($webpBinary) || $webpBinary === '') {
|
||||||
|
throw ValidationException::withMessages([
|
||||||
|
'preview_image_file' => 'The uploaded preview image could not be converted to WebP. Please try a different image.',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $webpBinary;
|
||||||
|
} finally {
|
||||||
|
imagedestroy($variant);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function promptPreviewVariantPath(string $path, string $variant): string
|
||||||
|
{
|
||||||
|
$directory = pathinfo($path, PATHINFO_DIRNAME);
|
||||||
|
$filename = pathinfo($path, PATHINFO_FILENAME);
|
||||||
|
$baseFilename = preg_replace('/-(thumb|md)$/', '', $filename) ?? $filename;
|
||||||
|
|
||||||
|
return sprintf('%s/%s-%s.webp', $directory, $baseFilename, $variant);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function canonicalPromptPreviewPath(string $path): string
|
||||||
|
{
|
||||||
|
$directory = pathinfo($path, PATHINFO_DIRNAME);
|
||||||
|
$filename = pathinfo($path, PATHINFO_FILENAME);
|
||||||
|
$baseFilename = preg_replace('/-(thumb|md)$/', '', $filename) ?? $filename;
|
||||||
|
|
||||||
|
return sprintf('%s/%s.webp', $directory, $baseFilename);
|
||||||
|
}
|
||||||
|
|
||||||
private function deleteStoredPromptPreviewIfLocal(?string $path): void
|
private function deleteStoredPromptPreviewIfLocal(?string $path): void
|
||||||
{
|
{
|
||||||
$path = trim((string) $path);
|
$path = trim((string) $path);
|
||||||
@@ -1985,10 +2652,14 @@ final class AcademyAdminController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$disk = $this->promptPreviewImageDisk();
|
$disk = $this->promptPreviewImageDisk();
|
||||||
|
$basePath = $this->canonicalPromptPreviewPath($path);
|
||||||
|
$paths = [
|
||||||
|
$basePath,
|
||||||
|
$this->promptPreviewVariantPath($basePath, 'thumb'),
|
||||||
|
$this->promptPreviewVariantPath($basePath, 'md'),
|
||||||
|
];
|
||||||
|
|
||||||
if (Storage::disk($disk)->exists($path)) {
|
Storage::disk($disk)->delete(array_values(array_unique($paths)));
|
||||||
Storage::disk($disk)->delete($path);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function promptPreviewImageUploadErrorMessage(UploadedFile $file): string
|
private function promptPreviewImageUploadErrorMessage(UploadedFile $file): string
|
||||||
|
|||||||
@@ -26,6 +26,11 @@ final class AcademyLessonMediaApiController extends Controller
|
|||||||
|
|
||||||
private const ASSET_CACHE_TTL_MINUTES = 15;
|
private const ASSET_CACHE_TTL_MINUTES = 15;
|
||||||
|
|
||||||
|
private const RESPONSIVE_VARIANT_WIDTHS = [
|
||||||
|
'thumb' => 480,
|
||||||
|
'md' => 960,
|
||||||
|
];
|
||||||
|
|
||||||
private ?ImageManager $manager = null;
|
private ?ImageManager $manager = null;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
@@ -68,6 +73,18 @@ final class AcademyLessonMediaApiController extends Controller
|
|||||||
'slot' => $slot,
|
'slot' => $slot,
|
||||||
'path' => $stored['path'],
|
'path' => $stored['path'],
|
||||||
'url' => $this->publicUrlForPath($stored['path']),
|
'url' => $this->publicUrlForPath($stored['path']),
|
||||||
|
'thumb_path' => $stored['thumb_path'],
|
||||||
|
'thumb_url' => $this->publicUrlForPath($stored['thumb_path']),
|
||||||
|
'thumb_width' => $stored['thumb_width'],
|
||||||
|
'thumb_height' => $stored['thumb_height'],
|
||||||
|
'medium_path' => $stored['medium_path'],
|
||||||
|
'medium_url' => $stored['medium_path'] !== '' ? $this->publicUrlForPath($stored['medium_path']) : null,
|
||||||
|
'medium_width' => $stored['medium_width'],
|
||||||
|
'medium_height' => $stored['medium_height'],
|
||||||
|
'srcset' => $this->buildResponsiveSrcset([
|
||||||
|
['path' => $stored['thumb_path'], 'width' => $stored['thumb_width']],
|
||||||
|
['path' => $stored['medium_path'], 'width' => $stored['medium_width']],
|
||||||
|
]),
|
||||||
'width' => $stored['width'],
|
'width' => $stored['width'],
|
||||||
'height' => $stored['height'],
|
'height' => $stored['height'],
|
||||||
'mime_type' => 'image/webp',
|
'mime_type' => 'image/webp',
|
||||||
@@ -161,7 +178,7 @@ final class AcademyLessonMediaApiController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array{path:string,width:int,height:int,size_bytes:int}
|
* @return array{path:string,thumb_path:string,thumb_width:int,thumb_height:int,medium_path:string,medium_width:int|null,medium_height:int|null,width:int,height:int,size_bytes:int}
|
||||||
*/
|
*/
|
||||||
private function storeMediaFile(UploadedFile $file, string $slot): array
|
private function storeMediaFile(UploadedFile $file, string $slot): array
|
||||||
{
|
{
|
||||||
@@ -202,14 +219,99 @@ final class AcademyLessonMediaApiController extends Controller
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
$image = $this->manager->read($raw)->scaleDown(width: $constraints['max_width'], height: $constraints['max_height']);
|
$encodedImage = $this->encodeScaledMedia($raw, $constraints['max_width'], $constraints['max_height']);
|
||||||
$encoded = (string) $image->encode(new WebpEncoder(85));
|
$encoded = $encodedImage['binary'];
|
||||||
|
|
||||||
$hash = hash('sha256', $encoded);
|
$hash = hash('sha256', $encoded);
|
||||||
$path = $this->mediaPath($hash, $slot);
|
$path = $this->mediaPath($hash, $slot);
|
||||||
$disk = Storage::disk($this->mediaDiskName());
|
$disk = Storage::disk($this->mediaDiskName());
|
||||||
|
|
||||||
$written = $disk->put($path, $encoded, [
|
$this->writeMediaBinary($disk, $path, $encoded);
|
||||||
|
|
||||||
|
$thumbVariant = $this->storeResponsiveVariant(
|
||||||
|
$disk,
|
||||||
|
$raw,
|
||||||
|
$constraints,
|
||||||
|
$path,
|
||||||
|
'thumb',
|
||||||
|
self::RESPONSIVE_VARIANT_WIDTHS['thumb'],
|
||||||
|
$encodedImage['width'],
|
||||||
|
$encodedImage['height'],
|
||||||
|
);
|
||||||
|
|
||||||
|
$mediumVariant = $this->storeResponsiveVariant(
|
||||||
|
$disk,
|
||||||
|
$raw,
|
||||||
|
$constraints,
|
||||||
|
$path,
|
||||||
|
'md',
|
||||||
|
self::RESPONSIVE_VARIANT_WIDTHS['md'],
|
||||||
|
$encodedImage['width'],
|
||||||
|
$encodedImage['height'],
|
||||||
|
);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'path' => $path,
|
||||||
|
'thumb_path' => $thumbVariant['path'] ?? $path,
|
||||||
|
'thumb_width' => $thumbVariant['width'] ?? $encodedImage['width'],
|
||||||
|
'thumb_height' => $thumbVariant['height'] ?? $encodedImage['height'],
|
||||||
|
'medium_path' => $mediumVariant['path'] ?? '',
|
||||||
|
'medium_width' => $mediumVariant['width'] ?? null,
|
||||||
|
'medium_height' => $mediumVariant['height'] ?? null,
|
||||||
|
'width' => $encodedImage['width'],
|
||||||
|
'height' => $encodedImage['height'],
|
||||||
|
'size_bytes' => strlen($encoded),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{binary:string,width:int,height:int}
|
||||||
|
*/
|
||||||
|
private function encodeScaledMedia(string $raw, int $maxWidth, int $maxHeight): array
|
||||||
|
{
|
||||||
|
$image = $this->manager->read($raw)->scaleDown(width: $maxWidth, height: $maxHeight);
|
||||||
|
$encoded = (string) $image->encode(new WebpEncoder(85));
|
||||||
|
|
||||||
|
if ($encoded === '') {
|
||||||
|
throw new RuntimeException('Unable to encode image to WebP.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'binary' => $encoded,
|
||||||
|
'width' => (int) $image->width(),
|
||||||
|
'height' => (int) $image->height(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array{max_width:int,max_height:int} $constraints
|
||||||
|
* @return array{path:string,width:int,height:int}|null
|
||||||
|
*/
|
||||||
|
private function storeResponsiveVariant($disk, string $raw, array $constraints, string $path, string $variant, int $targetWidth, int $sourceWidth, int $sourceHeight): ?array
|
||||||
|
{
|
||||||
|
if ($sourceWidth <= $targetWidth && $sourceHeight <= $constraints['max_height']) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$encodedVariant = $this->encodeScaledMedia($raw, $targetWidth, $constraints['max_height']);
|
||||||
|
|
||||||
|
if ($encodedVariant['width'] >= $sourceWidth && $encodedVariant['height'] >= $sourceHeight) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$variantPath = $this->responsiveVariantPath($path, $variant);
|
||||||
|
$this->writeMediaBinary($disk, $variantPath, $encodedVariant['binary']);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'path' => $variantPath,
|
||||||
|
'width' => $encodedVariant['width'],
|
||||||
|
'height' => $encodedVariant['height'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function writeMediaBinary($disk, string $path, string $binary): void
|
||||||
|
{
|
||||||
|
$written = $disk->put($path, $binary, [
|
||||||
'visibility' => 'public',
|
'visibility' => 'public',
|
||||||
'CacheControl' => 'public, max-age=31536000, immutable',
|
'CacheControl' => 'public, max-age=31536000, immutable',
|
||||||
'ContentType' => 'image/webp',
|
'ContentType' => 'image/webp',
|
||||||
@@ -218,13 +320,6 @@ final class AcademyLessonMediaApiController extends Controller
|
|||||||
if ($written !== true) {
|
if ($written !== true) {
|
||||||
throw new RuntimeException('Unable to store image in object storage.');
|
throw new RuntimeException('Unable to store image in object storage.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
|
||||||
'path' => $path,
|
|
||||||
'width' => (int) $image->width(),
|
|
||||||
'height' => (int) $image->height(),
|
|
||||||
'size_bytes' => strlen($encoded),
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function authorizeStaff(Request $request): void
|
private function authorizeStaff(Request $request): void
|
||||||
@@ -255,6 +350,54 @@ final class AcademyLessonMediaApiController extends Controller
|
|||||||
return rtrim((string) config('cdn.files_url', 'https://files.skinbase.org'), '/') . '/' . ltrim($path, '/');
|
return rtrim((string) config('cdn.files_url', 'https://files.skinbase.org'), '/') . '/' . ltrim($path, '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<int, array{path:string,width:int|null}> $variants
|
||||||
|
*/
|
||||||
|
private function buildResponsiveSrcset(array $variants): ?string
|
||||||
|
{
|
||||||
|
$entries = collect($variants)
|
||||||
|
->filter(function (array $variant): bool {
|
||||||
|
return trim((string) ($variant['path'] ?? '')) !== '' && (int) ($variant['width'] ?? 0) > 0;
|
||||||
|
})
|
||||||
|
->unique(fn (array $variant): string => trim((string) ($variant['path'] ?? '')))
|
||||||
|
->map(fn (array $variant): string => sprintf('%s %dw', $this->publicUrlForPath((string) $variant['path']), (int) $variant['width']))
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
|
||||||
|
return $entries !== [] ? implode(', ', $entries) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function responsiveVariantPath(string $path, string $variant): string
|
||||||
|
{
|
||||||
|
$directory = pathinfo($path, PATHINFO_DIRNAME);
|
||||||
|
$filename = pathinfo($path, PATHINFO_FILENAME);
|
||||||
|
|
||||||
|
return sprintf(
|
||||||
|
'%s/%s-%s.webp',
|
||||||
|
$directory === '.' ? '' : $directory,
|
||||||
|
preg_replace('/-(thumb|md)$/', '', $filename) ?? $filename,
|
||||||
|
$variant,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function canonicalMediaPath(string $path): string
|
||||||
|
{
|
||||||
|
$directory = pathinfo($path, PATHINFO_DIRNAME);
|
||||||
|
$filename = pathinfo($path, PATHINFO_FILENAME);
|
||||||
|
$baseFilename = preg_replace('/-(thumb|md)$/', '', $filename) ?? $filename;
|
||||||
|
|
||||||
|
return sprintf(
|
||||||
|
'%s/%s.webp',
|
||||||
|
$directory === '.' ? '' : $directory,
|
||||||
|
$baseFilename,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isResponsiveVariantPath(string $path): bool
|
||||||
|
{
|
||||||
|
return preg_match('/-(thumb|md)\.webp$/i', $path) === 1;
|
||||||
|
}
|
||||||
|
|
||||||
private function academyAssetManifest(): Collection
|
private function academyAssetManifest(): Collection
|
||||||
{
|
{
|
||||||
return Cache::remember($this->academyAssetCacheKey(), now()->addMinutes(self::ASSET_CACHE_TTL_MINUTES), function (): Collection {
|
return Cache::remember($this->academyAssetCacheKey(), now()->addMinutes(self::ASSET_CACHE_TTL_MINUTES), function (): Collection {
|
||||||
@@ -262,6 +405,7 @@ final class AcademyLessonMediaApiController extends Controller
|
|||||||
|
|
||||||
return collect($disk->allFiles('academy/lessons'))
|
return collect($disk->allFiles('academy/lessons'))
|
||||||
->filter(fn (string $path): bool => Str::endsWith(Str::lower($path), ['.webp', '.jpg', '.jpeg', '.png']))
|
->filter(fn (string $path): bool => Str::endsWith(Str::lower($path), ['.webp', '.jpg', '.jpeg', '.png']))
|
||||||
|
->reject(fn (string $path): bool => $this->isResponsiveVariantPath($path))
|
||||||
->map(function (string $path) use ($disk): array {
|
->map(function (string $path) use ($disk): array {
|
||||||
$modifiedAt = null;
|
$modifiedAt = null;
|
||||||
|
|
||||||
@@ -323,7 +467,14 @@ final class AcademyLessonMediaApiController extends Controller
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Storage::disk($this->mediaDiskName())->delete($trimmed);
|
$basePath = $this->canonicalMediaPath($trimmed);
|
||||||
|
$paths = [
|
||||||
|
$basePath,
|
||||||
|
$this->responsiveVariantPath($basePath, 'thumb'),
|
||||||
|
$this->responsiveVariantPath($basePath, 'md'),
|
||||||
|
];
|
||||||
|
|
||||||
|
Storage::disk($this->mediaDiskName())->delete(array_values(array_unique($paths)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function normalizeSlot(mixed $slot): string
|
private function normalizeSlot(mixed $slot): string
|
||||||
@@ -346,8 +497,8 @@ final class AcademyLessonMediaApiController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'min_width' => 1200,
|
'min_width' => 600,
|
||||||
'min_height' => 630,
|
'min_height' => 315,
|
||||||
'max_width' => 2200,
|
'max_width' => 2200,
|
||||||
'max_height' => 1400,
|
'max_height' => 1400,
|
||||||
];
|
];
|
||||||
|
|||||||
512
app/Http/Controllers/Settings/WorldWebStoryAdminController.php
Normal file
512
app/Http/Controllers/Settings/WorldWebStoryAdminController.php
Normal file
@@ -0,0 +1,512 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Settings;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Models\World;
|
||||||
|
use App\Models\WorldWebStory;
|
||||||
|
use App\Models\WorldWebStoryPage;
|
||||||
|
use App\Services\WebStories\WorldWebStoryAssetService;
|
||||||
|
use App\Services\WebStories\WorldWebStoryGenerator;
|
||||||
|
use App\Services\WebStories\WorldWebStoryValidationService;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Http\RedirectResponse;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
|
use Inertia\Inertia;
|
||||||
|
use Inertia\Response;
|
||||||
|
|
||||||
|
final class WorldWebStoryAdminController extends Controller
|
||||||
|
{
|
||||||
|
private const PER_PAGE = 20;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private readonly WorldWebStoryGenerator $generator,
|
||||||
|
private readonly WorldWebStoryAssetService $assets,
|
||||||
|
private readonly WorldWebStoryValidationService $validation,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function index(Request $request): Response
|
||||||
|
{
|
||||||
|
$filters = [
|
||||||
|
'q' => trim((string) $request->query('q', '')),
|
||||||
|
'status' => trim((string) $request->query('status', 'all')),
|
||||||
|
];
|
||||||
|
|
||||||
|
$stories = WorldWebStory::query()
|
||||||
|
->with('world')
|
||||||
|
->when($filters['q'] !== '', function ($query) use ($filters): void {
|
||||||
|
$query->where(function ($nested) use ($filters): void {
|
||||||
|
$nested->where('title', 'like', '%' . $filters['q'] . '%')
|
||||||
|
->orWhere('slug', 'like', '%' . $filters['q'] . '%')
|
||||||
|
->orWhereHas('world', fn ($worldQuery) => $worldQuery->where('title', 'like', '%' . $filters['q'] . '%')->orWhere('slug', 'like', '%' . $filters['q'] . '%'));
|
||||||
|
});
|
||||||
|
})
|
||||||
|
->when($filters['status'] !== 'all', fn ($query) => $query->where('status', $filters['status']))
|
||||||
|
->orderByDesc('published_at')
|
||||||
|
->orderByDesc('updated_at')
|
||||||
|
->paginate(self::PER_PAGE)
|
||||||
|
->withQueryString()
|
||||||
|
->through(fn (WorldWebStory $story): array => $this->mapStoryListItem($story));
|
||||||
|
|
||||||
|
return Inertia::render('Moderation/WorldWebStoriesIndex', [
|
||||||
|
'title' => 'World Web Stories',
|
||||||
|
'stories' => $stories,
|
||||||
|
'filters' => $filters,
|
||||||
|
'stats' => [
|
||||||
|
'total' => WorldWebStory::query()->count(),
|
||||||
|
'published' => WorldWebStory::query()->where('status', WorldWebStory::STATUS_PUBLISHED)->count(),
|
||||||
|
'draft' => WorldWebStory::query()->where('status', WorldWebStory::STATUS_DRAFT)->count(),
|
||||||
|
'hidden' => WorldWebStory::query()->where('noindex', true)->orWhere('active', false)->count(),
|
||||||
|
],
|
||||||
|
'worldOptions' => $this->worldOptions(),
|
||||||
|
'endpoints' => [
|
||||||
|
'index' => route('admin.web-stories.index'),
|
||||||
|
'create' => route('admin.web-stories.create'),
|
||||||
|
'editPattern' => route('admin.web-stories.edit', ['story' => '__STORY__']),
|
||||||
|
'destroyPattern' => route('admin.web-stories.destroy', ['story' => '__STORY__']),
|
||||||
|
'publishPattern' => route('admin.web-stories.publish', ['story' => '__STORY__']),
|
||||||
|
'unpublishPattern' => route('admin.web-stories.unpublish', ['story' => '__STORY__']),
|
||||||
|
'generatePattern' => route('admin.web-stories.generate', ['world' => '__WORLD__']),
|
||||||
|
],
|
||||||
|
])->rootView('moderation');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create(): Response
|
||||||
|
{
|
||||||
|
return Inertia::render('Moderation/WorldWebStoryEditor', [
|
||||||
|
'story' => $this->blankStoryPayload(),
|
||||||
|
'worldOptions' => $this->worldOptions(),
|
||||||
|
'endpoints' => $this->editorEndpoints(),
|
||||||
|
'isNew' => true,
|
||||||
|
])->rootView('moderation');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(Request $request): RedirectResponse
|
||||||
|
{
|
||||||
|
$attributes = $this->validatedStoryAttributes($request);
|
||||||
|
$story = new WorldWebStory();
|
||||||
|
$story->fill($attributes + [
|
||||||
|
'created_by' => (int) $request->user()->id,
|
||||||
|
'updated_by' => (int) $request->user()->id,
|
||||||
|
]);
|
||||||
|
$this->normalizeStatusTimestamps($story);
|
||||||
|
$this->assertPublishedStateIsValid($story);
|
||||||
|
$story->save();
|
||||||
|
|
||||||
|
return redirect()->route('admin.web-stories.edit', ['story' => $story])->with('success', 'Web story created.');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function edit(WorldWebStory $story): Response
|
||||||
|
{
|
||||||
|
$story->load(['world', 'orderedPages.artwork']);
|
||||||
|
|
||||||
|
return Inertia::render('Moderation/WorldWebStoryEditor', [
|
||||||
|
'story' => $this->mapStoryEditorPayload($story),
|
||||||
|
'worldOptions' => $this->worldOptions(),
|
||||||
|
'endpoints' => $this->editorEndpoints($story),
|
||||||
|
'isNew' => false,
|
||||||
|
])->rootView('moderation');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(Request $request, WorldWebStory $story): RedirectResponse
|
||||||
|
{
|
||||||
|
$story->fill($this->validatedStoryAttributes($request) + [
|
||||||
|
'updated_by' => (int) $request->user()->id,
|
||||||
|
]);
|
||||||
|
$this->normalizeStatusTimestamps($story);
|
||||||
|
$this->assertPublishedStateIsValid($story);
|
||||||
|
$story->save();
|
||||||
|
|
||||||
|
return back()->with('success', 'Web story updated.');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy(WorldWebStory $story): JsonResponse
|
||||||
|
{
|
||||||
|
$story->delete();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'ok' => true,
|
||||||
|
'message' => 'Web story deleted.',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function storePage(Request $request, WorldWebStory $story): JsonResponse
|
||||||
|
{
|
||||||
|
$attributes = $this->validatedPageAttributes($request, $story, null);
|
||||||
|
$page = $story->pages()->create($attributes);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'ok' => true,
|
||||||
|
'message' => 'Page created.',
|
||||||
|
'page' => $this->mapPage($page->fresh('artwork')),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updatePage(Request $request, WorldWebStory $story, WorldWebStoryPage $page): JsonResponse
|
||||||
|
{
|
||||||
|
abort_unless((int) $page->story_id === (int) $story->id, 404);
|
||||||
|
|
||||||
|
$page->fill($this->validatedPageAttributes($request, $story, $page));
|
||||||
|
$page->save();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'ok' => true,
|
||||||
|
'message' => 'Page updated.',
|
||||||
|
'page' => $this->mapPage($page->fresh('artwork')),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroyPage(WorldWebStory $story, WorldWebStoryPage $page): JsonResponse
|
||||||
|
{
|
||||||
|
abort_unless((int) $page->story_id === (int) $story->id, 404);
|
||||||
|
|
||||||
|
$page->delete();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'ok' => true,
|
||||||
|
'message' => 'Page deleted.',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function reorderPages(Request $request, WorldWebStory $story): JsonResponse
|
||||||
|
{
|
||||||
|
$validated = $request->validate([
|
||||||
|
'page_ids' => ['required', 'array', 'min:1'],
|
||||||
|
'page_ids.*' => ['integer'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$ids = collect($validated['page_ids'])->map(fn ($id): int => (int) $id)->values();
|
||||||
|
$pages = $story->orderedPages()->whereIn('id', $ids)->get()->keyBy('id');
|
||||||
|
|
||||||
|
abort_unless($pages->count() === $ids->count(), 422);
|
||||||
|
|
||||||
|
foreach ($ids as $index => $id) {
|
||||||
|
$pages[$id]->forceFill(['position' => $index + 1])->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'ok' => true,
|
||||||
|
'message' => 'Page order updated.',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generateFromWorld(Request $request, World $world): JsonResponse
|
||||||
|
{
|
||||||
|
$validated = $request->validate([
|
||||||
|
'force' => ['nullable', 'boolean'],
|
||||||
|
'publish' => ['nullable', 'boolean'],
|
||||||
|
'dry_run' => ['nullable', 'boolean'],
|
||||||
|
'pages' => ['nullable', 'integer', 'min:5', 'max:10'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$result = $this->generator->generateFromWorld(
|
||||||
|
$world,
|
||||||
|
$request->user(),
|
||||||
|
(int) ($validated['pages'] ?? 7),
|
||||||
|
(bool) ($validated['force'] ?? false),
|
||||||
|
(bool) ($validated['publish'] ?? false),
|
||||||
|
(bool) ($validated['dry_run'] ?? false),
|
||||||
|
);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'ok' => true,
|
||||||
|
'message' => $result['created'] ? 'Web story draft generated.' : 'Web story draft regenerated.',
|
||||||
|
'story' => [
|
||||||
|
'id' => $result['story']->id,
|
||||||
|
'slug' => $result['story']->slug,
|
||||||
|
'edit_url' => $result['story']->exists ? route('admin.web-stories.edit', ['story' => $result['story']->id]) : null,
|
||||||
|
],
|
||||||
|
'validation' => $result['validation'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function publish(WorldWebStory $story): JsonResponse
|
||||||
|
{
|
||||||
|
$this->assets->buildAssets($story, force: false);
|
||||||
|
$story->refresh()->load('orderedPages');
|
||||||
|
$this->validation->assertPublishable($story);
|
||||||
|
$story->forceFill([
|
||||||
|
'status' => WorldWebStory::STATUS_PUBLISHED,
|
||||||
|
'published_at' => $story->published_at ?: now(),
|
||||||
|
])->save();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'ok' => true,
|
||||||
|
'message' => 'Web story published.',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function unpublish(WorldWebStory $story): JsonResponse
|
||||||
|
{
|
||||||
|
$story->forceFill([
|
||||||
|
'status' => WorldWebStory::STATUS_DRAFT,
|
||||||
|
'published_at' => null,
|
||||||
|
])->save();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'ok' => true,
|
||||||
|
'message' => 'Web story reverted to draft.',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
private function validatedStoryAttributes(Request $request, ?WorldWebStory $story = null): array
|
||||||
|
{
|
||||||
|
$validated = $request->validate([
|
||||||
|
'world_id' => ['nullable', 'integer', Rule::exists('worlds', 'id')],
|
||||||
|
'slug' => ['required', 'string', 'max:120', Rule::unique('world_web_stories', 'slug')->ignore($story?->id)],
|
||||||
|
'title' => ['required', 'string', 'max:255'],
|
||||||
|
'subtitle' => ['nullable', 'string', 'max:255'],
|
||||||
|
'excerpt' => ['nullable', 'string', 'max:400'],
|
||||||
|
'description' => ['nullable', 'string', 'max:2000'],
|
||||||
|
'seo_title' => ['nullable', 'string', 'max:255'],
|
||||||
|
'seo_description' => ['nullable', 'string', 'max:400'],
|
||||||
|
'poster_portrait_path' => ['nullable', 'string', 'max:2048'],
|
||||||
|
'poster_square_path' => ['nullable', 'string', 'max:2048'],
|
||||||
|
'publisher_logo_path' => ['nullable', 'string', 'max:2048'],
|
||||||
|
'status' => ['required', Rule::in([WorldWebStory::STATUS_DRAFT, WorldWebStory::STATUS_PUBLISHED, WorldWebStory::STATUS_ARCHIVED])],
|
||||||
|
'featured' => ['required', 'boolean'],
|
||||||
|
'active' => ['required', 'boolean'],
|
||||||
|
'noindex' => ['required', 'boolean'],
|
||||||
|
'published_at' => ['nullable', 'date'],
|
||||||
|
'starts_at' => ['nullable', 'date'],
|
||||||
|
'ends_at' => ['nullable', 'date', 'after_or_equal:starts_at'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $validated;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
private function validatedPageAttributes(Request $request, WorldWebStory $story, ?WorldWebStoryPage $page): array
|
||||||
|
{
|
||||||
|
$validated = $request->validate([
|
||||||
|
'artwork_id' => ['nullable', 'integer', Rule::exists('artworks', 'id')],
|
||||||
|
'position' => ['nullable', 'integer', 'min:1'],
|
||||||
|
'layout' => ['required', Rule::in([
|
||||||
|
WorldWebStoryPage::LAYOUT_COVER,
|
||||||
|
WorldWebStoryPage::LAYOUT_ARTWORK,
|
||||||
|
WorldWebStoryPage::LAYOUT_CREATOR,
|
||||||
|
WorldWebStoryPage::LAYOUT_MOOD,
|
||||||
|
WorldWebStoryPage::LAYOUT_COLLECTION,
|
||||||
|
WorldWebStoryPage::LAYOUT_CTA,
|
||||||
|
])],
|
||||||
|
'background_type' => ['required', Rule::in([
|
||||||
|
WorldWebStoryPage::BACKGROUND_IMAGE,
|
||||||
|
WorldWebStoryPage::BACKGROUND_VIDEO,
|
||||||
|
WorldWebStoryPage::BACKGROUND_GRADIENT,
|
||||||
|
])],
|
||||||
|
'background_path' => ['nullable', 'string', 'max:2048'],
|
||||||
|
'background_mobile_path' => ['nullable', 'string', 'max:2048'],
|
||||||
|
'headline' => ['nullable', 'string', 'max:255'],
|
||||||
|
'body' => ['nullable', 'string', 'max:180'],
|
||||||
|
'cta_label' => ['nullable', 'string', 'max:120'],
|
||||||
|
'cta_url' => ['nullable', 'string', 'max:2048'],
|
||||||
|
'alt_text' => ['required', 'string', 'max:255'],
|
||||||
|
'caption' => ['nullable', 'string', 'max:120'],
|
||||||
|
'credit_text' => ['nullable', 'string', 'max:255'],
|
||||||
|
'text_position' => ['required', Rule::in(['top', 'center', 'bottom'])],
|
||||||
|
'overlay_strength' => ['required', 'integer', 'min:0', 'max:100'],
|
||||||
|
'animation' => ['nullable', Rule::in(['fade-in', 'fly-in-bottom', 'pulse', 'pan-left', 'pan-right'])],
|
||||||
|
'active' => ['required', 'boolean'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$validated['position'] = (int) ($validated['position'] ?? ($story->orderedPages()->max('position') + ($page ? 0 : 1) ?: 1));
|
||||||
|
$pageErrors = $this->validation->validatePagePayload($validated);
|
||||||
|
|
||||||
|
if ($pageErrors !== []) {
|
||||||
|
throw ValidationException::withMessages($pageErrors);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $validated;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function normalizeStatusTimestamps(WorldWebStory $story): void
|
||||||
|
{
|
||||||
|
if ((string) $story->status === WorldWebStory::STATUS_PUBLISHED && $story->published_at === null) {
|
||||||
|
$story->published_at = now();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((string) $story->status === WorldWebStory::STATUS_DRAFT) {
|
||||||
|
$story->published_at = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function assertPublishedStateIsValid(WorldWebStory $story): void
|
||||||
|
{
|
||||||
|
if ((string) $story->status !== WorldWebStory::STATUS_PUBLISHED) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$story->loadMissing('orderedPages');
|
||||||
|
$this->validation->assertPublishable($story);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, array{value:int,label:string,description:string}>
|
||||||
|
*/
|
||||||
|
private function worldOptions(): array
|
||||||
|
{
|
||||||
|
return World::query()
|
||||||
|
->orderByDesc('published_at')
|
||||||
|
->orderBy('title')
|
||||||
|
->limit(200)
|
||||||
|
->get(['id', 'title', 'slug'])
|
||||||
|
->map(fn (World $world): array => [
|
||||||
|
'value' => (int) $world->id,
|
||||||
|
'label' => (string) $world->title,
|
||||||
|
'description' => (string) $world->slug,
|
||||||
|
])
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
private function blankStoryPayload(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => null,
|
||||||
|
'world_id' => null,
|
||||||
|
'slug' => '',
|
||||||
|
'title' => '',
|
||||||
|
'subtitle' => '',
|
||||||
|
'excerpt' => '',
|
||||||
|
'description' => '',
|
||||||
|
'seo_title' => '',
|
||||||
|
'seo_description' => '',
|
||||||
|
'poster_portrait_path' => '',
|
||||||
|
'poster_square_path' => '',
|
||||||
|
'publisher_logo_path' => $this->assets->defaultPublisherLogoPath(),
|
||||||
|
'status' => WorldWebStory::STATUS_DRAFT,
|
||||||
|
'featured' => false,
|
||||||
|
'active' => true,
|
||||||
|
'noindex' => false,
|
||||||
|
'published_at' => null,
|
||||||
|
'starts_at' => null,
|
||||||
|
'ends_at' => null,
|
||||||
|
'world' => null,
|
||||||
|
'pages' => [],
|
||||||
|
'public_url' => null,
|
||||||
|
'validation' => ['valid' => false, 'errors' => [], 'warnings' => [], 'page_count' => 0],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
private function mapStoryEditorPayload(WorldWebStory $story): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => (int) $story->id,
|
||||||
|
'world_id' => $story->world_id ? (int) $story->world_id : null,
|
||||||
|
'slug' => (string) $story->slug,
|
||||||
|
'title' => (string) $story->title,
|
||||||
|
'subtitle' => (string) ($story->subtitle ?? ''),
|
||||||
|
'excerpt' => (string) ($story->excerpt ?? ''),
|
||||||
|
'description' => (string) ($story->description ?? ''),
|
||||||
|
'seo_title' => (string) ($story->seo_title ?? ''),
|
||||||
|
'seo_description' => (string) ($story->seo_description ?? ''),
|
||||||
|
'poster_portrait_path' => (string) ($story->poster_portrait_path ?? ''),
|
||||||
|
'poster_square_path' => (string) ($story->poster_square_path ?? ''),
|
||||||
|
'publisher_logo_path' => (string) ($story->publisher_logo_path ?? ''),
|
||||||
|
'status' => (string) $story->status,
|
||||||
|
'featured' => (bool) $story->featured,
|
||||||
|
'active' => (bool) $story->active,
|
||||||
|
'noindex' => (bool) $story->noindex,
|
||||||
|
'published_at' => optional($story->published_at)?->toIso8601String(),
|
||||||
|
'starts_at' => optional($story->starts_at)?->toIso8601String(),
|
||||||
|
'ends_at' => optional($story->ends_at)?->toIso8601String(),
|
||||||
|
'world' => $story->world ? [
|
||||||
|
'id' => (int) $story->world->id,
|
||||||
|
'title' => (string) $story->world->title,
|
||||||
|
'slug' => (string) $story->world->slug,
|
||||||
|
] : null,
|
||||||
|
'pages' => $story->orderedPages->map(fn (WorldWebStoryPage $page): array => $this->mapPage($page))->all(),
|
||||||
|
'public_url' => route('web-stories.show', ['slug' => $story->slug]),
|
||||||
|
'validation' => $this->validation->validate($story),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
private function mapStoryListItem(WorldWebStory $story): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => (int) $story->id,
|
||||||
|
'slug' => (string) $story->slug,
|
||||||
|
'title' => (string) $story->title,
|
||||||
|
'excerpt' => (string) ($story->excerpt ?? ''),
|
||||||
|
'status' => (string) $story->status,
|
||||||
|
'active' => (bool) $story->active,
|
||||||
|
'noindex' => (bool) $story->noindex,
|
||||||
|
'featured' => (bool) $story->featured,
|
||||||
|
'page_count' => (int) ($story->pages()->count()),
|
||||||
|
'published_at' => optional($story->published_at)?->toIso8601String(),
|
||||||
|
'poster_portrait_url' => $story->posterPortraitUrl(),
|
||||||
|
'world' => $story->world ? [
|
||||||
|
'id' => (int) $story->world->id,
|
||||||
|
'title' => (string) $story->world->title,
|
||||||
|
'slug' => (string) $story->world->slug,
|
||||||
|
] : null,
|
||||||
|
'public_url' => route('web-stories.show', ['slug' => $story->slug]),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
private function mapPage(WorldWebStoryPage $page): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => (int) $page->id,
|
||||||
|
'artwork_id' => $page->artwork_id ? (int) $page->artwork_id : null,
|
||||||
|
'position' => (int) $page->position,
|
||||||
|
'layout' => (string) $page->layout,
|
||||||
|
'background_type' => (string) $page->background_type,
|
||||||
|
'background_path' => (string) ($page->background_path ?? ''),
|
||||||
|
'background_mobile_path' => (string) ($page->background_mobile_path ?? ''),
|
||||||
|
'headline' => (string) ($page->headline ?? ''),
|
||||||
|
'body' => (string) ($page->body ?? ''),
|
||||||
|
'cta_label' => (string) ($page->cta_label ?? ''),
|
||||||
|
'cta_url' => (string) ($page->cta_url ?? ''),
|
||||||
|
'alt_text' => (string) ($page->alt_text ?? ''),
|
||||||
|
'caption' => (string) ($page->caption ?? ''),
|
||||||
|
'credit_text' => (string) ($page->credit_text ?? ''),
|
||||||
|
'text_position' => (string) ($page->text_position ?? 'bottom'),
|
||||||
|
'overlay_strength' => (int) ($page->overlay_strength ?? 35),
|
||||||
|
'animation' => (string) ($page->animation ?? ''),
|
||||||
|
'active' => (bool) $page->active,
|
||||||
|
'background_url' => $page->backgroundUrl(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, string>
|
||||||
|
*/
|
||||||
|
private function editorEndpoints(?WorldWebStory $story = null): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'store' => route('admin.web-stories.store'),
|
||||||
|
'update' => $story ? route('admin.web-stories.update', ['story' => $story]) : '',
|
||||||
|
'destroy' => $story ? route('admin.web-stories.destroy', ['story' => $story]) : '',
|
||||||
|
'pagesStore' => $story ? route('admin.web-stories.pages.store', ['story' => $story]) : '',
|
||||||
|
'pagesUpdatePattern' => $story ? route('admin.web-stories.pages.update', ['story' => $story, 'page' => '__PAGE__']) : '',
|
||||||
|
'pagesDestroyPattern' => $story ? route('admin.web-stories.pages.destroy', ['story' => $story, 'page' => '__PAGE__']) : '',
|
||||||
|
'pagesReorder' => $story ? route('admin.web-stories.pages.reorder', ['story' => $story]) : '',
|
||||||
|
'publish' => $story ? route('admin.web-stories.publish', ['story' => $story]) : '',
|
||||||
|
'unpublish' => $story ? route('admin.web-stories.unpublish', ['story' => $story]) : '',
|
||||||
|
'generateFromWorldPattern' => route('admin.web-stories.generate', ['world' => '__WORLD__']),
|
||||||
|
'index' => route('admin.web-stories.index'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,7 +32,7 @@ final class StudioNewsController extends Controller
|
|||||||
return Inertia::render('Studio/StudioNewsIndex', [
|
return Inertia::render('Studio/StudioNewsIndex', [
|
||||||
'title' => 'Newsroom',
|
'title' => 'Newsroom',
|
||||||
'description' => 'Plan announcements, publish editorial stories, and connect articles to the rest of Nova.',
|
'description' => 'Plan announcements, publish editorial stories, and connect articles to the rest of Nova.',
|
||||||
'listing' => $this->news->studioListing($request->only(['q', 'status', 'type', 'category_id', 'per_page', 'page'])),
|
'listing' => $this->news->studioListing($request->only(['q', 'status', 'type', 'category_id', 'per_page', 'page', 'order', 'direction'])),
|
||||||
'statusOptions' => $this->news->editorialStatusOptions(),
|
'statusOptions' => $this->news->editorialStatusOptions(),
|
||||||
'typeOptions' => $this->news->articleTypeOptions(),
|
'typeOptions' => $this->news->articleTypeOptions(),
|
||||||
'categoryOptions' => $this->news->categoryOptions(),
|
'categoryOptions' => $this->news->categoryOptions(),
|
||||||
@@ -56,7 +56,7 @@ final class StudioNewsController extends Controller
|
|||||||
'statusOptions' => $this->news->editorialStatusOptions(),
|
'statusOptions' => $this->news->editorialStatusOptions(),
|
||||||
'categoryOptions' => $this->news->categoryOptions(),
|
'categoryOptions' => $this->news->categoryOptions(),
|
||||||
'tagOptions' => $this->news->tagOptions(),
|
'tagOptions' => $this->news->tagOptions(),
|
||||||
'newsTagLimit' => 12,
|
'newsTagLimit' => 30,
|
||||||
'relationTypeOptions' => $this->news->relationTypeOptions(),
|
'relationTypeOptions' => $this->news->relationTypeOptions(),
|
||||||
'storeUrl' => route('studio.news.store'),
|
'storeUrl' => route('studio.news.store'),
|
||||||
'coverUploadUrl' => route('api.studio.news.media.upload'),
|
'coverUploadUrl' => route('api.studio.news.media.upload'),
|
||||||
@@ -92,7 +92,7 @@ final class StudioNewsController extends Controller
|
|||||||
'statusOptions' => $this->news->editorialStatusOptions(),
|
'statusOptions' => $this->news->editorialStatusOptions(),
|
||||||
'categoryOptions' => $this->news->categoryOptions(),
|
'categoryOptions' => $this->news->categoryOptions(),
|
||||||
'tagOptions' => $this->news->tagOptions(),
|
'tagOptions' => $this->news->tagOptions(),
|
||||||
'newsTagLimit' => 12,
|
'newsTagLimit' => 30,
|
||||||
'relationTypeOptions' => $this->news->relationTypeOptions(),
|
'relationTypeOptions' => $this->news->relationTypeOptions(),
|
||||||
'coverUploadUrl' => route('api.studio.news.media.upload'),
|
'coverUploadUrl' => route('api.studio.news.media.upload'),
|
||||||
'coverDeleteUrl' => route('api.studio.news.media.destroy'),
|
'coverDeleteUrl' => route('api.studio.news.media.destroy'),
|
||||||
@@ -367,21 +367,11 @@ final class StudioNewsController extends Controller
|
|||||||
'comments_enabled' => ['nullable', 'boolean'],
|
'comments_enabled' => ['nullable', 'boolean'],
|
||||||
'tag_ids' => ['nullable', 'array'],
|
'tag_ids' => ['nullable', 'array'],
|
||||||
'tag_ids.*' => ['integer', 'exists:news_tags,id'],
|
'tag_ids.*' => ['integer', 'exists:news_tags,id'],
|
||||||
'new_tag_names' => ['nullable', 'array', 'max:12'],
|
'new_tag_names' => ['nullable', 'array', 'max:30'],
|
||||||
'new_tag_names.*' => ['string', 'max:80'],
|
'new_tag_names.*' => ['string', 'max:80'],
|
||||||
'meta_title' => ['nullable', 'string', 'max:255'],
|
'meta_title' => ['nullable', 'string', 'max:255'],
|
||||||
'meta_description' => ['nullable', 'string', 'max:300'],
|
'meta_description' => ['nullable', 'string', 'max:300'],
|
||||||
'meta_keywords' => ['nullable', 'string', 'max:255'],
|
'meta_keywords' => ['nullable', 'string', 'max:255'],
|
||||||
'canonical_url' => ['nullable', 'string', 'max:2048', function (string $attribute, mixed $value, \Closure $fail): void {
|
|
||||||
if ($value === '' || $value === null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$isAbsolute = filter_var($value, FILTER_VALIDATE_URL) !== false;
|
|
||||||
$isRelative = str_starts_with($value, '/');
|
|
||||||
if (! $isAbsolute && ! $isRelative) {
|
|
||||||
$fail('The canonical URL must be a valid URL or a relative path starting with /.');
|
|
||||||
}
|
|
||||||
}],
|
|
||||||
'og_title' => ['nullable', 'string', 'max:255'],
|
'og_title' => ['nullable', 'string', 'max:255'],
|
||||||
'og_description' => ['nullable', 'string', 'max:300'],
|
'og_description' => ['nullable', 'string', 'max:300'],
|
||||||
'og_image' => ['nullable', 'string', 'max:2048'],
|
'og_image' => ['nullable', 'string', 'max:2048'],
|
||||||
|
|||||||
@@ -227,10 +227,11 @@ final class SimilarArtworksPageController extends Controller
|
|||||||
->public()
|
->public()
|
||||||
->published()
|
->published()
|
||||||
->with([
|
->with([
|
||||||
'categories:id,slug,name',
|
'categories:id,slug,name,content_type_id',
|
||||||
'categories.contentType:id,name,slug',
|
'categories.contentType:id,name,slug',
|
||||||
'user:id,name,username',
|
'user:id,name,username',
|
||||||
'user.profile:user_id,avatar_hash',
|
'user.profile:user_id,avatar_hash',
|
||||||
|
'group:id,name,slug,avatar_path',
|
||||||
])
|
])
|
||||||
->get()
|
->get()
|
||||||
->keyBy('id');
|
->keyBy('id');
|
||||||
@@ -268,6 +269,14 @@ final class SimilarArtworksPageController extends Controller
|
|||||||
'sort' => ['trending_score_7d:desc', 'created_at:desc'],
|
'sort' => ['trending_score_7d:desc', 'created_at:desc'],
|
||||||
])->paginate(self::PER_PAGE, 'page', $page);
|
])->paginate(self::PER_PAGE, 'page', $page);
|
||||||
|
|
||||||
|
$results->getCollection()->load([
|
||||||
|
'categories:id,slug,name,content_type_id',
|
||||||
|
'categories.contentType:id,name,slug',
|
||||||
|
'user:id,name,username',
|
||||||
|
'user.profile:user_id,avatar_hash',
|
||||||
|
'group:id,name,slug,avatar_path',
|
||||||
|
]);
|
||||||
|
|
||||||
$results->getCollection()->transform(fn ($a) => $this->presentArtwork($a));
|
$results->getCollection()->transform(fn ($a) => $this->presentArtwork($a));
|
||||||
|
|
||||||
return $results;
|
return $results;
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ final class TagController extends Controller
|
|||||||
$artworks = $this->search->byTag($tag->slug, $perPage, $sort);
|
$artworks = $this->search->byTag($tag->slug, $perPage, $sort);
|
||||||
|
|
||||||
// Eager-load relations used by the gallery presenter and thumbnails.
|
// Eager-load relations used by the gallery presenter and thumbnails.
|
||||||
$artworks->getCollection()->each(fn($m) => $m->loadMissing(['user.profile', 'categories']));
|
$artworks->getCollection()->loadMissing(['user.profile', 'categories.contentType']);
|
||||||
|
|
||||||
// Sidebar: main content type links (same as browse gallery)
|
// Sidebar: main content type links (same as browse gallery)
|
||||||
$mainCategories = ContentType::ordered()->where('hide_from_menu', false)->get(['name', 'slug'])
|
$mainCategories = ContentType::ordered()->where('hide_from_menu', false)->get(['name', 'slug'])
|
||||||
|
|||||||
52
app/Http/Controllers/Web/WorldWebStoryController.php
Normal file
52
app/Http/Controllers/Web/WorldWebStoryController.php
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Web;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Models\WorldWebStory;
|
||||||
|
use App\Services\WebStories\WorldWebStorySeoService;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Illuminate\View\View;
|
||||||
|
|
||||||
|
final class WorldWebStoryController extends Controller
|
||||||
|
{
|
||||||
|
public function __construct(private readonly WorldWebStorySeoService $seo)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function index(Request $request): View
|
||||||
|
{
|
||||||
|
$stories = Cache::remember('web_story_index', 300, fn () => WorldWebStory::query()
|
||||||
|
->with('world')
|
||||||
|
->visible()
|
||||||
|
->orderByDesc('featured')
|
||||||
|
->orderByDesc('published_at')
|
||||||
|
->paginate(12)
|
||||||
|
->withQueryString());
|
||||||
|
|
||||||
|
return view('web-stories.index', [
|
||||||
|
'stories' => $stories,
|
||||||
|
'seo' => $this->seo->indexSeo(),
|
||||||
|
'useUnifiedSeo' => true,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function show(string $slug): View
|
||||||
|
{
|
||||||
|
$story = Cache::remember('web_story:' . $slug, 300, fn () => WorldWebStory::query()
|
||||||
|
->with(['world', 'orderedPages.artwork.user'])
|
||||||
|
->visible()
|
||||||
|
->where('slug', $slug)
|
||||||
|
->first());
|
||||||
|
|
||||||
|
abort_unless($story instanceof WorldWebStory, 404);
|
||||||
|
|
||||||
|
return view('web-stories.show', [
|
||||||
|
'story' => $story,
|
||||||
|
'meta' => $this->seo->storyMeta($story),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -94,6 +94,9 @@ final class HandleInertiaRequests extends Middleware
|
|||||||
{
|
{
|
||||||
$canReadSessionAuth = $this->canReadSessionAuth($request);
|
$canReadSessionAuth = $this->canReadSessionAuth($request);
|
||||||
$user = $canReadSessionAuth ? $request->user() : null;
|
$user = $canReadSessionAuth ? $request->user() : null;
|
||||||
|
$sessionFlash = static fn (string $key): ?string => $canReadSessionAuth
|
||||||
|
? $request->session()->get($key)
|
||||||
|
: null;
|
||||||
|
|
||||||
return array_merge(parent::share($request), [
|
return array_merge(parent::share($request), [
|
||||||
'auth' => [
|
'auth' => [
|
||||||
@@ -108,6 +111,11 @@ final class HandleInertiaRequests extends Middleware
|
|||||||
'is_moderator' => $user->isModerator(),
|
'is_moderator' => $user->isModerator(),
|
||||||
] : null,
|
] : null,
|
||||||
],
|
],
|
||||||
|
'flash' => [
|
||||||
|
'success' => fn (): ?string => $sessionFlash('success'),
|
||||||
|
'error' => fn (): ?string => $sessionFlash('error'),
|
||||||
|
'warning' => fn (): ?string => $sessionFlash('warning'),
|
||||||
|
],
|
||||||
'cdn' => [
|
'cdn' => [
|
||||||
'files_url' => config('cdn.files_url'),
|
'files_url' => config('cdn.files_url'),
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ class UpsertAcademyLessonRequest extends FormRequest
|
|||||||
'cover_image' => ['nullable', 'string', 'max:2048'],
|
'cover_image' => ['nullable', 'string', 'max:2048'],
|
||||||
'article_cover_image' => ['nullable', 'string', 'max:2048'],
|
'article_cover_image' => ['nullable', 'string', 'max:2048'],
|
||||||
'tags' => ['nullable', 'array'],
|
'tags' => ['nullable', 'array'],
|
||||||
'tags.*' => ['string', 'max:60'],
|
'tags.*' => ['string', 'max:100'],
|
||||||
'video_url' => ['nullable', 'string', 'max:2048'],
|
'video_url' => ['nullable', 'string', 'max:2048'],
|
||||||
'reading_minutes' => ['required', 'integer', 'min:1', 'max:999'],
|
'reading_minutes' => ['required', 'integer', 'min:1', 'max:999'],
|
||||||
'featured' => ['required', 'boolean'],
|
'featured' => ['required', 'boolean'],
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Http\Requests\Academy;
|
namespace App\Http\Requests\Academy;
|
||||||
|
|
||||||
|
use JsonException;
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
use Illuminate\Validation\Rule;
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
@@ -22,6 +23,10 @@ class UpsertAcademyPromptTemplateRequest extends FormRequest
|
|||||||
'active' => $this->boolean('active', true),
|
'active' => $this->boolean('active', true),
|
||||||
'new_category_name' => trim((string) $this->input('new_category_name', '')),
|
'new_category_name' => trim((string) $this->input('new_category_name', '')),
|
||||||
'tags' => array_values(array_filter((array) $this->input('tags', []))),
|
'tags' => array_values(array_filter((array) $this->input('tags', []))),
|
||||||
|
'documentation' => $this->normalizeDocumentation($this->input('documentation')),
|
||||||
|
'placeholders' => $this->normalizePlaceholders($this->input('placeholders')),
|
||||||
|
'helper_prompts' => $this->normalizeHelperPrompts($this->input('helper_prompts')),
|
||||||
|
'prompt_variants' => $this->normalizePromptVariants($this->input('prompt_variants')),
|
||||||
'tool_notes' => collect($this->input('tool_notes', []))
|
'tool_notes' => collect($this->input('tool_notes', []))
|
||||||
->filter(static fn ($note): bool => is_array($note) || is_string($note))
|
->filter(static fn ($note): bool => is_array($note) || is_string($note))
|
||||||
->map(function ($note): array|string {
|
->map(function ($note): array|string {
|
||||||
@@ -30,6 +35,7 @@ class UpsertAcademyPromptTemplateRequest extends FormRequest
|
|||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
'display_type' => $note['display_type'] ?? null,
|
||||||
'provider' => $note['provider'] ?? null,
|
'provider' => $note['provider'] ?? null,
|
||||||
'model_name' => $note['model_name'] ?? null,
|
'model_name' => $note['model_name'] ?? null,
|
||||||
'notes' => $note['notes'] ?? null,
|
'notes' => $note['notes'] ?? null,
|
||||||
@@ -62,12 +68,57 @@ class UpsertAcademyPromptTemplateRequest extends FormRequest
|
|||||||
'negative_prompt' => ['nullable', 'string'],
|
'negative_prompt' => ['nullable', 'string'],
|
||||||
'usage_notes' => ['nullable', 'string'],
|
'usage_notes' => ['nullable', 'string'],
|
||||||
'workflow_notes' => ['nullable', 'string'],
|
'workflow_notes' => ['nullable', 'string'],
|
||||||
|
'documentation' => ['nullable', 'array'],
|
||||||
|
'documentation.summary' => ['nullable', 'string'],
|
||||||
|
'documentation.best_for' => ['nullable', 'array'],
|
||||||
|
'documentation.best_for.*' => ['nullable', 'string'],
|
||||||
|
'documentation.how_to_use' => ['nullable', 'array'],
|
||||||
|
'documentation.how_to_use.*' => ['nullable', 'string'],
|
||||||
|
'documentation.required_inputs' => ['nullable', 'array'],
|
||||||
|
'documentation.required_inputs.*' => ['nullable', 'string'],
|
||||||
|
'documentation.workflow' => ['nullable', 'array'],
|
||||||
|
'documentation.workflow.*' => ['nullable', 'string'],
|
||||||
|
'documentation.tips' => ['nullable', 'array'],
|
||||||
|
'documentation.tips.*' => ['nullable', 'string'],
|
||||||
|
'documentation.common_mistakes' => ['nullable', 'array'],
|
||||||
|
'documentation.common_mistakes.*' => ['nullable', 'string'],
|
||||||
|
'documentation.data_accuracy_notes' => ['nullable', 'array'],
|
||||||
|
'documentation.data_accuracy_notes.*' => ['nullable', 'string'],
|
||||||
|
'documentation.display_notes' => ['nullable', 'string'],
|
||||||
|
'placeholders' => ['nullable', 'array'],
|
||||||
|
'placeholders.*.key' => ['nullable', 'string', 'max:120'],
|
||||||
|
'placeholders.*.label' => ['nullable', 'string', 'max:180'],
|
||||||
|
'placeholders.*.description' => ['nullable', 'string'],
|
||||||
|
'placeholders.*.required' => ['nullable', 'boolean'],
|
||||||
|
'placeholders.*.example' => ['nullable'],
|
||||||
|
'placeholders.*.default' => ['nullable'],
|
||||||
|
'placeholders.*.type' => ['nullable', 'string', 'max:120'],
|
||||||
|
'helper_prompts' => ['nullable', 'array'],
|
||||||
|
'helper_prompts.*.title' => ['required_with:helper_prompts', 'string', 'max:180'],
|
||||||
|
'helper_prompts.*.type' => ['nullable', 'string', Rule::in(['data_collection', 'prompt_preparation', 'refinement', 'validation', 'variation', 'translation', 'seo', 'other'])],
|
||||||
|
'helper_prompts.*.description' => ['nullable', 'string'],
|
||||||
|
'helper_prompts.*.prompt' => ['required_with:helper_prompts', 'string'],
|
||||||
|
'helper_prompts.*.expected_output' => ['nullable', 'string', Rule::in(['json', 'text', 'markdown', 'image_prompt'])],
|
||||||
|
'helper_prompts.*.active' => ['nullable', 'boolean'],
|
||||||
|
'prompt_variants' => ['nullable', 'array'],
|
||||||
|
'prompt_variants.*.title' => ['required_with:prompt_variants', 'string', 'max:180'],
|
||||||
|
'prompt_variants.*.slug' => ['nullable', 'string', 'max:180'],
|
||||||
|
'prompt_variants.*.description' => ['nullable', 'string'],
|
||||||
|
'prompt_variants.*.prompt' => ['required_with:prompt_variants', 'string'],
|
||||||
|
'prompt_variants.*.negative_prompt' => ['nullable', 'string'],
|
||||||
|
'prompt_variants.*.recommended' => ['nullable', 'boolean'],
|
||||||
|
'prompt_variants.*.recommended_for' => ['nullable', 'array'],
|
||||||
|
'prompt_variants.*.recommended_for.*' => ['nullable', 'string'],
|
||||||
|
'prompt_variants.*.risk_notes' => ['nullable', 'array'],
|
||||||
|
'prompt_variants.*.risk_notes.*' => ['nullable', 'string'],
|
||||||
|
'prompt_variants.*.active' => ['nullable', 'boolean'],
|
||||||
'difficulty' => ['required', 'string', Rule::in((array) config('academy.difficulty_levels', []))],
|
'difficulty' => ['required', 'string', Rule::in((array) config('academy.difficulty_levels', []))],
|
||||||
'access_level' => ['required', 'string', Rule::in(['free', 'creator', 'pro'])],
|
'access_level' => ['required', 'string', Rule::in(['free', 'creator', 'pro'])],
|
||||||
'aspect_ratio' => ['nullable', 'string', 'max:20'],
|
'aspect_ratio' => ['nullable', 'string', 'max:20'],
|
||||||
'tags' => ['nullable', 'array'],
|
'tags' => ['nullable', 'array'],
|
||||||
'tags.*' => ['string', 'max:60'],
|
'tags.*' => ['string', 'max:60'],
|
||||||
'tool_notes' => ['nullable', 'array'],
|
'tool_notes' => ['nullable', 'array'],
|
||||||
|
'tool_notes.*.display_type' => ['nullable', 'string', 'max:50'],
|
||||||
'tool_notes.*.provider' => ['nullable', 'string', 'max:100'],
|
'tool_notes.*.provider' => ['nullable', 'string', 'max:100'],
|
||||||
'tool_notes.*.model_name' => ['nullable', 'string', 'max:150'],
|
'tool_notes.*.model_name' => ['nullable', 'string', 'max:150'],
|
||||||
'tool_notes.*.notes' => ['nullable', 'string'],
|
'tool_notes.*.notes' => ['nullable', 'string'],
|
||||||
@@ -89,4 +140,251 @@ class UpsertAcademyPromptTemplateRequest extends FormRequest
|
|||||||
'seo_description' => ['nullable', 'string', 'max:255'],
|
'seo_description' => ['nullable', 'string', 'max:255'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function decodeStructuredInput(mixed $value): mixed
|
||||||
|
{
|
||||||
|
if (! is_string($value)) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$trimmed = trim($value);
|
||||||
|
|
||||||
|
if ($trimmed === '') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return json_decode($trimmed, true, 512, JSON_THROW_ON_ERROR);
|
||||||
|
} catch (JsonException) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function normalizeDocumentation(mixed $value): mixed
|
||||||
|
{
|
||||||
|
$value = $this->decodeStructuredInput($value);
|
||||||
|
|
||||||
|
if ($value === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! is_array($value)) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$listFields = ['best_for', 'how_to_use', 'required_inputs', 'workflow', 'tips', 'common_mistakes', 'data_accuracy_notes'];
|
||||||
|
$documentation = [
|
||||||
|
'summary' => $this->normalizeOptionalString($value['summary'] ?? null),
|
||||||
|
'display_notes' => $this->normalizeOptionalString($value['display_notes'] ?? null),
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($listFields as $field) {
|
||||||
|
$documentation[$field] = $this->normalizeStringList($value[$field] ?? []);
|
||||||
|
}
|
||||||
|
|
||||||
|
$hasContent = $documentation['summary'] !== null
|
||||||
|
|| $documentation['display_notes'] !== null
|
||||||
|
|| collect($listFields)->contains(fn (string $field): bool => $documentation[$field] !== []);
|
||||||
|
|
||||||
|
return $hasContent ? $documentation : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function normalizePlaceholders(mixed $value): mixed
|
||||||
|
{
|
||||||
|
$value = $this->decodeStructuredInput($value);
|
||||||
|
|
||||||
|
if ($value === null) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! is_array($value)) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$value = $this->normalizeStructuredObjectList($value, ['key', 'label', 'description', 'required', 'example', 'default', 'type']);
|
||||||
|
|
||||||
|
return collect($value)
|
||||||
|
->values()
|
||||||
|
->map(function ($placeholder): mixed {
|
||||||
|
if (! is_array($placeholder)) {
|
||||||
|
return $placeholder;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'key' => $this->normalizeOptionalString($placeholder['key'] ?? null),
|
||||||
|
'label' => $this->normalizeOptionalString($placeholder['label'] ?? null),
|
||||||
|
'description' => $this->normalizeOptionalString($placeholder['description'] ?? null),
|
||||||
|
'required' => filter_var($placeholder['required'] ?? false, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE) ?? false,
|
||||||
|
'example' => $this->normalizeJsonScalar($placeholder['example'] ?? null),
|
||||||
|
'default' => $this->normalizeJsonScalar($placeholder['default'] ?? null),
|
||||||
|
'type' => $this->normalizeOptionalString($placeholder['type'] ?? null),
|
||||||
|
];
|
||||||
|
})
|
||||||
|
->filter(function ($placeholder): bool {
|
||||||
|
if (! is_array($placeholder)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return collect([
|
||||||
|
$placeholder['key'] ?? null,
|
||||||
|
$placeholder['label'] ?? null,
|
||||||
|
$placeholder['description'] ?? null,
|
||||||
|
$placeholder['example'] ?? null,
|
||||||
|
$placeholder['default'] ?? null,
|
||||||
|
$placeholder['type'] ?? null,
|
||||||
|
])->contains(fn ($item): bool => $item !== null && $item !== '' && $item !== []);
|
||||||
|
})
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function normalizeHelperPrompts(mixed $value): mixed
|
||||||
|
{
|
||||||
|
$value = $this->decodeStructuredInput($value);
|
||||||
|
|
||||||
|
if ($value === null) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! is_array($value)) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$value = $this->normalizeStructuredObjectList($value, ['title', 'type', 'description', 'prompt', 'expected_output', 'active']);
|
||||||
|
|
||||||
|
return collect($value)
|
||||||
|
->values()
|
||||||
|
->map(function ($helperPrompt): mixed {
|
||||||
|
if (! is_array($helperPrompt)) {
|
||||||
|
return $helperPrompt;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'title' => $this->normalizeOptionalString($helperPrompt['title'] ?? null),
|
||||||
|
'type' => $this->normalizeOptionalString($helperPrompt['type'] ?? null) ?? 'other',
|
||||||
|
'description' => $this->normalizeOptionalString($helperPrompt['description'] ?? null),
|
||||||
|
'prompt' => $this->normalizeOptionalString($helperPrompt['prompt'] ?? null),
|
||||||
|
'expected_output' => $this->normalizeOptionalString($helperPrompt['expected_output'] ?? null) ?? 'text',
|
||||||
|
'active' => filter_var($helperPrompt['active'] ?? true, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE) ?? true,
|
||||||
|
];
|
||||||
|
})
|
||||||
|
->filter(function ($helperPrompt): bool {
|
||||||
|
if (! is_array($helperPrompt)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return collect([
|
||||||
|
$helperPrompt['title'] ?? null,
|
||||||
|
$helperPrompt['description'] ?? null,
|
||||||
|
$helperPrompt['prompt'] ?? null,
|
||||||
|
])->contains(fn ($item): bool => $item !== null && $item !== '');
|
||||||
|
})
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function normalizePromptVariants(mixed $value): mixed
|
||||||
|
{
|
||||||
|
$value = $this->decodeStructuredInput($value);
|
||||||
|
|
||||||
|
if ($value === null) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! is_array($value)) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$value = $this->normalizeStructuredObjectList($value, ['title', 'slug', 'description', 'prompt', 'negative_prompt', 'recommended', 'recommended_for', 'risk_notes', 'active']);
|
||||||
|
|
||||||
|
return collect($value)
|
||||||
|
->values()
|
||||||
|
->map(function ($variant): mixed {
|
||||||
|
if (! is_array($variant)) {
|
||||||
|
return $variant;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'title' => $this->normalizeOptionalString($variant['title'] ?? null),
|
||||||
|
'slug' => $this->normalizeOptionalString($variant['slug'] ?? null),
|
||||||
|
'description' => $this->normalizeOptionalString($variant['description'] ?? null),
|
||||||
|
'prompt' => $this->normalizeOptionalString($variant['prompt'] ?? null),
|
||||||
|
'negative_prompt' => $this->normalizeOptionalString($variant['negative_prompt'] ?? null),
|
||||||
|
'recommended' => filter_var($variant['recommended'] ?? false, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE) ?? false,
|
||||||
|
'recommended_for' => $this->normalizeStringList($variant['recommended_for'] ?? []),
|
||||||
|
'risk_notes' => $this->normalizeStringList($variant['risk_notes'] ?? []),
|
||||||
|
'active' => filter_var($variant['active'] ?? true, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE) ?? true,
|
||||||
|
];
|
||||||
|
})
|
||||||
|
->filter(function ($variant): bool {
|
||||||
|
if (! is_array($variant)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return collect([
|
||||||
|
$variant['title'] ?? null,
|
||||||
|
$variant['description'] ?? null,
|
||||||
|
$variant['prompt'] ?? null,
|
||||||
|
$variant['negative_prompt'] ?? null,
|
||||||
|
])->contains(fn ($item): bool => $item !== null && $item !== '');
|
||||||
|
})
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function normalizeStringList(mixed $value): array
|
||||||
|
{
|
||||||
|
if (! is_array($value)) {
|
||||||
|
$value = $value === null ? [] : [$value];
|
||||||
|
}
|
||||||
|
|
||||||
|
return collect($value)
|
||||||
|
->map(fn ($item): string => trim((string) $item))
|
||||||
|
->filter(static fn (string $item): bool => $item !== '')
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function normalizeOptionalString(mixed $value): ?string
|
||||||
|
{
|
||||||
|
if ($value === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$normalized = trim((string) $value);
|
||||||
|
|
||||||
|
return $normalized !== '' ? $normalized : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function normalizeJsonScalar(mixed $value): mixed
|
||||||
|
{
|
||||||
|
if (! is_string($value)) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$trimmed = trim($value);
|
||||||
|
|
||||||
|
return $trimmed !== '' ? $trimmed : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<int|string, mixed> $value
|
||||||
|
* @param array<int, string> $expectedKeys
|
||||||
|
* @return array<int|string, mixed>
|
||||||
|
*/
|
||||||
|
private function normalizeStructuredObjectList(array $value, array $expectedKeys): array
|
||||||
|
{
|
||||||
|
if (array_is_list($value)) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$keys = array_keys($value);
|
||||||
|
$normalizedKeys = array_map(static fn ($key): string => (string) $key, $keys);
|
||||||
|
|
||||||
|
if ($normalizedKeys === [] || array_intersect($normalizedKeys, $expectedKeys) === []) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [$value];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
20
app/Listeners/Academy/HandleAcademyStripeWebhook.php
Normal file
20
app/Listeners/Academy/HandleAcademyStripeWebhook.php
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Listeners\Academy;
|
||||||
|
|
||||||
|
use App\Services\Academy\AcademyStripeWebhookAuditService;
|
||||||
|
use Laravel\Cashier\Events\WebhookReceived;
|
||||||
|
|
||||||
|
final class HandleAcademyStripeWebhook
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly AcademyStripeWebhookAuditService $audit,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function handle(WebhookReceived $event): void
|
||||||
|
{
|
||||||
|
$this->audit->recordReceived($event->payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
20
app/Listeners/Academy/HandleAcademyStripeWebhookHandled.php
Normal file
20
app/Listeners/Academy/HandleAcademyStripeWebhookHandled.php
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Listeners\Academy;
|
||||||
|
|
||||||
|
use App\Services\Academy\AcademyStripeWebhookAuditService;
|
||||||
|
use Laravel\Cashier\Events\WebhookHandled;
|
||||||
|
|
||||||
|
final class HandleAcademyStripeWebhookHandled
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly AcademyStripeWebhookAuditService $audit,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function handle(WebhookHandled $event): void
|
||||||
|
{
|
||||||
|
$this->audit->recordHandled($event->payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
45
app/Models/AcademyBillingEvent.php
Normal file
45
app/Models/AcademyBillingEvent.php
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
final class AcademyBillingEvent extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var list<string>
|
||||||
|
*/
|
||||||
|
protected $fillable = [
|
||||||
|
'user_id',
|
||||||
|
'stripe_event_id',
|
||||||
|
'stripe_customer_id',
|
||||||
|
'stripe_subscription_id',
|
||||||
|
'event_type',
|
||||||
|
'academy_tier',
|
||||||
|
'academy_plan',
|
||||||
|
'payload_summary',
|
||||||
|
'processed_at',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, string>
|
||||||
|
*/
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'payload_summary' => 'array',
|
||||||
|
'processed_at' => 'datetime',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function user(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
47
app/Models/AcademyContentMetricDaily.php
Normal file
47
app/Models/AcademyContentMetricDaily.php
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class AcademyContentMetricDaily extends Model
|
||||||
|
{
|
||||||
|
protected $table = 'academy_content_metrics_daily';
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'date',
|
||||||
|
'content_type',
|
||||||
|
'content_id',
|
||||||
|
'views',
|
||||||
|
'unique_visitors',
|
||||||
|
'guest_views',
|
||||||
|
'user_views',
|
||||||
|
'subscriber_views',
|
||||||
|
'engaged_views',
|
||||||
|
'scroll_50',
|
||||||
|
'scroll_75',
|
||||||
|
'scroll_100',
|
||||||
|
'likes',
|
||||||
|
'saves',
|
||||||
|
'prompt_copies',
|
||||||
|
'negative_prompt_copies',
|
||||||
|
'starts',
|
||||||
|
'completions',
|
||||||
|
'upgrade_clicks',
|
||||||
|
'premium_preview_views',
|
||||||
|
'search_impressions',
|
||||||
|
'search_clicks',
|
||||||
|
'bounce_count',
|
||||||
|
'avg_engaged_seconds',
|
||||||
|
'popularity_score',
|
||||||
|
'conversion_score',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'date' => 'date',
|
||||||
|
'popularity_score' => 'decimal:2',
|
||||||
|
'conversion_score' => 'decimal:2',
|
||||||
|
];
|
||||||
|
}
|
||||||
54
app/Models/AcademyEvent.php
Normal file
54
app/Models/AcademyEvent.php
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
class AcademyEvent extends Model
|
||||||
|
{
|
||||||
|
protected $fillable = [
|
||||||
|
'event_type',
|
||||||
|
'content_type',
|
||||||
|
'content_id',
|
||||||
|
'user_id',
|
||||||
|
'visitor_id',
|
||||||
|
'session_id',
|
||||||
|
'url',
|
||||||
|
'route_name',
|
||||||
|
'referrer',
|
||||||
|
'utm_source',
|
||||||
|
'utm_medium',
|
||||||
|
'utm_campaign',
|
||||||
|
'device_type',
|
||||||
|
'browser',
|
||||||
|
'platform',
|
||||||
|
'country_code',
|
||||||
|
'is_logged_in',
|
||||||
|
'is_subscriber',
|
||||||
|
'is_admin',
|
||||||
|
'is_bot',
|
||||||
|
'is_crawler',
|
||||||
|
'is_suspicious',
|
||||||
|
'metadata',
|
||||||
|
'occurred_at',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'metadata' => 'array',
|
||||||
|
'occurred_at' => 'datetime',
|
||||||
|
'is_logged_in' => 'boolean',
|
||||||
|
'is_subscriber' => 'boolean',
|
||||||
|
'is_admin' => 'boolean',
|
||||||
|
'is_bot' => 'boolean',
|
||||||
|
'is_crawler' => 'boolean',
|
||||||
|
'is_suspicious' => 'boolean',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function user(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class, 'user_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
22
app/Models/AcademyLike.php
Normal file
22
app/Models/AcademyLike.php
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
class AcademyLike extends Model
|
||||||
|
{
|
||||||
|
protected $fillable = [
|
||||||
|
'user_id',
|
||||||
|
'content_type',
|
||||||
|
'content_id',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function user(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class, 'user_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,6 +24,10 @@ class AcademyPromptTemplate extends Model
|
|||||||
'negative_prompt',
|
'negative_prompt',
|
||||||
'usage_notes',
|
'usage_notes',
|
||||||
'workflow_notes',
|
'workflow_notes',
|
||||||
|
'documentation',
|
||||||
|
'placeholders',
|
||||||
|
'helper_prompts',
|
||||||
|
'prompt_variants',
|
||||||
'difficulty',
|
'difficulty',
|
||||||
'access_level',
|
'access_level',
|
||||||
'aspect_ratio',
|
'aspect_ratio',
|
||||||
@@ -41,6 +45,10 @@ class AcademyPromptTemplate extends Model
|
|||||||
protected $casts = [
|
protected $casts = [
|
||||||
'tags' => 'array',
|
'tags' => 'array',
|
||||||
'tool_notes' => 'array',
|
'tool_notes' => 'array',
|
||||||
|
'documentation' => 'array',
|
||||||
|
'placeholders' => 'array',
|
||||||
|
'helper_prompts' => 'array',
|
||||||
|
'prompt_variants' => 'array',
|
||||||
'featured' => 'boolean',
|
'featured' => 'boolean',
|
||||||
'prompt_of_week' => 'boolean',
|
'prompt_of_week' => 'boolean',
|
||||||
'active' => 'boolean',
|
'active' => 'boolean',
|
||||||
|
|||||||
22
app/Models/AcademySave.php
Normal file
22
app/Models/AcademySave.php
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
class AcademySave extends Model
|
||||||
|
{
|
||||||
|
protected $fillable = [
|
||||||
|
'user_id',
|
||||||
|
'content_type',
|
||||||
|
'content_id',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function user(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class, 'user_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
37
app/Models/AcademySearchLog.php
Normal file
37
app/Models/AcademySearchLog.php
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
class AcademySearchLog extends Model
|
||||||
|
{
|
||||||
|
protected $fillable = [
|
||||||
|
'user_id',
|
||||||
|
'visitor_id',
|
||||||
|
'query',
|
||||||
|
'normalized_query',
|
||||||
|
'results_count',
|
||||||
|
'clicked_content_type',
|
||||||
|
'clicked_content_id',
|
||||||
|
'filters',
|
||||||
|
'is_logged_in',
|
||||||
|
'is_subscriber',
|
||||||
|
'is_bot',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'filters' => 'array',
|
||||||
|
'is_logged_in' => 'boolean',
|
||||||
|
'is_subscriber' => 'boolean',
|
||||||
|
'is_bot' => 'boolean',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function user(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class, 'user_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
47
app/Models/AcademyUserProgress.php
Normal file
47
app/Models/AcademyUserProgress.php
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
class AcademyUserProgress extends Model
|
||||||
|
{
|
||||||
|
protected $table = 'academy_user_progress';
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'user_id',
|
||||||
|
'course_id',
|
||||||
|
'lesson_id',
|
||||||
|
'status',
|
||||||
|
'progress_percent',
|
||||||
|
'started_at',
|
||||||
|
'completed_at',
|
||||||
|
'last_seen_at',
|
||||||
|
'metadata',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'metadata' => 'array',
|
||||||
|
'started_at' => 'datetime',
|
||||||
|
'completed_at' => 'datetime',
|
||||||
|
'last_seen_at' => 'datetime',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function user(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class, 'user_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function course(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(AcademyCourse::class, 'course_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function lesson(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(AcademyLesson::class, 'lesson_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,8 +18,13 @@ use App\Models\ConversationParticipant;
|
|||||||
use App\Models\AcademyBadge;
|
use App\Models\AcademyBadge;
|
||||||
use App\Models\AcademyCourseEnrollment;
|
use App\Models\AcademyCourseEnrollment;
|
||||||
use App\Models\AcademyChallengeSubmission;
|
use App\Models\AcademyChallengeSubmission;
|
||||||
|
use App\Models\AcademyEvent;
|
||||||
|
use App\Models\AcademyLike;
|
||||||
use App\Models\AcademyLessonProgress;
|
use App\Models\AcademyLessonProgress;
|
||||||
|
use App\Models\AcademySave;
|
||||||
use App\Models\AcademySavedPrompt;
|
use App\Models\AcademySavedPrompt;
|
||||||
|
use App\Models\AcademySearchLog;
|
||||||
|
use App\Models\AcademyUserProgress;
|
||||||
use App\Models\Message;
|
use App\Models\Message;
|
||||||
use App\Models\Notification;
|
use App\Models\Notification;
|
||||||
use App\Models\Achievement;
|
use App\Models\Achievement;
|
||||||
@@ -30,6 +35,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
|||||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||||
use Illuminate\Notifications\Notifiable;
|
use Illuminate\Notifications\Notifiable;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Laravel\Cashier\Billable;
|
||||||
use Laravel\Scout\Searchable;
|
use Laravel\Scout\Searchable;
|
||||||
|
|
||||||
class User extends Authenticatable
|
class User extends Authenticatable
|
||||||
@@ -40,7 +46,7 @@ class User extends Authenticatable
|
|||||||
];
|
];
|
||||||
|
|
||||||
/** @use HasFactory<\Database\Factories\UserFactory> */
|
/** @use HasFactory<\Database\Factories\UserFactory> */
|
||||||
use HasFactory, Notifiable, SoftDeletes;
|
use Billable, HasFactory, Notifiable, SoftDeletes;
|
||||||
use Searchable {
|
use Searchable {
|
||||||
Searchable::bootSearchable as private bootScoutSearchable;
|
Searchable::bootSearchable as private bootScoutSearchable;
|
||||||
}
|
}
|
||||||
@@ -218,6 +224,31 @@ class User extends Authenticatable
|
|||||||
return $this->hasMany(AcademySavedPrompt::class, 'user_id');
|
return $this->hasMany(AcademySavedPrompt::class, 'user_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function academyEvents(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(AcademyEvent::class, 'user_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function academyLikes(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(AcademyLike::class, 'user_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function academySaves(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(AcademySave::class, 'user_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function academyUserProgress(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(AcademyUserProgress::class, 'user_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function academySearchLogs(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(AcademySearchLog::class, 'user_id');
|
||||||
|
}
|
||||||
|
|
||||||
public function academyChallengeSubmissions(): HasMany
|
public function academyChallengeSubmissions(): HasMany
|
||||||
{
|
{
|
||||||
return $this->hasMany(AcademyChallengeSubmission::class, 'user_id');
|
return $this->hasMany(AcademyChallengeSubmission::class, 'user_id');
|
||||||
@@ -448,12 +479,12 @@ class User extends Authenticatable
|
|||||||
|
|
||||||
public function hasAcademyCreatorAccess(): bool
|
public function hasAcademyCreatorAccess(): bool
|
||||||
{
|
{
|
||||||
return $this->hasAcademyProAccess() || strtolower(trim((string) ($this->role ?? ''))) === 'academy_creator';
|
return in_array(app(\App\Services\Academy\AcademyAccessService::class)->currentTier($this), ['creator', 'pro', 'admin'], true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasAcademyProAccess(): bool
|
public function hasAcademyProAccess(): bool
|
||||||
{
|
{
|
||||||
return strtolower(trim((string) ($this->role ?? ''))) === 'academy_pro';
|
return in_array(app(\App\Services\Academy\AcademyAccessService::class)->currentTier($this), ['pro', 'admin'], true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function canAccessAcademyContent(object|array $content): bool
|
public function canAccessAcademyContent(object|array $content): bool
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
@@ -197,6 +198,16 @@ class World extends Model
|
|||||||
return $this->hasMany(WorldRewardGrant::class)->orderByDesc('granted_at')->orderByDesc('id');
|
return $this->hasMany(WorldRewardGrant::class)->orderByDesc('granted_at')->orderByDesc('id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function webStories(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(WorldWebStory::class)->orderByDesc('published_at')->orderByDesc('id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function publishedWebStory(): HasOne
|
||||||
|
{
|
||||||
|
return $this->hasOne(WorldWebStory::class)->visible()->latest('published_at')->latest('id');
|
||||||
|
}
|
||||||
|
|
||||||
public function scopePublished(Builder $query): Builder
|
public function scopePublished(Builder $query): Builder
|
||||||
{
|
{
|
||||||
return $query
|
return $query
|
||||||
|
|||||||
181
app/Models/WorldWebStory.php
Normal file
181
app/Models/WorldWebStory.php
Normal file
@@ -0,0 +1,181 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
class WorldWebStory extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
use SoftDeletes;
|
||||||
|
|
||||||
|
public const STATUS_DRAFT = 'draft';
|
||||||
|
public const STATUS_PUBLISHED = 'published';
|
||||||
|
public const STATUS_ARCHIVED = 'archived';
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'world_id',
|
||||||
|
'slug',
|
||||||
|
'title',
|
||||||
|
'subtitle',
|
||||||
|
'excerpt',
|
||||||
|
'description',
|
||||||
|
'seo_title',
|
||||||
|
'seo_description',
|
||||||
|
'poster_portrait_path',
|
||||||
|
'poster_square_path',
|
||||||
|
'publisher_logo_path',
|
||||||
|
'status',
|
||||||
|
'featured',
|
||||||
|
'active',
|
||||||
|
'noindex',
|
||||||
|
'published_at',
|
||||||
|
'starts_at',
|
||||||
|
'ends_at',
|
||||||
|
'created_by',
|
||||||
|
'updated_by',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'featured' => 'boolean',
|
||||||
|
'active' => 'boolean',
|
||||||
|
'noindex' => 'boolean',
|
||||||
|
'published_at' => 'datetime',
|
||||||
|
'starts_at' => 'datetime',
|
||||||
|
'ends_at' => 'datetime',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected static function booted(): void
|
||||||
|
{
|
||||||
|
$flushCache = static function (self $story): void {
|
||||||
|
Cache::forget('web_story_index');
|
||||||
|
Cache::forget('web_story:' . $story->slug);
|
||||||
|
|
||||||
|
if ($story->world?->slug) {
|
||||||
|
Cache::forget('world:' . $story->world->slug . ':web_story');
|
||||||
|
} elseif ($story->world_id) {
|
||||||
|
$worldSlug = World::query()->whereKey($story->world_id)->value('slug');
|
||||||
|
if (is_string($worldSlug) && $worldSlug !== '') {
|
||||||
|
Cache::forget('world:' . $worldSlug . ':web_story');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static::saved($flushCache);
|
||||||
|
static::deleted($flushCache);
|
||||||
|
static::restored($flushCache);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function world(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(World::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function pages(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(WorldWebStoryPage::class, 'story_id')->orderedPages();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function orderedPages(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(WorldWebStoryPage::class, 'story_id')->orderedPages();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function creator(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class, 'created_by');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updater(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class, 'updated_by');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function scopePublished(Builder $query): Builder
|
||||||
|
{
|
||||||
|
return $query->where('status', self::STATUS_PUBLISHED);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function scopeActive(Builder $query): Builder
|
||||||
|
{
|
||||||
|
return $query->where('active', true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function scopeFeatured(Builder $query): Builder
|
||||||
|
{
|
||||||
|
return $query->where('featured', true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function scopeVisible(Builder $query): Builder
|
||||||
|
{
|
||||||
|
return $query
|
||||||
|
->active()
|
||||||
|
->published()
|
||||||
|
->where('noindex', false)
|
||||||
|
->where(function (Builder $builder): void {
|
||||||
|
$builder->whereNull('published_at')
|
||||||
|
->orWhere('published_at', '<=', now());
|
||||||
|
})
|
||||||
|
->where(function (Builder $builder): void {
|
||||||
|
$builder->whereNull('starts_at')
|
||||||
|
->orWhere('starts_at', '<=', now());
|
||||||
|
})
|
||||||
|
->where(function (Builder $builder): void {
|
||||||
|
$builder->whereNull('ends_at')
|
||||||
|
->orWhere('ends_at', '>=', now());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function publicUrl(): string
|
||||||
|
{
|
||||||
|
return route('web-stories.show', ['slug' => $this->slug]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function posterPortraitUrl(): ?string
|
||||||
|
{
|
||||||
|
return $this->assetUrl($this->poster_portrait_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function posterSquareUrl(): ?string
|
||||||
|
{
|
||||||
|
return $this->assetUrl($this->poster_square_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function publisherLogoUrl(): ?string
|
||||||
|
{
|
||||||
|
return $this->assetUrl($this->publisher_logo_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function seoTitle(): string
|
||||||
|
{
|
||||||
|
return trim((string) ($this->seo_title ?: $this->title));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function seoDescription(): string
|
||||||
|
{
|
||||||
|
return trim((string) ($this->seo_description ?: $this->excerpt ?: $this->description ?: ''));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function assetUrl(?string $path): ?string
|
||||||
|
{
|
||||||
|
$resolved = trim((string) $path);
|
||||||
|
|
||||||
|
if ($resolved === '') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (str_starts_with($resolved, 'http://') || str_starts_with($resolved, 'https://')) {
|
||||||
|
return $resolved;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rtrim((string) config('cdn.files_url', 'https://files.skinbase.org'), '/') . '/' . ltrim($resolved, '/');
|
||||||
|
}
|
||||||
|
}
|
||||||
118
app/Models/WorldWebStoryPage.php
Normal file
118
app/Models/WorldWebStoryPage.php
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
class WorldWebStoryPage extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
use SoftDeletes;
|
||||||
|
|
||||||
|
public const LAYOUT_COVER = 'cover';
|
||||||
|
public const LAYOUT_ARTWORK = 'artwork';
|
||||||
|
public const LAYOUT_CREATOR = 'creator';
|
||||||
|
public const LAYOUT_MOOD = 'mood';
|
||||||
|
public const LAYOUT_COLLECTION = 'collection';
|
||||||
|
public const LAYOUT_CTA = 'cta';
|
||||||
|
|
||||||
|
public const BACKGROUND_IMAGE = 'image';
|
||||||
|
public const BACKGROUND_VIDEO = 'video';
|
||||||
|
public const BACKGROUND_GRADIENT = 'gradient';
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'story_id',
|
||||||
|
'artwork_id',
|
||||||
|
'position',
|
||||||
|
'layout',
|
||||||
|
'background_type',
|
||||||
|
'background_path',
|
||||||
|
'background_mobile_path',
|
||||||
|
'headline',
|
||||||
|
'body',
|
||||||
|
'cta_label',
|
||||||
|
'cta_url',
|
||||||
|
'alt_text',
|
||||||
|
'caption',
|
||||||
|
'credit_text',
|
||||||
|
'text_position',
|
||||||
|
'overlay_strength',
|
||||||
|
'animation',
|
||||||
|
'active',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'position' => 'integer',
|
||||||
|
'overlay_strength' => 'integer',
|
||||||
|
'active' => 'boolean',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected static function booted(): void
|
||||||
|
{
|
||||||
|
$flushCache = static function (self $page): void {
|
||||||
|
$story = $page->relationLoaded('story') ? $page->story : $page->story()->with('world')->first();
|
||||||
|
|
||||||
|
if (! ($story instanceof WorldWebStory)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cache::forget('web_story:' . $story->slug);
|
||||||
|
Cache::forget('web_story_index');
|
||||||
|
|
||||||
|
if ($story->world?->slug) {
|
||||||
|
Cache::forget('world:' . $story->world->slug . ':web_story');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static::saved($flushCache);
|
||||||
|
static::deleted($flushCache);
|
||||||
|
static::restored($flushCache);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function story(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(WorldWebStory::class, 'story_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function artwork(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Artwork::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function scopeOrderedPages(Builder $query): Builder
|
||||||
|
{
|
||||||
|
return $query->orderBy('position')->orderBy('id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function backgroundUrl(): ?string
|
||||||
|
{
|
||||||
|
return $this->assetUrl($this->background_mobile_path ?: $this->background_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function desktopBackgroundUrl(): ?string
|
||||||
|
{
|
||||||
|
return $this->assetUrl($this->background_path ?: $this->background_mobile_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function assetUrl(?string $path): ?string
|
||||||
|
{
|
||||||
|
$resolved = trim((string) $path);
|
||||||
|
|
||||||
|
if ($resolved === '') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (str_starts_with($resolved, 'http://') || str_starts_with($resolved, 'https://')) {
|
||||||
|
return $resolved;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rtrim((string) config('cdn.files_url', 'https://files.skinbase.org'), '/') . '/' . ltrim($resolved, '/');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -46,6 +46,10 @@ use App\Services\Images\Detectors\HeuristicSubjectDetector;
|
|||||||
use App\Services\Images\Detectors\NullSubjectDetector;
|
use App\Services\Images\Detectors\NullSubjectDetector;
|
||||||
use App\Services\Images\Detectors\VisionSubjectDetector;
|
use App\Services\Images\Detectors\VisionSubjectDetector;
|
||||||
use Klevze\ControlPanel\Framework\Core\Menu;
|
use Klevze\ControlPanel\Framework\Core\Menu;
|
||||||
|
use Laravel\Cashier\Events\WebhookHandled;
|
||||||
|
use Laravel\Cashier\Events\WebhookReceived;
|
||||||
|
use App\Listeners\Academy\HandleAcademyStripeWebhook;
|
||||||
|
use App\Listeners\Academy\HandleAcademyStripeWebhookHandled;
|
||||||
|
|
||||||
class AppServiceProvider extends ServiceProvider
|
class AppServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
@@ -154,6 +158,14 @@ class AppServiceProvider extends ServiceProvider
|
|||||||
\App\Events\Achievements\UserXpUpdated::class,
|
\App\Events\Achievements\UserXpUpdated::class,
|
||||||
\App\Listeners\Achievements\CheckUserAchievements::class,
|
\App\Listeners\Achievements\CheckUserAchievements::class,
|
||||||
);
|
);
|
||||||
|
Event::listen(
|
||||||
|
WebhookReceived::class,
|
||||||
|
HandleAcademyStripeWebhook::class,
|
||||||
|
);
|
||||||
|
Event::listen(
|
||||||
|
WebhookHandled::class,
|
||||||
|
HandleAcademyStripeWebhookHandled::class,
|
||||||
|
);
|
||||||
|
|
||||||
// Provide toolbar counts and user info to layout views (port of legacy toolbar logic)
|
// Provide toolbar counts and user info to layout views (port of legacy toolbar logic)
|
||||||
View::composer(['layouts.nova', 'layouts.nova.*'], function ($view) {
|
View::composer(['layouts.nova', 'layouts.nova.*'], function ($view) {
|
||||||
|
|||||||
@@ -15,11 +15,39 @@ use App\Models\AcademyPromptTemplate;
|
|||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
use Laravel\Cashier\Subscription;
|
||||||
|
|
||||||
final class AcademyAccessService
|
final class AcademyAccessService
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var array<string, bool>
|
||||||
|
*/
|
||||||
|
private array $assetExistsCache = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array<int, string|null>
|
||||||
|
*/
|
||||||
|
private array $paidTierCache = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array<int, Subscription|null>
|
||||||
|
*/
|
||||||
|
private array $subscriptionCache = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array<string, string>|null
|
||||||
|
*/
|
||||||
|
private ?array $priceTierMap = null;
|
||||||
|
|
||||||
|
public function canAccess(?User $user, string $requiredLevel): bool
|
||||||
|
{
|
||||||
|
return $this->canAccessContent($user, $requiredLevel);
|
||||||
|
}
|
||||||
|
|
||||||
public function canAccessContent(?User $user, string $accessLevel): bool
|
public function canAccessContent(?User $user, string $accessLevel): bool
|
||||||
{
|
{
|
||||||
|
$accessLevel = $this->normalizeAccessLevel($accessLevel);
|
||||||
|
|
||||||
if ($accessLevel === 'free') {
|
if ($accessLevel === 'free') {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -28,11 +56,40 @@ final class AcademyAccessService
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user->isAdmin()) {
|
return $this->rankForLevel($this->currentTier($user)) >= $this->rankForLevel($accessLevel);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->rankForUser($user) >= $this->rankForLevel($accessLevel);
|
public function currentTier(?User $user): string
|
||||||
|
{
|
||||||
|
if (! $user instanceof User) {
|
||||||
|
return 'free';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->isAcademyAdmin($user)) {
|
||||||
|
return 'admin';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->paidTier($user) ?? 'free';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function paidTier(?User $user): ?string
|
||||||
|
{
|
||||||
|
if (! $user instanceof User) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$cacheKey = (int) $user->getKey();
|
||||||
|
|
||||||
|
if (array_key_exists($cacheKey, $this->paidTierCache)) {
|
||||||
|
return $this->paidTierCache[$cacheKey];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->paidTierCache[$cacheKey] = $this->resolveSubscriptionTier($user) ?? $this->resolveLegacyPaidTier($user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasActiveAcademySubscription(User $user): bool
|
||||||
|
{
|
||||||
|
return $this->activeAcademySubscription($user) instanceof Subscription;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function canAccessLesson(?User $user, AcademyLesson $lesson): bool
|
public function canAccessLesson(?User $user, AcademyLesson $lesson): bool
|
||||||
@@ -59,11 +116,7 @@ final class AcademyAccessService
|
|||||||
{
|
{
|
||||||
$accessLevel = trim((string) ($courseLesson->access_override ?: $courseLesson->lesson?->access_level ?: 'free'));
|
$accessLevel = trim((string) ($courseLesson->access_override ?: $courseLesson->lesson?->access_level ?: 'free'));
|
||||||
|
|
||||||
if ($accessLevel === 'premium') {
|
return $this->canAccessContent($user, $accessLevel);
|
||||||
return $user?->isAdmin() ?? false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->canAccessContent($user, $accessLevel === 'mixed' ? 'free' : $accessLevel);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function lessonPayload(AcademyLesson $lesson, ?User $viewer, bool $includeFull = false, ?bool $authorizedOverride = null): array
|
public function lessonPayload(AcademyLesson $lesson, ?User $viewer, bool $includeFull = false, ?bool $authorizedOverride = null): array
|
||||||
@@ -172,6 +225,19 @@ final class AcademyAccessService
|
|||||||
public function promptPayload(AcademyPromptTemplate $prompt, ?User $viewer, bool $includeFull = false): array
|
public function promptPayload(AcademyPromptTemplate $prompt, ?User $viewer, bool $includeFull = false): array
|
||||||
{
|
{
|
||||||
$authorized = $this->canAccessPrompt($viewer, $prompt);
|
$authorized = $this->canAccessPrompt($viewer, $prompt);
|
||||||
|
$publicExamples = $this->promptPublicExamplesPayload($prompt, (array) ($prompt->tool_notes ?? []));
|
||||||
|
$previewImage = $this->promptPreviewImagePayload((string) ($prompt->preview_image ?? ''));
|
||||||
|
$documentation = $this->promptDocumentationPayload($prompt->documentation);
|
||||||
|
$placeholders = $this->promptPlaceholdersPayload((array) ($prompt->placeholders ?? []));
|
||||||
|
$hasPlaceholderInputs = $this->promptHasPlaceholderInputs((string) $prompt->prompt, $placeholders);
|
||||||
|
$hasHelperPrompts = $this->promptHelperPromptsPayload((array) ($prompt->helper_prompts ?? [])) !== [];
|
||||||
|
$hasPromptVariants = $this->promptVariantsPayload((array) ($prompt->prompt_variants ?? [])) !== [];
|
||||||
|
$helperPrompts = $authorized && $includeFull
|
||||||
|
? $this->promptHelperPromptsPayload((array) ($prompt->helper_prompts ?? []))
|
||||||
|
: [];
|
||||||
|
$promptVariants = $authorized && $includeFull
|
||||||
|
? $this->promptVariantsPayload((array) ($prompt->prompt_variants ?? []))
|
||||||
|
: [];
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'id' => (int) $prompt->id,
|
'id' => (int) $prompt->id,
|
||||||
@@ -183,12 +249,25 @@ final class AcademyAccessService
|
|||||||
'usage_notes' => ($authorized && $includeFull) ? (string) ($prompt->usage_notes ?? '') : null,
|
'usage_notes' => ($authorized && $includeFull) ? (string) ($prompt->usage_notes ?? '') : null,
|
||||||
'workflow_notes' => ($authorized && $includeFull) ? (string) ($prompt->workflow_notes ?? '') : null,
|
'workflow_notes' => ($authorized && $includeFull) ? (string) ($prompt->workflow_notes ?? '') : null,
|
||||||
'prompt_preview' => $authorized ? null : $this->previewText((string) $prompt->prompt, 220),
|
'prompt_preview' => $authorized ? null : $this->previewText((string) $prompt->prompt, 220),
|
||||||
|
'documentation' => $documentation,
|
||||||
|
'placeholders' => $placeholders,
|
||||||
|
'has_placeholder_inputs' => $hasPlaceholderInputs,
|
||||||
|
'has_helper_prompts' => $hasHelperPrompts,
|
||||||
|
'has_prompt_variants' => $hasPromptVariants,
|
||||||
|
'helper_prompts' => $helperPrompts,
|
||||||
|
'prompt_variants' => $promptVariants,
|
||||||
'difficulty' => (string) $prompt->difficulty,
|
'difficulty' => (string) $prompt->difficulty,
|
||||||
'access_level' => (string) $prompt->access_level,
|
'access_level' => (string) $prompt->access_level,
|
||||||
|
'access_requirement' => $this->promptAccessRequirement((string) $prompt->access_level),
|
||||||
|
'unlock_heading' => $this->promptUnlockHeading((string) $prompt->access_level),
|
||||||
|
'unlock_description' => $this->promptUnlockDescription((string) $prompt->access_level),
|
||||||
'aspect_ratio' => $prompt->aspect_ratio,
|
'aspect_ratio' => $prompt->aspect_ratio,
|
||||||
'tags' => array_values((array) ($prompt->tags ?? [])),
|
'tags' => array_values((array) ($prompt->tags ?? [])),
|
||||||
|
'public_examples' => $publicExamples,
|
||||||
'tool_notes' => $authorized ? $this->promptToolNotesPayload((array) ($prompt->tool_notes ?? [])) : [],
|
'tool_notes' => $authorized ? $this->promptToolNotesPayload((array) ($prompt->tool_notes ?? [])) : [],
|
||||||
'preview_image' => $this->resolvePreviewImageUrl((string) ($prompt->preview_image ?? '')),
|
'preview_image' => $previewImage['url'],
|
||||||
|
'preview_image_thumb' => $previewImage['thumb_url'],
|
||||||
|
'preview_image_srcset' => $previewImage['srcset'],
|
||||||
'featured' => (bool) $prompt->featured,
|
'featured' => (bool) $prompt->featured,
|
||||||
'prompt_of_week' => (bool) $prompt->prompt_of_week,
|
'prompt_of_week' => (bool) $prompt->prompt_of_week,
|
||||||
'published_at' => $prompt->published_at?->toISOString(),
|
'published_at' => $prompt->published_at?->toISOString(),
|
||||||
@@ -202,6 +281,235 @@ final class AcademyAccessService
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $documentation
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
private function promptDocumentationPayload(mixed $documentation): array
|
||||||
|
{
|
||||||
|
$normalized = is_array($documentation) ? $documentation : [];
|
||||||
|
$listFields = ['best_for', 'how_to_use', 'required_inputs', 'workflow', 'tips', 'common_mistakes', 'data_accuracy_notes'];
|
||||||
|
$payload = [
|
||||||
|
'summary' => $this->nullableTrimmedString($normalized['summary'] ?? null),
|
||||||
|
'display_notes' => $this->nullableTrimmedString($normalized['display_notes'] ?? null),
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($listFields as $field) {
|
||||||
|
$payload[$field] = $this->normalizeStringList($normalized[$field] ?? []);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<int, mixed> $placeholders
|
||||||
|
* @return array<int, array<string, mixed>>
|
||||||
|
*/
|
||||||
|
private function promptPlaceholdersPayload(array $placeholders): array
|
||||||
|
{
|
||||||
|
return collect($placeholders)
|
||||||
|
->filter(static fn ($placeholder): bool => is_array($placeholder))
|
||||||
|
->map(function (array $placeholder): array {
|
||||||
|
return [
|
||||||
|
'key' => trim((string) ($placeholder['key'] ?? '')),
|
||||||
|
'label' => $this->nullableTrimmedString($placeholder['label'] ?? null),
|
||||||
|
'description' => $this->nullableTrimmedString($placeholder['description'] ?? null),
|
||||||
|
'required' => filter_var($placeholder['required'] ?? false, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE) ?? false,
|
||||||
|
'example' => $placeholder['example'] ?? null,
|
||||||
|
'default' => $placeholder['default'] ?? null,
|
||||||
|
'type' => $this->nullableTrimmedString($placeholder['type'] ?? null),
|
||||||
|
];
|
||||||
|
})
|
||||||
|
->filter(function (array $placeholder): bool {
|
||||||
|
return collect([
|
||||||
|
$placeholder['key'],
|
||||||
|
$placeholder['label'],
|
||||||
|
$placeholder['description'],
|
||||||
|
$placeholder['example'],
|
||||||
|
$placeholder['default'],
|
||||||
|
$placeholder['type'],
|
||||||
|
])->contains(fn ($item): bool => $item !== null && $item !== '' && $item !== []);
|
||||||
|
})
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<int, array<string, mixed>> $placeholders
|
||||||
|
*/
|
||||||
|
private function promptHasPlaceholderInputs(string $prompt, array $placeholders): bool
|
||||||
|
{
|
||||||
|
if ($prompt === '' || $placeholders === []) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($placeholders as $placeholder) {
|
||||||
|
$key = trim((string) ($placeholder['key'] ?? ''));
|
||||||
|
|
||||||
|
if ($key === '') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mb_stripos($prompt, '['.$key.']') !== false) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<int, mixed> $helperPrompts
|
||||||
|
* @return array<int, array<string, mixed>>
|
||||||
|
*/
|
||||||
|
private function promptHelperPromptsPayload(array $helperPrompts): array
|
||||||
|
{
|
||||||
|
return collect($helperPrompts)
|
||||||
|
->filter(static fn ($helperPrompt): bool => is_array($helperPrompt))
|
||||||
|
->map(function (array $helperPrompt): array {
|
||||||
|
return [
|
||||||
|
'title' => trim((string) ($helperPrompt['title'] ?? '')),
|
||||||
|
'type' => trim((string) ($helperPrompt['type'] ?? 'other')) ?: 'other',
|
||||||
|
'description' => $this->nullableTrimmedString($helperPrompt['description'] ?? null),
|
||||||
|
'prompt' => trim((string) ($helperPrompt['prompt'] ?? '')),
|
||||||
|
'expected_output' => trim((string) ($helperPrompt['expected_output'] ?? 'text')) ?: 'text',
|
||||||
|
'active' => filter_var($helperPrompt['active'] ?? true, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE) ?? true,
|
||||||
|
];
|
||||||
|
})
|
||||||
|
->filter(function (array $helperPrompt): bool {
|
||||||
|
return $helperPrompt['active'] !== false
|
||||||
|
&& collect([
|
||||||
|
$helperPrompt['title'],
|
||||||
|
$helperPrompt['description'],
|
||||||
|
$helperPrompt['prompt'],
|
||||||
|
])->contains(fn ($item): bool => $item !== null && $item !== '');
|
||||||
|
})
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<int, mixed> $variants
|
||||||
|
* @return array<int, array<string, mixed>>
|
||||||
|
*/
|
||||||
|
private function promptVariantsPayload(array $variants): array
|
||||||
|
{
|
||||||
|
return collect($variants)
|
||||||
|
->filter(static fn ($variant): bool => is_array($variant))
|
||||||
|
->map(function (array $variant): array {
|
||||||
|
return [
|
||||||
|
'title' => trim((string) ($variant['title'] ?? '')),
|
||||||
|
'slug' => $this->nullableTrimmedString($variant['slug'] ?? null),
|
||||||
|
'description' => $this->nullableTrimmedString($variant['description'] ?? null),
|
||||||
|
'prompt' => trim((string) ($variant['prompt'] ?? '')),
|
||||||
|
'negative_prompt' => $this->nullableTrimmedString($variant['negative_prompt'] ?? null),
|
||||||
|
'recommended' => filter_var($variant['recommended'] ?? false, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE) ?? false,
|
||||||
|
'recommended_for' => $this->normalizeStringList($variant['recommended_for'] ?? []),
|
||||||
|
'risk_notes' => $this->normalizeStringList($variant['risk_notes'] ?? []),
|
||||||
|
'active' => filter_var($variant['active'] ?? true, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE) ?? true,
|
||||||
|
];
|
||||||
|
})
|
||||||
|
->filter(function (array $variant): bool {
|
||||||
|
return $variant['active'] !== false
|
||||||
|
&& collect([
|
||||||
|
$variant['title'],
|
||||||
|
$variant['description'],
|
||||||
|
$variant['prompt'],
|
||||||
|
$variant['negative_prompt'],
|
||||||
|
])->contains(fn ($item): bool => $item !== null && $item !== '');
|
||||||
|
})
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<int, mixed> $notes
|
||||||
|
* @return array<int, array<string, mixed>>
|
||||||
|
*/
|
||||||
|
private function promptPublicExamplesPayload(AcademyPromptTemplate $prompt, array $notes): array
|
||||||
|
{
|
||||||
|
$promptTitle = trim((string) $prompt->title);
|
||||||
|
|
||||||
|
return collect($notes)
|
||||||
|
->values()
|
||||||
|
->filter(static fn ($note): bool => is_array($note))
|
||||||
|
->filter(function (array $note): bool {
|
||||||
|
return (filter_var($note['active'] ?? true, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE) ?? true) !== false;
|
||||||
|
})
|
||||||
|
->map(function (array $note, int $index) use ($promptTitle): ?array {
|
||||||
|
$imagePayload = $this->responsiveLessonImagePayload(
|
||||||
|
(string) ($note['image_path'] ?? ''),
|
||||||
|
(string) ($note['thumb_path'] ?? ''),
|
||||||
|
);
|
||||||
|
$imagePath = $imagePayload['image_path'];
|
||||||
|
$thumbPath = $imagePayload['thumb_path'];
|
||||||
|
$imageUrl = $imagePayload['image_url'];
|
||||||
|
$thumbUrl = $imagePayload['thumb_url'];
|
||||||
|
|
||||||
|
if ($imageUrl === null && $thumbUrl === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$displayType = trim((string) ($note['display_type'] ?? ''));
|
||||||
|
$provider = trim((string) ($note['provider'] ?? ''));
|
||||||
|
$modelName = trim((string) ($note['model_name'] ?? ''));
|
||||||
|
$typeLabel = $displayType !== ''
|
||||||
|
? (string) Str::of($displayType)->replace(['_', '-'], ' ')->headline()
|
||||||
|
: 'Prompt variation';
|
||||||
|
$title = $displayType !== ''
|
||||||
|
? $typeLabel
|
||||||
|
: ($modelName !== '' ? $modelName : ($provider !== '' ? $provider : sprintf('Prompt Example %02d', $index + 1)));
|
||||||
|
$caption = $displayType !== ''
|
||||||
|
? sprintf('%s preview for %s.', $typeLabel, $promptTitle !== '' ? $promptTitle : 'this prompt')
|
||||||
|
: sprintf('Example result preview for %s.', $promptTitle !== '' ? $promptTitle : 'this prompt');
|
||||||
|
|
||||||
|
return [
|
||||||
|
'type_label' => $typeLabel,
|
||||||
|
'title' => $title,
|
||||||
|
'caption' => $caption,
|
||||||
|
'alt' => sprintf('%s preview image for %s', $title, $promptTitle !== '' ? $promptTitle : 'Skinbase Academy prompt'),
|
||||||
|
'provider' => $provider,
|
||||||
|
'model_name' => $modelName,
|
||||||
|
'image_path' => $imagePath,
|
||||||
|
'image_url' => $imageUrl,
|
||||||
|
'thumb_path' => $thumbPath,
|
||||||
|
'thumb_url' => $thumbUrl,
|
||||||
|
'image_srcset' => $imagePayload['srcset'],
|
||||||
|
'score' => filled($note['score'] ?? null) ? (int) $note['score'] : null,
|
||||||
|
];
|
||||||
|
})
|
||||||
|
->filter()
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function promptAccessRequirement(string $accessLevel): ?string
|
||||||
|
{
|
||||||
|
return match (trim(strtolower($accessLevel))) {
|
||||||
|
'pro' => 'Requires Pro access.',
|
||||||
|
'creator' => 'Requires Creator or Pro access.',
|
||||||
|
default => null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private function promptUnlockHeading(string $accessLevel): ?string
|
||||||
|
{
|
||||||
|
return match (trim(strtolower($accessLevel))) {
|
||||||
|
'pro' => 'Unlock the full Pro prompt.',
|
||||||
|
'creator' => 'Unlock the full Creator prompt.',
|
||||||
|
default => null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private function promptUnlockDescription(string $accessLevel): ?string
|
||||||
|
{
|
||||||
|
return match (trim(strtolower($accessLevel))) {
|
||||||
|
'pro' => 'Get the complete reusable prompt, negative prompt, workflow notes, model settings, and variation strategy.',
|
||||||
|
'creator' => 'Get the complete reusable prompt, negative prompt, workflow notes, and creative workflow.',
|
||||||
|
default => null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array<int, mixed> $notes
|
* @param array<int, mixed> $notes
|
||||||
* @return array<int, array<string, mixed>>
|
* @return array<int, array<string, mixed>>
|
||||||
@@ -211,17 +519,24 @@ final class AcademyAccessService
|
|||||||
return collect($notes)
|
return collect($notes)
|
||||||
->filter(static fn ($note): bool => is_array($note))
|
->filter(static fn ($note): bool => is_array($note))
|
||||||
->map(function (array $note): array {
|
->map(function (array $note): array {
|
||||||
|
$imagePayload = $this->responsiveLessonImagePayload(
|
||||||
|
(string) ($note['image_path'] ?? ''),
|
||||||
|
(string) ($note['thumb_path'] ?? ''),
|
||||||
|
);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
'display_type' => trim((string) ($note['display_type'] ?? '')),
|
||||||
'provider' => trim((string) ($note['provider'] ?? '')),
|
'provider' => trim((string) ($note['provider'] ?? '')),
|
||||||
'model_name' => trim((string) ($note['model_name'] ?? '')),
|
'model_name' => trim((string) ($note['model_name'] ?? '')),
|
||||||
'notes' => trim((string) ($note['notes'] ?? '')),
|
'notes' => trim((string) ($note['notes'] ?? '')),
|
||||||
'strengths' => trim((string) ($note['strengths'] ?? '')),
|
'strengths' => trim((string) ($note['strengths'] ?? '')),
|
||||||
'weaknesses' => trim((string) ($note['weaknesses'] ?? '')),
|
'weaknesses' => trim((string) ($note['weaknesses'] ?? '')),
|
||||||
'best_for' => trim((string) ($note['best_for'] ?? '')),
|
'best_for' => trim((string) ($note['best_for'] ?? '')),
|
||||||
'image_path' => trim((string) ($note['image_path'] ?? '')),
|
'image_path' => $imagePayload['image_path'],
|
||||||
'image_url' => $this->resolveLessonMediaUrl((string) ($note['image_path'] ?? '')),
|
'image_url' => $imagePayload['image_url'],
|
||||||
'thumb_path' => trim((string) ($note['thumb_path'] ?? '')),
|
'thumb_path' => $imagePayload['thumb_path'],
|
||||||
'thumb_url' => $this->resolveLessonMediaUrl((string) ($note['thumb_path'] ?? '')),
|
'thumb_url' => $imagePayload['thumb_url'],
|
||||||
|
'image_srcset' => $imagePayload['srcset'],
|
||||||
'settings' => trim((string) ($note['settings'] ?? '')),
|
'settings' => trim((string) ($note['settings'] ?? '')),
|
||||||
'score' => filled($note['score'] ?? null) ? (int) $note['score'] : null,
|
'score' => filled($note['score'] ?? null) ? (int) $note['score'] : null,
|
||||||
'active' => filter_var($note['active'] ?? true, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE) ?? true,
|
'active' => filter_var($note['active'] ?? true, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE) ?? true,
|
||||||
@@ -229,6 +544,7 @@ final class AcademyAccessService
|
|||||||
})
|
})
|
||||||
->filter(function (array $note): bool {
|
->filter(function (array $note): bool {
|
||||||
return collect([
|
return collect([
|
||||||
|
$note['display_type'],
|
||||||
$note['provider'],
|
$note['provider'],
|
||||||
$note['model_name'],
|
$note['model_name'],
|
||||||
$note['notes'],
|
$note['notes'],
|
||||||
@@ -296,30 +612,127 @@ final class AcademyAccessService
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function rankForUser(User $user): int
|
|
||||||
{
|
|
||||||
if (method_exists($user, 'hasAcademyProAccess') && $user->hasAcademyProAccess()) {
|
|
||||||
return $this->rankForLevel('pro');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (method_exists($user, 'hasAcademyCreatorAccess') && $user->hasAcademyCreatorAccess()) {
|
|
||||||
return $this->rankForLevel('creator');
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->rankForLevel('free');
|
|
||||||
}
|
|
||||||
|
|
||||||
private function rankForLevel(string $accessLevel): int
|
private function rankForLevel(string $accessLevel): int
|
||||||
{
|
{
|
||||||
return match (Str::lower(trim($accessLevel))) {
|
return match ($this->normalizeAccessLevel($accessLevel)) {
|
||||||
'admin' => 99,
|
'admin' => 99,
|
||||||
'premium' => 40,
|
|
||||||
'pro' => 30,
|
'pro' => 30,
|
||||||
'creator' => 20,
|
'creator' => 20,
|
||||||
default => 10,
|
default => 10,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function normalizeAccessLevel(string $accessLevel): string
|
||||||
|
{
|
||||||
|
return match (Str::lower(trim($accessLevel))) {
|
||||||
|
'admin' => 'admin',
|
||||||
|
'pro' => 'pro',
|
||||||
|
'creator', 'premium' => 'creator',
|
||||||
|
'mixed' => 'free',
|
||||||
|
default => 'free',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isAcademyAdmin(User $user): bool
|
||||||
|
{
|
||||||
|
return $user->hasStaffAccess() || $user->isModerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function resolveSubscriptionTier(User $user): ?string
|
||||||
|
{
|
||||||
|
$subscription = $this->activeAcademySubscription($user);
|
||||||
|
|
||||||
|
if (! $subscription instanceof Subscription) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$matchedTier = null;
|
||||||
|
|
||||||
|
foreach ($subscription->items as $item) {
|
||||||
|
$priceId = trim((string) $item->stripe_price);
|
||||||
|
|
||||||
|
if ($priceId === '') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$tier = $this->priceTierMap()[$priceId] ?? null;
|
||||||
|
|
||||||
|
if ($tier === null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($matchedTier === null || $this->rankForLevel($tier) > $this->rankForLevel($matchedTier)) {
|
||||||
|
$matchedTier = $tier;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $matchedTier;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function resolveLegacyPaidTier(User $user): ?string
|
||||||
|
{
|
||||||
|
return match (Str::lower(trim((string) ($user->role ?? '')))) {
|
||||||
|
'academy_pro' => 'pro',
|
||||||
|
'academy_creator' => 'creator',
|
||||||
|
default => null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private function activeAcademySubscription(User $user): ?Subscription
|
||||||
|
{
|
||||||
|
$cacheKey = (int) $user->getKey();
|
||||||
|
|
||||||
|
if (array_key_exists($cacheKey, $this->subscriptionCache)) {
|
||||||
|
return $this->subscriptionCache[$cacheKey];
|
||||||
|
}
|
||||||
|
|
||||||
|
$subscription = $user->subscription($this->subscriptionName());
|
||||||
|
|
||||||
|
if (! $subscription instanceof Subscription) {
|
||||||
|
return $this->subscriptionCache[$cacheKey] = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $subscription->active() && ! $subscription->onGracePeriod()) {
|
||||||
|
return $this->subscriptionCache[$cacheKey] = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->subscriptionCache[$cacheKey] = $subscription->loadMissing('items');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, string>
|
||||||
|
*/
|
||||||
|
private function priceTierMap(): array
|
||||||
|
{
|
||||||
|
if (is_array($this->priceTierMap)) {
|
||||||
|
return $this->priceTierMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
$map = [];
|
||||||
|
|
||||||
|
foreach ((array) config('academy_billing.plans', []) as $plan) {
|
||||||
|
if (! is_array($plan)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$priceId = trim((string) ($plan['stripe_price_id'] ?? ''));
|
||||||
|
$tier = $this->normalizeAccessLevel((string) ($plan['tier'] ?? 'free'));
|
||||||
|
|
||||||
|
if ($priceId === '' || $tier === 'free') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$map[$priceId] = $tier;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->priceTierMap = $map;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function subscriptionName(): string
|
||||||
|
{
|
||||||
|
return (string) config('academy_billing.subscription_name', 'academy');
|
||||||
|
}
|
||||||
|
|
||||||
private function previewText(string $value, int $limit): string
|
private function previewText(string $value, int $limit): string
|
||||||
{
|
{
|
||||||
$plain = trim(strip_tags($value));
|
$plain = trim(strip_tags($value));
|
||||||
@@ -338,6 +751,33 @@ final class AcademyAccessService
|
|||||||
return rtrim(mb_substr($plain, 0, $previewLength)).'...';
|
return rtrim(mb_substr($plain, 0, $previewLength)).'...';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function nullableTrimmedString(mixed $value): ?string
|
||||||
|
{
|
||||||
|
if ($value === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$normalized = trim((string) $value);
|
||||||
|
|
||||||
|
return $normalized !== '' ? $normalized : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, string>
|
||||||
|
*/
|
||||||
|
private function normalizeStringList(mixed $value): array
|
||||||
|
{
|
||||||
|
if (! is_array($value)) {
|
||||||
|
$value = $value === null ? [] : [$value];
|
||||||
|
}
|
||||||
|
|
||||||
|
return collect($value)
|
||||||
|
->map(fn ($item): string => trim((string) $item))
|
||||||
|
->filter(static fn (string $item): bool => $item !== '')
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
private function resolvePreviewImageUrl(string $previewImage): ?string
|
private function resolvePreviewImageUrl(string $previewImage): ?string
|
||||||
{
|
{
|
||||||
$previewImage = trim($previewImage);
|
$previewImage = trim($previewImage);
|
||||||
@@ -353,6 +793,25 @@ final class AcademyAccessService
|
|||||||
return Storage::disk((string) config('uploads.object_storage.disk', 's3'))->url($previewImage);
|
return Storage::disk((string) config('uploads.object_storage.disk', 's3'))->url($previewImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{url:?string,thumb_url:?string,srcset:?string}
|
||||||
|
*/
|
||||||
|
private function promptPreviewImagePayload(string $previewImage): array
|
||||||
|
{
|
||||||
|
$url = $this->resolvePreviewImageUrl($previewImage);
|
||||||
|
$thumbPath = $this->existingResponsiveVariantPath($previewImage, 'thumb');
|
||||||
|
$mediumPath = $this->existingResponsiveVariantPath($previewImage, 'md');
|
||||||
|
|
||||||
|
return [
|
||||||
|
'url' => $url,
|
||||||
|
'thumb_url' => $thumbPath !== null ? $this->resolvePreviewImageUrl($thumbPath) : $url,
|
||||||
|
'srcset' => $this->buildResponsiveSrcset([
|
||||||
|
['url' => $thumbPath !== null ? $this->resolvePreviewImageUrl($thumbPath) : null, 'width' => 480],
|
||||||
|
['url' => $mediumPath !== null ? $this->resolvePreviewImageUrl($mediumPath) : null, 'width' => 960],
|
||||||
|
]),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
private function resolveLessonCoverImageUrl(string $coverImage): ?string
|
private function resolveLessonCoverImageUrl(string $coverImage): ?string
|
||||||
{
|
{
|
||||||
$coverImage = trim($coverImage);
|
$coverImage = trim($coverImage);
|
||||||
@@ -383,6 +842,95 @@ final class AcademyAccessService
|
|||||||
return Storage::disk((string) config('uploads.object_storage.disk', 's3'))->url($path);
|
return Storage::disk((string) config('uploads.object_storage.disk', 's3'))->url($path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{image_path:string,image_url:?string,thumb_path:string,thumb_url:?string,srcset:?string}
|
||||||
|
*/
|
||||||
|
private function responsiveLessonImagePayload(string $imagePath, string $thumbPath = ''): array
|
||||||
|
{
|
||||||
|
$resolvedImagePath = trim($imagePath);
|
||||||
|
$resolvedThumbPath = trim($thumbPath);
|
||||||
|
$imageUrl = $this->resolveLessonMediaUrl($resolvedImagePath);
|
||||||
|
$thumbUrl = $resolvedThumbPath !== '' ? $this->resolveLessonMediaUrl($resolvedThumbPath) : $imageUrl;
|
||||||
|
$mediumPath = $resolvedThumbPath !== '' ? $this->existingResponsiveVariantPath($resolvedImagePath, 'md') : null;
|
||||||
|
|
||||||
|
return [
|
||||||
|
'image_path' => $resolvedImagePath,
|
||||||
|
'image_url' => $imageUrl,
|
||||||
|
'thumb_path' => $resolvedThumbPath,
|
||||||
|
'thumb_url' => $thumbUrl,
|
||||||
|
'srcset' => $this->buildResponsiveSrcset([
|
||||||
|
['url' => $thumbUrl, 'width' => $resolvedThumbPath !== '' ? 480 : null],
|
||||||
|
['url' => $mediumPath !== null ? $this->resolveLessonMediaUrl($mediumPath) : null, 'width' => $mediumPath !== null ? 960 : null],
|
||||||
|
]),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function responsiveVariantPath(string $path, string $variant): ?string
|
||||||
|
{
|
||||||
|
$path = trim($path);
|
||||||
|
|
||||||
|
if ($path === '' || str_starts_with($path, 'http://') || str_starts_with($path, 'https://') || str_starts_with($path, '/')) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$directory = pathinfo($path, PATHINFO_DIRNAME);
|
||||||
|
$filename = pathinfo($path, PATHINFO_FILENAME);
|
||||||
|
$baseFilename = preg_replace('/-(thumb|md)$/', '', $filename) ?? $filename;
|
||||||
|
|
||||||
|
return sprintf('%s/%s-%s.webp', $directory, $baseFilename, $variant);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function existingResponsiveVariantPath(string $path, string $variant): ?string
|
||||||
|
{
|
||||||
|
$variantPath = $this->responsiveVariantPath($path, $variant);
|
||||||
|
|
||||||
|
if ($variantPath === null || ! $this->storagePathExists($variantPath)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $variantPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function storagePathExists(string $path): bool
|
||||||
|
{
|
||||||
|
$normalizedPath = trim($path);
|
||||||
|
|
||||||
|
if ($normalizedPath === '' || str_starts_with($normalizedPath, 'http://') || str_starts_with($normalizedPath, 'https://') || str_starts_with($normalizedPath, '/')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$cacheKey = (string) config('uploads.object_storage.disk', 's3') . ':' . $normalizedPath;
|
||||||
|
|
||||||
|
if (array_key_exists($cacheKey, $this->assetExistsCache)) {
|
||||||
|
return $this->assetExistsCache[$cacheKey];
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$exists = Storage::disk((string) config('uploads.object_storage.disk', 's3'))->exists($normalizedPath);
|
||||||
|
} catch (\Throwable) {
|
||||||
|
$exists = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assetExistsCache[$cacheKey] = $exists;
|
||||||
|
|
||||||
|
return $exists;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<int, array{url:?string,width:int|null}> $variants
|
||||||
|
*/
|
||||||
|
private function buildResponsiveSrcset(array $variants): ?string
|
||||||
|
{
|
||||||
|
$entries = collect($variants)
|
||||||
|
->filter(static fn (array $variant): bool => filled($variant['url'] ?? null) && (int) ($variant['width'] ?? 0) > 0)
|
||||||
|
->unique(fn (array $variant): string => (string) $variant['url'])
|
||||||
|
->map(fn (array $variant): string => sprintf('%s %dw', (string) $variant['url'], (int) $variant['width']))
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
|
||||||
|
return $entries !== [] ? implode(', ', $entries) : null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array<string, mixed>|null
|
* @return array<string, mixed>|null
|
||||||
*/
|
*/
|
||||||
@@ -399,14 +947,21 @@ final class AcademyAccessService
|
|||||||
->values()
|
->values()
|
||||||
->all();
|
->all();
|
||||||
$results = $block->activeComparisonResults
|
$results = $block->activeComparisonResults
|
||||||
->map(fn (AcademyAiComparisonResult $result): array => [
|
->map(function (AcademyAiComparisonResult $result): array {
|
||||||
|
$imagePayload = $this->responsiveLessonImagePayload(
|
||||||
|
(string) $result->image_path,
|
||||||
|
(string) ($result->thumb_path ?? ''),
|
||||||
|
);
|
||||||
|
|
||||||
|
return [
|
||||||
'id' => (int) $result->id,
|
'id' => (int) $result->id,
|
||||||
'provider' => (string) ($result->provider ?? ''),
|
'provider' => (string) ($result->provider ?? ''),
|
||||||
'model_name' => (string) ($result->model_name ?? ''),
|
'model_name' => (string) ($result->model_name ?? ''),
|
||||||
'image_path' => (string) $result->image_path,
|
'image_path' => $imagePayload['image_path'],
|
||||||
'image_url' => $this->resolveLessonMediaUrl((string) $result->image_path),
|
'image_url' => $imagePayload['image_url'],
|
||||||
'thumb_path' => (string) ($result->thumb_path ?? ''),
|
'thumb_path' => $imagePayload['thumb_path'],
|
||||||
'thumb_url' => $this->resolveLessonMediaUrl((string) ($result->thumb_path ?? '')),
|
'thumb_url' => $imagePayload['thumb_url'],
|
||||||
|
'image_srcset' => $imagePayload['srcset'],
|
||||||
'settings' => (string) ($result->settings ?? ''),
|
'settings' => (string) ($result->settings ?? ''),
|
||||||
'strengths' => (string) ($result->strengths ?? ''),
|
'strengths' => (string) ($result->strengths ?? ''),
|
||||||
'weaknesses' => (string) ($result->weaknesses ?? ''),
|
'weaknesses' => (string) ($result->weaknesses ?? ''),
|
||||||
@@ -414,7 +969,8 @@ final class AcademyAccessService
|
|||||||
'score' => $result->score,
|
'score' => $result->score,
|
||||||
'sort_order' => (int) $result->sort_order,
|
'sort_order' => (int) $result->sort_order,
|
||||||
'active' => (bool) $result->active,
|
'active' => (bool) $result->active,
|
||||||
])
|
];
|
||||||
|
})
|
||||||
->values()
|
->values()
|
||||||
->all();
|
->all();
|
||||||
|
|
||||||
|
|||||||
194
app/Services/Academy/AcademyAdminBillingOverviewService.php
Normal file
194
app/Services/Academy/AcademyAdminBillingOverviewService.php
Normal file
@@ -0,0 +1,194 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Services\Academy;
|
||||||
|
|
||||||
|
use App\Models\AcademyBillingEvent;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use Laravel\Cashier\Subscription;
|
||||||
|
|
||||||
|
final class AcademyAdminBillingOverviewService
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly AcademyBillingPlanService $plans,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function summary(): array
|
||||||
|
{
|
||||||
|
$subscriptions = Subscription::query()
|
||||||
|
->where('type', $this->plans->subscriptionName())
|
||||||
|
->with('items')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$activeSubscriptions = $subscriptions->filter(
|
||||||
|
fn (Subscription $subscription): bool => $subscription->active() || $subscription->onGracePeriod()
|
||||||
|
);
|
||||||
|
|
||||||
|
$subscriberTiers = [];
|
||||||
|
$planBreakdown = [];
|
||||||
|
$gracePeriodSubscribers = [];
|
||||||
|
|
||||||
|
foreach ($activeSubscriptions as $subscription) {
|
||||||
|
$userId = (int) $subscription->user_id;
|
||||||
|
$tier = $this->tierForSubscription($subscription);
|
||||||
|
|
||||||
|
if ($subscription->onGracePeriod()) {
|
||||||
|
$gracePeriodSubscribers[$userId] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($tier !== null) {
|
||||||
|
$existingTier = $subscriberTiers[$userId] ?? null;
|
||||||
|
|
||||||
|
if ($existingTier === null || $this->rankForTier($tier) > $this->rankForTier($existingTier)) {
|
||||||
|
$subscriberTiers[$userId] = $tier;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this->planKeysForSubscription($subscription) as $planKey) {
|
||||||
|
$planBreakdown[$planKey] = (int) ($planBreakdown[$planKey] ?? 0) + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$recentEvents = AcademyBillingEvent::query()->count();
|
||||||
|
$lastWebhookAt = AcademyBillingEvent::query()->latest('processed_at')->value('processed_at');
|
||||||
|
|
||||||
|
return [
|
||||||
|
'enabled' => $this->plans->enabled(),
|
||||||
|
'active_subscribers' => count($subscriberTiers),
|
||||||
|
'creator_subscribers' => count(array_filter($subscriberTiers, static fn (string $tier): bool => $tier === 'creator')),
|
||||||
|
'pro_subscribers' => count(array_filter($subscriberTiers, static fn (string $tier): bool => $tier === 'pro')),
|
||||||
|
'grace_period_subscribers' => count($gracePeriodSubscribers),
|
||||||
|
'ended_subscriptions' => $subscriptions->filter(
|
||||||
|
fn (Subscription $subscription): bool => ! $subscription->active() && ! $subscription->onGracePeriod()
|
||||||
|
)->count(),
|
||||||
|
'configured_plan_count' => count(array_keys($this->plans->plans())),
|
||||||
|
'missing_plan_keys' => $this->plans->missingPriceIds(),
|
||||||
|
'plan_breakdown' => $this->formatPlanBreakdown($planBreakdown),
|
||||||
|
'recent_webhook_count' => $recentEvents,
|
||||||
|
'last_webhook_at' => $lastWebhookAt?->toISOString(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, array<string, mixed>>
|
||||||
|
*/
|
||||||
|
public function recentEvents(int $limit = 15): array
|
||||||
|
{
|
||||||
|
return AcademyBillingEvent::query()
|
||||||
|
->latest('processed_at')
|
||||||
|
->latest('id')
|
||||||
|
->limit($limit)
|
||||||
|
->get()
|
||||||
|
->map(fn (AcademyBillingEvent $event): array => [
|
||||||
|
'id' => (int) $event->id,
|
||||||
|
'event_type' => (string) $event->event_type,
|
||||||
|
'academy_tier' => $event->academy_tier ? (string) $event->academy_tier : null,
|
||||||
|
'academy_plan' => $event->academy_plan ? (string) $event->academy_plan : null,
|
||||||
|
'user_id' => $event->user_id ? (int) $event->user_id : null,
|
||||||
|
'stripe_customer_id' => $event->stripe_customer_id ? (string) $event->stripe_customer_id : null,
|
||||||
|
'stripe_subscription_id' => $event->stripe_subscription_id ? (string) $event->stripe_subscription_id : null,
|
||||||
|
'processed_at' => $event->processed_at?->toISOString(),
|
||||||
|
'created_at' => $event->created_at?->toISOString(),
|
||||||
|
'payload_summary' => is_array($event->payload_summary) ? $event->payload_summary : [],
|
||||||
|
])
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, array<string, mixed>>
|
||||||
|
*/
|
||||||
|
private function formatPlanBreakdown(array $planBreakdown): array
|
||||||
|
{
|
||||||
|
return collect(array_keys($this->plans->plans()))
|
||||||
|
->map(function (string $planKey) use ($planBreakdown): array {
|
||||||
|
$plan = $this->plans->plan($planKey);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'key' => $planKey,
|
||||||
|
'label' => (string) ($plan['label'] ?? $planKey),
|
||||||
|
'tier' => (string) ($plan['tier'] ?? 'free'),
|
||||||
|
'interval' => (string) ($plan['interval'] ?? 'monthly'),
|
||||||
|
'configured' => (bool) ($plan['configured'] ?? false),
|
||||||
|
'subscribers' => (int) ($planBreakdown[$planKey] ?? 0),
|
||||||
|
];
|
||||||
|
})
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list<string>
|
||||||
|
*/
|
||||||
|
private function planKeysForSubscription(Subscription $subscription): array
|
||||||
|
{
|
||||||
|
$keys = [];
|
||||||
|
|
||||||
|
foreach ($this->priceIdsForSubscription($subscription) as $priceId) {
|
||||||
|
$plan = $this->plans->planForPriceId($priceId);
|
||||||
|
|
||||||
|
if ($plan === null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$keys[] = (string) ($plan['key'] ?? '');
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_values(array_unique(array_filter($keys, static fn (string $key): bool => $key !== '')));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function tierForSubscription(Subscription $subscription): ?string
|
||||||
|
{
|
||||||
|
$matchedTier = null;
|
||||||
|
|
||||||
|
foreach ($this->priceIdsForSubscription($subscription) as $priceId) {
|
||||||
|
$plan = $this->plans->planForPriceId($priceId);
|
||||||
|
|
||||||
|
if ($plan === null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$tier = (string) ($plan['tier'] ?? 'free');
|
||||||
|
|
||||||
|
if ($matchedTier === null || $this->rankForTier($tier) > $this->rankForTier($matchedTier)) {
|
||||||
|
$matchedTier = $tier;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $matchedTier;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Collection<int, string>
|
||||||
|
*/
|
||||||
|
private function priceIdsForSubscription(Subscription $subscription): Collection
|
||||||
|
{
|
||||||
|
$priceIds = $subscription->items
|
||||||
|
->pluck('stripe_price')
|
||||||
|
->filter(fn ($value): bool => is_string($value) && trim($value) !== '')
|
||||||
|
->map(fn (string $value): string => trim($value));
|
||||||
|
|
||||||
|
if ($priceIds->isNotEmpty()) {
|
||||||
|
return $priceIds->values();
|
||||||
|
}
|
||||||
|
|
||||||
|
$fallbackPrice = trim((string) $subscription->stripe_price);
|
||||||
|
|
||||||
|
return $fallbackPrice === ''
|
||||||
|
? collect()
|
||||||
|
: collect([$fallbackPrice]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function rankForTier(string $tier): int
|
||||||
|
{
|
||||||
|
return match ($this->plans->normalizeTier($tier)) {
|
||||||
|
'pro' => 2,
|
||||||
|
'creator' => 1,
|
||||||
|
default => 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
70
app/Services/Academy/AcademyAnalyticsContentResolver.php
Normal file
70
app/Services/Academy/AcademyAnalyticsContentResolver.php
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Services\Academy;
|
||||||
|
|
||||||
|
use App\Models\AcademyChallenge;
|
||||||
|
use App\Models\AcademyCourse;
|
||||||
|
use App\Models\AcademyLesson;
|
||||||
|
use App\Models\AcademyPromptPack;
|
||||||
|
use App\Models\AcademyPromptTemplate;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
final class AcademyAnalyticsContentResolver
|
||||||
|
{
|
||||||
|
public function resolve(string $contentType, int $contentId): ?Model
|
||||||
|
{
|
||||||
|
$modelClass = match ($contentType) {
|
||||||
|
AcademyAnalyticsContentType::PROMPT => AcademyPromptTemplate::class,
|
||||||
|
AcademyAnalyticsContentType::LESSON => AcademyLesson::class,
|
||||||
|
AcademyAnalyticsContentType::COURSE => AcademyCourse::class,
|
||||||
|
AcademyAnalyticsContentType::PROMPT_PACK => AcademyPromptPack::class,
|
||||||
|
AcademyAnalyticsContentType::CHALLENGE => AcademyChallenge::class,
|
||||||
|
default => null,
|
||||||
|
};
|
||||||
|
|
||||||
|
if ($modelClass === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $modelClass::query()->find($contentId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function exists(string $contentType, int $contentId): bool
|
||||||
|
{
|
||||||
|
return $this->resolve($contentType, $contentId) instanceof Model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function title(string $contentType, ?int $contentId): string
|
||||||
|
{
|
||||||
|
if (! $contentId) {
|
||||||
|
return match ($contentType) {
|
||||||
|
AcademyAnalyticsContentType::HOME => 'Academy Home',
|
||||||
|
AcademyAnalyticsContentType::SEARCH => 'Academy Search',
|
||||||
|
AcademyAnalyticsContentType::UPGRADE => 'Academy Upgrade',
|
||||||
|
default => 'Unknown Academy Content',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
$content = $this->resolve($contentType, $contentId);
|
||||||
|
|
||||||
|
if (! $content instanceof Model) {
|
||||||
|
return 'Unknown Academy Content';
|
||||||
|
}
|
||||||
|
|
||||||
|
return (string) ($content->title ?? $content->name ?? sprintf('%s #%d', $contentType, $contentId));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function accessLevel(string $contentType, ?int $contentId): ?string
|
||||||
|
{
|
||||||
|
if (! $contentId) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$content = $this->resolve($contentType, $contentId);
|
||||||
|
|
||||||
|
return $content instanceof Model ? (string) ($content->access_level ?? '') : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
369
app/Services/Academy/AcademyAnalyticsService.php
Normal file
369
app/Services/Academy/AcademyAnalyticsService.php
Normal file
@@ -0,0 +1,369 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Services\Academy;
|
||||||
|
|
||||||
|
use App\Models\AcademyEvent;
|
||||||
|
use App\Models\AcademySearchLog;
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsEventType;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
final class AcademyAnalyticsService
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var list<string>
|
||||||
|
*/
|
||||||
|
private const KNOWN_BOT_PATTERNS = [
|
||||||
|
'googlebot',
|
||||||
|
'bingbot',
|
||||||
|
'ahrefsbot',
|
||||||
|
'semrushbot',
|
||||||
|
'dotbot',
|
||||||
|
'barkrowler',
|
||||||
|
'claudebot',
|
||||||
|
'gptbot',
|
||||||
|
'amazonbot',
|
||||||
|
'mj12bot',
|
||||||
|
'petalbot',
|
||||||
|
'yandexbot',
|
||||||
|
'bytespider',
|
||||||
|
'crawler',
|
||||||
|
'spider',
|
||||||
|
'headless',
|
||||||
|
'preview',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function __construct(private readonly AcademyAnalyticsContentResolver $contentResolver)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $payload
|
||||||
|
*/
|
||||||
|
public function track(array $payload, ?User $user = null, ?Request $request = null): AcademyEvent
|
||||||
|
{
|
||||||
|
$request ??= request();
|
||||||
|
$user ??= $request?->user();
|
||||||
|
|
||||||
|
$eventType = trim((string) ($payload['event_type'] ?? ''));
|
||||||
|
$contentType = $this->normalizeNullableString($payload['content_type'] ?? null);
|
||||||
|
$contentId = filled($payload['content_id'] ?? null) ? (int) $payload['content_id'] : null;
|
||||||
|
$metadata = is_array($payload['metadata'] ?? null) ? $payload['metadata'] : [];
|
||||||
|
$rawOccurredAt = $payload['occurred_at'] ?? null;
|
||||||
|
$occurredAt = $rawOccurredAt instanceof Carbon
|
||||||
|
? $rawOccurredAt
|
||||||
|
: Carbon::parse((string) ($rawOccurredAt ?? now()->toISOString()));
|
||||||
|
$userAgent = strtolower(trim((string) ($request?->userAgent() ?? '')));
|
||||||
|
$isBot = $this->looksLikeBot($userAgent);
|
||||||
|
$isCrawler = $isBot || str_contains($userAgent, 'crawl');
|
||||||
|
$isAdmin = $user ? ($user->hasStaffAccess() || $user->isModerator()) : false;
|
||||||
|
$isSubscriber = $user ? ($user->hasAcademyProAccess() || $user->hasAcademyCreatorAccess()) : false;
|
||||||
|
$visitorId = $this->resolveVisitorId($payload, $request, $user);
|
||||||
|
|
||||||
|
$event = AcademyEvent::query()->create([
|
||||||
|
'event_type' => $eventType,
|
||||||
|
'content_type' => $contentType,
|
||||||
|
'content_id' => $contentId,
|
||||||
|
'user_id' => $user?->id,
|
||||||
|
'visitor_id' => $visitorId,
|
||||||
|
'session_id' => $this->normalizeNullableString($payload['session_id'] ?? ($request?->hasSession() ? $request->session()->getId() : null)),
|
||||||
|
'url' => $this->normalizeNullableString($payload['url'] ?? $request?->fullUrl()),
|
||||||
|
'route_name' => $this->normalizeNullableString($payload['route_name'] ?? $request?->route()?->getName()),
|
||||||
|
'referrer' => $this->normalizeNullableString($payload['referrer'] ?? $request?->headers->get('referer')),
|
||||||
|
'utm_source' => $this->normalizeNullableString($payload['utm_source'] ?? $request?->query('utm_source')),
|
||||||
|
'utm_medium' => $this->normalizeNullableString($payload['utm_medium'] ?? $request?->query('utm_medium')),
|
||||||
|
'utm_campaign' => $this->normalizeNullableString($payload['utm_campaign'] ?? $request?->query('utm_campaign')),
|
||||||
|
'device_type' => $this->deviceTypeFromUserAgent($userAgent),
|
||||||
|
'browser' => $this->browserFromUserAgent($userAgent),
|
||||||
|
'platform' => $this->platformFromUserAgent($userAgent),
|
||||||
|
'country_code' => $this->countryCodeFromRequest($request),
|
||||||
|
'is_logged_in' => $user !== null,
|
||||||
|
'is_subscriber' => $isSubscriber,
|
||||||
|
'is_admin' => $isAdmin,
|
||||||
|
'is_bot' => $isBot,
|
||||||
|
'is_crawler' => $isCrawler,
|
||||||
|
'is_suspicious' => $isBot || $this->looksSuspicious($request, $userAgent),
|
||||||
|
'metadata' => $metadata === [] ? null : $metadata,
|
||||||
|
'occurred_at' => $occurredAt,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($eventType === AcademyAnalyticsEventType::SEARCH_RESULT_CLICK) {
|
||||||
|
$this->syncSearchResultClickAttribution($event, $metadata, $request, $user);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $event;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function trackContentView(string $contentType, ?int $contentId, Request $request): void
|
||||||
|
{
|
||||||
|
$this->track([
|
||||||
|
'event_type' => AcademyAnalyticsEventType::PAGE_VIEW,
|
||||||
|
'content_type' => $contentType,
|
||||||
|
'content_id' => $contentId,
|
||||||
|
'metadata' => ['source' => 'academy_page'],
|
||||||
|
], $request->user(), $request);
|
||||||
|
|
||||||
|
$specificEvent = match ($contentType) {
|
||||||
|
AcademyAnalyticsContentType::PROMPT => AcademyAnalyticsEventType::CONTENT_VIEW,
|
||||||
|
AcademyAnalyticsContentType::LESSON => AcademyAnalyticsEventType::LESSON_VIEW,
|
||||||
|
AcademyAnalyticsContentType::COURSE => AcademyAnalyticsEventType::COURSE_VIEW,
|
||||||
|
AcademyAnalyticsContentType::PROMPT_PACK => AcademyAnalyticsEventType::PROMPT_PACK_VIEW,
|
||||||
|
AcademyAnalyticsContentType::CHALLENGE => AcademyAnalyticsEventType::CHALLENGE_VIEW,
|
||||||
|
default => AcademyAnalyticsEventType::CONTENT_VIEW,
|
||||||
|
};
|
||||||
|
|
||||||
|
$this->track([
|
||||||
|
'event_type' => $specificEvent,
|
||||||
|
'content_type' => $contentType,
|
||||||
|
'content_id' => $contentId,
|
||||||
|
], $request->user(), $request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function trackPromptCopy(int $promptId, string $copyType, Request $request): void
|
||||||
|
{
|
||||||
|
$eventType = trim(strtolower($copyType)) === 'negative'
|
||||||
|
? AcademyAnalyticsEventType::PROMPT_NEGATIVE_COPY
|
||||||
|
: AcademyAnalyticsEventType::PROMPT_COPY;
|
||||||
|
|
||||||
|
$this->track([
|
||||||
|
'event_type' => $eventType,
|
||||||
|
'content_type' => AcademyAnalyticsContentType::PROMPT,
|
||||||
|
'content_id' => $promptId,
|
||||||
|
'metadata' => [
|
||||||
|
'copy_type' => $copyType,
|
||||||
|
'source' => 'prompt_detail',
|
||||||
|
],
|
||||||
|
], $request->user(), $request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function trackUpgradeClick(?string $source, ?string $contentType, ?int $contentId, Request $request): void
|
||||||
|
{
|
||||||
|
$this->track([
|
||||||
|
'event_type' => AcademyAnalyticsEventType::UPGRADE_CLICK,
|
||||||
|
'content_type' => $contentType ?: AcademyAnalyticsContentType::UPGRADE,
|
||||||
|
'content_id' => $contentId,
|
||||||
|
'metadata' => array_filter([
|
||||||
|
'source' => $this->normalizeNullableString($source),
|
||||||
|
]),
|
||||||
|
], $request->user(), $request);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $filters
|
||||||
|
*/
|
||||||
|
public function trackSearch(string $query, int $resultsCount, array $filters = [], ?Request $request = null): AcademySearchLog
|
||||||
|
{
|
||||||
|
$request ??= request();
|
||||||
|
$user = $request?->user();
|
||||||
|
$normalizedQuery = $this->normalizeSearchQuery($query);
|
||||||
|
$isBot = $this->looksLikeBot(strtolower(trim((string) ($request?->userAgent() ?? ''))));
|
||||||
|
|
||||||
|
$log = AcademySearchLog::query()->create([
|
||||||
|
'user_id' => $user?->id,
|
||||||
|
'visitor_id' => $this->resolveVisitorId([], $request, $user),
|
||||||
|
'query' => trim($query),
|
||||||
|
'normalized_query' => $normalizedQuery,
|
||||||
|
'results_count' => max(0, $resultsCount),
|
||||||
|
'filters' => $filters === [] ? null : $filters,
|
||||||
|
'is_logged_in' => $user !== null,
|
||||||
|
'is_subscriber' => $user ? ($user->hasAcademyCreatorAccess() || $user->hasAcademyProAccess()) : false,
|
||||||
|
'is_bot' => $isBot,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->track([
|
||||||
|
'event_type' => AcademyAnalyticsEventType::SEARCH,
|
||||||
|
'content_type' => AcademyAnalyticsContentType::SEARCH,
|
||||||
|
'metadata' => [
|
||||||
|
'query' => $normalizedQuery,
|
||||||
|
'results_count' => $resultsCount,
|
||||||
|
'filters' => $filters,
|
||||||
|
],
|
||||||
|
], $user, $request);
|
||||||
|
|
||||||
|
if ($resultsCount === 0) {
|
||||||
|
$this->track([
|
||||||
|
'event_type' => AcademyAnalyticsEventType::ZERO_SEARCH_RESULTS,
|
||||||
|
'content_type' => AcademyAnalyticsContentType::SEARCH,
|
||||||
|
'metadata' => [
|
||||||
|
'query' => $normalizedQuery,
|
||||||
|
'filters' => $filters,
|
||||||
|
],
|
||||||
|
], $user, $request);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $log;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function normalizeSearchQuery(string $query): string
|
||||||
|
{
|
||||||
|
$value = strtolower(trim($query));
|
||||||
|
$value = preg_replace('/\s+/', ' ', $value) ?? $value;
|
||||||
|
$value = preg_replace('/[^a-z0-9\s\-_]+/', '', $value) ?? $value;
|
||||||
|
|
||||||
|
return trim($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $metadata
|
||||||
|
*/
|
||||||
|
private function syncSearchResultClickAttribution(AcademyEvent $event, array $metadata, ?Request $request, ?User $user): AcademySearchLog
|
||||||
|
{
|
||||||
|
$query = trim((string) ($metadata['query'] ?? ''));
|
||||||
|
$normalizedQuery = $this->normalizeSearchQuery((string) ($metadata['normalized_query'] ?? $query));
|
||||||
|
$resultsCount = max(0, (int) ($metadata['results_count'] ?? 0));
|
||||||
|
$filters = is_array($metadata['filters'] ?? null) ? $metadata['filters'] : [];
|
||||||
|
$visitorId = $this->normalizeNullableString($event->visitor_id) ?? $this->resolveVisitorId([], $request, $user);
|
||||||
|
$recentThreshold = ($event->occurred_at ?? now())->copy()->subMinutes(30);
|
||||||
|
|
||||||
|
$searchLog = AcademySearchLog::query()
|
||||||
|
->where('normalized_query', $normalizedQuery)
|
||||||
|
->where('created_at', '>=', $recentThreshold)
|
||||||
|
->whereNull('clicked_content_id')
|
||||||
|
->where(function ($builder) use ($user, $visitorId): void {
|
||||||
|
if ($user?->id !== null) {
|
||||||
|
$builder->orWhere('user_id', $user->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($visitorId !== null) {
|
||||||
|
$builder->orWhere('visitor_id', $visitorId);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
->latest('id')
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if ($searchLog instanceof AcademySearchLog) {
|
||||||
|
$searchLog->forceFill([
|
||||||
|
'clicked_content_type' => $event->content_type,
|
||||||
|
'clicked_content_id' => $event->content_id,
|
||||||
|
])->save();
|
||||||
|
|
||||||
|
return $searchLog;
|
||||||
|
}
|
||||||
|
|
||||||
|
return AcademySearchLog::query()->create([
|
||||||
|
'user_id' => $user?->id,
|
||||||
|
'visitor_id' => $visitorId,
|
||||||
|
'query' => $query,
|
||||||
|
'normalized_query' => $normalizedQuery,
|
||||||
|
'results_count' => $resultsCount,
|
||||||
|
'clicked_content_type' => $event->content_type,
|
||||||
|
'clicked_content_id' => $event->content_id,
|
||||||
|
'filters' => $filters === [] ? null : $filters,
|
||||||
|
'is_logged_in' => $user !== null,
|
||||||
|
'is_subscriber' => (bool) $event->is_subscriber,
|
||||||
|
'is_bot' => (bool) $event->is_bot,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function resolveVisitorId(array $payload, ?Request $request, ?User $user): ?string
|
||||||
|
{
|
||||||
|
$payloadVisitorId = $this->normalizeNullableString($payload['visitor_id'] ?? null);
|
||||||
|
if ($payloadVisitorId !== null) {
|
||||||
|
return $payloadVisitorId;
|
||||||
|
}
|
||||||
|
|
||||||
|
$cookieVisitorId = $this->normalizeNullableString($request?->cookie('academy_visitor_id'));
|
||||||
|
if ($cookieVisitorId !== null) {
|
||||||
|
return $cookieVisitorId;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($user) {
|
||||||
|
return sprintf('user:%d', $user->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (string) Str::uuid();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function looksLikeBot(string $userAgent): bool
|
||||||
|
{
|
||||||
|
if ($userAgent === '') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (self::KNOWN_BOT_PATTERNS as $pattern) {
|
||||||
|
if (str_contains($userAgent, $pattern)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function looksSuspicious(?Request $request, string $userAgent): bool
|
||||||
|
{
|
||||||
|
if ($request === null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->looksLikeBot($userAgent)
|
||||||
|
|| str_contains(strtolower((string) $request->headers->get('accept', '')), '*/*')
|
||||||
|
|| $request->headers->get('sec-fetch-site') === null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function deviceTypeFromUserAgent(string $userAgent): string
|
||||||
|
{
|
||||||
|
if ($userAgent === '') {
|
||||||
|
return 'unknown';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (str_contains($userAgent, 'tablet') || str_contains($userAgent, 'ipad')) {
|
||||||
|
return 'tablet';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (str_contains($userAgent, 'mobile') || str_contains($userAgent, 'android')) {
|
||||||
|
return 'mobile';
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'desktop';
|
||||||
|
}
|
||||||
|
|
||||||
|
private function browserFromUserAgent(string $userAgent): ?string
|
||||||
|
{
|
||||||
|
if ($userAgent === '') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return match (true) {
|
||||||
|
str_contains($userAgent, 'edg/') => 'Edge',
|
||||||
|
str_contains($userAgent, 'chrome/') => 'Chrome',
|
||||||
|
str_contains($userAgent, 'firefox/') => 'Firefox',
|
||||||
|
str_contains($userAgent, 'safari/') && ! str_contains($userAgent, 'chrome/') => 'Safari',
|
||||||
|
default => 'Other',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private function platformFromUserAgent(string $userAgent): ?string
|
||||||
|
{
|
||||||
|
if ($userAgent === '') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return match (true) {
|
||||||
|
str_contains($userAgent, 'windows') => 'Windows',
|
||||||
|
str_contains($userAgent, 'mac os') || str_contains($userAgent, 'macintosh') => 'macOS',
|
||||||
|
str_contains($userAgent, 'android') => 'Android',
|
||||||
|
str_contains($userAgent, 'iphone') || str_contains($userAgent, 'ipad') || str_contains($userAgent, 'ios') => 'iOS',
|
||||||
|
str_contains($userAgent, 'linux') => 'Linux',
|
||||||
|
default => 'Other',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private function countryCodeFromRequest(?Request $request): ?string
|
||||||
|
{
|
||||||
|
$country = strtoupper(trim((string) ($request?->headers->get('cf-ipcountry') ?? $request?->headers->get('x-country-code') ?? '')));
|
||||||
|
|
||||||
|
return $country !== '' && strlen($country) <= 8 ? $country : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function normalizeNullableString(mixed $value): ?string
|
||||||
|
{
|
||||||
|
$normalized = trim((string) $value);
|
||||||
|
|
||||||
|
return $normalized === '' ? null : $normalized;
|
||||||
|
}
|
||||||
|
}
|
||||||
148
app/Services/Academy/AcademyBillingPlanService.php
Normal file
148
app/Services/Academy/AcademyBillingPlanService.php
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Services\Academy;
|
||||||
|
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
final class AcademyBillingPlanService
|
||||||
|
{
|
||||||
|
public function enabled(): bool
|
||||||
|
{
|
||||||
|
return (bool) config('academy_billing.enabled', false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function subscriptionName(): string
|
||||||
|
{
|
||||||
|
return (string) config('academy_billing.subscription_name', 'academy');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, array<string, mixed>>
|
||||||
|
*/
|
||||||
|
public function plans(): array
|
||||||
|
{
|
||||||
|
$plans = config('academy_billing.plans', []);
|
||||||
|
|
||||||
|
return is_array($plans) ? $plans : [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function normalizePlanKey(?string $planKey): string
|
||||||
|
{
|
||||||
|
return Str::of((string) $planKey)
|
||||||
|
->trim()
|
||||||
|
->lower()
|
||||||
|
->replace('-', '_')
|
||||||
|
->value();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>|null
|
||||||
|
*/
|
||||||
|
public function plan(?string $planKey): ?array
|
||||||
|
{
|
||||||
|
$normalized = $this->normalizePlanKey($planKey);
|
||||||
|
|
||||||
|
if ($normalized === '') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$plan = Arr::get($this->plans(), $normalized);
|
||||||
|
|
||||||
|
if (! is_array($plan)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$plan['key'] = $normalized;
|
||||||
|
$plan['tier'] = $this->normalizeTier((string) ($plan['tier'] ?? 'free'));
|
||||||
|
$plan['interval'] = Str::lower(trim((string) ($plan['interval'] ?? 'monthly')));
|
||||||
|
$plan['amount'] = trim((string) ($plan['amount'] ?? ''));
|
||||||
|
$plan['currency'] = Str::upper(trim((string) ($plan['currency'] ?? config('cashier.currency', 'EUR'))));
|
||||||
|
$plan['stripe_price_id'] = trim((string) ($plan['stripe_price_id'] ?? ''));
|
||||||
|
$plan['configured'] = $plan['stripe_price_id'] !== '';
|
||||||
|
$plan['price_id_valid'] = $this->isValidPriceId($plan['stripe_price_id']);
|
||||||
|
$plan['price_display'] = $plan['amount'] !== '' ? $plan['amount'].' '.$plan['currency'] : null;
|
||||||
|
|
||||||
|
return $plan;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>|null
|
||||||
|
*/
|
||||||
|
public function planForPriceId(?string $priceId): ?array
|
||||||
|
{
|
||||||
|
$priceId = trim((string) $priceId);
|
||||||
|
|
||||||
|
if ($priceId === '') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (array_keys($this->plans()) as $planKey) {
|
||||||
|
$plan = $this->plan((string) $planKey);
|
||||||
|
|
||||||
|
if ($plan !== null && ($plan['stripe_price_id'] ?? null) === $priceId) {
|
||||||
|
return $plan;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, string>
|
||||||
|
*/
|
||||||
|
public function missingPriceIds(?string $planKey = null): array
|
||||||
|
{
|
||||||
|
if ($planKey !== null) {
|
||||||
|
$plan = $this->plan($planKey);
|
||||||
|
|
||||||
|
return $plan !== null && ! ($plan['configured'] ?? false)
|
||||||
|
? [$this->normalizePlanKey($planKey)]
|
||||||
|
: [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return collect(array_keys($this->plans()))
|
||||||
|
->filter(fn (string $key): bool => ! ((bool) ($this->plan($key)['configured'] ?? false)))
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function assertConfigured(?string $planKey = null): void
|
||||||
|
{
|
||||||
|
if (app()->environment(['local', 'testing'])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$missingPlans = $this->missingPriceIds($planKey);
|
||||||
|
|
||||||
|
if ($missingPlans === []) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new RuntimeException('Academy billing price IDs are missing for: '.implode(', ', $missingPlans));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function normalizeTier(string $tier): string
|
||||||
|
{
|
||||||
|
return match (Str::lower(trim($tier))) {
|
||||||
|
'admin' => 'admin',
|
||||||
|
'pro' => 'pro',
|
||||||
|
'creator', 'premium' => 'creator',
|
||||||
|
default => 'free',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isValidPriceId(?string $priceId): bool
|
||||||
|
{
|
||||||
|
$priceId = trim((string) $priceId);
|
||||||
|
|
||||||
|
if ($priceId === '') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return preg_match('/^price_[A-Za-z0-9]+$/', $priceId) === 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
784
app/Services/Academy/AcademyContentIntelligenceService.php
Normal file
784
app/Services/Academy/AcademyContentIntelligenceService.php
Normal file
@@ -0,0 +1,784 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Services\Academy;
|
||||||
|
|
||||||
|
use App\Models\AcademyContentMetricDaily;
|
||||||
|
use App\Models\AcademySearchLog;
|
||||||
|
use App\Models\AcademyUserProgress;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
final class AcademyContentIntelligenceService
|
||||||
|
{
|
||||||
|
public function __construct(private readonly AcademyAnalyticsContentResolver $resolver) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $filters
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function getContentOpportunities(array $filters = []): array
|
||||||
|
{
|
||||||
|
return $this->remember('content-opportunities', $filters, function (Carbon $from, Carbon $to, int $limit): array {
|
||||||
|
$searchGaps = $this->getSearchGaps(['from' => $from, 'to' => $to, 'limit' => $limit]);
|
||||||
|
$promptInsights = $this->getPromptInsights(['from' => $from, 'to' => $to, 'limit' => $limit]);
|
||||||
|
$lessonDropoffs = $this->getLessonDropoffs(['from' => $from, 'to' => $to, 'limit' => $limit]);
|
||||||
|
$courseHealth = $this->getCourseHealth(['from' => $from, 'to' => $to, 'limit' => $limit]);
|
||||||
|
$premiumInterest = $this->getPremiumInterest(['from' => $from, 'to' => $to, 'limit' => $limit]);
|
||||||
|
$recommendations = $this->getEditorialRecommendations(['from' => $from, 'to' => $to, 'limit' => $limit]);
|
||||||
|
|
||||||
|
$cards = [
|
||||||
|
[
|
||||||
|
'label' => 'Content opportunities',
|
||||||
|
'value' => count($recommendations['rows']),
|
||||||
|
'description' => 'Actionable content, conversion, and editorial recommendations generated from Academy analytics.',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'label' => 'Search gaps',
|
||||||
|
'value' => (int) $searchGaps['summary']['gap_count'],
|
||||||
|
'description' => 'Queries with zero results, weak CTR, or no result clicks.',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'label' => 'Prompt insights',
|
||||||
|
'value' => (int) $promptInsights['summary']['signal_count'],
|
||||||
|
'description' => 'Prompts that should be improved, promoted, or expanded.',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'label' => 'Lesson drop-offs',
|
||||||
|
'value' => (int) $lessonDropoffs['summary']['signal_count'],
|
||||||
|
'description' => 'Lessons losing users before they meaningfully start or finish.',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'label' => 'Course health',
|
||||||
|
'value' => (int) $courseHealth['summary']['signal_count'],
|
||||||
|
'description' => 'Courses that need restructuring or are ready for expansion.',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'label' => 'Premium interest',
|
||||||
|
'value' => (int) $premiumInterest['summary']['signal_count'],
|
||||||
|
'description' => 'Content that shows premium teaser strength or weakness.',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'label' => 'Editorial recommendations',
|
||||||
|
'value' => (int) $recommendations['summary']['total'],
|
||||||
|
'description' => 'Prioritized actions for what to create, improve, promote, or premiumize next.',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
return [
|
||||||
|
'cards' => $cards,
|
||||||
|
'highlights' => collect($recommendations['rows'])
|
||||||
|
->take(6)
|
||||||
|
->map(fn (array $row): array => [
|
||||||
|
'title' => (string) $row['title'],
|
||||||
|
'priority' => (string) $row['priority'],
|
||||||
|
'reason' => (string) $row['reason'],
|
||||||
|
'suggested_action' => (string) $row['suggested_action'],
|
||||||
|
])
|
||||||
|
->values()
|
||||||
|
->all(),
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $filters
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function getSearchGaps(array $filters = []): array
|
||||||
|
{
|
||||||
|
return $this->remember('search-gaps', $filters, function (Carbon $from, Carbon $to, int $limit): array {
|
||||||
|
$rows = $this->searchQuery($from, $to)->get()->map(function ($row): array {
|
||||||
|
$searches = max(0, (int) $row->searches);
|
||||||
|
$clicks = max(0, (int) ($row->clicks ?? 0));
|
||||||
|
$resultsCount = round((float) ($row->avg_results_count ?? 0), 1);
|
||||||
|
$ctr = $searches > 0 ? round(($clicks / $searches) * 100, 1) : 0.0;
|
||||||
|
$signal = $this->classifySearchGap(
|
||||||
|
searches: $searches,
|
||||||
|
resultsCount: $resultsCount,
|
||||||
|
clicks: $clicks,
|
||||||
|
ctr: $ctr,
|
||||||
|
loggedInSearches: max(0, (int) ($row->logged_in_searches ?? 0)),
|
||||||
|
subscriberSearches: max(0, (int) ($row->subscriber_searches ?? 0)),
|
||||||
|
);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'query' => (string) ($row->query ?: $row->normalized_query),
|
||||||
|
'normalized_query' => (string) $row->normalized_query,
|
||||||
|
'searches' => $searches,
|
||||||
|
'results_count' => $resultsCount,
|
||||||
|
'clicks' => $clicks,
|
||||||
|
'ctr' => $ctr,
|
||||||
|
'last_searched_at' => $row->last_searched_at ? Carbon::parse((string) $row->last_searched_at)->toDateTimeString() : null,
|
||||||
|
'logged_in_searches' => max(0, (int) ($row->logged_in_searches ?? 0)),
|
||||||
|
'subscriber_searches' => max(0, (int) ($row->subscriber_searches ?? 0)),
|
||||||
|
'issue' => $signal['issue'],
|
||||||
|
'priority' => $signal['priority'],
|
||||||
|
'priority_score' => $signal['priority_score'],
|
||||||
|
'suggested_action' => $signal['suggested_action'],
|
||||||
|
];
|
||||||
|
})->filter(fn (array $row): bool => $row['issue'] !== null)->values();
|
||||||
|
|
||||||
|
$zeroResultSearches = $rows
|
||||||
|
->filter(fn (array $row): bool => $row['issue'] === 'Zero-result demand')
|
||||||
|
->sortByDesc('searches')
|
||||||
|
->take($limit)
|
||||||
|
->values();
|
||||||
|
$searchesWithResultsNoClicks = $rows
|
||||||
|
->filter(fn (array $row): bool => $row['issue'] === 'Results with no clicks')
|
||||||
|
->sortByDesc('searches')
|
||||||
|
->take($limit)
|
||||||
|
->values();
|
||||||
|
$lowCtrSearches = $rows
|
||||||
|
->filter(fn (array $row): bool => $row['issue'] === 'Low click-through rate')
|
||||||
|
->sortBy('ctr')
|
||||||
|
->take($limit)
|
||||||
|
->values();
|
||||||
|
$highCtrSearches = $rows
|
||||||
|
->filter(fn (array $row): bool => $row['issue'] === 'High click-through topic')
|
||||||
|
->sortByDesc('ctr')
|
||||||
|
->take($limit)
|
||||||
|
->values();
|
||||||
|
$repeatedQueries = $rows
|
||||||
|
->filter(fn (array $row): bool => $row['logged_in_searches'] >= 2 || $row['subscriber_searches'] >= 2)
|
||||||
|
->sortByDesc('searches')
|
||||||
|
->take($limit)
|
||||||
|
->values();
|
||||||
|
|
||||||
|
$dedupedRows = $this->dedupeByKey(
|
||||||
|
collect([$zeroResultSearches, $searchesWithResultsNoClicks, $lowCtrSearches, $highCtrSearches])
|
||||||
|
->flatten(1)
|
||||||
|
->sortByDesc('priority_score')
|
||||||
|
->sortByDesc('searches')
|
||||||
|
->values(),
|
||||||
|
'normalized_query',
|
||||||
|
)->take($limit)->values();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'summary' => [
|
||||||
|
'gap_count' => $dedupedRows->count(),
|
||||||
|
'zero_result_count' => $zeroResultSearches->count(),
|
||||||
|
'no_click_count' => $searchesWithResultsNoClicks->count(),
|
||||||
|
'low_ctr_count' => $lowCtrSearches->count(),
|
||||||
|
'high_ctr_count' => $highCtrSearches->count(),
|
||||||
|
'repeated_member_count' => $repeatedQueries->count(),
|
||||||
|
],
|
||||||
|
'rows' => $dedupedRows->all(),
|
||||||
|
'zero_result_searches' => $zeroResultSearches->all(),
|
||||||
|
'searches_with_results_no_clicks' => $searchesWithResultsNoClicks->all(),
|
||||||
|
'low_ctr_searches' => $lowCtrSearches->all(),
|
||||||
|
'high_ctr_searches' => $highCtrSearches->all(),
|
||||||
|
'repeated_queries' => $repeatedQueries->all(),
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $filters
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function getPromptInsights(array $filters = []): array
|
||||||
|
{
|
||||||
|
return $this->remember('prompt-insights', $filters, function (Carbon $from, Carbon $to, int $limit): array {
|
||||||
|
$rows = $this->contentMetrics($from, $to, AcademyAnalyticsContentType::PROMPT)->map(function (array $row): ?array {
|
||||||
|
$signal = $this->classifyPromptInsight($row);
|
||||||
|
|
||||||
|
if ($signal === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_merge($row, $signal);
|
||||||
|
})->filter()->sortByDesc('priority_score')->take($limit)->values();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'summary' => [
|
||||||
|
'signal_count' => $rows->count(),
|
||||||
|
'high_view_low_copy' => $rows->where('issue', 'High views, low copies')->count(),
|
||||||
|
'low_view_high_copy_rate' => $rows->where('issue', 'Low views, high copy rate')->count(),
|
||||||
|
'high_save_low_copy' => $rows->where('issue', 'High saves, low copies')->count(),
|
||||||
|
'high_copy_low_like' => $rows->where('issue', 'High copies, low likes')->count(),
|
||||||
|
'high_upgrade_interest' => $rows->where('issue', 'High upgrade interest')->count(),
|
||||||
|
],
|
||||||
|
'rows' => $rows->all(),
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $filters
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function getLessonDropoffs(array $filters = []): array
|
||||||
|
{
|
||||||
|
return $this->remember('lesson-dropoffs', $filters, function (Carbon $from, Carbon $to, int $limit): array {
|
||||||
|
$rows = $this->contentMetrics($from, $to, AcademyAnalyticsContentType::LESSON)->map(function (array $row): ?array {
|
||||||
|
$signal = $this->classifyLessonDropoff($row);
|
||||||
|
|
||||||
|
if ($signal === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_merge($row, $signal);
|
||||||
|
})->filter()->sortByDesc('priority_score')->take($limit)->values();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'summary' => [
|
||||||
|
'signal_count' => $rows->count(),
|
||||||
|
'low_start_rate' => $rows->where('issue', 'High views, low starts')->count(),
|
||||||
|
'low_completion_rate' => $rows->where('issue', 'High starts, low completions')->count(),
|
||||||
|
'underpromoted_winners' => $rows->where('issue', 'High completions, low views')->count(),
|
||||||
|
'upgrade_interest' => $rows->where('issue', 'Upgrade interest')->count(),
|
||||||
|
],
|
||||||
|
'rows' => $rows->all(),
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $filters
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function getCourseHealth(array $filters = []): array
|
||||||
|
{
|
||||||
|
return $this->remember('course-health', $filters, function (Carbon $from, Carbon $to, int $limit): array {
|
||||||
|
$progress = AcademyUserProgress::query()
|
||||||
|
->selectRaw('course_id, avg(progress_percent) as avg_progress_percent, count(*) as learners')
|
||||||
|
->whereNotNull('course_id')
|
||||||
|
->whereNull('lesson_id')
|
||||||
|
->whereBetween('updated_at', [$from, $to])
|
||||||
|
->groupBy('course_id')
|
||||||
|
->get()
|
||||||
|
->keyBy(fn ($row): int => (int) $row->course_id);
|
||||||
|
|
||||||
|
$rows = $this->contentMetrics($from, $to, AcademyAnalyticsContentType::COURSE)->map(function (array $row) use ($progress): ?array {
|
||||||
|
$courseProgress = $progress->get((int) $row['content_id']);
|
||||||
|
$row['avg_progress'] = $courseProgress ? round((float) ($courseProgress->avg_progress_percent ?? 0), 1) : 0.0;
|
||||||
|
$row['learners'] = $courseProgress ? (int) ($courseProgress->learners ?? 0) : 0;
|
||||||
|
$signal = $this->classifyCourseHealth($row);
|
||||||
|
|
||||||
|
if ($signal === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_merge($row, $signal);
|
||||||
|
})->filter()->sortByDesc('priority_score')->take($limit)->values();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'summary' => [
|
||||||
|
'signal_count' => $rows->count(),
|
||||||
|
'low_start_rate' => $rows->where('issue', 'Low course start rate')->count(),
|
||||||
|
'low_completion_rate' => $rows->where('issue', 'Low course completion rate')->count(),
|
||||||
|
'expandable_courses' => $rows->where('issue', 'Expansion candidate')->count(),
|
||||||
|
'upgrade_interest' => $rows->where('issue', 'Premium follow-up opportunity')->count(),
|
||||||
|
],
|
||||||
|
'rows' => $rows->all(),
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $filters
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function getPremiumInterest(array $filters = []): array
|
||||||
|
{
|
||||||
|
return $this->remember('premium-interest', $filters, function (Carbon $from, Carbon $to, int $limit): array {
|
||||||
|
$rows = collect([
|
||||||
|
...$this->contentMetrics($from, $to, AcademyAnalyticsContentType::PROMPT)->all(),
|
||||||
|
...$this->contentMetrics($from, $to, AcademyAnalyticsContentType::LESSON)->all(),
|
||||||
|
...$this->contentMetrics($from, $to, AcademyAnalyticsContentType::COURSE)->all(),
|
||||||
|
...$this->contentMetrics($from, $to, AcademyAnalyticsContentType::PROMPT_PACK)->all(),
|
||||||
|
...$this->contentMetrics($from, $to, AcademyAnalyticsContentType::CHALLENGE)->all(),
|
||||||
|
])->map(function (array $row): ?array {
|
||||||
|
$signal = $this->classifyPremiumInterest($row);
|
||||||
|
|
||||||
|
if ($signal === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_merge($row, $signal, [
|
||||||
|
'premium_interest_score' => round(((float) $row['premium_preview_views'] * 2) + ((float) $row['upgrade_clicks'] * 10), 1),
|
||||||
|
]);
|
||||||
|
})->filter()->sortByDesc('priority_score')->sortByDesc('premium_interest_score')->take($limit)->values();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'summary' => [
|
||||||
|
'signal_count' => $rows->count(),
|
||||||
|
'strong_candidates' => $rows->where('issue', 'Strong premium candidate')->count(),
|
||||||
|
'weak_teasers' => $rows->where('issue', 'Weak premium teaser')->count(),
|
||||||
|
],
|
||||||
|
'rows' => $rows->all(),
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $filters
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function getEditorialRecommendations(array $filters = []): array
|
||||||
|
{
|
||||||
|
return $this->remember('editorial-recommendations', $filters, function (Carbon $from, Carbon $to, int $limit): array {
|
||||||
|
$searchGaps = $this->getSearchGaps(['from' => $from, 'to' => $to, 'limit' => $limit]);
|
||||||
|
$promptInsights = $this->getPromptInsights(['from' => $from, 'to' => $to, 'limit' => $limit]);
|
||||||
|
$lessonDropoffs = $this->getLessonDropoffs(['from' => $from, 'to' => $to, 'limit' => $limit]);
|
||||||
|
$courseHealth = $this->getCourseHealth(['from' => $from, 'to' => $to, 'limit' => $limit]);
|
||||||
|
$premiumInterest = $this->getPremiumInterest(['from' => $from, 'to' => $to, 'limit' => $limit]);
|
||||||
|
|
||||||
|
$recommendations = collect();
|
||||||
|
|
||||||
|
foreach (array_slice($searchGaps['zero_result_searches'], 0, 5) as $row) {
|
||||||
|
$recommendations->push([
|
||||||
|
'title' => sprintf('Create content for "%s"', $row['query']),
|
||||||
|
'description' => sprintf('Users searched for "%s" %d times and saw %.1f results.', $row['query'], $row['searches'], $row['results_count']),
|
||||||
|
'reason' => 'Repeated zero-result searches indicate missing Academy content coverage.',
|
||||||
|
'priority' => $row['searches'] >= 3 ? 'high' : 'medium',
|
||||||
|
'priority_score' => $row['searches'] >= 3 ? 300 + $row['searches'] : 200 + $row['searches'],
|
||||||
|
'content_type' => null,
|
||||||
|
'content_id' => null,
|
||||||
|
'metric_snapshot' => [
|
||||||
|
'searches' => $row['searches'],
|
||||||
|
'results_count' => $row['results_count'],
|
||||||
|
'clicks' => $row['clicks'],
|
||||||
|
],
|
||||||
|
'suggested_action' => 'Create content for this topic',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (array_slice($promptInsights['rows'], 0, 4) as $row) {
|
||||||
|
$recommendations->push([
|
||||||
|
'title' => sprintf('Review prompt "%s"', $row['title']),
|
||||||
|
'description' => sprintf('%s with %d views, %d copies, and a %.1f%% copy rate.', $row['issue'], $row['views'], $row['prompt_copies'], $row['copy_rate']),
|
||||||
|
'reason' => 'Prompt performance suggests either discoverability or quality improvements are needed.',
|
||||||
|
'priority' => $row['priority'],
|
||||||
|
'priority_score' => 180 + (int) $row['priority_score'],
|
||||||
|
'content_type' => $row['content_type'],
|
||||||
|
'content_id' => $row['content_id'],
|
||||||
|
'metric_snapshot' => [
|
||||||
|
'views' => $row['views'],
|
||||||
|
'copies' => $row['prompt_copies'],
|
||||||
|
'copy_rate' => $row['copy_rate'],
|
||||||
|
'upgrade_clicks' => $row['upgrade_clicks'],
|
||||||
|
],
|
||||||
|
'suggested_action' => $row['suggested_action'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (array_slice($lessonDropoffs['rows'], 0, 4) as $row) {
|
||||||
|
$recommendations->push([
|
||||||
|
'title' => sprintf('Improve lesson "%s"', $row['title']),
|
||||||
|
'description' => sprintf('%s with %d starts and a %.1f%% completion rate.', $row['issue'], $row['starts'], $row['completion_rate']),
|
||||||
|
'reason' => 'Lesson funnel data shows where learners hesitate or drop off.',
|
||||||
|
'priority' => $row['priority'],
|
||||||
|
'priority_score' => 170 + (int) $row['priority_score'],
|
||||||
|
'content_type' => $row['content_type'],
|
||||||
|
'content_id' => $row['content_id'],
|
||||||
|
'metric_snapshot' => [
|
||||||
|
'views' => $row['views'],
|
||||||
|
'starts' => $row['starts'],
|
||||||
|
'completions' => $row['completions'],
|
||||||
|
'completion_rate' => $row['completion_rate'],
|
||||||
|
],
|
||||||
|
'suggested_action' => $row['suggested_action'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (array_slice($courseHealth['rows'], 0, 4) as $row) {
|
||||||
|
$recommendations->push([
|
||||||
|
'title' => sprintf('Review course "%s"', $row['title']),
|
||||||
|
'description' => sprintf('%s with a %.1f%% completion rate and %.1f%% average progress.', $row['issue'], $row['completion_rate'], $row['avg_progress']),
|
||||||
|
'reason' => 'Course progression data highlights where sequencing or positioning may be blocking learners.',
|
||||||
|
'priority' => $row['priority'],
|
||||||
|
'priority_score' => 160 + (int) $row['priority_score'],
|
||||||
|
'content_type' => $row['content_type'],
|
||||||
|
'content_id' => $row['content_id'],
|
||||||
|
'metric_snapshot' => [
|
||||||
|
'views' => $row['views'],
|
||||||
|
'starts' => $row['starts'],
|
||||||
|
'completions' => $row['completions'],
|
||||||
|
'avg_progress' => $row['avg_progress'],
|
||||||
|
],
|
||||||
|
'suggested_action' => $row['suggested_action'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (array_slice($premiumInterest['rows'], 0, 4) as $row) {
|
||||||
|
$recommendations->push([
|
||||||
|
'title' => sprintf('Use "%s" as a premium signal', $row['title']),
|
||||||
|
'description' => sprintf('%s with %d preview views and %d upgrade clicks.', $row['issue'], $row['premium_preview_views'], $row['upgrade_clicks']),
|
||||||
|
'reason' => 'Premium preview behavior shows which topics can sell subscriptions or need better teaser copy.',
|
||||||
|
'priority' => $row['priority'],
|
||||||
|
'priority_score' => 150 + (int) $row['priority_score'],
|
||||||
|
'content_type' => $row['content_type'],
|
||||||
|
'content_id' => $row['content_id'],
|
||||||
|
'metric_snapshot' => [
|
||||||
|
'premium_preview_views' => $row['premium_preview_views'],
|
||||||
|
'upgrade_clicks' => $row['upgrade_clicks'],
|
||||||
|
'upgrade_rate' => $row['upgrade_rate'],
|
||||||
|
],
|
||||||
|
'suggested_action' => $row['suggested_action'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$rows = $recommendations
|
||||||
|
->sortByDesc('priority_score')
|
||||||
|
->take($limit)
|
||||||
|
->values()
|
||||||
|
->map(function (array $row): array {
|
||||||
|
unset($row['priority_score']);
|
||||||
|
|
||||||
|
return $row;
|
||||||
|
});
|
||||||
|
|
||||||
|
return [
|
||||||
|
'summary' => [
|
||||||
|
'total' => $rows->count(),
|
||||||
|
'high_priority' => $rows->where('priority', 'high')->count(),
|
||||||
|
'medium_priority' => $rows->where('priority', 'medium')->count(),
|
||||||
|
'low_priority' => $rows->where('priority', 'low')->count(),
|
||||||
|
],
|
||||||
|
'rows' => $rows->all(),
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $filters
|
||||||
|
* @return array{0: Carbon, 1: Carbon, 2: int}
|
||||||
|
*/
|
||||||
|
private function resolveFilters(array $filters): array
|
||||||
|
{
|
||||||
|
$from = ($filters['from'] ?? null) instanceof Carbon
|
||||||
|
? $filters['from']->copy()->startOfDay()
|
||||||
|
: Carbon::parse((string) ($filters['from'] ?? now()->subDays(29)->toDateString()))->startOfDay();
|
||||||
|
$to = ($filters['to'] ?? null) instanceof Carbon
|
||||||
|
? $filters['to']->copy()->endOfDay()
|
||||||
|
: Carbon::parse((string) ($filters['to'] ?? now()->toDateString()))->endOfDay();
|
||||||
|
$limit = max(1, min(50, (int) ($filters['limit'] ?? 25)));
|
||||||
|
|
||||||
|
return [$from, $to, $limit];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $filters
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
private function remember(string $suffix, array $filters, callable $callback): array
|
||||||
|
{
|
||||||
|
[$from, $to, $limit] = $this->resolveFilters($filters);
|
||||||
|
|
||||||
|
return Cache::remember(
|
||||||
|
sprintf('academy_analytics_%s:%s:%s:%d', $suffix, $from->toDateString(), $to->toDateString(), $limit),
|
||||||
|
now()->addMinutes(10),
|
||||||
|
fn (): array => $callback($from, $to, $limit),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function searchQuery(Carbon $from, Carbon $to): Builder
|
||||||
|
{
|
||||||
|
return AcademySearchLog::query()
|
||||||
|
->whereBetween('created_at', [$from, $to])
|
||||||
|
->selectRaw('normalized_query, max(query) as query, count(*) as searches, avg(results_count) as avg_results_count, sum(case when clicked_content_id is not null then 1 else 0 end) as clicks, max(created_at) as last_searched_at, sum(case when is_logged_in = 1 then 1 else 0 end) as logged_in_searches, sum(case when is_subscriber = 1 then 1 else 0 end) as subscriber_searches')
|
||||||
|
->whereNotNull('normalized_query')
|
||||||
|
->groupBy('normalized_query');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Collection<int, array<string, int|float|string|null>>
|
||||||
|
*/
|
||||||
|
private function contentMetrics(Carbon $from, Carbon $to, string $contentType): Collection
|
||||||
|
{
|
||||||
|
return AcademyContentMetricDaily::query()
|
||||||
|
->whereBetween('date', [$from->toDateString(), $to->toDateString()])
|
||||||
|
->where('content_type', $contentType)
|
||||||
|
->selectRaw('content_type, content_id, sum(views) as views, sum(unique_visitors) as unique_visitors, sum(engaged_views) as engaged_views, sum(likes) as likes, sum(saves) as saves, sum(prompt_copies) as prompt_copies, sum(negative_prompt_copies) as negative_prompt_copies, sum(starts) as starts, sum(completions) as completions, sum(upgrade_clicks) as upgrade_clicks, sum(premium_preview_views) as premium_preview_views, sum(search_clicks) as search_clicks, sum(popularity_score) as popularity_score')
|
||||||
|
->groupBy('content_type', 'content_id')
|
||||||
|
->get()
|
||||||
|
->map(function ($row) use ($contentType): array {
|
||||||
|
$contentId = (int) $row->content_id;
|
||||||
|
$uniqueVisitors = max(0, (int) ($row->unique_visitors ?? 0));
|
||||||
|
$promptCopies = max(0, (int) ($row->prompt_copies ?? 0));
|
||||||
|
$likes = max(0, (int) ($row->likes ?? 0));
|
||||||
|
$saves = max(0, (int) ($row->saves ?? 0));
|
||||||
|
$starts = max(0, (int) ($row->starts ?? 0));
|
||||||
|
$completions = max(0, (int) ($row->completions ?? 0));
|
||||||
|
$searchClicks = max(0, (int) ($row->search_clicks ?? 0));
|
||||||
|
$premiumPreviewViews = max(0, (int) ($row->premium_preview_views ?? 0));
|
||||||
|
$upgradeClicks = max(0, (int) ($row->upgrade_clicks ?? 0));
|
||||||
|
|
||||||
|
return [
|
||||||
|
'content_type' => $contentType,
|
||||||
|
'content_id' => $contentId,
|
||||||
|
'content_type_label' => (string) Str::of(str_replace('academy_', '', $contentType))->replace('_', ' ')->headline(),
|
||||||
|
'title' => $this->resolver->title($contentType, $contentId),
|
||||||
|
'views' => max(0, (int) ($row->views ?? 0)),
|
||||||
|
'unique_visitors' => $uniqueVisitors,
|
||||||
|
'engaged_views' => max(0, (int) ($row->engaged_views ?? 0)),
|
||||||
|
'likes' => $likes,
|
||||||
|
'saves' => $saves,
|
||||||
|
'prompt_copies' => $promptCopies,
|
||||||
|
'negative_prompt_copies' => max(0, (int) ($row->negative_prompt_copies ?? 0)),
|
||||||
|
'starts' => $starts,
|
||||||
|
'completions' => $completions,
|
||||||
|
'search_clicks' => $searchClicks,
|
||||||
|
'premium_preview_views' => $premiumPreviewViews,
|
||||||
|
'upgrade_clicks' => $upgradeClicks,
|
||||||
|
'popularity_score' => round((float) ($row->popularity_score ?? 0), 2),
|
||||||
|
'copy_rate' => $uniqueVisitors > 0 ? round(($promptCopies / $uniqueVisitors) * 100, 1) : 0.0,
|
||||||
|
'save_rate' => $uniqueVisitors > 0 ? round(($saves / $uniqueVisitors) * 100, 1) : 0.0,
|
||||||
|
'like_rate' => $uniqueVisitors > 0 ? round(($likes / $uniqueVisitors) * 100, 1) : 0.0,
|
||||||
|
'search_click_rate' => $uniqueVisitors > 0 ? round(($searchClicks / $uniqueVisitors) * 100, 1) : 0.0,
|
||||||
|
'start_rate' => $uniqueVisitors > 0 ? round(($starts / $uniqueVisitors) * 100, 1) : 0.0,
|
||||||
|
'completion_rate' => $starts > 0 ? round(($completions / $starts) * 100, 1) : 0.0,
|
||||||
|
'engagement_rate' => $uniqueVisitors > 0 ? round((((int) ($row->engaged_views ?? 0)) / $uniqueVisitors) * 100, 1) : 0.0,
|
||||||
|
'upgrade_rate' => $premiumPreviewViews > 0 ? round(($upgradeClicks / $premiumPreviewViews) * 100, 1) : 0.0,
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{issue: string|null, priority: string, priority_score: int, suggested_action: string}
|
||||||
|
*/
|
||||||
|
private function classifySearchGap(int $searches, float $resultsCount, int $clicks, float $ctr, int $loggedInSearches, int $subscriberSearches): array
|
||||||
|
{
|
||||||
|
if ($resultsCount <= 0.4) {
|
||||||
|
return [
|
||||||
|
'issue' => 'Zero-result demand',
|
||||||
|
'priority' => $searches >= 3 || $subscriberSearches >= 2 ? 'high' : 'medium',
|
||||||
|
'priority_score' => 300 + $searches,
|
||||||
|
'suggested_action' => 'Create content for this topic',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($resultsCount > 0 && $clicks === 0) {
|
||||||
|
return [
|
||||||
|
'issue' => 'Results with no clicks',
|
||||||
|
'priority' => $searches >= 3 || $loggedInSearches >= 2 ? 'high' : 'medium',
|
||||||
|
'priority_score' => 240 + $searches,
|
||||||
|
'suggested_action' => 'Improve titles, excerpts, thumbnails, or relevance',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($searches >= 2 && $ctr < 10) {
|
||||||
|
return [
|
||||||
|
'issue' => 'Low click-through rate',
|
||||||
|
'priority' => 'medium',
|
||||||
|
'priority_score' => 180 + $searches,
|
||||||
|
'suggested_action' => 'Improve matching content or create better content',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($searches >= 2 && $ctr >= 40) {
|
||||||
|
return [
|
||||||
|
'issue' => 'High click-through topic',
|
||||||
|
'priority' => 'medium',
|
||||||
|
'priority_score' => 140 + $searches,
|
||||||
|
'suggested_action' => 'Consider expanding this topic',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'issue' => null,
|
||||||
|
'priority' => 'low',
|
||||||
|
'priority_score' => 0,
|
||||||
|
'suggested_action' => 'Monitor search intent',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, int|float|string|null> $row
|
||||||
|
* @return array<string, int|string>|null
|
||||||
|
*/
|
||||||
|
private function classifyPromptInsight(array $row): ?array
|
||||||
|
{
|
||||||
|
if ((int) $row['upgrade_clicks'] >= 3 || ((float) $row['upgrade_rate'] >= 15 && (int) $row['premium_preview_views'] >= 5)) {
|
||||||
|
return [
|
||||||
|
'issue' => 'High upgrade interest',
|
||||||
|
'priority' => 'high',
|
||||||
|
'priority_score' => 300 + (int) $row['upgrade_clicks'],
|
||||||
|
'suggested_action' => 'Create premium pack or advanced lesson around this topic',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((int) $row['views'] >= 120 && (float) $row['copy_rate'] < 8) {
|
||||||
|
return [
|
||||||
|
'issue' => 'High views, low copies',
|
||||||
|
'priority' => 'medium',
|
||||||
|
'priority_score' => 230 + (int) $row['views'],
|
||||||
|
'suggested_action' => 'Improve prompt quality, preview image, title, or negative prompt',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((int) $row['views'] <= 30 && (int) $row['prompt_copies'] >= 3 && (float) $row['copy_rate'] >= 35) {
|
||||||
|
return [
|
||||||
|
'issue' => 'Low views, high copy rate',
|
||||||
|
'priority' => 'medium',
|
||||||
|
'priority_score' => 210 + (int) $row['prompt_copies'],
|
||||||
|
'suggested_action' => 'Feature this prompt, improve SEO, add to related content',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((int) $row['saves'] >= 5 && (int) $row['prompt_copies'] < (int) $row['saves']) {
|
||||||
|
return [
|
||||||
|
'issue' => 'High saves, low copies',
|
||||||
|
'priority' => 'medium',
|
||||||
|
'priority_score' => 190 + (int) $row['saves'],
|
||||||
|
'suggested_action' => 'Add examples, variations, or usage notes',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((int) $row['prompt_copies'] >= 8 && (float) $row['like_rate'] < 5) {
|
||||||
|
return [
|
||||||
|
'issue' => 'High copies, low likes',
|
||||||
|
'priority' => 'low',
|
||||||
|
'priority_score' => 160 + (int) $row['prompt_copies'],
|
||||||
|
'suggested_action' => 'Improve like/save UI visibility or ask for feedback',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, int|float|string|null> $row
|
||||||
|
* @return array<string, int|string>|null
|
||||||
|
*/
|
||||||
|
private function classifyLessonDropoff(array $row): ?array
|
||||||
|
{
|
||||||
|
if ((int) $row['starts'] >= 12 && (float) $row['completion_rate'] < 35) {
|
||||||
|
return [
|
||||||
|
'issue' => 'High starts, low completions',
|
||||||
|
'priority' => 'high',
|
||||||
|
'priority_score' => 300 + (int) $row['starts'],
|
||||||
|
'suggested_action' => 'Lesson may be too long, confusing, or missing examples',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((int) $row['views'] >= 80 && (float) $row['start_rate'] < 18) {
|
||||||
|
return [
|
||||||
|
'issue' => 'High views, low starts',
|
||||||
|
'priority' => 'medium',
|
||||||
|
'priority_score' => 230 + (int) $row['views'],
|
||||||
|
'suggested_action' => 'Improve lesson intro, title, excerpt, or call-to-action',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((int) $row['completions'] >= 8 && (int) $row['views'] <= 35) {
|
||||||
|
return [
|
||||||
|
'issue' => 'High completions, low views',
|
||||||
|
'priority' => 'medium',
|
||||||
|
'priority_score' => 200 + (int) $row['completions'],
|
||||||
|
'suggested_action' => 'Promote this lesson more',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((int) $row['upgrade_clicks'] >= 3 || ((float) $row['upgrade_rate'] >= 12 && (int) $row['premium_preview_views'] >= 5)) {
|
||||||
|
return [
|
||||||
|
'issue' => 'Upgrade interest',
|
||||||
|
'priority' => 'medium',
|
||||||
|
'priority_score' => 180 + (int) $row['upgrade_clicks'],
|
||||||
|
'suggested_action' => 'This lesson may be useful as a subscription conversion entry point',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, int|float|string|null> $row
|
||||||
|
* @return array<string, int|string>|null
|
||||||
|
*/
|
||||||
|
private function classifyCourseHealth(array $row): ?array
|
||||||
|
{
|
||||||
|
if ((int) $row['starts'] >= 10 && (float) $row['completion_rate'] < 35) {
|
||||||
|
return [
|
||||||
|
'issue' => 'Low course completion rate',
|
||||||
|
'priority' => 'high',
|
||||||
|
'priority_score' => 300 + (int) $row['starts'],
|
||||||
|
'suggested_action' => 'Add shorter lessons, move the strongest lesson earlier, or improve examples',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((int) $row['views'] >= 60 && (float) $row['start_rate'] < 18) {
|
||||||
|
return [
|
||||||
|
'issue' => 'Low course start rate',
|
||||||
|
'priority' => 'medium',
|
||||||
|
'priority_score' => 220 + (int) $row['views'],
|
||||||
|
'suggested_action' => 'Improve course landing page, cover image, or course positioning',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((int) $row['upgrade_clicks'] >= 3) {
|
||||||
|
return [
|
||||||
|
'issue' => 'Premium follow-up opportunity',
|
||||||
|
'priority' => 'medium',
|
||||||
|
'priority_score' => 190 + (int) $row['upgrade_clicks'],
|
||||||
|
'suggested_action' => 'Add a premium follow-up course around this topic',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((int) $row['completions'] >= 8 && (float) $row['completion_rate'] >= 65) {
|
||||||
|
return [
|
||||||
|
'issue' => 'Expansion candidate',
|
||||||
|
'priority' => 'medium',
|
||||||
|
'priority_score' => 170 + (int) $row['completions'],
|
||||||
|
'suggested_action' => 'Expand this course with advanced follow-up material',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, int|float|string|null> $row
|
||||||
|
* @return array<string, int|string>|null
|
||||||
|
*/
|
||||||
|
private function classifyPremiumInterest(array $row): ?array
|
||||||
|
{
|
||||||
|
if ((int) $row['upgrade_clicks'] >= 3) {
|
||||||
|
return [
|
||||||
|
'issue' => 'Strong premium candidate',
|
||||||
|
'priority' => 'high',
|
||||||
|
'priority_score' => 300 + (int) $row['upgrade_clicks'],
|
||||||
|
'suggested_action' => 'Create advanced premium content around this topic',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((int) $row['premium_preview_views'] >= 15 && (int) $row['upgrade_clicks'] <= 1) {
|
||||||
|
return [
|
||||||
|
'issue' => 'Weak premium teaser',
|
||||||
|
'priority' => 'medium',
|
||||||
|
'priority_score' => 190 + (int) $row['premium_preview_views'],
|
||||||
|
'suggested_action' => 'Improve teaser copy, preview images, or value proposition',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Collection<int, array<string, mixed>> $rows
|
||||||
|
* @return Collection<int, array<string, mixed>>
|
||||||
|
*/
|
||||||
|
private function dedupeByKey(Collection $rows, string $key): Collection
|
||||||
|
{
|
||||||
|
$seen = [];
|
||||||
|
|
||||||
|
return $rows->filter(function (array $row) use (&$seen, $key): bool {
|
||||||
|
$value = (string) ($row[$key] ?? '');
|
||||||
|
|
||||||
|
if ($value === '' || isset($seen[$value])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$seen[$value] = true;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
171
app/Services/Academy/AcademyInteractionService.php
Normal file
171
app/Services/Academy/AcademyInteractionService.php
Normal file
@@ -0,0 +1,171 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Services\Academy;
|
||||||
|
|
||||||
|
use App\Models\AcademyLike;
|
||||||
|
use App\Models\AcademyPromptTemplate;
|
||||||
|
use App\Models\AcademySave;
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsEventType;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
|
final class AcademyInteractionService
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly AcademyAnalyticsContentResolver $contentResolver,
|
||||||
|
private readonly AcademyAnalyticsService $analytics,
|
||||||
|
private readonly AcademyProgressService $progress,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, int|bool>
|
||||||
|
*/
|
||||||
|
public function toggleLike(User $user, string $contentType, int $contentId, ?Request $request = null): array
|
||||||
|
{
|
||||||
|
$this->assertSupportedContent($contentType, $contentId);
|
||||||
|
|
||||||
|
$existing = AcademyLike::query()
|
||||||
|
->where('user_id', $user->id)
|
||||||
|
->where('content_type', $contentType)
|
||||||
|
->where('content_id', $contentId)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if ($existing) {
|
||||||
|
$existing->delete();
|
||||||
|
$liked = false;
|
||||||
|
} else {
|
||||||
|
AcademyLike::query()->create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'content_type' => $contentType,
|
||||||
|
'content_id' => $contentId,
|
||||||
|
]);
|
||||||
|
$liked = true;
|
||||||
|
|
||||||
|
if ($contentType === AcademyAnalyticsContentType::PROMPT) {
|
||||||
|
$this->analytics->track([
|
||||||
|
'event_type' => AcademyAnalyticsEventType::PROMPT_LIKE,
|
||||||
|
'content_type' => $contentType,
|
||||||
|
'content_id' => $contentId,
|
||||||
|
], $user, $request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'liked' => $liked,
|
||||||
|
'likes_count' => $this->likesCount($contentType, $contentId),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, int|bool>
|
||||||
|
*/
|
||||||
|
public function toggleSave(User $user, string $contentType, int $contentId, ?Request $request = null): array
|
||||||
|
{
|
||||||
|
$content = $this->assertSupportedContent($contentType, $contentId);
|
||||||
|
|
||||||
|
if ($contentType === AcademyAnalyticsContentType::PROMPT && $content instanceof AcademyPromptTemplate) {
|
||||||
|
$existing = AcademySave::query()
|
||||||
|
->where('user_id', $user->id)
|
||||||
|
->where('content_type', $contentType)
|
||||||
|
->where('content_id', $contentId)
|
||||||
|
->exists();
|
||||||
|
|
||||||
|
if ($existing) {
|
||||||
|
$this->progress->unsavePrompt($user, $content);
|
||||||
|
$saved = false;
|
||||||
|
} else {
|
||||||
|
$this->progress->savePrompt($user, $content);
|
||||||
|
$saved = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'saved' => $saved,
|
||||||
|
'saves_count' => $this->savesCount($contentType, $contentId),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$existing = AcademySave::query()
|
||||||
|
->where('user_id', $user->id)
|
||||||
|
->where('content_type', $contentType)
|
||||||
|
->where('content_id', $contentId)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if ($existing) {
|
||||||
|
$existing->delete();
|
||||||
|
$saved = false;
|
||||||
|
} else {
|
||||||
|
AcademySave::query()->create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'content_type' => $contentType,
|
||||||
|
'content_id' => $contentId,
|
||||||
|
]);
|
||||||
|
$saved = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'saved' => $saved,
|
||||||
|
'saves_count' => $this->savesCount($contentType, $contentId),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, int|bool>
|
||||||
|
*/
|
||||||
|
public function getInteractionState(?User $user, string $contentType, int $contentId): array
|
||||||
|
{
|
||||||
|
$this->assertSupportedContent($contentType, $contentId);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'liked' => $user
|
||||||
|
? AcademyLike::query()->where('user_id', $user->id)->where('content_type', $contentType)->where('content_id', $contentId)->exists()
|
||||||
|
: false,
|
||||||
|
'saved' => $user
|
||||||
|
? AcademySave::query()->where('user_id', $user->id)->where('content_type', $contentType)->where('content_id', $contentId)->exists()
|
||||||
|
: false,
|
||||||
|
'likes_count' => $this->likesCount($contentType, $contentId),
|
||||||
|
'saves_count' => $this->savesCount($contentType, $contentId),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function likesCount(string $contentType, int $contentId): int
|
||||||
|
{
|
||||||
|
return AcademyLike::query()
|
||||||
|
->where('content_type', $contentType)
|
||||||
|
->where('content_id', $contentId)
|
||||||
|
->count();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function savesCount(string $contentType, int $contentId): int
|
||||||
|
{
|
||||||
|
return AcademySave::query()
|
||||||
|
->where('content_type', $contentType)
|
||||||
|
->where('content_id', $contentId)
|
||||||
|
->count();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function assertSupportedContent(string $contentType, int $contentId): mixed
|
||||||
|
{
|
||||||
|
if (! in_array($contentType, [
|
||||||
|
AcademyAnalyticsContentType::PROMPT,
|
||||||
|
AcademyAnalyticsContentType::LESSON,
|
||||||
|
AcademyAnalyticsContentType::COURSE,
|
||||||
|
AcademyAnalyticsContentType::PROMPT_PACK,
|
||||||
|
AcademyAnalyticsContentType::CHALLENGE,
|
||||||
|
], true)) {
|
||||||
|
throw new InvalidArgumentException('Unsupported Academy interaction content type.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$content = $this->contentResolver->resolve($contentType, $contentId);
|
||||||
|
|
||||||
|
if ($content === null) {
|
||||||
|
throw new InvalidArgumentException('Unknown Academy interaction content target.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
}
|
||||||
60
app/Services/Academy/AcademyPopularityService.php
Normal file
60
app/Services/Academy/AcademyPopularityService.php
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Services\Academy;
|
||||||
|
|
||||||
|
use App\Models\AcademyContentMetricDaily;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
|
final class AcademyPopularityService
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param array<string, int|float|null> $metrics
|
||||||
|
*/
|
||||||
|
public function calculatePopularityScore(array $metrics): float
|
||||||
|
{
|
||||||
|
return round(
|
||||||
|
((float) ($metrics['unique_visitors'] ?? 0) * 1)
|
||||||
|
+ ((float) ($metrics['engaged_views'] ?? 0) * 3)
|
||||||
|
+ ((float) ($metrics['likes'] ?? 0) * 5)
|
||||||
|
+ ((float) ($metrics['saves'] ?? 0) * 7)
|
||||||
|
+ ((float) ($metrics['prompt_copies'] ?? 0) * 8)
|
||||||
|
+ ((float) ($metrics['negative_prompt_copies'] ?? 0) * 4)
|
||||||
|
+ ((float) ($metrics['starts'] ?? 0) * 4)
|
||||||
|
+ ((float) ($metrics['completions'] ?? 0) * 10)
|
||||||
|
+ ((float) ($metrics['upgrade_clicks'] ?? 0) * 15)
|
||||||
|
+ ((float) ($metrics['premium_preview_views'] ?? 0) * 3)
|
||||||
|
- ((float) ($metrics['bounce_count'] ?? 0) * 2),
|
||||||
|
2,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, int|float|null> $metrics
|
||||||
|
*/
|
||||||
|
public function calculateConversionScore(array $metrics): float
|
||||||
|
{
|
||||||
|
$uniqueVisitors = max(1, (int) ($metrics['unique_visitors'] ?? 0));
|
||||||
|
|
||||||
|
return round((((float) ($metrics['upgrade_clicks'] ?? 0) * 100) / $uniqueVisitors), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function queryBetween(Carbon $from, Carbon $to): Builder
|
||||||
|
{
|
||||||
|
return AcademyContentMetricDaily::query()
|
||||||
|
->whereBetween('date', [$from->toDateString(), $to->toDateString()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function topContent(Carbon $from, Carbon $to, int $limit = 10): Collection
|
||||||
|
{
|
||||||
|
return $this->queryBetween($from, $to)
|
||||||
|
->selectRaw('content_type, content_id, sum(views) as views, sum(unique_visitors) as unique_visitors, sum(engaged_views) as engaged_views, sum(likes) as likes, sum(saves) as saves, sum(prompt_copies) as prompt_copies, sum(completions) as completions, sum(upgrade_clicks) as upgrade_clicks, sum(popularity_score) as popularity_score')
|
||||||
|
->groupBy('content_type', 'content_id')
|
||||||
|
->orderByDesc('popularity_score')
|
||||||
|
->limit($limit)
|
||||||
|
->get();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,17 +9,120 @@ use App\Models\AcademyLesson;
|
|||||||
use App\Models\AcademyLessonProgress;
|
use App\Models\AcademyLessonProgress;
|
||||||
use App\Models\AcademyPromptTemplate;
|
use App\Models\AcademyPromptTemplate;
|
||||||
use App\Models\AcademySavedPrompt;
|
use App\Models\AcademySavedPrompt;
|
||||||
|
use App\Models\AcademySave;
|
||||||
|
use App\Models\AcademyUserProgress;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsEventType;
|
||||||
|
use App\Support\AcademyAnalytics\AcademyAnalyticsProgressStatus;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
final class AcademyProgressService
|
final class AcademyProgressService
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly AcademyBadgeService $badges,
|
private readonly AcademyBadgeService $badges,
|
||||||
private readonly AcademyCourseProgressService $courses,
|
private readonly AcademyCourseProgressService $courses,
|
||||||
|
private readonly AcademyAnalyticsService $analytics,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function markLessonComplete(User $user, AcademyLesson $lesson, ?AcademyCourse $course = null): AcademyLessonProgress
|
public function startLesson(User $user, int $lessonId, ?int $courseId = null, ?Request $request = null): AcademyUserProgress
|
||||||
|
{
|
||||||
|
$progress = $this->updateUserProgressRecord($user, $courseId, $lessonId, [
|
||||||
|
'status' => AcademyAnalyticsProgressStatus::STARTED,
|
||||||
|
'progress_percent' => 0,
|
||||||
|
'started_at' => now(),
|
||||||
|
'completed_at' => null,
|
||||||
|
'last_seen_at' => now(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($courseId) {
|
||||||
|
$course = AcademyCourse::query()->find($courseId);
|
||||||
|
if ($course instanceof AcademyCourse) {
|
||||||
|
$this->courses->markEnrollmentStarted($user, $course);
|
||||||
|
$lesson = AcademyLesson::query()->find($lessonId);
|
||||||
|
if ($lesson instanceof AcademyLesson) {
|
||||||
|
$this->courses->updateLastLesson($user, $course, $lesson);
|
||||||
|
}
|
||||||
|
$this->syncCourseProgressRecord($user, $course);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->analytics->track([
|
||||||
|
'event_type' => AcademyAnalyticsEventType::LESSON_STARTED,
|
||||||
|
'content_type' => AcademyAnalyticsContentType::LESSON,
|
||||||
|
'content_id' => $lessonId,
|
||||||
|
'metadata' => array_filter([
|
||||||
|
'course_id' => $courseId,
|
||||||
|
], static fn (mixed $value): bool => $value !== null),
|
||||||
|
], $user, $request);
|
||||||
|
|
||||||
|
return $progress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function completeLesson(User $user, int $lessonId, ?int $courseId = null, ?Request $request = null): AcademyUserProgress
|
||||||
|
{
|
||||||
|
$progress = $this->updateUserProgressRecord($user, $courseId, $lessonId, [
|
||||||
|
'status' => AcademyAnalyticsProgressStatus::COMPLETED,
|
||||||
|
'progress_percent' => 100,
|
||||||
|
'started_at' => now(),
|
||||||
|
'completed_at' => now(),
|
||||||
|
'last_seen_at' => now(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($courseId) {
|
||||||
|
$course = AcademyCourse::query()->find($courseId);
|
||||||
|
if ($course instanceof AcademyCourse) {
|
||||||
|
$this->syncCourseProgressRecord($user, $course);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->analytics->track([
|
||||||
|
'event_type' => AcademyAnalyticsEventType::LESSON_COMPLETED,
|
||||||
|
'content_type' => AcademyAnalyticsContentType::LESSON,
|
||||||
|
'content_id' => $lessonId,
|
||||||
|
'metadata' => array_filter([
|
||||||
|
'course_id' => $courseId,
|
||||||
|
], static fn (mixed $value): bool => $value !== null),
|
||||||
|
], $user, $request);
|
||||||
|
|
||||||
|
return $progress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function startCourse(User $user, int $courseId, ?Request $request = null): AcademyUserProgress
|
||||||
|
{
|
||||||
|
$course = AcademyCourse::query()->findOrFail($courseId);
|
||||||
|
$this->courses->markEnrollmentStarted($user, $course);
|
||||||
|
|
||||||
|
$progress = $this->syncCourseProgressRecord($user, $course, true);
|
||||||
|
|
||||||
|
$this->analytics->track([
|
||||||
|
'event_type' => AcademyAnalyticsEventType::COURSE_STARTED,
|
||||||
|
'content_type' => AcademyAnalyticsContentType::COURSE,
|
||||||
|
'content_id' => $courseId,
|
||||||
|
], $user, $request);
|
||||||
|
|
||||||
|
return $progress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function completeCourse(User $user, int $courseId, ?Request $request = null): AcademyUserProgress
|
||||||
|
{
|
||||||
|
$course = AcademyCourse::query()->findOrFail($courseId);
|
||||||
|
$this->courses->markCourseCompletedIfFinished($user, $course);
|
||||||
|
$progress = $this->syncCourseProgressRecord($user, $course);
|
||||||
|
|
||||||
|
if ($progress->status === AcademyAnalyticsProgressStatus::COMPLETED) {
|
||||||
|
$this->analytics->track([
|
||||||
|
'event_type' => AcademyAnalyticsEventType::COURSE_COMPLETED,
|
||||||
|
'content_type' => AcademyAnalyticsContentType::COURSE,
|
||||||
|
'content_id' => $courseId,
|
||||||
|
], $user, $request);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $progress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function markLessonComplete(User $user, AcademyLesson $lesson, ?AcademyCourse $course = null, ?Request $request = null): AcademyLessonProgress
|
||||||
{
|
{
|
||||||
$progress = AcademyLessonProgress::query()->updateOrCreate(
|
$progress = AcademyLessonProgress::query()->updateOrCreate(
|
||||||
[
|
[
|
||||||
@@ -36,6 +139,8 @@ final class AcademyProgressService
|
|||||||
$this->courses->markCourseCompletedIfFinished($user, $course);
|
$this->courses->markCourseCompletedIfFinished($user, $course);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->completeLesson($user, (int) $lesson->id, $course?->id, $request);
|
||||||
|
|
||||||
$this->badges->syncForUser($user);
|
$this->badges->syncForUser($user);
|
||||||
|
|
||||||
return $progress;
|
return $progress;
|
||||||
@@ -48,6 +153,18 @@ final class AcademyProgressService
|
|||||||
'user_id' => $user->id,
|
'user_id' => $user->id,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
AcademySave::query()->firstOrCreate([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'content_type' => AcademyAnalyticsContentType::PROMPT,
|
||||||
|
'content_id' => $prompt->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->analytics->track([
|
||||||
|
'event_type' => AcademyAnalyticsEventType::PROMPT_SAVE,
|
||||||
|
'content_type' => AcademyAnalyticsContentType::PROMPT,
|
||||||
|
'content_id' => (int) $prompt->id,
|
||||||
|
], $user);
|
||||||
|
|
||||||
$this->badges->syncForUser($user);
|
$this->badges->syncForUser($user);
|
||||||
|
|
||||||
return $saved;
|
return $saved;
|
||||||
@@ -60,6 +177,50 @@ final class AcademyProgressService
|
|||||||
->where('user_id', $user->id)
|
->where('user_id', $user->id)
|
||||||
->delete();
|
->delete();
|
||||||
|
|
||||||
|
AcademySave::query()
|
||||||
|
->where('user_id', $user->id)
|
||||||
|
->where('content_type', AcademyAnalyticsContentType::PROMPT)
|
||||||
|
->where('content_id', $prompt->id)
|
||||||
|
->delete();
|
||||||
|
|
||||||
$this->badges->syncForUser($user);
|
$this->badges->syncForUser($user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $attributes
|
||||||
|
*/
|
||||||
|
private function updateUserProgressRecord(User $user, ?int $courseId, ?int $lessonId, array $attributes): AcademyUserProgress
|
||||||
|
{
|
||||||
|
return AcademyUserProgress::query()->updateOrCreate(
|
||||||
|
[
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'course_id' => $courseId,
|
||||||
|
'lesson_id' => $lessonId,
|
||||||
|
],
|
||||||
|
$attributes,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function syncCourseProgressRecord(User $user, AcademyCourse $course, bool $forceStarted = false): AcademyUserProgress
|
||||||
|
{
|
||||||
|
$progressPercent = $this->courses->getProgressPercent($user, $course);
|
||||||
|
$isComplete = $this->courses->getTotalRequiredLessonsCount($course) > 0 && $progressPercent >= 100;
|
||||||
|
$status = $isComplete
|
||||||
|
? AcademyAnalyticsProgressStatus::COMPLETED
|
||||||
|
: ($progressPercent > 0 || $forceStarted
|
||||||
|
? AcademyAnalyticsProgressStatus::IN_PROGRESS
|
||||||
|
: AcademyAnalyticsProgressStatus::STARTED);
|
||||||
|
|
||||||
|
return $this->updateUserProgressRecord($user, (int) $course->id, null, [
|
||||||
|
'status' => $status,
|
||||||
|
'progress_percent' => $progressPercent,
|
||||||
|
'started_at' => now(),
|
||||||
|
'completed_at' => $isComplete ? now() : null,
|
||||||
|
'last_seen_at' => now(),
|
||||||
|
'metadata' => [
|
||||||
|
'completed_required' => $this->courses->getCompletedRequiredLessonsCount($user, $course),
|
||||||
|
'total_required' => $this->courses->getTotalRequiredLessonsCount($course),
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
298
app/Services/Academy/AcademyStripeWebhookAuditService.php
Normal file
298
app/Services/Academy/AcademyStripeWebhookAuditService.php
Normal file
@@ -0,0 +1,298 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Services\Academy;
|
||||||
|
|
||||||
|
use App\Models\AcademyBillingEvent;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Laravel\Cashier\Subscription;
|
||||||
|
|
||||||
|
final class AcademyStripeWebhookAuditService
|
||||||
|
{
|
||||||
|
private const TRACKED_EVENT_TYPES = [
|
||||||
|
'checkout.session.completed',
|
||||||
|
'customer.subscription.created',
|
||||||
|
'customer.subscription.updated',
|
||||||
|
'customer.subscription.deleted',
|
||||||
|
'invoice.payment_succeeded',
|
||||||
|
'invoice.payment_failed',
|
||||||
|
'invoice.payment_action_required',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private readonly AcademyBillingPlanService $plans,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $payload
|
||||||
|
*/
|
||||||
|
public function recordReceived(array $payload): void
|
||||||
|
{
|
||||||
|
$context = $this->buildContext($payload);
|
||||||
|
$tracked = in_array($context['event_type'], self::TRACKED_EVENT_TYPES, true);
|
||||||
|
$cacheKeys = [];
|
||||||
|
|
||||||
|
if ($tracked && $context['user'] instanceof User) {
|
||||||
|
$cacheKeys = [
|
||||||
|
'academy.billing.account.'.$context['user']->id,
|
||||||
|
'academy.billing.pricing.'.$context['user']->id,
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($cacheKeys as $cacheKey) {
|
||||||
|
Cache::forget($cacheKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$event = $this->persistEvent($context, [
|
||||||
|
'received' => true,
|
||||||
|
'received_at' => now()->toISOString(),
|
||||||
|
'tracked' => $tracked,
|
||||||
|
'action' => $tracked ? 'received_for_cashier_processing' : 'ignored_untracked_event',
|
||||||
|
'user_resolved' => $context['user'] instanceof User,
|
||||||
|
'cache_cleared' => $cacheKeys !== [],
|
||||||
|
'cache_keys' => $cacheKeys,
|
||||||
|
'status' => $context['object']['status'] ?? null,
|
||||||
|
'mode' => $context['object']['mode'] ?? null,
|
||||||
|
'amount_total' => $context['object']['amount_total'] ?? null,
|
||||||
|
'currency' => $context['object']['currency'] ?? null,
|
||||||
|
'price_ids' => $this->extractPriceIds($context['object']),
|
||||||
|
]);
|
||||||
|
|
||||||
|
Log::info('academy.stripe.webhook.received', [
|
||||||
|
'stripe_event_id' => $context['event_id'],
|
||||||
|
'event_type' => $context['event_type'],
|
||||||
|
'tracked' => $tracked,
|
||||||
|
'user_id' => $context['user']?->id,
|
||||||
|
'academy_plan' => $context['plan']['key'] ?? null,
|
||||||
|
'academy_tier' => $context['plan']['tier'] ?? null,
|
||||||
|
'audit_event_id' => $event->id,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $payload
|
||||||
|
*/
|
||||||
|
public function recordHandled(array $payload): void
|
||||||
|
{
|
||||||
|
$context = $this->buildContext($payload);
|
||||||
|
$localSubscription = $this->resolveLocalSubscription($context['subscription_id'], $context['user']);
|
||||||
|
|
||||||
|
$outcome = $localSubscription instanceof Subscription
|
||||||
|
? 'local_subscription_synced'
|
||||||
|
: 'handled_without_local_subscription_change';
|
||||||
|
|
||||||
|
$event = $this->persistEvent($context, [
|
||||||
|
'handled' => true,
|
||||||
|
'handled_at' => now()->toISOString(),
|
||||||
|
'outcome' => $outcome,
|
||||||
|
'local_subscription_found' => $localSubscription instanceof Subscription,
|
||||||
|
'local_subscription_status' => $localSubscription?->stripe_status,
|
||||||
|
'local_subscription_active' => $localSubscription?->active(),
|
||||||
|
'local_subscription_on_grace_period' => $localSubscription?->onGracePeriod(),
|
||||||
|
'local_price_ids' => $localSubscription instanceof Subscription
|
||||||
|
? $localSubscription->items->pluck('stripe_price')->filter()->values()->all()
|
||||||
|
: [],
|
||||||
|
]);
|
||||||
|
|
||||||
|
Log::info('academy.stripe.webhook.handled', [
|
||||||
|
'stripe_event_id' => $context['event_id'],
|
||||||
|
'event_type' => $context['event_type'],
|
||||||
|
'user_id' => $context['user']?->id,
|
||||||
|
'academy_plan' => $context['plan']['key'] ?? null,
|
||||||
|
'academy_tier' => $context['plan']['tier'] ?? null,
|
||||||
|
'outcome' => $outcome,
|
||||||
|
'audit_event_id' => $event->id,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $payload
|
||||||
|
* @return array{event_id:string,event_type:string,object:array<string,mixed>,customer_id:?string,subscription_id:?string,plan:?array<string,mixed>,user:?User}
|
||||||
|
*/
|
||||||
|
private function buildContext(array $payload): array
|
||||||
|
{
|
||||||
|
$eventType = trim((string) ($payload['type'] ?? ''));
|
||||||
|
$object = is_array($payload['data']['object'] ?? null)
|
||||||
|
? $payload['data']['object']
|
||||||
|
: [];
|
||||||
|
|
||||||
|
$customerId = $this->extractCustomerId($object);
|
||||||
|
$subscriptionId = $this->extractSubscriptionId($object);
|
||||||
|
$plan = $this->resolvePlan($object);
|
||||||
|
$user = $this->resolveUser($customerId, $subscriptionId, $object);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'event_id' => trim((string) ($payload['id'] ?? '')),
|
||||||
|
'event_type' => $eventType,
|
||||||
|
'object' => $object,
|
||||||
|
'customer_id' => $customerId,
|
||||||
|
'subscription_id' => $subscriptionId,
|
||||||
|
'plan' => $plan,
|
||||||
|
'user' => $user,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $context
|
||||||
|
* @param array<string, mixed> $summary
|
||||||
|
*/
|
||||||
|
private function persistEvent(array $context, array $summary): AcademyBillingEvent
|
||||||
|
{
|
||||||
|
$eventId = $context['event_id'];
|
||||||
|
|
||||||
|
$event = $eventId !== ''
|
||||||
|
? AcademyBillingEvent::query()->firstOrNew(['stripe_event_id' => $eventId])
|
||||||
|
: new AcademyBillingEvent();
|
||||||
|
|
||||||
|
$existingSummary = is_array($event->payload_summary) ? $event->payload_summary : [];
|
||||||
|
|
||||||
|
$event->fill([
|
||||||
|
'user_id' => $context['user']?->id,
|
||||||
|
'stripe_event_id' => $eventId !== '' ? $eventId : null,
|
||||||
|
'stripe_customer_id' => $context['customer_id'],
|
||||||
|
'stripe_subscription_id' => $context['subscription_id'],
|
||||||
|
'event_type' => $context['event_type'] !== '' ? $context['event_type'] : 'unknown',
|
||||||
|
'academy_tier' => $context['plan']['tier'] ?? null,
|
||||||
|
'academy_plan' => $context['plan']['key'] ?? null,
|
||||||
|
'payload_summary' => array_merge($existingSummary, $summary),
|
||||||
|
'processed_at' => now(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$event->save();
|
||||||
|
|
||||||
|
return $event;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $object
|
||||||
|
* @return array<string, mixed>|null
|
||||||
|
*/
|
||||||
|
private function resolvePlan(array $object): ?array
|
||||||
|
{
|
||||||
|
$metadataPlan = trim((string) Arr::get($object, 'metadata.academy_plan', ''));
|
||||||
|
|
||||||
|
if ($metadataPlan !== '') {
|
||||||
|
return $this->plans->plan($metadataPlan);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this->extractPriceIds($object) as $priceId) {
|
||||||
|
$plan = $this->plans->planForPriceId($priceId);
|
||||||
|
|
||||||
|
if ($plan !== null) {
|
||||||
|
return $plan;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $object
|
||||||
|
* @return list<string>
|
||||||
|
*/
|
||||||
|
private function extractPriceIds(array $object): array
|
||||||
|
{
|
||||||
|
$priceIds = [];
|
||||||
|
|
||||||
|
foreach ((array) Arr::get($object, 'items.data', []) as $item) {
|
||||||
|
if (! is_array($item)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$priceId = trim((string) Arr::get($item, 'price.id', ''));
|
||||||
|
|
||||||
|
if ($priceId !== '') {
|
||||||
|
$priceIds[] = $priceId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$lineItemPriceId = trim((string) Arr::get($object, 'display_items.0.price.id', ''));
|
||||||
|
|
||||||
|
if ($lineItemPriceId !== '') {
|
||||||
|
$priceIds[] = $lineItemPriceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_values(array_unique($priceIds));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $object
|
||||||
|
*/
|
||||||
|
private function extractCustomerId(array $object): ?string
|
||||||
|
{
|
||||||
|
$value = trim((string) ($object['customer'] ?? ''));
|
||||||
|
|
||||||
|
return $value !== '' ? $value : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $object
|
||||||
|
*/
|
||||||
|
private function extractSubscriptionId(array $object): ?string
|
||||||
|
{
|
||||||
|
$subscriptionId = trim((string) ($object['id'] ?? ''));
|
||||||
|
|
||||||
|
if (str_starts_with($subscriptionId, 'sub_')) {
|
||||||
|
return $subscriptionId;
|
||||||
|
}
|
||||||
|
|
||||||
|
$nested = trim((string) ($object['subscription'] ?? ''));
|
||||||
|
|
||||||
|
return $nested !== '' ? $nested : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $object
|
||||||
|
*/
|
||||||
|
private function resolveUser(?string $customerId, ?string $subscriptionId, array $object): ?User
|
||||||
|
{
|
||||||
|
$metadataUserId = (int) Arr::get($object, 'metadata.user_id', 0);
|
||||||
|
|
||||||
|
if ($metadataUserId > 0) {
|
||||||
|
return User::query()->find($metadataUserId);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($customerId !== null) {
|
||||||
|
$user = User::query()->where('stripe_id', $customerId)->first();
|
||||||
|
|
||||||
|
if ($user instanceof User) {
|
||||||
|
return $user;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($subscriptionId !== null) {
|
||||||
|
$subscription = Subscription::query()->where('stripe_id', $subscriptionId)->first();
|
||||||
|
|
||||||
|
if ($subscription !== null && $subscription->user instanceof User) {
|
||||||
|
return $subscription->user;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function resolveLocalSubscription(?string $subscriptionId, ?User $user): ?Subscription
|
||||||
|
{
|
||||||
|
if ($subscriptionId !== null) {
|
||||||
|
$subscription = Subscription::query()->where('stripe_id', $subscriptionId)->with('items')->first();
|
||||||
|
|
||||||
|
if ($subscription instanceof Subscription) {
|
||||||
|
return $subscription;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $user instanceof User) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$subscription = $user->subscription($this->plans->subscriptionName());
|
||||||
|
|
||||||
|
return $subscription instanceof Subscription
|
||||||
|
? $subscription->loadMissing('items')
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -48,7 +48,8 @@ class ArtworkService
|
|||||||
'user.profile:user_id,avatar_hash',
|
'user.profile:user_id,avatar_hash',
|
||||||
'awardStat:artwork_id,gold_count,silver_count,bronze_count,score_total,score_7d,score_30d,last_medaled_at,updated_at',
|
'awardStat:artwork_id,gold_count,silver_count,bronze_count,score_total,score_7d,score_30d,last_medaled_at,updated_at',
|
||||||
'categories' => function ($q) {
|
'categories' => function ($q) {
|
||||||
$q->select('categories.id', 'categories.content_type_id', 'categories.parent_id', 'categories.name', 'categories.slug', 'categories.sort_order');
|
$q->select('categories.id', 'categories.content_type_id', 'categories.parent_id', 'categories.name', 'categories.slug', 'categories.sort_order')
|
||||||
|
->with(['contentType:id,slug,name']);
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -136,6 +136,8 @@ final class NewsService
|
|||||||
$categoryId = (int) ($filters['category_id'] ?? 0);
|
$categoryId = (int) ($filters['category_id'] ?? 0);
|
||||||
$search = trim((string) ($filters['q'] ?? ''));
|
$search = trim((string) ($filters['q'] ?? ''));
|
||||||
$perPage = max(10, min(50, (int) ($filters['per_page'] ?? 15)));
|
$perPage = max(10, min(50, (int) ($filters['per_page'] ?? 15)));
|
||||||
|
$order = trim((string) ($filters['order'] ?? ''));
|
||||||
|
$direction = trim((string) ($filters['direction'] ?? ''));
|
||||||
|
|
||||||
if ($status !== '') {
|
if ($status !== '') {
|
||||||
$query->where('editorial_status', $status);
|
$query->where('editorial_status', $status);
|
||||||
@@ -158,6 +160,20 @@ final class NewsService
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($order !== '') {
|
||||||
|
$map = [
|
||||||
|
'date' => 'published_at',
|
||||||
|
'title' => 'title',
|
||||||
|
'views' => 'views',
|
||||||
|
];
|
||||||
|
|
||||||
|
if (array_key_exists($order, $map)) {
|
||||||
|
$dir = in_array(Str::lower($direction), ['asc', 'desc'], true) ? Str::lower($direction) : 'desc';
|
||||||
|
// Replace any existing ordering (editorialOrder) with the user-specified ordering.
|
||||||
|
$query->reorder($map[$order], $dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$paginator = $query->paginate($perPage)->withQueryString();
|
$paginator = $query->paginate($perPage)->withQueryString();
|
||||||
|
|
||||||
return [
|
return [
|
||||||
@@ -169,6 +185,8 @@ final class NewsService
|
|||||||
'type' => $type,
|
'type' => $type,
|
||||||
'category_id' => $categoryId > 0 ? $categoryId : '',
|
'category_id' => $categoryId > 0 ? $categoryId : '',
|
||||||
'per_page' => $perPage,
|
'per_page' => $perPage,
|
||||||
|
'order' => $order,
|
||||||
|
'direction' => in_array(Str::lower($direction), ['asc', 'desc'], true) ? Str::lower($direction) : '',
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -179,7 +197,7 @@ final class NewsService
|
|||||||
|
|
||||||
return [
|
return [
|
||||||
'id' => (int) $article->id,
|
'id' => (int) $article->id,
|
||||||
'title' => (string) $article->title,
|
'title' => $this->decodeLegacyHtml((string) $article->title),
|
||||||
'slug' => (string) $article->slug,
|
'slug' => (string) $article->slug,
|
||||||
'excerpt' => (string) ($article->excerpt ?? ''),
|
'excerpt' => (string) ($article->excerpt ?? ''),
|
||||||
'content' => (string) ($article->content ?? ''),
|
'content' => (string) ($article->content ?? ''),
|
||||||
@@ -421,6 +439,8 @@ final class NewsService
|
|||||||
$title = 'Untitled News Article';
|
$title = 'Untitled News Article';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$slug = $this->resolveSlug($title, $article, $data);
|
||||||
|
|
||||||
$previousCoverImage = trim((string) ($article->cover_image ?? ''));
|
$previousCoverImage = trim((string) ($article->cover_image ?? ''));
|
||||||
|
|
||||||
$editorialStatus = $this->normalizeEditorialStatus((string) ($data['editorial_status'] ?? $article->editorial_status ?? NewsArticle::EDITORIAL_STATUS_DRAFT));
|
$editorialStatus = $this->normalizeEditorialStatus((string) ($data['editorial_status'] ?? $article->editorial_status ?? NewsArticle::EDITORIAL_STATUS_DRAFT));
|
||||||
@@ -429,7 +449,7 @@ final class NewsService
|
|||||||
|
|
||||||
$article->fill([
|
$article->fill([
|
||||||
'title' => $title,
|
'title' => $title,
|
||||||
'slug' => $this->resolveSlug($title, $article, $data),
|
'slug' => $slug,
|
||||||
'excerpt' => $this->nullableText($data['excerpt'] ?? null),
|
'excerpt' => $this->nullableText($data['excerpt'] ?? null),
|
||||||
'content' => (string) ($data['content'] ?? ''),
|
'content' => (string) ($data['content'] ?? ''),
|
||||||
'cover_image' => $this->nullableText($data['cover_image'] ?? null),
|
'cover_image' => $this->nullableText($data['cover_image'] ?? null),
|
||||||
@@ -445,7 +465,7 @@ final class NewsService
|
|||||||
'meta_title' => $this->nullableText($data['meta_title'] ?? null),
|
'meta_title' => $this->nullableText($data['meta_title'] ?? null),
|
||||||
'meta_description' => $this->nullableText($data['meta_description'] ?? null),
|
'meta_description' => $this->nullableText($data['meta_description'] ?? null),
|
||||||
'meta_keywords' => $this->nullableText($data['meta_keywords'] ?? null),
|
'meta_keywords' => $this->nullableText($data['meta_keywords'] ?? null),
|
||||||
'canonical_url' => $this->nullableText($data['canonical_url'] ?? null),
|
'canonical_url' => route('news.show', ['slug' => $slug]),
|
||||||
'og_title' => $this->nullableText($data['og_title'] ?? null),
|
'og_title' => $this->nullableText($data['og_title'] ?? null),
|
||||||
'og_description' => $this->nullableText($data['og_description'] ?? null),
|
'og_description' => $this->nullableText($data['og_description'] ?? null),
|
||||||
'og_image' => $this->nullableText($data['og_image'] ?? null),
|
'og_image' => $this->nullableText($data['og_image'] ?? null),
|
||||||
@@ -472,7 +492,7 @@ final class NewsService
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'id' => (int) $article->id,
|
'id' => (int) $article->id,
|
||||||
'title' => (string) $article->title,
|
'title' => $this->decodeLegacyHtml((string) $article->title),
|
||||||
'slug' => (string) $article->slug,
|
'slug' => (string) $article->slug,
|
||||||
'type' => (string) ($article->type ?? NewsArticle::TYPE_ANNOUNCEMENT),
|
'type' => (string) ($article->type ?? NewsArticle::TYPE_ANNOUNCEMENT),
|
||||||
'type_label' => (string) $article->type_label,
|
'type_label' => (string) $article->type_label,
|
||||||
@@ -598,6 +618,23 @@ final class NewsService
|
|||||||
Storage::disk((string) config('uploads.object_storage.disk', 's3'))->delete($paths);
|
Storage::disk((string) config('uploads.object_storage.disk', 's3'))->delete($paths);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function decodeLegacyHtml(string $value): string
|
||||||
|
{
|
||||||
|
$decoded = $value;
|
||||||
|
|
||||||
|
for ($pass = 0; $pass < 5; $pass++) {
|
||||||
|
$next = html_entity_decode($decoded, ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
||||||
|
|
||||||
|
if ($next === $decoded) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$decoded = $next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return str_replace(['´', '´'], ["'", "'"], $decoded);
|
||||||
|
}
|
||||||
|
|
||||||
private function searchGroups(string $query, ?User $viewer): array
|
private function searchGroups(string $query, ?User $viewer): array
|
||||||
{
|
{
|
||||||
return Group::query()
|
return Group::query()
|
||||||
|
|||||||
@@ -66,6 +66,13 @@ final class HybridSimilarArtworksService
|
|||||||
->whereIn('id', $idSlice)
|
->whereIn('id', $idSlice)
|
||||||
->public()
|
->public()
|
||||||
->published()
|
->published()
|
||||||
|
->with([
|
||||||
|
'categories:id,slug,name,content_type_id',
|
||||||
|
'categories.contentType:id,name,slug',
|
||||||
|
'user:id,name,username',
|
||||||
|
'user.profile:user_id,avatar_hash',
|
||||||
|
'group:id,name,slug,avatar_path',
|
||||||
|
])
|
||||||
->get()
|
->get()
|
||||||
->keyBy('id');
|
->keyBy('id');
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ final class StaticPagesSitemapBuilder extends AbstractSitemapBuilder
|
|||||||
$this->urls->staticRoute('/'),
|
$this->urls->staticRoute('/'),
|
||||||
$this->urls->staticRoute('/academy'),
|
$this->urls->staticRoute('/academy'),
|
||||||
$this->urls->staticRoute('/academy/pricing'),
|
$this->urls->staticRoute('/academy/pricing'),
|
||||||
|
$this->urls->staticRoute('/web-stories'),
|
||||||
$this->urls->staticRoute('/faq'),
|
$this->urls->staticRoute('/faq'),
|
||||||
$this->urls->staticRoute('/rules-and-guidelines'),
|
$this->urls->staticRoute('/rules-and-guidelines'),
|
||||||
$this->urls->staticRoute('/privacy-policy'),
|
$this->urls->staticRoute('/privacy-policy'),
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Services\Sitemaps\Builders;
|
||||||
|
|
||||||
|
use App\Models\WorldWebStory;
|
||||||
|
use App\Services\Sitemaps\AbstractSitemapBuilder;
|
||||||
|
use App\Services\Sitemaps\SitemapUrlBuilder;
|
||||||
|
use DateTimeInterface;
|
||||||
|
|
||||||
|
final class WorldWebStoriesSitemapBuilder extends AbstractSitemapBuilder
|
||||||
|
{
|
||||||
|
public function __construct(private readonly SitemapUrlBuilder $urls)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function name(): string
|
||||||
|
{
|
||||||
|
return 'web-stories';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function items(): array
|
||||||
|
{
|
||||||
|
return WorldWebStory::query()
|
||||||
|
->visible()
|
||||||
|
->with('world')
|
||||||
|
->orderByDesc('published_at')
|
||||||
|
->orderByDesc('id')
|
||||||
|
->get()
|
||||||
|
->map(fn (WorldWebStory $story) => $this->urls->webStory($story))
|
||||||
|
->filter()
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function lastModified(): ?DateTimeInterface
|
||||||
|
{
|
||||||
|
return $this->dateTime(WorldWebStory::query()->visible()->max('updated_at'));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,6 +22,7 @@ use App\Services\Sitemaps\Builders\StaticPagesSitemapBuilder;
|
|||||||
use App\Services\Sitemaps\Builders\StoriesSitemapBuilder;
|
use App\Services\Sitemaps\Builders\StoriesSitemapBuilder;
|
||||||
use App\Services\Sitemaps\Builders\TagsSitemapBuilder;
|
use App\Services\Sitemaps\Builders\TagsSitemapBuilder;
|
||||||
use App\Services\Sitemaps\Builders\UsersSitemapBuilder;
|
use App\Services\Sitemaps\Builders\UsersSitemapBuilder;
|
||||||
|
use App\Services\Sitemaps\Builders\WorldWebStoriesSitemapBuilder;
|
||||||
|
|
||||||
final class SitemapRegistry
|
final class SitemapRegistry
|
||||||
{
|
{
|
||||||
@@ -43,6 +44,7 @@ final class SitemapRegistry
|
|||||||
CollectionsSitemapBuilder $collections,
|
CollectionsSitemapBuilder $collections,
|
||||||
CardsSitemapBuilder $cards,
|
CardsSitemapBuilder $cards,
|
||||||
StoriesSitemapBuilder $stories,
|
StoriesSitemapBuilder $stories,
|
||||||
|
WorldWebStoriesSitemapBuilder $webStories,
|
||||||
NewsSitemapBuilder $news,
|
NewsSitemapBuilder $news,
|
||||||
GoogleNewsSitemapBuilder $googleNews,
|
GoogleNewsSitemapBuilder $googleNews,
|
||||||
ForumIndexSitemapBuilder $forumIndex,
|
ForumIndexSitemapBuilder $forumIndex,
|
||||||
@@ -63,6 +65,7 @@ final class SitemapRegistry
|
|||||||
$collections->name() => $collections,
|
$collections->name() => $collections,
|
||||||
$cards->name() => $cards,
|
$cards->name() => $cards,
|
||||||
$stories->name() => $stories,
|
$stories->name() => $stories,
|
||||||
|
$webStories->name() => $webStories,
|
||||||
$news->name() => $news,
|
$news->name() => $news,
|
||||||
$googleNews->name() => $googleNews,
|
$googleNews->name() => $googleNews,
|
||||||
$forumIndex->name() => $forumIndex,
|
$forumIndex->name() => $forumIndex,
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ use App\Models\Page;
|
|||||||
use App\Models\Story;
|
use App\Models\Story;
|
||||||
use App\Models\Tag;
|
use App\Models\Tag;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use App\Models\WorldWebStory;
|
||||||
use App\Services\ThumbnailPresenter;
|
use App\Services\ThumbnailPresenter;
|
||||||
use cPad\Plugins\Forum\Models\ForumBoard;
|
use cPad\Plugins\Forum\Models\ForumBoard;
|
||||||
use cPad\Plugins\Forum\Models\ForumCategory;
|
use cPad\Plugins\Forum\Models\ForumCategory;
|
||||||
@@ -187,6 +188,21 @@ final class SitemapUrlBuilder extends AbstractSitemapBuilder
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function webStory(WorldWebStory $story): ?SitemapUrl
|
||||||
|
{
|
||||||
|
if (trim((string) $story->slug) === '') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new SitemapUrl(
|
||||||
|
$story->publicUrl(),
|
||||||
|
$this->newest($story->updated_at, $story->published_at, $story->created_at),
|
||||||
|
$this->images([
|
||||||
|
$this->image($story->posterPortraitUrl(), (string) $story->title),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function forumIndex(): SitemapUrl
|
public function forumIndex(): SitemapUrl
|
||||||
{
|
{
|
||||||
return new SitemapUrl(route('forum.index'));
|
return new SitemapUrl(route('forum.index'));
|
||||||
|
|||||||
166
app/Services/WebStories/WorldWebStoryAssetService.php
Normal file
166
app/Services/WebStories/WorldWebStoryAssetService.php
Normal file
@@ -0,0 +1,166 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Services\WebStories;
|
||||||
|
|
||||||
|
use App\Models\Artwork;
|
||||||
|
use App\Models\World;
|
||||||
|
use App\Models\WorldSubmission;
|
||||||
|
use App\Models\WorldWebStory;
|
||||||
|
use App\Models\WorldWebStoryPage;
|
||||||
|
use App\Services\ThumbnailPresenter;
|
||||||
|
|
||||||
|
final class WorldWebStoryAssetService
|
||||||
|
{
|
||||||
|
public function defaultPublisherLogoPath(): string
|
||||||
|
{
|
||||||
|
return 'https://cdn.skinbase.org/images/skinbase_logo_96.webp';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{updated: bool, story: array<string, string>, pages: array<int, array<string, mixed>>}
|
||||||
|
*/
|
||||||
|
public function buildAssets(WorldWebStory $story, bool $force = false, bool $dryRun = false): array
|
||||||
|
{
|
||||||
|
$story->loadMissing(['world', 'orderedPages.artwork']);
|
||||||
|
$world = $story->world;
|
||||||
|
$storyChanges = [];
|
||||||
|
$pageChanges = [];
|
||||||
|
|
||||||
|
$primaryImage = $this->bestWorldImage($story);
|
||||||
|
|
||||||
|
if (($force || blank($story->poster_portrait_path)) && filled($primaryImage)) {
|
||||||
|
$storyChanges['poster_portrait_path'] = $primaryImage;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (($force || blank($story->poster_square_path)) && filled($primaryImage)) {
|
||||||
|
$storyChanges['poster_square_path'] = $primaryImage;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($force || blank($story->publisher_logo_path)) {
|
||||||
|
$storyChanges['publisher_logo_path'] = $this->defaultPublisherLogoPath();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($story->orderedPages as $page) {
|
||||||
|
$changes = [];
|
||||||
|
$background = $this->bestPageBackground($page, $world, $primaryImage);
|
||||||
|
|
||||||
|
if (($force || blank($page->background_path)) && filled($background)) {
|
||||||
|
$changes['background_path'] = $background;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (($force || blank($page->background_mobile_path)) && filled($background)) {
|
||||||
|
$changes['background_mobile_path'] = $background;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (($force || blank($page->alt_text)) && filled($page->headline)) {
|
||||||
|
$changes['alt_text'] = (string) $page->headline;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($changes !== []) {
|
||||||
|
$pageChanges[(int) $page->id] = $changes;
|
||||||
|
if (! $dryRun) {
|
||||||
|
$page->forceFill($changes)->save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($storyChanges !== [] && ! $dryRun) {
|
||||||
|
$story->forceFill($storyChanges)->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'updated' => $storyChanges !== [] || $pageChanges !== [],
|
||||||
|
'story' => $storyChanges,
|
||||||
|
'pages' => $pageChanges,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function storyBasePath(WorldWebStory $story): string
|
||||||
|
{
|
||||||
|
$slug = trim((string) ($story->world?->slug ?: $story->slug));
|
||||||
|
|
||||||
|
return 'web-stories/worlds/' . $slug;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function bestWorldImage(WorldWebStory $story): ?string
|
||||||
|
{
|
||||||
|
$world = $story->world;
|
||||||
|
|
||||||
|
if ($world instanceof World) {
|
||||||
|
foreach ([$world->ogImageUrl(), $world->coverUrl(), $world->teaserImageUrl()] as $candidate) {
|
||||||
|
if (filled($candidate)) {
|
||||||
|
return (string) $candidate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$artwork = $this->bestWorldArtwork($world);
|
||||||
|
if ($artwork instanceof Artwork) {
|
||||||
|
return $this->artworkImage($artwork);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function bestPageBackground(WorldWebStoryPage $page, ?World $world, ?string $fallback): ?string
|
||||||
|
{
|
||||||
|
if ($page->artwork instanceof Artwork) {
|
||||||
|
$artworkImage = $this->artworkImage($page->artwork);
|
||||||
|
if (filled($artworkImage)) {
|
||||||
|
return $artworkImage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($world instanceof World) {
|
||||||
|
$artwork = $this->bestWorldArtwork($world);
|
||||||
|
if ($artwork instanceof Artwork) {
|
||||||
|
$artworkImage = $this->artworkImage($artwork);
|
||||||
|
if (filled($artworkImage)) {
|
||||||
|
return $artworkImage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function bestWorldArtwork(World $world): ?Artwork
|
||||||
|
{
|
||||||
|
$relatedArtworkIds = $world->worldRelations()
|
||||||
|
->where('related_type', 'artwork')
|
||||||
|
->orderByDesc('is_featured')
|
||||||
|
->orderBy('sort_order')
|
||||||
|
->pluck('related_id')
|
||||||
|
->map(fn ($id) => (int) $id)
|
||||||
|
->filter()
|
||||||
|
->values();
|
||||||
|
|
||||||
|
if ($relatedArtworkIds->isNotEmpty()) {
|
||||||
|
return Artwork::query()
|
||||||
|
->whereIn('id', $relatedArtworkIds)
|
||||||
|
->get()
|
||||||
|
->sortBy(fn (Artwork $artwork): int => (int) ($relatedArtworkIds->search((int) $artwork->id) ?? PHP_INT_MAX))
|
||||||
|
->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
$submission = WorldSubmission::query()
|
||||||
|
->with('artwork')
|
||||||
|
->where('world_id', $world->id)
|
||||||
|
->where('status', WorldSubmission::STATUS_LIVE)
|
||||||
|
->orderByDesc('is_featured')
|
||||||
|
->orderByDesc('featured_at')
|
||||||
|
->orderByDesc('id')
|
||||||
|
->first();
|
||||||
|
|
||||||
|
return $submission?->artwork;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function artworkImage(Artwork $artwork): ?string
|
||||||
|
{
|
||||||
|
$preview = ThumbnailPresenter::present($artwork, 'xl');
|
||||||
|
|
||||||
|
return (string) ($preview['url'] ?? $artwork->thumbnail_url ?? $artwork->thumb_url ?? '');
|
||||||
|
}
|
||||||
|
}
|
||||||
296
app/Services/WebStories/WorldWebStoryGenerator.php
Normal file
296
app/Services/WebStories/WorldWebStoryGenerator.php
Normal file
@@ -0,0 +1,296 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Services\WebStories;
|
||||||
|
|
||||||
|
use App\Models\Artwork;
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Models\World;
|
||||||
|
use App\Models\WorldSubmission;
|
||||||
|
use App\Models\WorldWebStory;
|
||||||
|
use App\Models\WorldWebStoryPage;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
|
|
||||||
|
final class WorldWebStoryGenerator
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly WorldWebStoryAssetService $assets,
|
||||||
|
private readonly WorldWebStoryValidationService $validation,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{story: WorldWebStory, created: bool, validation: array{valid: bool, errors: list<string>, warnings: list<string>, page_count: int}}
|
||||||
|
*/
|
||||||
|
public function generateFromWorld(World $world, ?User $actor = null, int $pages = 7, bool $force = false, bool $publish = false, bool $dryRun = false): array
|
||||||
|
{
|
||||||
|
$pageCount = max(5, min(10, $pages));
|
||||||
|
$existing = WorldWebStory::query()->where('world_id', $world->id)->orderByDesc('id')->first();
|
||||||
|
|
||||||
|
if ($existing && ! $force && ! $dryRun) {
|
||||||
|
throw ValidationException::withMessages([
|
||||||
|
'world' => ['A web story already exists for this world. Use --force to rebuild it.'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$selectedArtworks = $this->candidateArtworks($world)->take(max(3, $pageCount - 3))->values();
|
||||||
|
$storyAttributes = [
|
||||||
|
'world_id' => $world->id,
|
||||||
|
'slug' => $existing?->slug ?: $this->uniqueSlug($world->slug, $existing?->id),
|
||||||
|
'title' => $existing?->title ?: (string) $world->title,
|
||||||
|
'subtitle' => $world->tagline,
|
||||||
|
'excerpt' => $world->summary ?: $world->tagline,
|
||||||
|
'description' => $world->description ?: $world->summary,
|
||||||
|
'seo_title' => trim((string) ($world->seo_title ?: ($world->title . ' – Skinbase Web Story'))),
|
||||||
|
'seo_description' => trim((string) ($world->seo_description ?: $world->summary ?: $world->description ?: '')),
|
||||||
|
'status' => WorldWebStory::STATUS_DRAFT,
|
||||||
|
'active' => true,
|
||||||
|
'noindex' => false,
|
||||||
|
'featured' => false,
|
||||||
|
'updated_by' => $actor?->id,
|
||||||
|
];
|
||||||
|
|
||||||
|
if (! $existing) {
|
||||||
|
$storyAttributes['created_by'] = $actor?->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
$pagePayloads = $this->buildPagePayloads($world, $selectedArtworks, $pageCount);
|
||||||
|
|
||||||
|
if ($dryRun) {
|
||||||
|
$story = $existing ?? new WorldWebStory($storyAttributes);
|
||||||
|
$story->fill($storyAttributes);
|
||||||
|
$story->setRelation('orderedPages', collect($pagePayloads)->map(fn (array $page): WorldWebStoryPage => new WorldWebStoryPage($page)));
|
||||||
|
|
||||||
|
$this->assets->buildAssets($story, force: $force, dryRun: true);
|
||||||
|
$validation = $this->validation->validate($story);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'story' => $story,
|
||||||
|
'created' => ! $existing,
|
||||||
|
'validation' => $validation,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$story = DB::transaction(function () use ($existing, $storyAttributes, $pagePayloads): WorldWebStory {
|
||||||
|
$story = $existing ?? new WorldWebStory();
|
||||||
|
$story->fill($storyAttributes);
|
||||||
|
$story->save();
|
||||||
|
|
||||||
|
$story->pages()->delete();
|
||||||
|
|
||||||
|
foreach ($pagePayloads as $pagePayload) {
|
||||||
|
$story->pages()->create($pagePayload);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $story->fresh(['orderedPages', 'world']);
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->assets->buildAssets($story, force: $force);
|
||||||
|
$story->refresh()->load('orderedPages', 'world');
|
||||||
|
|
||||||
|
if ($publish) {
|
||||||
|
$this->validation->assertPublishable($story);
|
||||||
|
$story->forceFill([
|
||||||
|
'status' => WorldWebStory::STATUS_PUBLISHED,
|
||||||
|
'published_at' => now(),
|
||||||
|
])->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'story' => $story->fresh(['orderedPages', 'world']),
|
||||||
|
'created' => ! $existing,
|
||||||
|
'validation' => $this->validation->validate($story),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Collection<int, Artwork>
|
||||||
|
*/
|
||||||
|
private function candidateArtworks(World $world): Collection
|
||||||
|
{
|
||||||
|
$relationIds = $world->worldRelations()
|
||||||
|
->where('related_type', 'artwork')
|
||||||
|
->orderByDesc('is_featured')
|
||||||
|
->orderBy('sort_order')
|
||||||
|
->pluck('related_id')
|
||||||
|
->map(fn ($id): int => (int) $id)
|
||||||
|
->filter()
|
||||||
|
->values();
|
||||||
|
|
||||||
|
$artworks = collect();
|
||||||
|
|
||||||
|
if ($relationIds->isNotEmpty()) {
|
||||||
|
$artworks = Artwork::query()
|
||||||
|
->whereIn('id', $relationIds)
|
||||||
|
->get()
|
||||||
|
->sortBy(fn (Artwork $artwork): int => $relationIds->search((int) $artwork->id))
|
||||||
|
->values();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($artworks->count() < 3) {
|
||||||
|
$submissionArtworks = WorldSubmission::query()
|
||||||
|
->with('artwork.user')
|
||||||
|
->where('world_id', $world->id)
|
||||||
|
->where('status', WorldSubmission::STATUS_LIVE)
|
||||||
|
->orderByDesc('is_featured')
|
||||||
|
->orderByDesc('featured_at')
|
||||||
|
->orderByDesc('id')
|
||||||
|
->get()
|
||||||
|
->pluck('artwork')
|
||||||
|
->filter(fn ($artwork): bool => $artwork instanceof Artwork);
|
||||||
|
|
||||||
|
$artworks = $artworks->concat($submissionArtworks)->unique(fn (Artwork $artwork): int => (int) $artwork->id)->values();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $artworks;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Collection<int, Artwork> $artworks
|
||||||
|
* @return list<array<string, mixed>>
|
||||||
|
*/
|
||||||
|
private function buildPagePayloads(World $world, Collection $artworks, int $pageCount): array
|
||||||
|
{
|
||||||
|
$primaryArtwork = $artworks->get(0);
|
||||||
|
$secondaryArtwork = $artworks->get(1) ?: $primaryArtwork;
|
||||||
|
$tertiaryArtwork = $artworks->get(2) ?: $secondaryArtwork;
|
||||||
|
|
||||||
|
$pages = [
|
||||||
|
[
|
||||||
|
'position' => 1,
|
||||||
|
'layout' => WorldWebStoryPage::LAYOUT_COVER,
|
||||||
|
'background_type' => WorldWebStoryPage::BACKGROUND_IMAGE,
|
||||||
|
'headline' => (string) $world->title,
|
||||||
|
'body' => Str::limit((string) ($world->tagline ?: $world->summary ?: 'A cinematic Skinbase World.'), 160, ''),
|
||||||
|
'caption' => 'Skinbase World',
|
||||||
|
'alt_text' => (string) $world->title,
|
||||||
|
'text_position' => 'bottom',
|
||||||
|
'overlay_strength' => 45,
|
||||||
|
'animation' => 'fade-in',
|
||||||
|
'active' => true,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'position' => 2,
|
||||||
|
'layout' => WorldWebStoryPage::LAYOUT_MOOD,
|
||||||
|
'background_type' => WorldWebStoryPage::BACKGROUND_IMAGE,
|
||||||
|
'headline' => 'Step into ' . $world->title,
|
||||||
|
'body' => Str::limit((string) ($world->summary ?: $world->description ?: 'Curated visuals, featured creators, and a clear editorial mood.'), 170, ''),
|
||||||
|
'caption' => 'World intro',
|
||||||
|
'alt_text' => 'Intro for ' . $world->title,
|
||||||
|
'text_position' => 'bottom',
|
||||||
|
'overlay_strength' => 35,
|
||||||
|
'animation' => 'fly-in-bottom',
|
||||||
|
'active' => true,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
if ($primaryArtwork instanceof Artwork) {
|
||||||
|
$pages[] = [
|
||||||
|
'position' => count($pages) + 1,
|
||||||
|
'layout' => WorldWebStoryPage::LAYOUT_ARTWORK,
|
||||||
|
'artwork_id' => $primaryArtwork->id,
|
||||||
|
'background_type' => WorldWebStoryPage::BACKGROUND_IMAGE,
|
||||||
|
'headline' => (string) ($primaryArtwork->title ?: 'Featured artwork'),
|
||||||
|
'body' => Str::limit('A featured visual from ' . $world->title . ' by ' . ($primaryArtwork->user?->name ?: $primaryArtwork->user?->username ?: 'a Skinbase creator') . '.', 160, ''),
|
||||||
|
'caption' => 'Featured artwork',
|
||||||
|
'alt_text' => (string) ($primaryArtwork->title ?: 'Featured artwork'),
|
||||||
|
'text_position' => 'bottom',
|
||||||
|
'overlay_strength' => 35,
|
||||||
|
'animation' => 'pan-left',
|
||||||
|
'active' => true,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($secondaryArtwork instanceof Artwork) {
|
||||||
|
$pages[] = [
|
||||||
|
'position' => count($pages) + 1,
|
||||||
|
'layout' => WorldWebStoryPage::LAYOUT_CREATOR,
|
||||||
|
'artwork_id' => $secondaryArtwork->id,
|
||||||
|
'background_type' => WorldWebStoryPage::BACKGROUND_IMAGE,
|
||||||
|
'headline' => 'Creator spotlight',
|
||||||
|
'body' => Str::limit(($secondaryArtwork->user?->name ?: $secondaryArtwork->user?->username ?: 'A featured creator') . ' helps define the mood of ' . $world->title . '.', 160, ''),
|
||||||
|
'caption' => 'Creator spotlight',
|
||||||
|
'alt_text' => (string) ($secondaryArtwork->title ?: 'Creator spotlight artwork'),
|
||||||
|
'text_position' => 'bottom',
|
||||||
|
'overlay_strength' => 40,
|
||||||
|
'animation' => 'fade-in',
|
||||||
|
'active' => true,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($tertiaryArtwork instanceof Artwork) {
|
||||||
|
$pages[] = [
|
||||||
|
'position' => count($pages) + 1,
|
||||||
|
'layout' => WorldWebStoryPage::LAYOUT_COLLECTION,
|
||||||
|
'artwork_id' => $tertiaryArtwork->id,
|
||||||
|
'background_type' => WorldWebStoryPage::BACKGROUND_IMAGE,
|
||||||
|
'headline' => 'More from this World',
|
||||||
|
'body' => Str::limit('Explore more wallpapers, digital art, and creator picks collected inside ' . $world->title . '.', 155, ''),
|
||||||
|
'caption' => 'Community picks',
|
||||||
|
'alt_text' => (string) ($tertiaryArtwork->title ?: 'World picks'),
|
||||||
|
'text_position' => 'bottom',
|
||||||
|
'overlay_strength' => 35,
|
||||||
|
'animation' => 'pan-right',
|
||||||
|
'active' => true,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
while (count($pages) < max(5, $pageCount - 1)) {
|
||||||
|
$pages[] = [
|
||||||
|
'position' => count($pages) + 1,
|
||||||
|
'layout' => WorldWebStoryPage::LAYOUT_MOOD,
|
||||||
|
'background_type' => WorldWebStoryPage::BACKGROUND_IMAGE,
|
||||||
|
'headline' => 'Inside the theme',
|
||||||
|
'body' => Str::limit('A short visual pause that keeps the story connected to ' . $world->title . '.', 150, ''),
|
||||||
|
'caption' => 'World mood',
|
||||||
|
'alt_text' => 'Mood page for ' . $world->title,
|
||||||
|
'text_position' => 'bottom',
|
||||||
|
'overlay_strength' => 35,
|
||||||
|
'animation' => 'fade-in',
|
||||||
|
'active' => true,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$pages[] = [
|
||||||
|
'position' => count($pages) + 1,
|
||||||
|
'layout' => WorldWebStoryPage::LAYOUT_CTA,
|
||||||
|
'background_type' => WorldWebStoryPage::BACKGROUND_IMAGE,
|
||||||
|
'headline' => 'Explore ' . $world->title,
|
||||||
|
'body' => Str::limit('Open the full World page for the complete artwork grid, featured picks, and related creator content.', 160, ''),
|
||||||
|
'caption' => 'Continue on Skinbase',
|
||||||
|
'cta_label' => 'View World',
|
||||||
|
'cta_url' => $world->publicUrl(),
|
||||||
|
'alt_text' => 'Explore ' . $world->title . ' on Skinbase',
|
||||||
|
'text_position' => 'bottom',
|
||||||
|
'overlay_strength' => 45,
|
||||||
|
'animation' => 'pulse',
|
||||||
|
'active' => true,
|
||||||
|
];
|
||||||
|
|
||||||
|
return collect($pages)
|
||||||
|
->take($pageCount)
|
||||||
|
->values()
|
||||||
|
->map(fn (array $page, int $index): array => array_merge($page, [
|
||||||
|
'position' => $index + 1,
|
||||||
|
]))
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function uniqueSlug(string $base, ?int $ignoreId = null): string
|
||||||
|
{
|
||||||
|
$candidate = Str::slug($base) ?: 'web-story';
|
||||||
|
$slug = $candidate;
|
||||||
|
$suffix = 2;
|
||||||
|
|
||||||
|
while (WorldWebStory::query()->when($ignoreId, fn ($query) => $query->whereKeyNot($ignoreId))->where('slug', $slug)->exists()) {
|
||||||
|
$slug = $candidate . '-' . $suffix;
|
||||||
|
$suffix++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $slug;
|
||||||
|
}
|
||||||
|
}
|
||||||
47
app/Services/WebStories/WorldWebStorySeoService.php
Normal file
47
app/Services/WebStories/WorldWebStorySeoService.php
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Services\WebStories;
|
||||||
|
|
||||||
|
use App\Models\WorldWebStory;
|
||||||
|
use App\Support\Seo\SeoFactory;
|
||||||
|
|
||||||
|
final class WorldWebStorySeoService
|
||||||
|
{
|
||||||
|
public function __construct(private readonly SeoFactory $seo)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function indexSeo(): array
|
||||||
|
{
|
||||||
|
return $this->seo->collectionListing(
|
||||||
|
'Skinbase Web Stories',
|
||||||
|
'Explore Skinbase Web Stories featuring digital art Worlds, wallpapers, creator highlights, seasonal collections, and visual stories from the Skinbase community.',
|
||||||
|
route('web-stories.index'),
|
||||||
|
)->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, string>
|
||||||
|
*/
|
||||||
|
public function storyMeta(WorldWebStory $story): array
|
||||||
|
{
|
||||||
|
$title = $story->seoTitle();
|
||||||
|
$description = $story->seoDescription();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'title' => $title,
|
||||||
|
'description' => $description,
|
||||||
|
'canonical' => $story->publicUrl(),
|
||||||
|
'robots' => $story->noindex ? 'noindex,follow' : 'index,follow,max-image-preview:large',
|
||||||
|
'og_title' => $title,
|
||||||
|
'og_description' => $description,
|
||||||
|
'og_url' => $story->publicUrl(),
|
||||||
|
'og_image' => (string) $story->posterPortraitUrl(),
|
||||||
|
'twitter_title' => $title,
|
||||||
|
'twitter_description' => $description,
|
||||||
|
'twitter_image' => (string) $story->posterPortraitUrl(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
174
app/Services/WebStories/WorldWebStoryValidationService.php
Normal file
174
app/Services/WebStories/WorldWebStoryValidationService.php
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Services\WebStories;
|
||||||
|
|
||||||
|
use App\Models\WorldWebStory;
|
||||||
|
use App\Models\WorldWebStoryPage;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
|
|
||||||
|
final class WorldWebStoryValidationService
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return array{valid: bool, errors: list<string>, warnings: list<string>, page_count: int}
|
||||||
|
*/
|
||||||
|
public function validate(WorldWebStory $story): array
|
||||||
|
{
|
||||||
|
$story->loadMissing('orderedPages');
|
||||||
|
$pages = $story->orderedPages->where('active', true)->values();
|
||||||
|
|
||||||
|
$errors = [];
|
||||||
|
$warnings = [];
|
||||||
|
|
||||||
|
if (trim((string) $story->title) === '') {
|
||||||
|
$errors[] = 'Story title is required.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (trim((string) $story->slug) === '') {
|
||||||
|
$errors[] = 'Story slug is required.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (trim((string) $story->poster_portrait_path) === '') {
|
||||||
|
$errors[] = 'Poster portrait image is required.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (trim((string) $story->publisher_logo_path) === '') {
|
||||||
|
$errors[] = 'Publisher logo is required.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($pages->count() < 5) {
|
||||||
|
$errors[] = 'A published web story must have at least 5 active pages.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($pages->count() > 10) {
|
||||||
|
$errors[] = 'A published web story may not have more than 10 active pages.';
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($pages as $page) {
|
||||||
|
$pageNumber = (int) $page->position;
|
||||||
|
$body = trim((string) $page->body);
|
||||||
|
$headline = trim((string) $page->headline);
|
||||||
|
|
||||||
|
if (in_array((string) $page->background_type, [WorldWebStoryPage::BACKGROUND_IMAGE, WorldWebStoryPage::BACKGROUND_VIDEO], true)
|
||||||
|
&& trim((string) ($page->background_mobile_path ?: $page->background_path)) === '') {
|
||||||
|
$errors[] = sprintf('Page %d is missing required background media.', $pageNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mb_strlen($body) > 180) {
|
||||||
|
$errors[] = sprintf('Page %d body exceeds 180 characters.', $pageNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (trim((string) $page->alt_text) === '') {
|
||||||
|
$errors[] = sprintf('Page %d is missing alt text.', $pageNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($headline === '' && $body === '') {
|
||||||
|
$warnings[] = sprintf('Page %d has no story text.', $pageNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filled($page->cta_label) || filled($page->cta_url)) {
|
||||||
|
if (! filled($page->cta_label) || ! filled($page->cta_url)) {
|
||||||
|
$errors[] = sprintf('Page %d CTA requires both label and URL.', $pageNumber);
|
||||||
|
} elseif (! $this->isAllowedCtaUrl((string) $page->cta_url)) {
|
||||||
|
$errors[] = sprintf('Page %d CTA URL is not allowed.', $pageNumber);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'valid' => $errors === [],
|
||||||
|
'errors' => array_values(array_unique($errors)),
|
||||||
|
'warnings' => array_values(array_unique($warnings)),
|
||||||
|
'page_count' => $pages->count(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $page
|
||||||
|
*/
|
||||||
|
public function validatePagePayload(array $page): array
|
||||||
|
{
|
||||||
|
$errors = [];
|
||||||
|
$position = (int) ($page['position'] ?? 0);
|
||||||
|
$body = trim((string) ($page['body'] ?? ''));
|
||||||
|
$backgroundType = (string) ($page['background_type'] ?? WorldWebStoryPage::BACKGROUND_IMAGE);
|
||||||
|
$backgroundPath = trim((string) ($page['background_mobile_path'] ?? $page['background_path'] ?? ''));
|
||||||
|
$altText = trim((string) ($page['alt_text'] ?? ''));
|
||||||
|
$ctaUrl = trim((string) ($page['cta_url'] ?? ''));
|
||||||
|
$ctaLabel = trim((string) ($page['cta_label'] ?? ''));
|
||||||
|
|
||||||
|
if ($body !== '' && mb_strlen($body) > 180) {
|
||||||
|
$errors['body'] = sprintf('Page %d body exceeds 180 characters.', max(1, $position));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (in_array($backgroundType, [WorldWebStoryPage::BACKGROUND_IMAGE, WorldWebStoryPage::BACKGROUND_VIDEO], true) && $backgroundPath === '') {
|
||||||
|
$errors['background_path'] = 'Background media is required for image and video pages.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($altText === '') {
|
||||||
|
$errors['alt_text'] = 'Alt text is required.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (($ctaUrl !== '' || $ctaLabel !== '') && ($ctaUrl === '' || $ctaLabel === '')) {
|
||||||
|
$errors['cta'] = 'CTA label and URL must both be present.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($ctaUrl !== '' && ! $this->isAllowedCtaUrl($ctaUrl)) {
|
||||||
|
$errors['cta_url'] = 'CTA URL must stay on Skinbase or use a relative path.';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $errors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function assertPublishable(WorldWebStory $story): void
|
||||||
|
{
|
||||||
|
$result = $this->validate($story);
|
||||||
|
|
||||||
|
if ($result['valid']) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw ValidationException::withMessages([
|
||||||
|
'story' => $result['errors'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isAllowedCtaUrl(string $url): bool
|
||||||
|
{
|
||||||
|
$value = trim($url);
|
||||||
|
|
||||||
|
if ($value === '') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Str::startsWith($value, ['/'])) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$parts = parse_url($value);
|
||||||
|
$host = strtolower((string) Arr::get($parts, 'host', ''));
|
||||||
|
|
||||||
|
if ($host === '') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$allowedHosts = array_filter([
|
||||||
|
strtolower((string) parse_url((string) config('app.url'), PHP_URL_HOST)),
|
||||||
|
'skinbase.org',
|
||||||
|
'www.skinbase.org',
|
||||||
|
'skinbase.top',
|
||||||
|
'www.skinbase.top',
|
||||||
|
]);
|
||||||
|
|
||||||
|
foreach ($allowedHosts as $allowedHost) {
|
||||||
|
if ($host === $allowedHost || Str::endsWith($host, '.' . $allowedHost)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,6 +17,7 @@ use App\Models\User;
|
|||||||
use App\Models\World;
|
use App\Models\World;
|
||||||
use App\Models\WorldRelation;
|
use App\Models\WorldRelation;
|
||||||
use App\Models\WorldSubmission;
|
use App\Models\WorldSubmission;
|
||||||
|
use App\Models\WorldWebStory;
|
||||||
use App\Services\CollectionService;
|
use App\Services\CollectionService;
|
||||||
use App\Services\GroupCardService;
|
use App\Services\GroupCardService;
|
||||||
use App\Services\Maturity\ArtworkMaturityService;
|
use App\Services\Maturity\ArtworkMaturityService;
|
||||||
@@ -591,7 +592,7 @@ final class WorldService
|
|||||||
|
|
||||||
public function publicShowPayload(World $world, ?User $viewer = null, bool $includeDraftRecap = false): array
|
public function publicShowPayload(World $world, ?User $viewer = null, bool $includeDraftRecap = false): array
|
||||||
{
|
{
|
||||||
$world->loadMissing(['createdBy.profile', 'parentWorld', 'worldRelations', 'linkedChallenge.group', 'recapArticle.author.profile', 'recapArticle.category']);
|
$world->loadMissing(['createdBy.profile', 'parentWorld', 'worldRelations', 'linkedChallenge.group', 'recapArticle.author.profile', 'recapArticle.category', 'publishedWebStory']);
|
||||||
|
|
||||||
$sections = $this->resolveSections($world, $viewer);
|
$sections = $this->resolveSections($world, $viewer);
|
||||||
$familyEditions = $this->familyEditionsForWorld($world);
|
$familyEditions = $this->familyEditionsForWorld($world);
|
||||||
@@ -673,6 +674,7 @@ final class WorldService
|
|||||||
'archiveEditions' => $archiveEditions,
|
'archiveEditions' => $archiveEditions,
|
||||||
'familySummary' => $this->mapRecurringFamilySummary($world),
|
'familySummary' => $this->mapRecurringFamilySummary($world),
|
||||||
'relatedWorlds' => $relatedWorlds,
|
'relatedWorlds' => $relatedWorlds,
|
||||||
|
'webStory' => $this->publishedWebStoryPayload($world),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1406,7 +1408,7 @@ final class WorldService
|
|||||||
|
|
||||||
private function mapWorldDetail(World $world): array
|
private function mapWorldDetail(World $world): array
|
||||||
{
|
{
|
||||||
$world->loadMissing(['linkedChallenge.group', 'worldRelations', 'recapArticle.author.profile', 'recapArticle.category']);
|
$world->loadMissing(['linkedChallenge.group', 'worldRelations', 'recapArticle.author.profile', 'recapArticle.category', 'publishedWebStory']);
|
||||||
$theme = $this->themePayload($world);
|
$theme = $this->themePayload($world);
|
||||||
$familyTitle = $this->recurrenceFamilyLabel($world);
|
$familyTitle = $this->recurrenceFamilyLabel($world);
|
||||||
$familyUrl = $this->familyUrlForWorld($world);
|
$familyUrl = $this->familyUrlForWorld($world);
|
||||||
@@ -1477,6 +1479,26 @@ final class WorldService
|
|||||||
'rewarded_contributor_count' => (int) $world->worldRewardGrants()->count(),
|
'rewarded_contributor_count' => (int) $world->worldRewardGrants()->count(),
|
||||||
'relation_count' => (int) ($world->world_relations_count ?? $world->worldRelations()->count()),
|
'relation_count' => (int) ($world->world_relations_count ?? $world->worldRelations()->count()),
|
||||||
'public_url' => $this->publicUrlForWorld($world),
|
'public_url' => $this->publicUrlForWorld($world),
|
||||||
|
'published_web_story' => $this->publishedWebStoryPayload($world),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function publishedWebStoryPayload(World $world): ?array
|
||||||
|
{
|
||||||
|
$story = $world->publishedWebStory;
|
||||||
|
|
||||||
|
if (! $story instanceof WorldWebStory) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'id' => (int) $story->id,
|
||||||
|
'slug' => (string) $story->slug,
|
||||||
|
'title' => (string) $story->title,
|
||||||
|
'excerpt' => (string) ($story->excerpt ?? ''),
|
||||||
|
'poster_portrait_url' => $story->posterPortraitUrl(),
|
||||||
|
'url' => $story->publicUrl(),
|
||||||
|
'published_at' => optional($story->published_at)?->toIso8601String(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
45
app/Support/AcademyAnalytics/AcademyAnalyticsContentType.php
Normal file
45
app/Support/AcademyAnalytics/AcademyAnalyticsContentType.php
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Support\AcademyAnalytics;
|
||||||
|
|
||||||
|
final class AcademyAnalyticsContentType
|
||||||
|
{
|
||||||
|
public const HOME = 'academy_home';
|
||||||
|
public const PROMPT = 'academy_prompt';
|
||||||
|
public const LESSON = 'academy_lesson';
|
||||||
|
public const COURSE = 'academy_course';
|
||||||
|
public const PROMPT_PACK = 'academy_prompt_pack';
|
||||||
|
public const CHALLENGE = 'academy_challenge';
|
||||||
|
public const SEARCH = 'academy_search';
|
||||||
|
public const UPGRADE = 'academy_upgrade';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list<string>
|
||||||
|
*/
|
||||||
|
public static function values(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
self::HOME,
|
||||||
|
self::PROMPT,
|
||||||
|
self::LESSON,
|
||||||
|
self::COURSE,
|
||||||
|
self::PROMPT_PACK,
|
||||||
|
self::CHALLENGE,
|
||||||
|
self::SEARCH,
|
||||||
|
self::UPGRADE,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function requiresContentId(string $contentType): bool
|
||||||
|
{
|
||||||
|
return in_array($contentType, [
|
||||||
|
self::PROMPT,
|
||||||
|
self::LESSON,
|
||||||
|
self::COURSE,
|
||||||
|
self::PROMPT_PACK,
|
||||||
|
self::CHALLENGE,
|
||||||
|
], true);
|
||||||
|
}
|
||||||
|
}
|
||||||
72
app/Support/AcademyAnalytics/AcademyAnalyticsEventType.php
Normal file
72
app/Support/AcademyAnalytics/AcademyAnalyticsEventType.php
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Support\AcademyAnalytics;
|
||||||
|
|
||||||
|
final class AcademyAnalyticsEventType
|
||||||
|
{
|
||||||
|
public const PAGE_VIEW = 'academy_page_view';
|
||||||
|
public const CONTENT_VIEW = 'academy_content_view';
|
||||||
|
public const ENGAGED_VIEW = 'academy_engaged_view';
|
||||||
|
public const SCROLL_50 = 'academy_scroll_50';
|
||||||
|
public const SCROLL_75 = 'academy_scroll_75';
|
||||||
|
public const SCROLL_100 = 'academy_scroll_100';
|
||||||
|
public const PROMPT_COPY = 'academy_prompt_copy';
|
||||||
|
public const PROMPT_NEGATIVE_COPY = 'academy_prompt_negative_copy';
|
||||||
|
public const PROMPT_LIKE = 'academy_prompt_like';
|
||||||
|
public const PROMPT_SAVE = 'academy_prompt_save';
|
||||||
|
public const PROMPT_PACK_VIEW = 'academy_prompt_pack_view';
|
||||||
|
public const PROMPT_PACK_DOWNLOAD = 'academy_prompt_pack_download';
|
||||||
|
public const LESSON_VIEW = 'academy_lesson_view';
|
||||||
|
public const LESSON_STARTED = 'academy_lesson_started';
|
||||||
|
public const LESSON_COMPLETED = 'academy_lesson_completed';
|
||||||
|
public const COURSE_VIEW = 'academy_course_view';
|
||||||
|
public const COURSE_STARTED = 'academy_course_started';
|
||||||
|
public const COURSE_COMPLETED = 'academy_course_completed';
|
||||||
|
public const CHALLENGE_VIEW = 'academy_challenge_view';
|
||||||
|
public const CHALLENGE_STARTED = 'academy_challenge_started';
|
||||||
|
public const CHALLENGE_SUBMITTED = 'academy_challenge_submitted';
|
||||||
|
public const SEARCH = 'academy_search';
|
||||||
|
public const ZERO_SEARCH_RESULTS = 'academy_zero_search_results';
|
||||||
|
public const SEARCH_RESULT_CLICK = 'academy_search_result_click';
|
||||||
|
public const PREMIUM_PREVIEW_VIEW = 'academy_premium_preview_view';
|
||||||
|
public const UPGRADE_CLICK = 'academy_upgrade_click';
|
||||||
|
public const OUTBOUND_CLICK = 'academy_outbound_click';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list<string>
|
||||||
|
*/
|
||||||
|
public static function values(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
self::PAGE_VIEW,
|
||||||
|
self::CONTENT_VIEW,
|
||||||
|
self::ENGAGED_VIEW,
|
||||||
|
self::SCROLL_50,
|
||||||
|
self::SCROLL_75,
|
||||||
|
self::SCROLL_100,
|
||||||
|
self::PROMPT_COPY,
|
||||||
|
self::PROMPT_NEGATIVE_COPY,
|
||||||
|
self::PROMPT_LIKE,
|
||||||
|
self::PROMPT_SAVE,
|
||||||
|
self::PROMPT_PACK_VIEW,
|
||||||
|
self::PROMPT_PACK_DOWNLOAD,
|
||||||
|
self::LESSON_VIEW,
|
||||||
|
self::LESSON_STARTED,
|
||||||
|
self::LESSON_COMPLETED,
|
||||||
|
self::COURSE_VIEW,
|
||||||
|
self::COURSE_STARTED,
|
||||||
|
self::COURSE_COMPLETED,
|
||||||
|
self::CHALLENGE_VIEW,
|
||||||
|
self::CHALLENGE_STARTED,
|
||||||
|
self::CHALLENGE_SUBMITTED,
|
||||||
|
self::SEARCH,
|
||||||
|
self::ZERO_SEARCH_RESULTS,
|
||||||
|
self::SEARCH_RESULT_CLICK,
|
||||||
|
self::PREMIUM_PREVIEW_VIEW,
|
||||||
|
self::UPGRADE_CLICK,
|
||||||
|
self::OUTBOUND_CLICK,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Support\AcademyAnalytics;
|
||||||
|
|
||||||
|
final class AcademyAnalyticsProgressStatus
|
||||||
|
{
|
||||||
|
public const NOT_STARTED = 'not_started';
|
||||||
|
public const STARTED = 'started';
|
||||||
|
public const IN_PROGRESS = 'in_progress';
|
||||||
|
public const COMPLETED = 'completed';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list<string>
|
||||||
|
*/
|
||||||
|
public static function values(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
self::NOT_STARTED,
|
||||||
|
self::STARTED,
|
||||||
|
self::IN_PROGRESS,
|
||||||
|
self::COMPLETED,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -52,9 +52,11 @@ final class SeoFactory
|
|||||||
$description = Str::limit($description !== '' ? $description : $title, 160, '…');
|
$description = Str::limit($description !== '' ? $description : $title, 160, '…');
|
||||||
$image = $thumbs['xl']['url'] ?? $thumbs['lg']['url'] ?? $thumbs['md']['url'] ?? null;
|
$image = $thumbs['xl']['url'] ?? $thumbs['lg']['url'] ?? $thumbs['md']['url'] ?? null;
|
||||||
$keywords = $artwork->tags->pluck('name')->filter()->unique()->values()->all();
|
$keywords = $artwork->tags->pluck('name')->filter()->unique()->values()->all();
|
||||||
$licenseUrl = $this->clean((string) ($artwork->license_url ?? ''));
|
|
||||||
$publisherName = (string) config('seo.site_name', 'Skinbase');
|
$publisherName = (string) config('seo.site_name', 'Skinbase');
|
||||||
$publisherUrl = url('/');
|
$publisherUrl = url('/');
|
||||||
|
$licensePageUrl = route('terms-of-service');
|
||||||
|
$licenseUrl = $this->clean((string) ($artwork->license_url ?? ''));
|
||||||
|
$licenseUrl = $licenseUrl !== null ? $licenseUrl : $licensePageUrl;
|
||||||
|
|
||||||
$imageWidth = $thumbs['xl']['width'] ?? $thumbs['lg']['width'] ?? null;
|
$imageWidth = $thumbs['xl']['width'] ?? $thumbs['lg']['width'] ?? null;
|
||||||
$imageHeight = $thumbs['xl']['height'] ?? $thumbs['lg']['height'] ?? null;
|
$imageHeight = $thumbs['xl']['height'] ?? $thumbs['lg']['height'] ?? null;
|
||||||
@@ -83,6 +85,8 @@ final class SeoFactory
|
|||||||
'creditText' => $authorName,
|
'creditText' => $authorName,
|
||||||
'datePublished' => optional($artwork->published_at)->toAtomString(),
|
'datePublished' => optional($artwork->published_at)->toAtomString(),
|
||||||
'license' => $licenseUrl,
|
'license' => $licenseUrl,
|
||||||
|
'acquireLicensePage' => $licensePageUrl,
|
||||||
|
'copyrightNotice' => $authorName,
|
||||||
'keywords' => $keywords !== [] ? $keywords : null,
|
'keywords' => $keywords !== [] ? $keywords : null,
|
||||||
'representativeOfPage' => true,
|
'representativeOfPage' => true,
|
||||||
], fn (mixed $value): bool => $value !== null && $value !== '' && $value !== []))
|
], fn (mixed $value): bool => $value !== null && $value !== '' && $value !== []))
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ return Application::configure(basePath: dirname(__DIR__))
|
|||||||
'chat_post',
|
'chat_post',
|
||||||
'chat_post/*',
|
'chat_post/*',
|
||||||
'api/art/*/view',
|
'api/art/*/view',
|
||||||
|
'stripe/*',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$middleware->web(append: [
|
$middleware->web(append: [
|
||||||
|
|||||||
@@ -2038,13 +2038,23 @@
|
|||||||
"resources/js/Layouts/AdminLayout.jsx": [],
|
"resources/js/Layouts/AdminLayout.jsx": [],
|
||||||
"resources/js/Layouts/SettingsLayout.jsx": [],
|
"resources/js/Layouts/SettingsLayout.jsx": [],
|
||||||
"resources/js/Layouts/StudioLayout.jsx": [],
|
"resources/js/Layouts/StudioLayout.jsx": [],
|
||||||
|
"resources/js/Pages/Academy/Billing/Account.jsx": [],
|
||||||
|
"resources/js/Pages/Academy/Billing/Cancel.jsx": [],
|
||||||
|
"resources/js/Pages/Academy/Billing/Pricing.jsx": [],
|
||||||
|
"resources/js/Pages/Academy/Billing/Success.jsx": [],
|
||||||
"resources/js/Pages/Academy/ChallengeSubmit.jsx": [],
|
"resources/js/Pages/Academy/ChallengeSubmit.jsx": [],
|
||||||
"resources/js/Pages/Academy/CoursesIndex.jsx": [],
|
"resources/js/Pages/Academy/CoursesIndex.jsx": [],
|
||||||
"resources/js/Pages/Academy/CoursesShow.jsx": [],
|
"resources/js/Pages/Academy/CoursesShow.jsx": [],
|
||||||
"resources/js/Pages/Academy/Index.jsx": [],
|
"resources/js/Pages/Academy/Index.jsx": [],
|
||||||
"resources/js/Pages/Academy/List.jsx": [],
|
"resources/js/Pages/Academy/List.jsx": [],
|
||||||
"resources/js/Pages/Academy/Pricing.jsx": [],
|
|
||||||
"resources/js/Pages/Academy/Show.jsx": [],
|
"resources/js/Pages/Academy/Show.jsx": [],
|
||||||
|
"resources/js/Pages/Admin/Academy/AnalyticsContent.jsx": [],
|
||||||
|
"resources/js/Pages/Admin/Academy/AnalyticsFunnel.jsx": [],
|
||||||
|
"resources/js/Pages/Admin/Academy/AnalyticsIntelligence.jsx": [],
|
||||||
|
"resources/js/Pages/Admin/Academy/AnalyticsNav.jsx": [],
|
||||||
|
"resources/js/Pages/Admin/Academy/AnalyticsOverview.jsx": [],
|
||||||
|
"resources/js/Pages/Admin/Academy/AnalyticsSearch.jsx": [],
|
||||||
|
"resources/js/Pages/Admin/Academy/Billing.jsx": [],
|
||||||
"resources/js/Pages/Admin/Academy/CourseBuilder.jsx": [],
|
"resources/js/Pages/Admin/Academy/CourseBuilder.jsx": [],
|
||||||
"resources/js/Pages/Admin/Academy/CourseEditor.jsx": [],
|
"resources/js/Pages/Admin/Academy/CourseEditor.jsx": [],
|
||||||
"resources/js/Pages/Admin/Academy/CrudForm.jsx": [],
|
"resources/js/Pages/Admin/Academy/CrudForm.jsx": [],
|
||||||
@@ -2132,6 +2142,8 @@
|
|||||||
"resources/js/Pages/Messages/Index.jsx": [],
|
"resources/js/Pages/Messages/Index.jsx": [],
|
||||||
"resources/js/Pages/Moderation/AiBiographyAdmin.jsx": [],
|
"resources/js/Pages/Moderation/AiBiographyAdmin.jsx": [],
|
||||||
"resources/js/Pages/Moderation/ArtworkMaturityQueue.jsx": [],
|
"resources/js/Pages/Moderation/ArtworkMaturityQueue.jsx": [],
|
||||||
|
"resources/js/Pages/Moderation/WorldWebStoriesIndex.jsx": [],
|
||||||
|
"resources/js/Pages/Moderation/WorldWebStoryEditor.jsx": [],
|
||||||
"resources/js/Pages/News/NewsComments.jsx": [],
|
"resources/js/Pages/News/NewsComments.jsx": [],
|
||||||
"resources/js/Pages/News/NewsImagePreview.jsx": [],
|
"resources/js/Pages/News/NewsImagePreview.jsx": [],
|
||||||
"resources/js/Pages/Profile/ProfileGallery.jsx": [],
|
"resources/js/Pages/Profile/ProfileGallery.jsx": [],
|
||||||
@@ -2210,6 +2222,8 @@
|
|||||||
"resources/js/components/Feed/VisibilityPill.jsx": [],
|
"resources/js/components/Feed/VisibilityPill.jsx": [],
|
||||||
"resources/js/components/Studio/ConfirmDangerModal.jsx": [],
|
"resources/js/components/Studio/ConfirmDangerModal.jsx": [],
|
||||||
"resources/js/components/Studio/StudioContentBrowser.jsx": [],
|
"resources/js/components/Studio/StudioContentBrowser.jsx": [],
|
||||||
|
"resources/js/components/academy/billing/AccessBadge.jsx": [],
|
||||||
|
"resources/js/components/academy/billing/PlanCard.jsx": [],
|
||||||
"resources/js/components/achievements/AchievementBadge.jsx": [],
|
"resources/js/components/achievements/AchievementBadge.jsx": [],
|
||||||
"resources/js/components/achievements/AchievementCard.jsx": [],
|
"resources/js/components/achievements/AchievementCard.jsx": [],
|
||||||
"resources/js/components/achievements/AchievementsList.jsx": [],
|
"resources/js/components/achievements/AchievementsList.jsx": [],
|
||||||
@@ -2434,6 +2448,7 @@
|
|||||||
"resources/js/hooks/upload/useUploadMachine.js": [],
|
"resources/js/hooks/upload/useUploadMachine.js": [],
|
||||||
"resources/js/hooks/upload/useVisionTags.js": [],
|
"resources/js/hooks/upload/useVisionTags.js": [],
|
||||||
"resources/js/hooks/useWebShare.js": [],
|
"resources/js/hooks/useWebShare.js": [],
|
||||||
|
"resources/js/lib/academyAnalytics.js": [],
|
||||||
"resources/js/lib/security/botFingerprint.js": [],
|
"resources/js/lib/security/botFingerprint.js": [],
|
||||||
"resources/js/lib/uploadAnalytics.js": [],
|
"resources/js/lib/uploadAnalytics.js": [],
|
||||||
"resources/js/lib/uploadEndpoints.js": [],
|
"resources/js/lib/uploadEndpoints.js": [],
|
||||||
|
|||||||
4966
bootstrap/ssr/ssr.js
4966
bootstrap/ssr/ssr.js
File diff suppressed because one or more lines are too long
@@ -16,6 +16,7 @@
|
|||||||
"inertiajs/inertia-laravel": "^1.0",
|
"inertiajs/inertia-laravel": "^1.0",
|
||||||
"intervention/image": "^3.11",
|
"intervention/image": "^3.11",
|
||||||
"jenssegers/agent": "*",
|
"jenssegers/agent": "*",
|
||||||
|
"laravel/cashier": "^16.5",
|
||||||
"laravel/framework": "^12.0",
|
"laravel/framework": "^12.0",
|
||||||
"laravel/horizon": "^5.45",
|
"laravel/horizon": "^5.45",
|
||||||
"laravel/reverb": "^1.0",
|
"laravel/reverb": "^1.0",
|
||||||
@@ -113,6 +114,10 @@
|
|||||||
"composer/installers": true,
|
"composer/installers": true,
|
||||||
"pestphp/pest-plugin": true,
|
"pestphp/pest-plugin": true,
|
||||||
"php-http/discovery": true
|
"php-http/discovery": true
|
||||||
|
},
|
||||||
|
"platform": {
|
||||||
|
"ext-pcntl": "8.4.0",
|
||||||
|
"ext-posix": "8.4.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"minimum-stability": "stable",
|
"minimum-stability": "stable",
|
||||||
|
|||||||
673
composer.lock
generated
673
composer.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
return [
|
return [
|
||||||
'enabled' => (bool) env('SKINBASE_ACADEMY_ENABLED', true),
|
'enabled' => (bool) env('SKINBASE_ACADEMY_ENABLED', true),
|
||||||
'payments_enabled' => (bool) env('SKINBASE_ACADEMY_PAYMENTS_ENABLED', false),
|
'payments_enabled' => (bool) env('SKINBASE_ACADEMY_PAYMENTS_ENABLED', env('ACADEMY_BILLING_ENABLED', false)),
|
||||||
'free_content_enabled' => (bool) env('SKINBASE_ACADEMY_FREE_CONTENT_ENABLED', true),
|
'free_content_enabled' => (bool) env('SKINBASE_ACADEMY_FREE_CONTENT_ENABLED', true),
|
||||||
'challenges_enabled' => (bool) env('SKINBASE_ACADEMY_CHALLENGES_ENABLED', true),
|
'challenges_enabled' => (bool) env('SKINBASE_ACADEMY_CHALLENGES_ENABLED', true),
|
||||||
'badges_enabled' => (bool) env('SKINBASE_ACADEMY_BADGES_ENABLED', true),
|
'badges_enabled' => (bool) env('SKINBASE_ACADEMY_BADGES_ENABLED', true),
|
||||||
@@ -25,6 +25,7 @@ return [
|
|||||||
'providers' => [
|
'providers' => [
|
||||||
'ChatGPT',
|
'ChatGPT',
|
||||||
'Gemini',
|
'Gemini',
|
||||||
|
'Adobe Firefly',
|
||||||
'Leonardo',
|
'Leonardo',
|
||||||
'Bing',
|
'Bing',
|
||||||
'Midjourney',
|
'Midjourney',
|
||||||
|
|||||||
28
config/academy_billing.php
Normal file
28
config/academy_billing.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
'enabled' => (bool) env('ACADEMY_BILLING_ENABLED', false),
|
||||||
|
|
||||||
|
'subscription_name' => env('ACADEMY_STRIPE_SUBSCRIPTION_NAME', 'academy'),
|
||||||
|
|
||||||
|
'plans' => [
|
||||||
|
'creator_monthly' => [
|
||||||
|
'label' => 'Creator Monthly',
|
||||||
|
'tier' => 'creator',
|
||||||
|
'interval' => 'monthly',
|
||||||
|
'amount' => '4.99',
|
||||||
|
'currency' => 'EUR',
|
||||||
|
'stripe_price_id' => env('ACADEMY_CREATOR_MONTHLY_PRICE_ID'),
|
||||||
|
'featured' => false,
|
||||||
|
],
|
||||||
|
'pro_monthly' => [
|
||||||
|
'label' => 'Pro Monthly',
|
||||||
|
'tier' => 'pro',
|
||||||
|
'interval' => 'monthly',
|
||||||
|
'amount' => '9.99',
|
||||||
|
'currency' => 'EUR',
|
||||||
|
'stripe_price_id' => env('ACADEMY_PRO_MONTHLY_PRICE_ID'),
|
||||||
|
'featured' => true,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
130
config/cashier.php
Normal file
130
config/cashier.php
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Laravel\Cashier\Console\WebhookCommand;
|
||||||
|
use Laravel\Cashier\Invoices\DompdfInvoiceRenderer;
|
||||||
|
|
||||||
|
// use Laravel\Cashier\Invoices\LaravelPdfInvoiceRenderer;
|
||||||
|
|
||||||
|
return [
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Stripe Keys
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| The Stripe publishable key and secret key give you access to Stripe's
|
||||||
|
| API. The "publishable" key is typically used when interacting with
|
||||||
|
| Stripe.js while the "secret" key accesses private API endpoints.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'key' => env('STRIPE_KEY'),
|
||||||
|
|
||||||
|
'secret' => env('STRIPE_SECRET'),
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Cashier Path
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| This is the base URI path where Cashier's views, such as the payment
|
||||||
|
| verification screen, will be available from. You're free to tweak
|
||||||
|
| this path according to your preferences and application design.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'path' => env('CASHIER_PATH', 'stripe'),
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Stripe Webhooks
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| Your Stripe webhook secret is used to prevent unauthorized requests to
|
||||||
|
| your Stripe webhook handling controllers. The tolerance setting will
|
||||||
|
| check the drift between the current time and the signed request's.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'webhook' => [
|
||||||
|
'secret' => env('STRIPE_WEBHOOK_SECRET'),
|
||||||
|
'tolerance' => env('STRIPE_WEBHOOK_TOLERANCE', 300),
|
||||||
|
'events' => WebhookCommand::DEFAULT_EVENTS,
|
||||||
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Currency
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| This is the default currency that will be used when generating charges
|
||||||
|
| from your application. Of course, you are welcome to use any of the
|
||||||
|
| various world currencies that are currently supported via Stripe.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'currency' => env('CASHIER_CURRENCY', 'usd'),
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Currency Locale
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| This is the default locale in which your money values are formatted in
|
||||||
|
| for display. To utilize other locales besides the default en locale
|
||||||
|
| verify you have the "intl" PHP extension installed on the system.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'currency_locale' => env('CASHIER_CURRENCY_LOCALE', 'en'),
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Payment Confirmation Notification
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| If this setting is enabled, Cashier will automatically notify customers
|
||||||
|
| whose payments require additional verification. You should listen to
|
||||||
|
| Stripe's webhooks in order for this feature to function correctly.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'payment_notification' => env('CASHIER_PAYMENT_NOTIFICATION'),
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Invoice Settings
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| The following options determine how Cashier invoices are converted from
|
||||||
|
| HTML into PDFs. You're free to change the options based on the needs
|
||||||
|
| of your application or your preferences regarding invoice styling.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'invoices' => [
|
||||||
|
// Supported: DompdfInvoiceRenderer::class, LaravelPdfInvoiceRenderer::class
|
||||||
|
'renderer' => env('CASHIER_INVOICE_RENDERER', DompdfInvoiceRenderer::class),
|
||||||
|
|
||||||
|
'options' => [
|
||||||
|
// Supported: 'letter', 'legal', 'A4'
|
||||||
|
'paper' => env('CASHIER_PAPER', 'letter'),
|
||||||
|
|
||||||
|
'remote_enabled' => env('CASHIER_REMOTE_ENABLED', false),
|
||||||
|
],
|
||||||
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Stripe Logger
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| This setting defines which logging channel will be used by the Stripe
|
||||||
|
| library to write log messages. You are free to specify any of your
|
||||||
|
| logging channels listed inside the "logging" configuration file.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'logger' => env('CASHIER_LOGGER'),
|
||||||
|
|
||||||
|
];
|
||||||
67
database/factories/WorldWebStoryFactory.php
Normal file
67
database/factories/WorldWebStoryFactory.php
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Models\World;
|
||||||
|
use App\Models\WorldWebStory;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends Factory<WorldWebStory>
|
||||||
|
*/
|
||||||
|
class WorldWebStoryFactory extends Factory
|
||||||
|
{
|
||||||
|
protected $model = WorldWebStory::class;
|
||||||
|
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
$title = Str::title($this->faker->unique()->words(3, true));
|
||||||
|
$slug = Str::slug($title);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'world_id' => World::factory(),
|
||||||
|
'slug' => $slug,
|
||||||
|
'title' => $title,
|
||||||
|
'subtitle' => $this->faker->sentence(6),
|
||||||
|
'excerpt' => $this->faker->sentence(14),
|
||||||
|
'description' => $this->faker->paragraph(),
|
||||||
|
'seo_title' => $title . ' – Skinbase Web Story',
|
||||||
|
'seo_description' => $this->faker->sentence(18),
|
||||||
|
'poster_portrait_path' => 'web-stories/worlds/' . $slug . '/poster-portrait.webp',
|
||||||
|
'poster_square_path' => 'web-stories/worlds/' . $slug . '/poster-square.webp',
|
||||||
|
'publisher_logo_path' => 'images/skinbase_logo_96.webp',
|
||||||
|
'status' => WorldWebStory::STATUS_DRAFT,
|
||||||
|
'featured' => false,
|
||||||
|
'active' => true,
|
||||||
|
'noindex' => false,
|
||||||
|
'published_at' => null,
|
||||||
|
'starts_at' => null,
|
||||||
|
'ends_at' => null,
|
||||||
|
'created_by' => User::factory(),
|
||||||
|
'updated_by' => User::factory(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function published(): self
|
||||||
|
{
|
||||||
|
return $this->state(fn (): array => [
|
||||||
|
'status' => WorldWebStory::STATUS_PUBLISHED,
|
||||||
|
'published_at' => Carbon::now()->subHour(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function visible(): self
|
||||||
|
{
|
||||||
|
return $this->published()->state(fn (): array => [
|
||||||
|
'active' => true,
|
||||||
|
'noindex' => false,
|
||||||
|
'starts_at' => Carbon::now()->subDay(),
|
||||||
|
'ends_at' => Carbon::now()->addDay(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
50
database/factories/WorldWebStoryPageFactory.php
Normal file
50
database/factories/WorldWebStoryPageFactory.php
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use App\Models\Artwork;
|
||||||
|
use App\Models\WorldWebStory;
|
||||||
|
use App\Models\WorldWebStoryPage;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends Factory<WorldWebStoryPage>
|
||||||
|
*/
|
||||||
|
class WorldWebStoryPageFactory extends Factory
|
||||||
|
{
|
||||||
|
protected $model = WorldWebStoryPage::class;
|
||||||
|
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'story_id' => WorldWebStory::factory(),
|
||||||
|
'artwork_id' => null,
|
||||||
|
'position' => 1,
|
||||||
|
'layout' => WorldWebStoryPage::LAYOUT_ARTWORK,
|
||||||
|
'background_type' => WorldWebStoryPage::BACKGROUND_IMAGE,
|
||||||
|
'background_path' => 'web-stories/worlds/example/pages/page-01.webp',
|
||||||
|
'background_mobile_path' => 'web-stories/worlds/example/pages/page-01.webp',
|
||||||
|
'headline' => 'Story headline',
|
||||||
|
'body' => 'Short supporting copy for this world web story page.',
|
||||||
|
'cta_label' => null,
|
||||||
|
'cta_url' => null,
|
||||||
|
'alt_text' => 'World story background',
|
||||||
|
'caption' => 'Skinbase World',
|
||||||
|
'credit_text' => null,
|
||||||
|
'text_position' => 'bottom',
|
||||||
|
'overlay_strength' => 35,
|
||||||
|
'animation' => 'fade-in',
|
||||||
|
'active' => true,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function withArtwork(): self
|
||||||
|
{
|
||||||
|
return $this->state(fn (): array => [
|
||||||
|
'artwork_id' => Artwork::factory(),
|
||||||
|
'layout' => WorldWebStoryPage::LAYOUT_ARTWORK,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('world_web_stories', function (Blueprint $table): void {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('world_id')->nullable()->constrained('worlds')->nullOnDelete();
|
||||||
|
$table->string('slug')->unique();
|
||||||
|
$table->string('title');
|
||||||
|
$table->string('subtitle')->nullable();
|
||||||
|
$table->text('excerpt')->nullable();
|
||||||
|
$table->text('description')->nullable();
|
||||||
|
$table->string('seo_title')->nullable();
|
||||||
|
$table->text('seo_description')->nullable();
|
||||||
|
$table->string('poster_portrait_path')->nullable();
|
||||||
|
$table->string('poster_square_path')->nullable();
|
||||||
|
$table->string('publisher_logo_path')->nullable();
|
||||||
|
$table->enum('status', ['draft', 'published', 'archived'])->default('draft');
|
||||||
|
$table->boolean('featured')->default(false);
|
||||||
|
$table->boolean('active')->default(true);
|
||||||
|
$table->boolean('noindex')->default(false);
|
||||||
|
$table->timestamp('published_at')->nullable();
|
||||||
|
$table->timestamp('starts_at')->nullable();
|
||||||
|
$table->timestamp('ends_at')->nullable();
|
||||||
|
$table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
|
||||||
|
$table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
|
||||||
|
$table->timestamps();
|
||||||
|
$table->softDeletes();
|
||||||
|
|
||||||
|
$table->index('world_id');
|
||||||
|
$table->index('slug');
|
||||||
|
$table->index(['status', 'active', 'published_at']);
|
||||||
|
$table->index(['featured', 'status', 'active']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('world_web_stories');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('world_web_story_pages', function (Blueprint $table): void {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('story_id')->constrained('world_web_stories')->cascadeOnDelete();
|
||||||
|
$table->foreignId('artwork_id')->nullable()->constrained('artworks')->nullOnDelete();
|
||||||
|
$table->unsignedInteger('position');
|
||||||
|
$table->enum('layout', ['cover', 'artwork', 'creator', 'mood', 'collection', 'cta']);
|
||||||
|
$table->enum('background_type', ['image', 'video', 'gradient']);
|
||||||
|
$table->string('background_path')->nullable();
|
||||||
|
$table->string('background_mobile_path')->nullable();
|
||||||
|
$table->string('headline')->nullable();
|
||||||
|
$table->text('body')->nullable();
|
||||||
|
$table->string('cta_label')->nullable();
|
||||||
|
$table->string('cta_url')->nullable();
|
||||||
|
$table->text('alt_text')->nullable();
|
||||||
|
$table->string('caption')->nullable();
|
||||||
|
$table->string('credit_text')->nullable();
|
||||||
|
$table->enum('text_position', ['top', 'center', 'bottom'])->default('bottom');
|
||||||
|
$table->unsignedTinyInteger('overlay_strength')->default(35);
|
||||||
|
$table->enum('animation', ['fade-in', 'fly-in-bottom', 'pulse', 'pan-left', 'pan-right'])->nullable();
|
||||||
|
$table->boolean('active')->default(true);
|
||||||
|
$table->timestamps();
|
||||||
|
$table->softDeletes();
|
||||||
|
|
||||||
|
$table->index(['story_id', 'position']);
|
||||||
|
$table->index(['story_id', 'active']);
|
||||||
|
$table->index('artwork_id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('world_web_story_pages');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('academy_events', function (Blueprint $table): void {
|
||||||
|
$table->id();
|
||||||
|
$table->string('event_type')->index();
|
||||||
|
$table->string('content_type')->nullable()->index();
|
||||||
|
$table->unsignedBigInteger('content_id')->nullable()->index();
|
||||||
|
$table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
|
||||||
|
$table->string('visitor_id', 120)->nullable()->index();
|
||||||
|
$table->string('session_id', 120)->nullable()->index();
|
||||||
|
$table->text('url')->nullable();
|
||||||
|
$table->string('route_name')->nullable()->index();
|
||||||
|
$table->text('referrer')->nullable();
|
||||||
|
$table->string('utm_source')->nullable()->index();
|
||||||
|
$table->string('utm_medium')->nullable()->index();
|
||||||
|
$table->string('utm_campaign')->nullable()->index();
|
||||||
|
$table->string('device_type')->nullable()->index();
|
||||||
|
$table->string('browser')->nullable();
|
||||||
|
$table->string('platform')->nullable();
|
||||||
|
$table->string('country_code', 8)->nullable()->index();
|
||||||
|
$table->boolean('is_logged_in')->default(false)->index();
|
||||||
|
$table->boolean('is_subscriber')->default(false)->index();
|
||||||
|
$table->boolean('is_admin')->default(false)->index();
|
||||||
|
$table->boolean('is_bot')->default(false)->index();
|
||||||
|
$table->boolean('is_crawler')->default(false)->index();
|
||||||
|
$table->boolean('is_suspicious')->default(false)->index();
|
||||||
|
$table->json('metadata')->nullable();
|
||||||
|
$table->timestamp('occurred_at')->index();
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->index(['event_type', 'occurred_at']);
|
||||||
|
$table->index(['content_type', 'content_id', 'occurred_at'], 'academy_events_content_occurred_idx');
|
||||||
|
$table->index(['user_id', 'occurred_at']);
|
||||||
|
$table->index(['visitor_id', 'occurred_at']);
|
||||||
|
$table->index(['is_bot', 'is_admin', 'occurred_at'], 'academy_events_bot_admin_occurred_idx');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('academy_events');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('academy_content_metrics_daily', function (Blueprint $table): void {
|
||||||
|
$table->id();
|
||||||
|
$table->date('date')->index();
|
||||||
|
$table->string('content_type')->index();
|
||||||
|
$table->unsignedBigInteger('content_id')->nullable()->index();
|
||||||
|
$table->unsignedInteger('views')->default(0);
|
||||||
|
$table->unsignedInteger('unique_visitors')->default(0);
|
||||||
|
$table->unsignedInteger('guest_views')->default(0);
|
||||||
|
$table->unsignedInteger('user_views')->default(0);
|
||||||
|
$table->unsignedInteger('subscriber_views')->default(0);
|
||||||
|
$table->unsignedInteger('engaged_views')->default(0);
|
||||||
|
$table->unsignedInteger('scroll_50')->default(0);
|
||||||
|
$table->unsignedInteger('scroll_75')->default(0);
|
||||||
|
$table->unsignedInteger('scroll_100')->default(0);
|
||||||
|
$table->unsignedInteger('likes')->default(0);
|
||||||
|
$table->unsignedInteger('saves')->default(0);
|
||||||
|
$table->unsignedInteger('prompt_copies')->default(0);
|
||||||
|
$table->unsignedInteger('negative_prompt_copies')->default(0);
|
||||||
|
$table->unsignedInteger('starts')->default(0);
|
||||||
|
$table->unsignedInteger('completions')->default(0);
|
||||||
|
$table->unsignedInteger('upgrade_clicks')->default(0);
|
||||||
|
$table->unsignedInteger('premium_preview_views')->default(0);
|
||||||
|
$table->unsignedInteger('search_impressions')->default(0);
|
||||||
|
$table->unsignedInteger('search_clicks')->default(0);
|
||||||
|
$table->unsignedInteger('bounce_count')->default(0);
|
||||||
|
$table->unsignedInteger('avg_engaged_seconds')->nullable();
|
||||||
|
$table->decimal('popularity_score', 12, 2)->default(0);
|
||||||
|
$table->decimal('conversion_score', 12, 2)->default(0);
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['date', 'content_type', 'content_id'], 'academy_metrics_daily_unique');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('academy_content_metrics_daily');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('academy_likes', function (Blueprint $table): void {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->string('content_type')->index();
|
||||||
|
$table->unsignedBigInteger('content_id')->index();
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['user_id', 'content_type', 'content_id'], 'academy_likes_unique');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('academy_likes');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('academy_saves', function (Blueprint $table): void {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->string('content_type')->index();
|
||||||
|
$table->unsignedBigInteger('content_id')->index();
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['user_id', 'content_type', 'content_id'], 'academy_saves_unique');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('academy_saves');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('academy_user_progress', function (Blueprint $table): void {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->foreignId('course_id')->nullable()->constrained('academy_courses')->nullOnDelete();
|
||||||
|
$table->foreignId('lesson_id')->nullable()->constrained('academy_lessons')->nullOnDelete();
|
||||||
|
$table->string('status')->index();
|
||||||
|
$table->unsignedTinyInteger('progress_percent')->default(0);
|
||||||
|
$table->timestamp('started_at')->nullable();
|
||||||
|
$table->timestamp('completed_at')->nullable();
|
||||||
|
$table->timestamp('last_seen_at')->nullable();
|
||||||
|
$table->json('metadata')->nullable();
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['user_id', 'course_id', 'lesson_id'], 'academy_user_progress_unique');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('academy_user_progress');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('academy_search_logs', function (Blueprint $table): void {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
|
||||||
|
$table->string('visitor_id', 120)->nullable()->index();
|
||||||
|
$table->string('query')->index();
|
||||||
|
$table->string('normalized_query')->index();
|
||||||
|
$table->unsignedInteger('results_count')->default(0)->index();
|
||||||
|
$table->string('clicked_content_type')->nullable()->index();
|
||||||
|
$table->unsignedBigInteger('clicked_content_id')->nullable()->index();
|
||||||
|
$table->json('filters')->nullable();
|
||||||
|
$table->boolean('is_logged_in')->default(false)->index();
|
||||||
|
$table->boolean('is_subscriber')->default(false)->index();
|
||||||
|
$table->boolean('is_bot')->default(false)->index();
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->index(['normalized_query', 'created_at']);
|
||||||
|
$table->index(['results_count', 'created_at']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('academy_search_logs');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('academy_prompt_templates', function (Blueprint $table): void {
|
||||||
|
if (! Schema::hasColumn('academy_prompt_templates', 'documentation')) {
|
||||||
|
$table->json('documentation')->nullable()->after('workflow_notes');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! Schema::hasColumn('academy_prompt_templates', 'placeholders')) {
|
||||||
|
$table->json('placeholders')->nullable()->after('documentation');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! Schema::hasColumn('academy_prompt_templates', 'helper_prompts')) {
|
||||||
|
$table->json('helper_prompts')->nullable()->after('placeholders');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! Schema::hasColumn('academy_prompt_templates', 'prompt_variants')) {
|
||||||
|
$table->json('prompt_variants')->nullable()->after('helper_prompts');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('academy_prompt_templates', function (Blueprint $table): void {
|
||||||
|
$columns = array_values(array_filter([
|
||||||
|
Schema::hasColumn('academy_prompt_templates', 'documentation') ? 'documentation' : null,
|
||||||
|
Schema::hasColumn('academy_prompt_templates', 'placeholders') ? 'placeholders' : null,
|
||||||
|
Schema::hasColumn('academy_prompt_templates', 'helper_prompts') ? 'helper_prompts' : null,
|
||||||
|
Schema::hasColumn('academy_prompt_templates', 'prompt_variants') ? 'prompt_variants' : null,
|
||||||
|
]));
|
||||||
|
|
||||||
|
if ($columns !== []) {
|
||||||
|
$table->dropColumn($columns);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('comment_reactions', function (Blueprint $table): void {
|
||||||
|
$table->index(['created_at', 'id'], 'idx_comment_reactions_created_at');
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::table('user_mentions', function (Blueprint $table): void {
|
||||||
|
$table->index(['created_at', 'id'], 'idx_user_mentions_created_at');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('user_mentions', function (Blueprint $table): void {
|
||||||
|
$table->dropIndex('idx_user_mentions_created_at');
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::table('comment_reactions', function (Blueprint $table): void {
|
||||||
|
$table->dropIndex('idx_comment_reactions_created_at');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('users', function (Blueprint $table) {
|
||||||
|
$table->string('stripe_id')->nullable()->index();
|
||||||
|
$table->string('pm_type')->nullable();
|
||||||
|
$table->string('pm_last_four', 4)->nullable();
|
||||||
|
$table->timestamp('trial_ends_at')->nullable();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('users', function (Blueprint $table) {
|
||||||
|
$table->dropIndex([
|
||||||
|
'stripe_id',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$table->dropColumn([
|
||||||
|
'stripe_id',
|
||||||
|
'pm_type',
|
||||||
|
'pm_last_four',
|
||||||
|
'trial_ends_at',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user