NotificationController.php 3.8 KB

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