Skip to content
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

notification management #2

Open
wants to merge 1 commit into
base: production
Choose a base branch
from
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
1 change: 1 addition & 0 deletions app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function edit(Request $request): View
{
return view('profile.edit', [
'user' => $request->user(),
'settings' => $request->user()->settings,
]);
}

Expand Down
24 changes: 24 additions & 0 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Illuminate\Support\Facades\Redirect;

class SettingsController extends Controller
{
/**
* Update user's settings
*/
public function update(Request $request): RedirectResponse
{
$settings = $request->user()->settings;
$settings['notification']['wishlist-comment-created'] = (bool) $request->request->get('notification-wishlist-comment-created');
$settings['notification']['wish-created'] = (bool) $request->request->get('notification-wish-created');
$request->user()->settings = $settings;
$request->user()->save();

return Redirect::route('profile.edit')->with('status', 'settings-updated');
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/WishController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function store(Request $request, Wishlist $wishlist)
'description' => ['nullable', 'string', 'max:2000'],
]));

Notification::send($wishlist->viewers, new WishCreatedNotification($wish));
Notification::send($wishlist->viewers->filter(fn ($user) => $user->settings['notification']['wish-created']), new WishCreatedNotification($wish));

return to_route('wishlists.show', $wishlist);
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/WishlistCommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function store(Request $request, Wishlist $wishlist)

$comment = $wishlist->addComment($request->comment, $request->user());

$watchers = $wishlist->viewers->push($wishlist->user)->reject(fn ($user) => $user->is($request->user()));
$watchers = $wishlist->viewers->push($wishlist->user)->reject(fn ($user) => $user->is($request->user()) || !$user->settings['notification']['wishlist-comment-created']);

Notification::send($watchers, new WishlistCommentCreatedNotification($comment));

Expand Down
8 changes: 7 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class User extends Authenticatable
'name',
'email',
'password',
'settings'
];

protected $hidden = [
Expand All @@ -26,6 +27,11 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'settings' => 'array'
];

protected $attributes = [
'settings' => '{"notification":{"wishlist-comment-created":true,"wish-created":true}}'
];

public function wishlists()
Expand All @@ -41,7 +47,7 @@ public function joinedWishlists()
protected function avatarUrl(): Attribute
{
return Attribute::make(
get: fn ($value, $attributes) => "https://unavatar.io/{$attributes['email']}?".http_build_query([
get: fn ($value, $attributes) => "https://unavatar.io/{$attributes['email']}?" . http_build_query([
'fallback' => "https://ui-avatars.com/api/{$attributes['name']}/32/bae6fd/0c4a6e",
])
)->shouldCache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function up(): void
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->json('settings')->nullable();
$table->rememberToken();
$table->timestamps();
});
Expand Down
6 changes: 6 additions & 0 deletions resources/views/profile/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
</div>
</div>

<div class="p-4 sm:p-8 bg-white shadow rounded-lg">
<div class="max-w-xl">
@include('profile.partials.update-settings-form')
</div>
</div>

<div class="p-4 sm:p-8 bg-white shadow rounded-lg">
<div class="max-w-xl">
@include('profile.partials.delete-user-form')
Expand Down
43 changes: 43 additions & 0 deletions resources/views/profile/partials/update-settings-form.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<section>
<header>
<h2 class="text-lg font-medium text-gray-900">
{{ __('Settings') }}
</h2>

<p class="mt-1 text-sm text-gray-600">
{{ __('Configure which notifications you want to receive') }}
</p>
</header>
<x-form method="put" action="{{ route('settings.update') }}" class="mt-6 space-y-6">
<div class="flex flex-row flex-wrap justify-start items-center gap-4">
<x-field class="flex flex-row justify-start items-center gap-1">
<x-input name="notification-wish-created" type="checkbox" id="notification-wish-created" value="notification-wish-created" style="width: 1.4rem; height: 1.4rem"
:checked="$settings['notification']['wish-created']"
/>
<x-label for="notification-wish-created" :value="__('Notify on wish created')" class="!mt-0"/>
</x-field>

<x-field class="flex flex-row justify-start items-center gap-1">
<x-input name="notification-wishlist-comment-created" type="checkbox" id="notification-wishlist-comment-created" style="width: 1.4rem; height: 1.4rem" value="notification-wishlist-comment-created"
:checked="$settings['notification']['wishlist-comment-created']"
/>
<x-label for="notification-wishlist-comment-created" :value="__('Notify on wishlist comment')" class="!mt-0"/>
</x-field>
</div>

<div class="flex items-center gap-4">
<x-button-primary>{{ __('Save changes') }}</x-button-primary>

@if (session('status') === 'settings-updated')
<p
x-data="{ show: true }"
x-show="show"
x-transition
x-init="setTimeout(() => show = false, 2000)"
class="text-sm text-gray-600"
>{{ __('Saved.') }}</p>
@endif
</div>
</x-form>
</section>

2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Http\Controllers\Guest\GuestWishController;
use App\Http\Controllers\Guest\GuestWishlistController;
use App\Http\Controllers\Guest\GuestSortWishlistController;
use App\Http\Controllers\SettingsController;
use App\Models\Wishlist;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
Expand All @@ -36,6 +37,7 @@
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
Route::put('/settings', [SettingsController::class, 'update'])->name('settings.update');

Route::get('/wishlists', [WishlistController::class, 'index'])->name('wishlists.index');
Route::get('/wishlists/create', [WishlistController::class, 'create'])->name('wishlists.create')->can('create', Wishlist::class);
Expand Down
6 changes: 0 additions & 6 deletions tests/Feature/GuestWishlistTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
<?php

const DEFAULT_WISHLIST = [
'name' => 'My wishlist',
'id' => 0,
'wishes' => []
];

test('guest see the default wishlist', function() {
$response = $this->get(route('guests.wishlists.show'));

Expand Down
20 changes: 20 additions & 0 deletions tests/Feature/ProfileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,23 @@

$this->assertNotNull($user->fresh());
});

test('settings can be updated', function () {
$user = User::factory()->create();

$response = $this
->actingAs($user)
->put(route('settings.update'), [
'notification-wishlist-comment-created' => 'notification-wishlist-comment-created',
'notification-wish-created' => null,
]);

$response
->assertSessionHasNoErrors()
->assertRedirect(route('profile.edit'));

$user->refresh();

$this->assertSame(true, $user->settings['notification']['wishlist-comment-created']);
$this->assertSame(false, $user->settings['notification']['wish-created']);
});
26 changes: 26 additions & 0 deletions tests/Feature/WishlistNotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,29 @@ function (WishCreatedNotification $notification) {
}
);
});

test('viewers are not notified when a wish is added to a wishlist if settings is off', function () {
Notification::fake();

$wishlist = Wishlist::factory()->create();
$viewer = User::factory()->create();
$settings = $viewer->settings;
$settings['notification']['wish-created'] = false;
$viewer->settings = $settings;
$viewer->save();
$wishlist->viewers()->attach($viewer);

$this->actingAs($wishlist->user);
$this->post(route('wishes.create', $wishlist), [
'name' => 'New Wish',
'url' => 'https://imacrayon.com',
'description' => 'Test description.',
]);

Notification::assertNotSentTo(
$viewer,
function (WishCreatedNotification $notification) {
return $notification->wish->id === Wish::first()->id;
}
);
});