NotificationController.php 4.6 KB

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