Skip to content
Open
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
5 changes: 5 additions & 0 deletions app/Actions/CreateCommitPastel.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public function handle(User $user, Commit $commit): bool

$recipientCommit->increment('pasteis_count');
User::query()->whereKey($commit->user_id)->increment('pasteis_received');
DB::table('commit_pasteis')->upsert(
['commit_id' => $commit->id, 'user_id' => $sender->id, 'count' => 1, 'created_at' => now(), 'updated_at' => now()],
['commit_id', 'user_id'],
['count' => DB::raw('commit_pasteis.count + 1'), 'updated_at' => now()],
);

// The count is everyone's to see, and the giver's other devices learn what they have left to give.
if ($recipientCommit->status === CommitStatus::Approved) {
Expand Down
20 changes: 20 additions & 0 deletions app/Http/Controllers/CommitPastelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,33 @@

use App\Actions\CreateCommitPastel;
use App\Http\Requests\CreateCommitPastelRequest;
use App\Http\Requests\ListCommitPasteisRequest;
use App\Models\Commit;
use App\Models\User;
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Http\JsonResponse;

final readonly class CommitPastelController
{
/**
* Who gave the commit pastéis and how many each, latest first. People across a block with the author are left out, like everywhere else.
*/
public function index(ListCommitPasteisRequest $request, #[CurrentUser] User $user, Commit $commit): JsonResponse
{
$givers = $commit->pastelGivers()
->select(['users.*', 'commit_pasteis.count as pasteis'])
->whereNotIn('users.id', $user->idsAcrossBlocks())
->latest('commit_pasteis.updated_at')
->latest('users.id')
->cursorPaginate(20);

foreach ($givers->items() as $giver) {
$giver->makeHidden('pivot');
}

return response()->json($givers);
}

public function store(CreateCommitPastelRequest $request, #[CurrentUser] User $user, Commit $commit, CreateCommitPastel $action): JsonResponse
{
$awarded = $action->handle($user, $commit);
Expand Down
35 changes: 35 additions & 0 deletions app/Http/Requests/ListCommitPasteisRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use App\Models\Commit;
use App\Models\User;
use Illuminate\Auth\Access\Response;
use Illuminate\Foundation\Http\FormRequest;

final class ListCommitPasteisRequest extends FormRequest
{
/**
* Only authors see who gave their commit pastéis, as everyone else only sees how many there are.
*/
public function authorize(): Response
{
$user = $this->user();
assert($user instanceof User);

$commit = $this->route('commit');
assert($commit instanceof Commit);

return $commit->isOwnedBy($user) ? Response::allow() : Response::denyAsNotFound();
}

/**
* @return array{}
*/
public function rules(): array
{
return [];
}
}
11 changes: 11 additions & 0 deletions app/Models/Commit.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* @property-read User|null $user
* @property-read Profile|null $author
* @property-read Collection<int, User> $stars
* @property-read Collection<int, User> $pastelGivers
* @property-read Commit|null $parent
* @property-read Collection<int, Commit> $replies
* @property-read int $replies_count
Expand Down Expand Up @@ -199,6 +200,16 @@ public function stars(): BelongsToMany
return $this->belongsToMany(User::class, 'stars')->withTimestamps();
}

/**
* Who gave the commit pastéis, with how many each gave as the pivot's count.
*
* @return BelongsToMany<User, $this>
*/
public function pastelGivers(): BelongsToMany
{
return $this->belongsToMany(User::class, 'commit_pasteis')->withPivot('count')->withTimestamps();
}

/**
* @return BelongsToMany<Headline, $this>
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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
{
/**
* Who gave a commit its pastéis, and how many each, so its author can see it. Pastéis given before this only count.
*/
public function up(): void
{
Schema::create('commit_pasteis', function (Blueprint $table): void {
$table->foreignUuid('commit_id')->constrained()->cascadeOnDelete();
$table->foreignUuid('user_id')->constrained()->cascadeOnDelete();
$table->unsignedInteger('count')->default(0);
$table->timestamps();

$table->primary(['commit_id', 'user_id']);
$table->index('user_id');
$table->index(['commit_id', 'updated_at']);
});
}
};
56 changes: 55 additions & 1 deletion resources/js/components/commit-pastel-button.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { router, useHttp, usePage } from '@inertiajs/react';
import { useEffect, useEffectEvent, useRef } from 'react';
import type { ReactNode } from 'react';
import { toast } from 'sonner';
import { store } from '@/actions/App/Http/Controllers/CommitPastelController';
import { ActionTooltip } from '@/components/action-tooltip';
import { CommitPastelGiversDialog } from '@/components/commit-pastel-givers-dialog';
import { useCommitNotFound } from '@/hooks/use-deleted-resources';
import { useNow } from '@/hooks/use-now';
import { refreshSharedState, useSharedState } from '@/hooks/use-shared-state';
Expand Down Expand Up @@ -71,7 +73,7 @@ export function CommitPastelButton({

const userId = auth.user.id;
const isOwn = commit.author.id === userId;
const readOnly = isOwn || commit.status !== 'approved';
const readOnly = commit.status !== 'approved';
const label = `${formatExactCount(count)} ${count === 1 ? 'pastel' : 'pastéis'}`;

const award = async () => {
Expand Down Expand Up @@ -185,6 +187,19 @@ export function CommitPastelButton({
className,
);

if (isOwn) {
return (
<PastelCount
commit={commit}
count={count}
label={label}
className={classes}
>
{content}
</PastelCount>
);
}

return (
<ActionTooltip
label={
Expand Down Expand Up @@ -219,3 +234,42 @@ export function CommitPastelButton({
</ActionTooltip>
);
}

/**
* Authors can't give their own commits pastéis, so they see how many others gave, and who gave them.
*/
function PastelCount({
commit,
count,
label,
className,
children,
}: {
commit: Commit;
count: number;
label: string;
className: string;
children: ReactNode;
}) {
const description =
count === 0 ? 'No pastéis yet' : `${label}, show who gave them`;

return (
<ActionTooltip label={description}>
<CommitPastelGiversDialog commit={commit} count={count}>
<button
type="button"
disabled={count === 0}
aria-label={description}
className={cn(
className,
'transition-colors hover:bg-accent hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring disabled:cursor-default disabled:opacity-50 disabled:hover:bg-transparent disabled:hover:text-muted-foreground',
)}
data-test="own-pastel-count"
>
{children}
</button>
</CommitPastelGiversDialog>
</ActionTooltip>
);
}
Loading