NotificationController.php 4.0 KB

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