Repository navigation
feat: watch someone to get notified of each new commit #1267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
hbenabdallah
wants to merge
3
commits into
dev-null-sh:production
from
hbenabdallah:feat/watch-commits
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Actions; | ||
|
|
||
| use App\Enums\CommitStatus; | ||
| use App\Enums\NotificationType; | ||
| use App\Models\Commit; | ||
| use App\Models\Notification; | ||
| use App\Queries\SilencedActorQuery; | ||
| use Illuminate\Support\Facades\DB; | ||
| use Illuminate\Support\Str; | ||
|
|
||
| final readonly class CreateWatchNotifications | ||
| { | ||
| /** | ||
| * Notifies everyone watching the author of each new commit, but only once it is approved, so nobody hears about a | ||
| * commit they cannot see yet. Replies are left out, as watching is about what someone posts, not every | ||
| * conversation they join. Never for the people a commit mentions or quotes, who already hear about it as a mention | ||
| * or a quote. Nobody hears from people they mute either. Safe to call more than once. | ||
| */ | ||
| public function handle(Commit ...$commits): void | ||
| { | ||
| $commits = Commit::query() | ||
| ->whereKey(array_map(fn (Commit $commit): string => $commit->id, $commits)) | ||
| ->where('status', CommitStatus::Approved) | ||
| ->whereNull('parent_id') | ||
| ->whereNotNull('user_id') | ||
| ->with('quotedCommit') | ||
| ->get(); | ||
|
|
||
| if ($commits->isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| $silenced = new SilencedActorQuery('follows.follower_id', 'follows.followed_id'); | ||
|
|
||
| $watchersByAuthor = DB::table('follows') | ||
| ->whereIn('followed_id', $commits->pluck('user_id')->unique()->values()->all()) | ||
| ->where('watching', true) | ||
| ->whereNotExists($silenced->mutes(...)) | ||
| ->whereNotExists($silenced->blocking(...)) | ||
| ->whereNotExists($silenced->blockers(...)) | ||
| ->get(['follower_id', 'followed_id']) | ||
| ->groupBy('followed_id'); | ||
|
|
||
| $notifications = []; | ||
|
|
||
| foreach ($commits as $commit) { | ||
| $alreadyNotified = [...array_values($commit->mentions ?? []), $commit->quotedCommit?->user_id]; | ||
|
|
||
| foreach ($watchersByAuthor->get($commit->user_id, []) as $follow) { | ||
| if (in_array($follow->follower_id, $alreadyNotified, true)) { | ||
| continue; | ||
| } | ||
|
|
||
| $notifications[] = [ | ||
| 'id' => Str::uuid7()->toString(), | ||
| 'recipient_id' => $follow->follower_id, | ||
| 'actor_id' => $commit->user_id, | ||
| 'type' => NotificationType::Committed->value, | ||
| 'commit_id' => $commit->id, | ||
| 'key' => 'committed:'.$commit->id, | ||
| 'created_at' => now(), | ||
| 'updated_at' => now(), | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| // A popular author can have more watchers than one statement can bind. | ||
| foreach (array_chunk($notifications, 500) as $chunk) { | ||
| Notification::query()->insertOrIgnore($chunk); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Actions; | ||
|
|
||
| use App\Models\User; | ||
|
|
||
| final readonly class UpdateUserWatch | ||
| { | ||
| /** | ||
| * Only a follow can be watched, so this does nothing for someone the user doesn't follow. | ||
| */ | ||
| public function handle(User $user, User $watched, bool $watching): void | ||
| { | ||
| $user->following()->newPivotQuery() | ||
| ->where('followed_id', $watched->id) | ||
| ->update(['watching' => $watching, 'updated_at' => now()]); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Controllers; | ||
|
|
||
| use App\Actions\UpdateUserWatch; | ||
| use App\Http\Requests\CreateUserWatchRequest; | ||
| use App\Http\Requests\DeleteUserWatchRequest; | ||
| use App\Models\User; | ||
| use Illuminate\Container\Attributes\CurrentUser; | ||
| use Illuminate\Http\RedirectResponse; | ||
| use Inertia\Inertia; | ||
|
|
||
| final readonly class UserWatchController | ||
| { | ||
| public function store(CreateUserWatchRequest $request, #[CurrentUser] User $currentUser, User $user, UpdateUserWatch $action): RedirectResponse | ||
| { | ||
| $action->handle($currentUser, $user, watching: true); | ||
|
|
||
| Inertia::flash('toast', [ | ||
| 'type' => 'success', | ||
| 'message' => "You'll be notified when {$user->mention()} commits.", | ||
| ]); | ||
|
|
||
| return back(); | ||
| } | ||
|
|
||
| public function destroy(DeleteUserWatchRequest $request, #[CurrentUser] User $currentUser, User $user, UpdateUserWatch $action): RedirectResponse | ||
| { | ||
| $action->handle($currentUser, $user, watching: false); | ||
|
|
||
| Inertia::flash('toast', [ | ||
| 'type' => 'success', | ||
| 'message' => "You won't be notified when {$user->mention()} commits.", | ||
| ]); | ||
|
|
||
| return back(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Requests; | ||
|
|
||
| use App\Models\User; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| final class CreateUserWatchRequest extends FormRequest | ||
| { | ||
| public function authorize(): bool | ||
| { | ||
| $user = $this->user(); | ||
| assert($user instanceof User); | ||
|
|
||
| $watched = $this->route('user'); | ||
| assert($watched instanceof User); | ||
|
|
||
| return $user->isFollowing($watched); | ||
| } | ||
|
|
||
| /** | ||
| * @return array{} | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return []; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Requests; | ||
|
|
||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| final class DeleteUserWatchRequest extends FormRequest | ||
| { | ||
| public function authorize(): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * @return array{} | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return []; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
database/migrations/2026_10_28_120000_add_watching_to_follows_table.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?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 | ||
| { | ||
| /** | ||
| * Watching lives on the follow, so unfollowing someone, or a block ending the follow, stops watching them too. | ||
| */ | ||
| public function up(): void | ||
| { | ||
| Schema::table('follows', function (Blueprint $table): void { | ||
| $table->boolean('watching')->default(false); | ||
| }); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { Link } from '@inertiajs/react'; | ||
| import { Bell, BellRing } from 'lucide-react'; | ||
| import UserWatchController from '@/actions/App/Http/Controllers/UserWatchController'; | ||
| import { useIsFollowing } from '@/components/follow-button'; | ||
| import { Button } from '@/components/ui/button'; | ||
| import type { User } from '@/types'; | ||
|
|
||
| /** | ||
| * Watching is part of a follow, so the bell only shows for someone you follow, and goes away with the follow. | ||
| */ | ||
| export function WatchButton({ | ||
| user, | ||
| isFollowing: initialIsFollowing, | ||
| isWatching, | ||
| }: { | ||
| user: Pick<User, 'id' | 'handle'>; | ||
| isFollowing: boolean; | ||
| isWatching: boolean; | ||
| }) { | ||
| const [isFollowing] = useIsFollowing(user, initialIsFollowing); | ||
|
|
||
| if (!isFollowing) { | ||
| return null; | ||
| } | ||
|
|
||
| const label = isWatching | ||
| ? `Stop notifications for @${user.handle}'s commits` | ||
| : `Get notified when @${user.handle} commits`; | ||
|
|
||
| return ( | ||
| <Button | ||
| variant="outline" | ||
| size="icon" | ||
| className={ | ||
| isWatching ? 'text-primary hover:text-primary' : undefined | ||
| } | ||
| asChild | ||
| > | ||
| <Link | ||
| href={ | ||
| isWatching | ||
| ? UserWatchController.destroy(user) | ||
| : UserWatchController.store(user) | ||
| } | ||
| as="button" | ||
| preserveScroll | ||
| only={['isWatching']} | ||
| aria-label={label} | ||
| aria-pressed={isWatching} | ||
| title={label} | ||
| data-test="watch-button" | ||
| > | ||
| {isWatching ? <BellRing /> : <Bell />} | ||
| </Link> | ||
| </Button> | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.