Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions app/Actions/CreateWrappedNotifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace App\Actions;

use App\Enums\CommitStatus;
use App\Enums\NotificationType;
use App\Enums\UserUpdate;
use App\Events\UsersUpdated;
use App\Models\Notification;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;

final readonly class CreateWrappedNotifications
{
/**
* Tells every handle with an approved commit that year that their Wrapped is ready, linking them to their own
* review. One row per handle per year, so running it again changes nothing. Never to bots. Safe to call more
* than once.
*/
public function handle(int $year): int
{
$recipientIds = User::query()
->whereNotIn('github_id', User::BOT_GITHUB_IDS)
->whereHas(
'commits',
fn (Builder $commits): Builder => $commits
->where('status', CommitStatus::Approved)
->whereYear('created_at', $year),
)
->pluck('users.id');

$now = now();

$notifications = [];

foreach ($recipientIds as $recipientId) {
$notifications[] = [
'id' => Str::uuid7()->toString(),
'recipient_id' => $recipientId,
'actor_id' => $recipientId,
'type' => NotificationType::WrappedReady->value,
'commit_id' => null,
'key' => 'wrapped:'.$year,
'created_at' => $now,
'updated_at' => $now,
];
}

foreach (array_chunk($notifications, 500) as $chunk) {
Notification::query()->insertOrIgnore($chunk);
}

if ($recipientIds->isNotEmpty()) {
event(new UsersUpdated($recipientIds->all(), UserUpdate::Notifications));
}

return count($notifications);
}
}
32 changes: 32 additions & 0 deletions app/Console/Commands/NotifyWrappedCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Actions\CreateWrappedNotifications;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;

#[Signature('wrapped:notify {--year= : The year to wrap, defaults to the current year}')]
#[Description('Tell every handle with a commit that year that their Wrapped is ready')]
final class NotifyWrappedCommand extends Command
{
public function handle(CreateWrappedNotifications $createWrappedNotifications): int
{
$year = (int) ($this->option('year') ?? date('Y'));

if ($year !== (int) date('Y')) {
$this->components->error('Wrapped notifications can only be queued for the current year.');

return self::FAILURE;
}

$count = $createWrappedNotifications->handle($year);

$this->components->info("Wrapped notifications queued for {$count} handles for {$year}.");

return self::SUCCESS;
}
}
1 change: 1 addition & 0 deletions app/Enums/NotificationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ enum NotificationType: string
case LeveledUp = 'leveled_up';
case FirstCommit = 'first_commit';
case TimedOut = 'timed_out';
case WrappedReady = 'wrapped_ready';
}
83 changes: 83 additions & 0 deletions app/Http/Controllers/YearInReviewController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Models\Profile;
use App\Models\User;
use App\Queries\YearInReviewQuery;
use App\Services\PageMetadata;
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

final readonly class YearInReviewController
{
public function show(#[CurrentUser] ?User $currentUser, Profile $profile, YearInReviewQuery $review, PageMetadata $metadata, Request $request): Response
{
$user = $profile->user;
$currentYear = (int) now()->year;
$requestedYear = $request->integer('year');
$year = $requestedYear > 0 && $requestedYear <= $currentYear ? $requestedYear : $currentYear;

abort_unless($user instanceof User, 404);

$parameters = ['profile' => $profile->handle];

if ($year !== $currentYear) {
$parameters['year'] = $year;
}

$shareUrl = route('wrapped.show', $parameters);

// Wrapped drops with the notification, every December 3rd at 09:00.
$unlocksAt = now()->setDate($year, 12, 3)->setTime(9, 0);

$hasPreviousYear = $user->commits()
->whereBetween('commits.created_at', [
now()->setDate($year - 1, 1, 1)->startOfDay(),
now()->setDate($year - 1, 12, 31)->endOfDay(),
])
->exists();

if (now()->lt($unlocksAt)) {
return Inertia::render('wrapped/show', [
'handle' => $profile->handle,
'name' => $user->name,
'avatarUrl' => $user->avatar_url,
'stats' => null,
'shareUrl' => $shareUrl,
'locked' => true,
'unlocksAt' => $unlocksAt->toIso8601String(),
'year' => $year,
'currentYear' => $currentYear,
'hasPreviousYear' => $hasPreviousYear,
])->withViewData(['preview' => $metadata->page(
"{$profile->handle}'s {$year} Wrapped - /dev/null",
"The {$year} Wrapped for @{$profile->handle} unwraps December 3rd.",
$shareUrl,
)]);
}

$stats = $review->get($user, $currentUser, $year);

return Inertia::render('wrapped/show', [
'handle' => $profile->handle,
'name' => $user->name,
'avatarUrl' => $user->avatar_url,
'stats' => $stats,
'shareUrl' => $shareUrl,
'locked' => false,
'unlocksAt' => null,
'year' => $year,
'currentYear' => $currentYear,
'hasPreviousYear' => $hasPreviousYear,
])->withViewData(['preview' => $metadata->page(
"{$profile->handle}'s {$year} in review - /dev/null",
"What @{$profile->handle} shipped in {$year}: {$stats['commits']} commits, {$stats['starsEarned']} stars and a {$stats['longestStreak']}-day streak.",
$shareUrl,
)]);
}
}
2 changes: 1 addition & 1 deletion app/Queries/NotificationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function relevant(): self
->whereColumn('organization_members.organization_id', 'notifications.organization_id')
->whereColumn('organization_members.user_id', 'notifications.recipient_id')))
->offset(0)))
->orWhereIn('type', [NotificationType::LeveledUp, NotificationType::TimedOut]))
->orWhereIn('type', [NotificationType::LeveledUp, NotificationType::TimedOut, NotificationType::WrappedReady]))
->withoutSilencedActors();
}

Expand Down
Loading