Skip to content

(Add) Use shouldSend for notifications #4374

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

Merged
Merged
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
4 changes: 1 addition & 3 deletions app/Helpers/TorrentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ public static function approveHelper(int $id): void

if (!$torrent->anon && $uploader !== null) {
foreach ($uploader->followers()->get() as $follower) {
if ($follower->acceptsNotification($uploader, $follower, 'following', 'show_following_upload')) {
$follower->notify(new NewUpload('follower', $torrent));
}
$follower->notify(new NewUpload('follower', $torrent));
}
}

Expand Down
4 changes: 1 addition & 3 deletions app/Http/Controllers/ApprovedRequestFillController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ public function store(Request $request, TorrentRequest $torrentRequest): \Illumi
);
}

if ($filler->acceptsNotification($approver, $filler, 'request', 'show_request_fill_approve')) {
$filler->notify(new NewRequestFillApprove($torrentRequest));
}
$filler->notify(new NewRequestFillApprove($torrentRequest));

return to_route('requests.show', ['torrentRequest' => $torrentRequest])
->with('success', \sprintf(trans('request.approved-user'), $torrentRequest->name, $torrentRequest->filled_anon ? 'Anonymous' : $filler->username));
Expand Down
4 changes: 1 addition & 3 deletions app/Http/Controllers/BountyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ public function store(StoreTorrentRequestBountyRequest $request, TorrentRequest

$requester = $torrentRequest->user;

if ($requester->acceptsNotification($request->user(), $requester, 'request', 'show_request_bounty')) {
$requester->notify(new NewRequestBounty($bounty));
}
$requester->notify(new NewRequestBounty($bounty));

return to_route('requests.show', ['torrentRequest' => $torrentRequest])
->with('success', trans('request.added-bonus'));
Expand Down
12 changes: 4 additions & 8 deletions app/Http/Controllers/ClaimController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ public function store(StoreTorrentRequestClaimRequest $request, TorrentRequest $

$requester = $torrentRequest->user;

if ($requester->acceptsNotification($request->user(), $requester, 'request', 'show_request_claim')) {
$requester->notify(new NewRequestClaim($claim));
}
$requester->notify(new NewRequestClaim($claim));

return to_route('requests.show', ['torrentRequest' => $torrentRequest])
->with('success', trans('request.claimed-success'));
Expand All @@ -64,18 +62,16 @@ public function destroy(Request $request, TorrentRequest $torrentRequest, Torren
{
abort_unless($request->user()->group->is_modo || $request->user()->id == $claim->user_id, 403);

$claim->delete();

$torrentRequest->update([
'claimed' => null,
]);

$claimer = $claim->anon ? 'Anonymous' : $request->user()->username;
$requester = $torrentRequest->user;

if ($requester->acceptsNotification($request->user(), $requester, 'request', 'show_request_unclaim')) {
$requester->notify(new NewRequestUnclaim('torrent', $claimer, $torrentRequest));
}
$requester->notify((new NewRequestUnclaim('torrent', $claimer, $claim)));

$claim->delete();

return to_route('requests.show', ['torrentRequest' => $torrentRequest])
->with('success', trans('request.unclaimed-success'));
Expand Down
8 changes: 2 additions & 6 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ public function store(Request $request): \Illuminate\Http\RedirectResponse
$topicStarter = $topic->user;

// Notify All Subscribers Of New Reply
if ($topicStarter && $topicStarter->isNot($user) && $topicStarter->acceptsNotification($user, $topicStarter, 'forum', 'show_forum_topic')) {
$topicStarter->notify(new NewPost('topic', $user, $post));
}
$topicStarter->notify(new NewPost('topic', $user, $post));

$subscribers = User::query()
->where('id', '!=', $user->id)
Expand All @@ -136,9 +134,7 @@ public function store(Request $request): \Illuminate\Http\RedirectResponse
->get();

foreach ($subscribers as $subscriber) {
if ($subscriber->acceptsNotification($user, $subscriber, 'subscription', 'show_subscription_topic')) {
$subscriber->notify(new NewPost('subscription', $user, $post));
}
$subscriber->notify(new NewPost('subscription', $user, $post));
}

// Achievements
Expand Down
8 changes: 2 additions & 6 deletions app/Http/Controllers/RequestFillController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ public function store(StoreRequestFillRequest $request, TorrentRequest $torrentR
$sender = $request->boolean('filled_anon') ? 'Anonymous' : $request->user()->username;
$requester = $torrentRequest->user;

if ($requester->acceptsNotification($request->user(), $requester, 'request', 'show_request_fill')) {
$requester->notify(new NewRequestFill($torrentRequest));
}
$requester->notify(new NewRequestFill($torrentRequest));

return to_route('requests.show', ['torrentRequest' => $torrentRequest])
->with('success', trans('request.pending-approval'));
Expand All @@ -77,9 +75,7 @@ public function destroy(Request $request, TorrentRequest $torrentRequest): \Illu
'torrent_id' => null,
]);

if ($filler->acceptsNotification($approver, $filler, 'request', 'show_request_fill_reject')) {
$filler->notify(new NewRequestFillReject('torrent', $approver->is($requester) ? ($torrentRequest->anon ? 'Anonymous' : $requester->username) : $approver->username, $torrentRequest));
}
$filler->notify(new NewRequestFillReject('torrent', $approver->is($requester) ? ($torrentRequest->anon ? 'Anonymous' : $requester->username) : $approver->username, $torrentRequest));

return to_route('requests.show', ['torrentRequest' => $torrentRequest])
->with('success', trans('request.request-reset'));
Expand Down
4 changes: 1 addition & 3 deletions app/Http/Controllers/TopicController.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ public function store(Request $request, int $id): \Illuminate\Http\RedirectRespo
->get();

foreach ($subscribers as $subscriber) {
if ($subscriber->acceptsNotification($user, $subscriber, 'subscription', 'show_subscription_forum')) {
$subscriber->notify(new NewTopic('forum', $user, $topic));
}
$subscriber->notify(new NewTopic('forum', $user, $topic));
}

//Achievements
Expand Down
8 changes: 2 additions & 6 deletions app/Http/Controllers/User/FollowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ public function store(Request $request, User $user): \Illuminate\Http\RedirectRe

$user->followers()->attach($request->user()->id);

if ($user->acceptsNotification($request->user(), $user, 'account', 'show_account_follow')) {
$user->notify(new NewFollow('user', $request->user()));
}
$user->notify(new NewFollow('user', $request->user()));

return to_route('users.show', ['user' => $user])
->with('success', \sprintf(trans('user.follow-user'), $user->username));
Expand All @@ -65,9 +63,7 @@ public function destroy(Request $request, User $user): \Illuminate\Http\Redirect
{
$user->followers()->detach($request->user()->id);

if ($user->acceptsNotification($request->user(), $user, 'account', 'show_account_unfollow')) {
$user->notify(new NewUnfollow('user', $request->user()));
}
$user->notify(new NewUnfollow('user', $request->user()));

return to_route('users.show', ['user' => $user])
->with('success', \sprintf(trans('user.follow-revoked'), $user->username));
Expand Down
4 changes: 1 addition & 3 deletions app/Http/Controllers/User/GiftController.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ public function store(StoreGiftRequest $request): \Illuminate\Http\RedirectRespo
'message' => $request->message,
]);

if ($receiver->acceptsNotification($sender, $receiver, 'bon', 'show_bon_gift')) {
$receiver->notify((new NewBon($gift))->afterCommit());
}
$receiver->notify((new NewBon($gift))->afterCommit());
});

$this->chatRepository->systemMessage(
Expand Down
16 changes: 5 additions & 11 deletions app/Http/Livewire/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,18 @@ final public function postComment(): void
// New Comment Notification
switch (true) {
case $this->model instanceof Ticket:
$ticket = $this->model;
// Notify assigned staff if needed
User::find($this->model->staff_id)?->notify(new NewComment($this->model, $comment));

if ($this->user->id !== $ticket->staff_id && $ticket->staff_id !== null) {
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($this->model->user_id)?->notify(new NewComment($this->model, $comment));

break;
case $this->model instanceof Article:
case $this->model instanceof Playlist:
case $this->model instanceof TorrentRequest:
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
24 changes: 24 additions & 0 deletions app/Notifications/NewBon.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace App\Notifications;

use App\Models\Gift;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
Expand All @@ -42,6 +43,29 @@ public function via(object $notifiable): array
return ['database'];
}

/**
* Determine if the notification should be sent.
*/
public function shouldSend(User $notifiable): bool
{
// Enforce non-anonymous staff notifications to be sent
if ($this->gift->sender->group->is_modo) {
return true;
}

if ($notifiable->notification?->block_notifications == 1) {
return false;
}

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

// 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->gift->sender->group_id, $notifiable->notification->json_bon_groups, true);
}

/**
* Get the array representation of the notification.
*
Expand Down
60 changes: 53 additions & 7 deletions app/Notifications/NewComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
namespace App\Notifications;

use App\Models\Article;
use App\Models\Collection;
use App\Models\Comment;
use App\Models\Playlist;
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 @@ -33,7 +33,7 @@ class NewComment extends Notification
/**
* NewComment Constructor.
*/
public function __construct(public Torrent|TorrentRequest|Ticket|Playlist|Collection|Article $model, public Comment $comment)
public function __construct(public Torrent|TorrentRequest|Ticket|Playlist|Article $model, public Comment $comment)
{
}

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

/**
* Determine if the notification should be sent.
*/
public function shouldSend(User $notifiable): bool
{
// Do not notify self
if ($this->comment->user_id === $notifiable->id) {
return false;
}

// Enforce non-anonymous staff notifications to be sent
if ($this->comment->user->group->is_modo &&
! $this->comment->anon) {
return true;
}

// Evaluate general settings
if ($notifiable->notification?->block_notifications == 1) {
return false;
}

// Evaluate model based settings
switch (true) {
case $this->model instanceof Torrent:
if (!$notifiable->notification?->show_torrent_comment) {
return false;
}

// 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->json_torrent_groups, true);
case $this->model instanceof TorrentRequest:
if (!$notifiable->notification?->show_request_comment) {
return false;
}

// 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->json_request_groups, true);
case $this->model instanceof Ticket:
return ! ($this->model->staff_id === $this->comment->id && $this->model->staff_id !== null)
;

case $this->model instanceof Playlist:
case $this->model instanceof Article:
break;
}

return true;
}

/**
* Get the array representation of the notification.
*
Expand Down Expand Up @@ -77,11 +128,6 @@ public function toArray(object $notifiable): array
'body' => $username.' has left you a comment on Playlist '.$this->model->name,
'url' => '/playlists/'.$this->model->id.'#comment-'.$this->comment->id,
],
$this->model instanceof Collection => [
'title' => 'New Collection Comment Received',
'body' => $username.' has left you a comment on Collection '.$this->model->name,
'url' => '/mediahub/collections/'.$this->model->id.'#comment-'.$this->comment->id,
],
$this->model instanceof Article => [
'title' => 'New Article Comment Received',
'body' => $username.' has left you a comment on Article '.$this->model->title,
Expand Down
58 changes: 58 additions & 0 deletions app/Notifications/NewCommentTag.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\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
Expand All @@ -48,6 +49,63 @@ public function via(object $notifiable): array
return ['database'];
}

/**
* Determine if the notification should be sent.
*/
public function shouldSend(User $notifiable): bool
{
// Do not notify self
if ($this->comment->user_id === $notifiable->id) {
return false;
}

// Enforce non-anonymous staff notifications to be sent
if ($this->comment->user->group->is_modo &&
! $this->comment->anon) {
return true;
}

// Evaluate general settings
if ($notifiable->notification?->block_notifications == 1) {
return false;
}

// Evaluate model based settings
switch (true) {
case $this->model instanceof Torrent:
if (!$notifiable->notification?->show_mention_torrent_comment) {
return false;
}

// 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->json_mention_groups, true);
case $this->model instanceof TorrentRequest:
if (!$notifiable->notification?->show_mention_request_comment) {
return false;
}

// 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->json_mention_groups, true);
case $this->model instanceof Ticket:
return ! ($this->model->staff_id === $this->comment->id);
case $this->model instanceof Playlist:
case $this->model instanceof Article:
if (!$notifiable->notification?->show_mention_article_comment) {
return false;
}

// 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->json_mention_groups, true);
case $this->model instanceof Collection:
break;
}

return true;
}

/**
* Get the array representation of the notification.
*
Expand Down
Loading