1
0

GroupsNotificationsController.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Http\Controllers\Groups;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Services\AccountService;
  6. use App\Services\StatusService;
  7. use App\Services\GroupService;
  8. use App\Models\Group;
  9. use App\Notification;
  10. class GroupsNotificationsController extends Controller
  11. {
  12. public function __construct()
  13. {
  14. $this->middleware('auth');
  15. }
  16. public function selfGlobalNotifications(Request $request)
  17. {
  18. abort_if(!$request->user(), 404);
  19. $pid = $request->user()->profile_id;
  20. $res = Notification::whereProfileId($pid)
  21. ->where('action', 'like', 'group%')
  22. ->latest()
  23. ->paginate(10)
  24. ->map(function($n) {
  25. $res = [
  26. 'id' => $n->id,
  27. 'type' => $n->action,
  28. 'account' => AccountService::get($n->actor_id),
  29. 'object' => [
  30. 'id' => $n->item_id,
  31. 'type' => last(explode('\\', $n->item_type)),
  32. ],
  33. 'created_at' => $n->created_at->format('c')
  34. ];
  35. if($res['object']['type'] == 'Status' || in_array($n->action, ['group:comment'])) {
  36. $res['status'] = StatusService::get($n->item_id, false);
  37. $res['group'] = GroupService::get($res['status']['gid']);
  38. }
  39. if($res['object']['type'] == 'Group') {
  40. $res['group'] = GroupService::get($n->item_id);
  41. }
  42. return $res;
  43. });
  44. return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  45. }
  46. }