Skip to content

Commit

Permalink
Use shouldSend for NewComment notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Obi-Wana committed Dec 20, 2024
1 parent 8ada720 commit ed7a276
Show file tree
Hide file tree
Showing 7 changed files with 1,316 additions and 9 deletions.
23 changes: 14 additions & 9 deletions app/Http/Livewire/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,27 @@ final public function postComment(): void
case $this->model instanceof Ticket:
$ticket = $this->model;

if ($this->user->id !== $ticket->staff_id && $ticket->staff_id !== null) {
User::find($ticket->staff_id)?->notify(new NewComment($this->model, $comment));
}
// Notify assigned staff if needed
User::find($ticket->staff_id)?->notify(new NewComment($this->model, $comment));

if ($this->user->id !== $ticket->user_id) {
User::find($ticket->user_id)?->notify(new NewComment($this->model, $comment));
}
// Notify ticket creator if needed
User::find($ticket->user_id)?->notify(new NewComment($this->model, $comment));

break;
case $this->model instanceof Article:
User::find($this->model->user_id)?->notify(new NewComment($this->model, $comment));

break;
case $this->model instanceof Playlist:
User::find($this->model->user_id)?->notify(new NewComment($this->model, $comment));

break;
case $this->model instanceof TorrentRequest:
User::find($this->model->user_id)?->notify(new NewComment($this->model, $comment));

break;
case $this->model instanceof Torrent:
if ($this->user->id !== $this->model->user_id) {
User::find($this->model->user_id)?->notify(new NewComment($this->model, $comment));
}
User::find($this->model->user_id)?->notify(new NewComment($this->model, $comment));

break;
}
Expand Down
56 changes: 56 additions & 0 deletions app/Notifications/NewComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use App\Models\Ticket;
use App\Models\Torrent;
use App\Models\TorrentRequest;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;

Expand All @@ -47,6 +48,61 @@ public function via(object $notifiable): array
return ['database'];
}

/**
* Determine if the notification should be sent.
*
* @return bool
*/
public function shouldSend(User $notifiable): bool
{
// Evaluate general settings
if ($this->comment->user_id === $notifiable->id || $notifiable->notification?->block_notifications == 1) {
return false;
}

// Initialize target variables
$targetGroup = '';
$targetSetting = '';

// Evaluate model based settings
switch (true) {
case $this->model instanceof Torrent:
$targetGroup = 'json_torrent_groups';
$targetSetting = 'show_torrent_comment';

break;
case $this->model instanceof TorrentRequest:
$targetGroup = 'json_request_groups';
$targetSetting = 'show_request_comment';

break;
case $this->model instanceof Ticket:
return ! ($this->comment->staff_id === $notifiable->id && $this->comment->staff_id !== null)

Check failure on line 80 in app/Notifications/NewComment.php

View workflow job for this annotation

GitHub Actions / php 8.3 on ubuntu-22.04

Access to an undefined property App\Models\Comment::$staff_id.
;

case $this->model instanceof Playlist:
case $this->model instanceof Article:
return true;
case $this->model instanceof Collection:
break;
default:
// In the unlikely case none matches, fallback to true to not lose a notification
return true;
}

if (!$notifiable->notification?->$targetSetting) {
return false;
}

if (\is_array($notifiable->notification->$targetGroup)) {
// If the sender's group ID is found in the "Block all notifications from the selected groups" array,
// the expression will return false.
return !\in_array($this->comment->user->group->id, $notifiable->notification->$targetGroup, true);
}

return true;
}

/**
* Get the array representation of the notification.
*
Expand Down
174 changes: 174 additions & 0 deletions tests/Feature/Notifications/NewCommentArticleNotificationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

declare(strict_types=1);

/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <[email protected]>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/

use App\Http\Livewire\Comments;
use App\Models\Article;
use App\Models\Bot;
use App\Models\Chatroom;
use App\Models\Comment;
use App\Models\Group;
use App\Models\User;
use App\Models\UserNotification;
use App\Notifications\NewComment;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Livewire\Livewire;

uses(RefreshDatabase::class);

test('user comments on article creates a notification for staff', function (): void {
Notification::fake();

// Required for ChatRepository()
$this->seed(UsersTableSeeder::class);

$bot = Bot::factory()->create([
'command' => 'Systembot',
]);
$chat = Chatroom::factory()->create([
'name' => config('chat.system_chatroom'),
]);

$group = Group::factory()->create([
'can_comment' => true,
]);
$staffGroup = Group::factory()->create([
'can_comment' => true,
'is_modo' => true,
]);

$user = User::factory()->create([
'group_id' => $group->id,
'can_comment' => true,
]);
$staff = User::factory()->create([
'group_id' => $staffGroup->id,
'can_comment' => true,
]);

$staffNotificationSettings = UserNotification::factory()->for($staff)->create([
'block_notifications' => 0,
]);

$article = Article::factory()->for($staff)->create();

$commentText = 'This is a test comment';

Livewire::actingAs($user)
->test(Comments::class, ['model' => $article])
->set('newCommentState', $commentText)
->set('anon', false)
->call('postComment');

$this->assertEquals(1, Comment::count());

Notification::assertSentTo(
[$staff],
NewComment::class
);
Notification::assertCount(1);
});

test('staff comments on own article does not create a notification for staff', function (): void {
Notification::fake();

// Required for ChatRepository()
$this->seed(UsersTableSeeder::class);

$bot = Bot::factory()->create([
'command' => 'Systembot',
]);
$chat = Chatroom::factory()->create([
'name' => config('chat.system_chatroom'),
]);

$staffGroup = Group::factory()->create([
'can_comment' => true,
'is_modo' => true,
]);

$staff = User::factory()->create([
'group_id' => $staffGroup->id,
'can_comment' => true,
]);

$staffNotificationSettings = UserNotification::factory()->for($staff)->create([
'block_notifications' => 0,
]);

$article = Article::factory()->for($staff)->create();

$commentText = 'This is a test comment';

Livewire::actingAs($staff)
->test(Comments::class, ['model' => $article])
->set('newCommentState', $commentText)
->set('anon', false)
->call('postComment');

$this->assertEquals(1, Comment::count());

Notification::assertCount(0);
});

test('user comments on article does not a notification for staff user when all notifications are disabled', function (): void {
Notification::fake();

// Required for ChatRepository()
$this->seed(UsersTableSeeder::class);

$bot = Bot::factory()->create([
'command' => 'Systembot',
]);
$chat = Chatroom::factory()->create([
'name' => config('chat.system_chatroom'),
]);

$group = Group::factory()->create([
'can_comment' => true,
]);
$staffGroup = Group::factory()->create([
'can_comment' => true,
'is_modo' => true,
]);

$user = User::factory()->create([
'group_id' => $group->id,
'can_comment' => true,
]);
$staff = User::factory()->create([
'group_id' => $staffGroup->id,
'can_comment' => true,
]);

$staffNotificationSettings = UserNotification::factory()->for($staff)->create([
'block_notifications' => 1,
]);

$article = Article::factory()->for($staff)->create();

$commentText = 'This is a test comment';

Livewire::actingAs($user)
->test(Comments::class, ['model' => $article])
->set('newCommentState', $commentText)
->set('anon', false)
->call('postComment');

$this->assertEquals(1, Comment::count());

Notification::assertCount(0);
});
Loading

0 comments on commit ed7a276

Please sign in to comment.