NotificationController.php 4.4 KB

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