Skip to content

Commit

Permalink
refactor: use laravel notifications for system user private messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Roardom committed Feb 2, 2025
1 parent 18ba1a5 commit 6a103ce
Show file tree
Hide file tree
Showing 32 changed files with 1,007 additions and 122 deletions.
10 changes: 4 additions & 6 deletions app/Console/Commands/AutoRemoveExpiredDonors.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
namespace App\Console\Commands;

use App\Models\User;
use App\Notifications\DonationExpired;
use App\Services\Unit3dAnnounce;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Notification;
use Throwable;

class AutoRemoveExpiredDonors extends Command
Expand Down Expand Up @@ -53,16 +55,12 @@ final public function handle(): void
$query->where('ends_at', '>', Carbon::now());
})->get();

Notification::send($expiredDonors, new DonationExpired());

foreach ($expiredDonors as $user) {
$user->is_donor = false;
$user->save();

User::sendSystemNotificationTo(
userId: $user->id,
subject: 'Your Donor Status Has Expired',
message: 'Your donor status has expired. Feel free to donate again to regain your donor status. Thank you for your support!',
);

cache()->forget('user:'.$user->passkey);
Unit3dAnnounce::addUser($user);
}
Expand Down
9 changes: 3 additions & 6 deletions app/Console/Commands/AutoRemovePersonalFreeleech.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

use App\Models\PersonalFreeleech;
use App\Models\User;
use App\Notifications\PersonalFreeleechDeleted;
use App\Services\Unit3dAnnounce;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Notification;
use Throwable;

class AutoRemovePersonalFreeleech extends Command
Expand Down Expand Up @@ -51,12 +53,7 @@ final public function handle(): void
$personalFreeleech = PersonalFreeleech::where('created_at', '<', $current->copy()->subDays(1))->get();

foreach ($personalFreeleech as $pfl) {
// Send Private Message
User::sendSystemNotificationTo(
userId: $pfl->user_id,
subject: 'Personal 24 Hour Freeleech Expired',
message: 'Your [b]Personal 24 Hour Freeleech[/b] has expired! Feel free to reenable it in the BON Store!',
);
Notification::send(new User(['id' => $pfl->user_id]), new PersonalFreeleechDeleted());

// Delete The Record From DB
$pfl->delete();
Expand Down
6 changes: 2 additions & 4 deletions app/Console/Commands/AutoRewardResurrection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace App\Console\Commands;

use App\Models\Resurrection;
use App\Notifications\ResurrectionCompleted;
use App\Repositories\ChatRepository;
use App\Services\Unit3dAnnounce;
use Exception;
Expand Down Expand Up @@ -96,10 +97,7 @@ final public function handle(): void
Unit3dAnnounce::addTorrent($resurrection->torrent);

// Send Private Message
$resurrection->user->sendSystemNotification(
subject: 'Successful Graveyard Resurrection',
message: \sprintf('You have successfully resurrected [url=%s/torrents/', $appurl).$resurrection->torrent->id.']'.$resurrection->torrent->name.'[/url] ! Thank you for bringing a torrent back from the dead! Enjoy the freeleech tokens!',
);
$resurrection->user->notify(new ResurrectionCompleted($resurrection->torrent));
}
});

Expand Down
13 changes: 6 additions & 7 deletions app/Http/Controllers/TorrentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use App\Models\Tv;
use App\Models\Type;
use App\Models\User;
use App\Notifications\TorrentDeleted;
use App\Repositories\ChatRepository;
use App\Services\Tmdb\TMDBScraper;
use App\Services\Unit3dAnnounce;
Expand All @@ -46,6 +47,7 @@
use MarcReichel\IGDBLaravel\Models\Game;
use MarcReichel\IGDBLaravel\Models\PlatformLogo;
use Exception;
use Illuminate\Support\Facades\Notification;
use ReflectionException;
use JsonException;

Expand Down Expand Up @@ -298,13 +300,10 @@ public function destroy(Request $request, int $id): \Illuminate\Http\RedirectRes

abort_unless($user->group->is_modo || ($user->id === $torrent->user_id && Carbon::now()->lt($torrent->created_at->addDay())), 403);

foreach (History::where('torrent_id', '=', $torrent->id)->pluck('user_id') as $user_id) {
User::sendSystemNotificationTo(
userId: $user_id,
subject: 'Torrent Deleted! - '.$torrent->name,
message: '[b]Attention:[/b] Torrent '.$torrent->name." has been removed from our site. Our system shows that you were either the uploader, a seeder or a leecher on said torrent. We just wanted to let you know you can safely remove it from your client.\n\n[b]Removal Reason:[/b] ".$request->message,
);
}
Notification::send(
User::query()->whereHas('history', fn ($query) => $query->where('torrent_id', '=', $torrent->id))->get(),
new TorrentDeleted($torrent, $request->message),
);

// Reset Requests
$torrent->requests()->update([
Expand Down
6 changes: 2 additions & 4 deletions app/Http/Controllers/User/ApikeyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Notifications\ApikeyReset;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -45,10 +46,7 @@ protected function update(Request $request, User $user): \Illuminate\Http\Redire
$user->apikeys()->create(['content' => $user->api_token]);

if ($changedByStaff) {
$user->sendSystemNotification(
subject: 'ATTENTION - Your API key has been reset',
message: "Your API key has been reset by staff. You will need to update your API key in all your scripts to continue using the API.\n\nFor more information, please create a helpdesk ticket.",
);
$user->notify(new ApikeyReset());
}
});

Expand Down
6 changes: 2 additions & 4 deletions app/Http/Controllers/User/PasskeyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Notifications\PasskeyReset;
use App\Services\Unit3dAnnounce;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
Expand Down Expand Up @@ -62,10 +63,7 @@ protected function update(Request $request, User $user): \Illuminate\Http\Redire
$user->passkeys()->create(['content' => $user->passkey]);

if ($changedByStaff) {
$user->sendSystemNotification(
subject: 'ATTENTION - Your passkey has been reset',
message: "Your passkey has been reset by staff. You will need to update your passkey in all your torrent clients to continue seeding.\n\nFor more information, please create a helpdesk ticket.",
);
$user->notify(new PasskeyReset());
}
});

Expand Down
6 changes: 2 additions & 4 deletions app/Http/Controllers/User/PasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Notifications\PasswordUpdate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
Expand Down Expand Up @@ -57,10 +58,7 @@ protected function update(Request $request, User $user): \Illuminate\Http\Redire
$user->passwordResetHistories()->create();

if ($changedByStaff) {
$user->sendSystemNotification(
subject: 'ATTENTION - Your password has been changed',
message: "Your password has been changed by staff. You will need to update your password manager with the new password.\n\nFor more information, please create a helpdesk ticket.",
);
$user->notify(new PasswordUpdate());
}
});

Expand Down
6 changes: 2 additions & 4 deletions app/Http/Controllers/User/RsskeyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Notifications\RsskeyReset;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

Expand All @@ -44,10 +45,7 @@ protected function update(Request $request, User $user): \Illuminate\Http\Redire
$user->rsskeys()->create(['content' => $user->rsskey]);

if ($changedByStaff) {
$user->sendSystemNotification(
subject: 'ATTENTION - Your RSS key has been reset',
message: "Your RSS key has been reset by staff. You will need to update your RSS key in your torrent client to continue receiving new torrents.\n\nFor more information, please create a helpdesk ticket.",
);
$user->notify(new RsskeyReset());
}
});

Expand Down
7 changes: 2 additions & 5 deletions app/Http/Controllers/User/TransactionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
use App\Models\BonTransactions;
use App\Models\PersonalFreeleech;
use App\Models\User;
use App\Notifications\PersonalFreeleechCreated;
use App\Services\Unit3dAnnounce;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;

/**
Expand Down Expand Up @@ -97,10 +97,7 @@ public function store(StoreTransactionRequest $request, User $user): \Illuminate

Unit3dAnnounce::addPersonalFreeleech($user->id);

$user->sendSystemNotification(
subject: trans('bon.pm-subject'),
message: \sprintf(trans('bon.pm-message'), Carbon::now()->addDays(1)->toDayDateTimeString()).config('app.timezone').'[/b]!',
);
$user->notify(new PersonalFreeleechCreated());

break;
case $bonExchange->invite:
Expand Down
18 changes: 6 additions & 12 deletions app/Http/Controllers/User/WarningController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Warning;
use App\Notifications\WarningCreated;
use App\Notifications\WarningTorrentDeleted;
use App\Notifications\WarningsDeleted;
use Illuminate\Http\Request;
use Exception;
use Illuminate\Support\Carbon;
Expand All @@ -44,10 +47,7 @@ protected function store(Request $request, User $user): \Illuminate\Http\Redirec
'active' => true,
]);

$user->sendSystemNotification(
subject: 'Received warning',
message: 'You have received a [b]warning[/b]. Reason: '.$request->string('message'),
);
$user->notify(new WarningCreated($request->string('message')->toString()));

return to_route('users.show', ['user' => $user])
->with('success', 'Warning issued successfully!');
Expand All @@ -65,10 +65,7 @@ public function destroy(Request $request, User $user, Warning $warning): \Illumi

$staff = $request->user();

$user->sendSystemNotification(
subject: 'Hit and Run Warning Deleted',
message: $staff->username.' has decided to delete your warning for torrent '.$warning->torrent.' You lucked out!',
);
$user->notify(new WarningTorrentDeleted($staff, $warning));

$warning->update([
'deleted_by' => $staff->id,
Expand All @@ -95,10 +92,7 @@ public function massDestroy(Request $request, User $user): \Illuminate\Http\Redi

$user->warnings()->delete();

$user->sendSystemNotification(
subject: 'All Hit and Run Warnings Deleted',
message: $staff->username.' has decided to delete all of your warnings. You lucked out!',
);
$user->notify(new WarningsDeleted($staff));

return to_route('users.show', ['user' => $user])
->with('success', 'All Warnings Were Successfully Deleted');
Expand Down
19 changes: 3 additions & 16 deletions app/Http/Livewire/SimilarTorrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
use App\Models\TorrentRequest;
use App\Models\Tv;
use App\Models\Type;
use App\Models\User;
use App\Notifications\TorrentsDeleted;
use App\Services\Unit3dAnnounce;
use App\Traits\CastLivewireProperties;
use App\Traits\LivewireSort;
use Illuminate\Support\Facades\Notification;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Url;
use Livewire\Component;
Expand Down Expand Up @@ -420,7 +421,6 @@ final public function deleteRecords(): void
}

$torrents = Torrent::whereKey($this->checked)->get();
$names = [];
$users = [];
$title = match (true) {
$this->category->movie_meta => ($movie = Movie::find($this->tmdbId))->title.' ('.$movie->release_date->format('Y').')',
Expand All @@ -430,8 +430,6 @@ final public function deleteRecords(): void
};

foreach ($torrents as $torrent) {
$names[] = $torrent->name;

foreach (History::where('torrent_id', '=', $torrent->id)->get() as $pm) {
if (!\in_array($pm->user_id, $users)) {
$users[] = $pm->user_id;
Expand Down Expand Up @@ -471,18 +469,7 @@ final public function deleteRecords(): void
$torrent->delete();
}

foreach ($users as $user) {
User::sendSystemNotificationTo(
userId: $user,
subject: 'Bulk Torrents Deleted - '.$title.'! ',
message: '[b]Attention: [/b] The following torrents have been removed from our site.
[list]
[*]'.implode(' [*]', $names).'
[/list]
Our system shows that you were either the uploader, a seeder or a leecher on said torrent. We just wanted to let you know you can safely remove it from your client.
[b]Removal Reason: [/b] '.$this->reason,
);
}
Notification::send($users, new TorrentsDeleted($torrents, $title, $this->reason));

$this->checked = [];
$this->selectPage = false;
Expand Down
30 changes: 10 additions & 20 deletions app/Http/Livewire/UserWarnings.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

use App\Models\User;
use App\Models\Warning;
use App\Notifications\WarningCreated;
use App\Notifications\WarningDeactivated;
use App\Notifications\WarningsDeactivated;
use App\Notifications\WarningsDeleted;
use App\Notifications\WarningTorrentDeleted;
use App\Traits\LivewireSort;
use Illuminate\Support\Carbon;
use Livewire\Attributes\Computed;
Expand Down Expand Up @@ -118,10 +123,7 @@ final public function store(): void
'active' => true,
]);

$this->user->sendSystemNotification(
subject: 'Received warning',
message: 'You have received a [b]warning[/b]. Reason: '.$this->message,
);
$this->user->notify(new WarningCreated($this->message));

$this->message = '';

Expand All @@ -142,10 +144,7 @@ final public function deactivate(Warning $warning): void
'active' => false,
]);

$this->user->sendSystemNotification(
subject: 'Hit and Run Warning Deleted',
message: $staff->username.' has decided to deactivate your warning for torrent '.$warning->torrent.' You lucked out!',
);
$this->user->notify(new WarningDeactivated($staff, $warning));

$this->dispatch('success', type: 'success', message: 'Warning Was Successfully Deactivated');
}
Expand Down Expand Up @@ -182,10 +181,7 @@ final public function massDeactivate(): void
'active' => false,
]);

$this->user->sendSystemNotification(
subject: 'All Hit and Run Warnings Deleted',
message: $staff->username.' has decided to deactivate all of your warnings. You lucked out!',
);
$this->user->notify(new WarningsDeactivated($staff));

$this->dispatch('success', type: 'success', message: 'All Warnings Were Successfully Deactivated');
}
Expand All @@ -207,10 +203,7 @@ final public function destroy(Warning $warning): void

$warning->delete();

$this->user->sendSystemNotification(
subject: 'Hit and Run Warning Deleted',
message: $staff->username.' has decided to delete your warning for torrent '.$warning->torrent.' You lucked out!',
);
$this->user->notify(new WarningTorrentDeleted($staff, $warning));

$this->dispatch('success', type: 'success', message: 'Warning Was Successfully Deleted');
}
Expand All @@ -232,10 +225,7 @@ final public function massDestroy(): void

$this->user->warnings()->delete();

$this->user->sendSystemNotification(
subject: 'All Hit and Run Warnings Deleted',
message: $staff->username.' has decided to delete all of your warnings. You lucked out!',
);
$this->user->notify(new WarningsDeleted($staff));

$this->dispatch('success', type: 'success', message: 'All Warnings Were Successfully Deleted');
}
Expand Down
Loading

0 comments on commit 6a103ce

Please sign in to comment.