NotificationController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Str;
  5. use Illuminate\Support\Facades\Log;
  6. use App\Models\Notification;
  7. use App\Http\Api\AuthApi;
  8. use App\Http\Resources\NotificationResource;
  9. class NotificationController extends Controller
  10. {
  11. /**
  12. * Display a listing of the resource.
  13. *
  14. * @return \Illuminate\Http\Response
  15. */
  16. public function index(Request $request)
  17. {
  18. //
  19. $user = AuthApi::current($request);
  20. if (!$user) {
  21. Log::error('notification auth failed {request}', ['request' => $request]);
  22. return $this->error(__('auth.failed'), 401, 401);
  23. }
  24. switch ($request->get('view')) {
  25. case 'to':
  26. $table = Notification::where('to', $user['user_uid']);
  27. $unread = Notification::where('to', $user['user_uid'])
  28. ->where('status', 'unread')->count();
  29. break;
  30. }
  31. if ($request->has('status')) {
  32. $table = $table->whereIn('status', explode(',', $request->get('status')));
  33. }
  34. $count = $table->count();
  35. $table = $table->orderBy($request->get('order', 'created_at'), $request->get('dir', 'desc'));
  36. $table = $table->skip($request->get("offset", 0))
  37. ->take($request->get('limit', 10));
  38. $result = $table->get();
  39. return $this->ok(
  40. [
  41. "rows" => NotificationResource::collection($result),
  42. "count" => $count,
  43. 'unread' => $unread,
  44. ]
  45. );
  46. }
  47. /**
  48. * Store a newly created resource in storage.
  49. *
  50. * @param \Illuminate\Http\Request $request
  51. * @return \Illuminate\Http\Response
  52. */
  53. public function store(Request $request)
  54. {
  55. //
  56. $user = AuthApi::current($request);
  57. if (!$user) {
  58. Log::error('notification auth failed {request}', ['request' => $request]);
  59. return $this->error(__('auth.failed'), 401, 401);
  60. }
  61. $new = new Notification;
  62. $new->id = Str::uuid();
  63. $new->from = $user['user_uid'];
  64. $new->to = $request->get('to');
  65. $new->url = $request->get('url');
  66. $new->content = $request->get('content');
  67. $new->res_type = $request->get('res_type');
  68. $new->res_id = $request->get('res_id');
  69. $new->channel = $request->get('channel');
  70. $new->save();
  71. return $this->ok(new NotificationResource($new));
  72. }
  73. public static function insert($from, $to, $res_type, $res_id, $channel)
  74. {
  75. foreach ($to as $key => $one) {
  76. $new = new Notification;
  77. $new->id = Str::uuid();
  78. $new->from = $from;
  79. $new->to = $one;
  80. $new->url = '';
  81. $new->content = '';
  82. $new->res_type = $res_type;
  83. $new->res_id = $res_id;
  84. $new->channel = $channel;
  85. $new->save();
  86. }
  87. return count($to);
  88. }
  89. /**
  90. * Display the specified resource.
  91. *
  92. * @param Notification $notification
  93. * @return \Illuminate\Http\Response
  94. */
  95. public function show(Notification $notification)
  96. {
  97. //
  98. return $this->ok(new NotificationResource($notification));
  99. }
  100. /**
  101. * Update the specified resource in storage.
  102. *
  103. * @param \Illuminate\Http\Request $request
  104. * @param Notification $notification
  105. * @return \Illuminate\Http\Response
  106. */
  107. public function update(Request $request, Notification $notification)
  108. {
  109. //
  110. $user = AuthApi::current($request);
  111. if (!$user) {
  112. return $this->error(__('auth.failed'), 401, 401);
  113. }
  114. if ($notification->to === $user['user_uid']) {
  115. $notification->status = $request->get('status', 'read');
  116. $notification->save();
  117. $unread = Notification::where('to', $notification->to)
  118. ->where('status', 'unread')
  119. ->count();
  120. return $this->ok(['unread' => $unread]);
  121. } else {
  122. return $this->error(__('auth.failed'), 403, 403);
  123. }
  124. }
  125. /**
  126. * Remove the specified resource from storage.
  127. *
  128. * @param Notification $notification
  129. * @return \Illuminate\Http\Response
  130. */
  131. public function destroy(Notification $notification)
  132. {
  133. //
  134. $notification->delete();
  135. if ($notification->trashed()) {
  136. return $this->ok('ok');
  137. } else {
  138. return $this->error('fail', 500, 500);
  139. }
  140. }
  141. }