-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #368 from pacoorozco/issue-359
Implement notifications for unlocked badges
- Loading branch information
Showing
13 changed files
with
303 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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,50 @@ | ||
<?php | ||
/** | ||
* Gamify - Gamification platform to implement any serious game mechanic. | ||
* | ||
* Copyright (c) 2018 by Paco Orozco <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* Some rights reserved. See LICENSE and AUTHORS files. | ||
* | ||
* @author Paco Orozco <[email protected]> | ||
* @copyright 2018 Paco Orozco | ||
* @license GPL-3.0 <http://spdx.org/licenses/GPL-3.0> | ||
* | ||
* @link https://github.com/pacoorozco/gamify-laravel | ||
*/ | ||
|
||
namespace Gamify\Http\Controllers; | ||
|
||
use Gamify\Models\User; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class MarkNotificationAsReadController extends Controller | ||
{ | ||
public function __invoke(Request $request): Response | ||
{ | ||
/** @var User $user */ | ||
$user = User::findOrFail(Auth::id()); | ||
|
||
$user | ||
->unreadNotifications | ||
->when($request->input('id'), function ($query) use ($request) { | ||
return $query->where('id', $request->input('id')); | ||
}) | ||
->markAsRead(); | ||
|
||
return response() | ||
->noContent(); | ||
} | ||
} |
This file contains 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 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 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,36 @@ | ||
<?php | ||
|
||
namespace Gamify\Notifications; | ||
|
||
use Gamify\Models\Badge; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Notifications\Notification; | ||
|
||
class BadgeUnlocked extends Notification | ||
{ | ||
use Queueable; | ||
|
||
public function __construct( | ||
private Badge $badge, | ||
) { | ||
// | ||
} | ||
|
||
public function via(mixed $notifiable): array | ||
{ | ||
return ['database']; | ||
} | ||
|
||
public function toDatabase(mixed $notifiable): array | ||
{ | ||
return $this->toArray($notifiable); | ||
} | ||
|
||
public function toArray(mixed $notifiable): array | ||
{ | ||
return [ | ||
'title' => __('notifications.badge_unlocked_title'), | ||
'message' => __('notifications.badge_unlocked_message', ['name' => $this->badge->name, 'url' => route('account.index')]), | ||
]; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
database/migrations/2022_10_11_150317_create_notifications_table.php
This file contains 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,35 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('notifications', function (Blueprint $table) { | ||
$table->uuid('id')->primary(); | ||
$table->string('type'); | ||
$table->morphs('notifiable'); | ||
$table->text('data'); | ||
$table->timestamp('read_at')->nullable(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('notifications'); | ||
} | ||
}; |
This file contains 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,44 @@ | ||
<?php | ||
/** | ||
* Gamify - Gamification platform to implement any serious game mechanic. | ||
* | ||
* Copyright (c) 2018 by Paco Orozco <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* Some rights reserved. See LICENSE and AUTHORS files. | ||
* | ||
* @author Paco Orozco <[email protected]> | ||
* @copyright 2018 Paco Orozco | ||
* @license GPL-3.0 <http://spdx.org/licenses/GPL-3.0> | ||
* | ||
* @link https://github.com/pacoorozco/gamify-laravel | ||
*/ | ||
|
||
return [ | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Pagination Language Lines | ||
|-------------------------------------------------------------------------- | ||
| | ||
| The following language lines are used by the paginator library to build | ||
| the simple pagination links. You are free to change them to anything | ||
| you want to customize your views to better match your application. | ||
| | ||
*/ | ||
|
||
'mark_as_read' => 'Mark as read', | ||
|
||
'badge_unlocked_title' => 'Badge unlocked', | ||
'badge_unlocked_message' => 'Congrats! You unlocked the badge \'<strong>:name</strong>\'. Visit <a href=":url">your profile</a> to check it.', | ||
|
||
]; |
This file contains 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 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 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 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 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 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
82 changes: 82 additions & 0 deletions
82
tests/Feature/Http/Controllers/MarkNotificationAsReadControllerTest.php
This file contains 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,82 @@ | ||
<?php | ||
/** | ||
* Gamify - Gamification platform to implement any serious game mechanic. | ||
* | ||
* Copyright (c) 2018 by Paco Orozco <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* Some rights reserved. See LICENSE and AUTHORS files. | ||
* | ||
* @author Paco Orozco <[email protected]> | ||
* @copyright 2018 Paco Orozco | ||
* @license GPL-3.0 <http://spdx.org/licenses/GPL-3.0> | ||
* | ||
* @link https://github.com/pacoorozco/gamify-laravel | ||
*/ | ||
|
||
namespace Tests\Feature\Http\Controllers; | ||
|
||
use Gamify\Models\Badge; | ||
use Gamify\Models\User; | ||
use Gamify\Notifications\BadgeUnlocked; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Tests\TestCase; | ||
|
||
class MarkNotificationAsReadControllerTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
/** @test */ | ||
public function it_should_mark_a_notification_as_read(): void | ||
{ | ||
/** @var User $user */ | ||
$user = User::factory()->create(); | ||
|
||
/** @var Badge $badge */ | ||
$badge = Badge::factory()->create(); | ||
|
||
// Let's add two notifications. | ||
$user->notify(new BadgeUnlocked($badge)); | ||
$user->notify(new BadgeUnlocked($badge)); | ||
|
||
// We want to mark only one as read. | ||
$notification = $user->unreadNotifications->first(); | ||
|
||
$this | ||
->actingAs($user) | ||
->patch(route('notifications.read'), ['id' => $notification->id]) | ||
->assertNoContent(); | ||
|
||
$this->assertCount(1, $user->refresh()->unreadNotifications); | ||
} | ||
|
||
/** @test */ | ||
public function it_should_mark_all_notifications_as_read(): void | ||
{ | ||
/** @var User $user */ | ||
$user = User::factory()->create(); | ||
|
||
/** @var Badge $badge */ | ||
$badge = Badge::factory()->create(); | ||
|
||
// Let's add two notifications. | ||
$user->notify(new BadgeUnlocked($badge)); | ||
$user->notify(new BadgeUnlocked($badge)); | ||
|
||
$this | ||
->actingAs($user) | ||
->patch(route('notifications.read')) | ||
->assertNoContent(); | ||
|
||
$this->assertCount(0, $user->refresh()->unreadNotifications); | ||
} | ||
} |