NotificationController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\DiscordUser;
  5. use App\Models\User;
  6. use App\Notifications\DynamicNotification;
  7. use Illuminate\Http\JsonResponse;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Notifications\Messages\MailMessage;
  10. use Illuminate\Support\Facades\Notification;
  11. use Illuminate\Support\HtmlString;
  12. use Spatie\ValidationRules\Rules\Delimited;
  13. class NotificationController extends Controller
  14. {
  15. /**
  16. * Display all notifications of an user.
  17. * @param Request $request
  18. * @param int $userId
  19. * @return Response
  20. */
  21. public function index(Request $request, int $userId)
  22. {
  23. $discordUser = DiscordUser::find($userId);
  24. $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
  25. return $user->notifications()->paginate($request->query("per_page", 50));
  26. }
  27. /**
  28. * Display a specific notification
  29. *
  30. * @param int $userId
  31. * @param int $notificationId
  32. * @return JsonResponse
  33. */
  34. public function view(int $userId, $notificationId)
  35. {
  36. $discordUser = DiscordUser::find($userId);
  37. $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
  38. $notification = $user->notifications()->where("id", $notificationId)->get()->first();
  39. if (!$notification) {
  40. return response()->json(["message" => "Notification not found."], 404);
  41. }
  42. return $notification;
  43. }
  44. /**
  45. * Send a notification to an user.
  46. *
  47. * @param Request $request
  48. * @param int $userId
  49. * @return JsonResponse
  50. */
  51. public function send(Request $request)
  52. {
  53. $data = $request->validate([
  54. "via" => ["required", new Delimited("in:mail,database")],
  55. "all" => "required_without:users|boolean",
  56. "users" => ["required_without:all", new Delimited("exists:users,id")],
  57. "title" => "required|string|min:1",
  58. "content" => "required|string|min:1"
  59. ]);
  60. $via = explode(",", $data["via"]);
  61. $mail = null;
  62. $database = null;
  63. if (in_array("database", $via)) {
  64. $database = [
  65. "title" => $data["title"],
  66. "content" => $data["content"]
  67. ];
  68. }
  69. if (in_array("mail", $via)) {
  70. $mail = (new MailMessage)
  71. ->subject($data["title"])
  72. ->line(new HtmlString($data["content"]));
  73. }
  74. $all = $data["all"] ?? false;
  75. $users = $all ? User::all() : User::whereIn("id", explode(",", $data["users"]))->get();
  76. Notification::send($users, new DynamicNotification($via, $database, $mail));
  77. return response()->json(["message" => "Notification successfully sent."]);
  78. }
  79. /**
  80. * Delete all notifications from an user
  81. *
  82. * @param int $userId
  83. * @return JsonResponse
  84. */
  85. public function delete(int $userId)
  86. {
  87. $discordUser = DiscordUser::find($userId);
  88. $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
  89. $count = $user->notifications()->delete();
  90. return response()->json(["message" => "All notifications have been successfully deleted.", "count" => $count]);
  91. }
  92. /**
  93. * Delete a specific notification
  94. *
  95. * @param int $userId
  96. * @param int $notificationId
  97. * @return JsonResponse
  98. */
  99. public function deleteOne(int $userId, $notificationid)
  100. {
  101. $discordUser = DiscordUser::find($userId);
  102. $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
  103. $notification = $user->notifications()->where("id", $notificationid)->get()->first();
  104. if (!$notification) {
  105. return response()->json(["message" => "Notification not found."], 404);
  106. }
  107. $notification->delete();
  108. return response()->json($notification);
  109. }
  110. }