NotificationController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. */
  52. public function send(Request $request)
  53. {
  54. $data = $request->validate([
  55. "via" => ["required", new Delimited("in:mail,database")],
  56. "all" => "required_without:users|boolean",
  57. "users" => ["required_without:all"],
  58. "title" => "required|string|min:1",
  59. "content" => "required|string|min:1"
  60. ]);
  61. $via = explode(",", $data["via"]);
  62. $mail = null;
  63. $database = null;
  64. if (in_array("database", $via)) {
  65. $database = [
  66. "title" => $data["title"],
  67. "content" => $data["content"]
  68. ];
  69. }
  70. if (in_array("mail", $via)) {
  71. $mail = (new MailMessage)
  72. ->subject($data["title"])
  73. ->line(new HtmlString($data["content"]));
  74. }
  75. $all = $data["all"] ?? false;
  76. if ($all) {
  77. $users = User::all();
  78. } else {
  79. $userIds = explode(",", $data["users"]);
  80. $users = User::query()
  81. ->whereIn("id", $userIds)
  82. ->orWhereHas('discordUser' , function (Builder $builder) use ($userIds) {
  83. $builder->whereIn('id' , $userIds);
  84. })
  85. ->get();
  86. }
  87. if ($users->count() == 0) {
  88. throw ValidationException::withMessages([
  89. 'users' => ['No users found!'],
  90. ]);
  91. }
  92. Notification::send($users, new DynamicNotification($via, $database, $mail));
  93. return response()->json(["message" => "Notification successfully sent.", 'user_count' => $users->count()]);
  94. }
  95. /**
  96. * Delete all notifications from an user
  97. *
  98. * @param int $userId
  99. * @return JsonResponse
  100. */
  101. public function delete(int $userId)
  102. {
  103. $discordUser = DiscordUser::find($userId);
  104. $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
  105. $count = $user->notifications()->delete();
  106. return response()->json(["message" => "All notifications have been successfully deleted.", "count" => $count]);
  107. }
  108. /**
  109. * Delete a specific notification
  110. *
  111. * @param int $userId
  112. * @param int $notificationId
  113. * @return JsonResponse
  114. */
  115. public function deleteOne(int $userId, $notificationid)
  116. {
  117. $discordUser = DiscordUser::find($userId);
  118. $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
  119. $notification = $user->notifications()->where("id", $notificationid)->get()->first();
  120. if (!$notification) {
  121. return response()->json(["message" => "Notification not found."], 404);
  122. }
  123. $notification->delete();
  124. return response()->json($notification);
  125. }
  126. }