ctrlpanel/app/Http/Controllers/Api/NotificationController.php

128 lines
3.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\DiscordUser;
use App\Models\User;
use App\Notifications\DynamicNotification;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
2021-08-02 22:10:59 +00:00
use Illuminate\Notifications\Messages\MailMessage;
2021-08-04 20:01:50 +00:00
use Illuminate\Support\Facades\Notification;
2021-08-02 22:10:59 +00:00
use Illuminate\Support\HtmlString;
use Spatie\ValidationRules\Rules\Delimited;
class NotificationController extends Controller
{
/**
2021-07-28 14:56:02 +00:00
* Display all notifications of an user.
* @param Request $request
* @param int $userId
* @return Response
*/
public function index(Request $request, int $userId)
{
$discordUser = DiscordUser::find($userId);
$user = $discordUser ? $discordUser->user : User::findOrFail($userId);
return $user->notifications()->paginate($request->query("per_page", 50));
}
/**
2021-07-28 14:56:02 +00:00
* Display a specific notification
*
* @param int $userId
* @param int $notificationId
* @return JsonResponse
*/
public function view(int $userId, $notificationId)
{
$discordUser = DiscordUser::find($userId);
$user = $discordUser ? $discordUser->user : User::findOrFail($userId);
$notification = $user->notifications()->where("id", $notificationId)->get()->first();
if (!$notification) {
return response()->json(["message" => "Notification not found."], 404);
}
return $notification;
}
/**
* Send a notification to an user.
*
* @param Request $request
* @param int $userId
* @return JsonResponse
*/
2021-08-04 20:01:50 +00:00
public function send(Request $request)
{
2021-08-03 20:46:58 +00:00
$data = $request->validate([
"via" => ["required", new Delimited("in:mail,database")],
2021-08-04 20:01:50 +00:00
"all" => "required_without:users|boolean",
"users" => ["required_without:all", new Delimited("exists:users,id")],
2021-08-03 20:46:58 +00:00
"title" => "required|string|min:1",
"content" => "required|string|min:1"
]);
2021-08-04 20:01:50 +00:00
$via = explode(",", $data["via"]);
2021-08-02 22:10:59 +00:00
$mail = null;
$database = null;
2021-08-04 20:01:50 +00:00
if (in_array("database", $via)) {
2021-08-03 20:46:58 +00:00
$database = [
"title" => $data["title"],
"content" => $data["content"]
];
2021-08-02 22:10:59 +00:00
}
2021-08-04 20:01:50 +00:00
if (in_array("mail", $via)) {
2021-08-03 20:46:58 +00:00
$mail = (new MailMessage)
->subject($data["title"])
->line(new HtmlString($data["content"]));
2021-08-02 22:10:59 +00:00
}
2021-08-04 20:01:50 +00:00
$all = $data["all"] ?? false;
$users = $all ? User::all() : User::whereIn("id", explode(",", $data["users"]))->get();
Notification::send($users, new DynamicNotification($via, $database, $mail));
return response()->json(["message" => "Notification successfully sent."]);
}
/**
* Delete all notifications from an user
*
* @param int $userId
* @return JsonResponse
*/
public function delete(int $userId)
{
$discordUser = DiscordUser::find($userId);
$user = $discordUser ? $discordUser->user : User::findOrFail($userId);
$count = $user->notifications()->delete();
return response()->json(["message" => "All notifications have been successfully deleted.", "count" => $count]);
}
/**
2021-07-28 14:56:02 +00:00
* Delete a specific notification
*
* @param int $userId
* @param int $notificationId
* @return JsonResponse
*/
public function deleteOne(int $userId, $notificationid)
{
$discordUser = DiscordUser::find($userId);
$user = $discordUser ? $discordUser->user : User::findOrFail($userId);
$notification = $user->notifications()->where("id", $notificationid)->get()->first();
if (!$notification) {
return response()->json(["message" => "Notification not found."], 404);
}
$notification->delete();
return response()->json($notification);
}
}