NotificationController.php 4.4 KB

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