Przeglądaj źródła

Merge pull request #1030 from pixelfed/frontend-ui-refactor

Update PublicApiController
daniel 6 lat temu
rodzic
commit
1efe349674
1 zmienionych plików z 101 dodań i 13 usunięć
  1. 101 13
      app/Http/Controllers/PublicApiController.php

+ 101 - 13
app/Http/Controllers/PublicApiController.php

@@ -203,10 +203,6 @@ class PublicApiController extends Controller
 
 
     public function publicTimelineApi(Request $request)
     public function publicTimelineApi(Request $request)
     {
     {
-        if(!Auth::check()) {
-            return abort(403);
-        }
-
         $this->validate($request,[
         $this->validate($request,[
           'page'        => 'nullable|integer|max:40',
           'page'        => 'nullable|integer|max:40',
           'min_id'      => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
           'min_id'      => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
@@ -219,21 +215,23 @@ class PublicApiController extends Controller
         $max = $request->input('max_id');
         $max = $request->input('max_id');
         $limit = $request->input('limit') ?? 3;
         $limit = $request->input('limit') ?? 3;
 
 
-        // TODO: Use redis for timelines
-        // $timeline = Timeline::build()->local();
-        $pid = Auth::user()->profile->id;
-
         $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
         $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
             return Profile::whereIsPrivate(true)
             return Profile::whereIsPrivate(true)
                 ->orWhere('unlisted', true)
                 ->orWhere('unlisted', true)
                 ->orWhere('status', '!=', null)
                 ->orWhere('status', '!=', null)
                 ->pluck('id');
                 ->pluck('id');
         });
         });
-        $filters = UserFilter::whereUserId($pid)
-                  ->whereFilterableType('App\Profile')
-                  ->whereIn('filter_type', ['mute', 'block'])
-                  ->pluck('filterable_id')->toArray();
-        $filtered = array_merge($private->toArray(), $filters);
+
+        if(Auth::check()) {
+            $pid = Auth::user()->profile->id;
+            $filters = UserFilter::whereUserId($pid)
+                      ->whereFilterableType('App\Profile')
+                      ->whereIn('filter_type', ['mute', 'block'])
+                      ->pluck('filterable_id')->toArray();
+            $filtered = array_merge($private->toArray(), $filters);
+        } else {
+            $filtered = $private->toArray();
+        }
 
 
         if($min || $max) {
         if($min || $max) {
             $dir = $min ? '>' : '<';
             $dir = $min ? '>' : '<';
@@ -397,6 +395,96 @@ class PublicApiController extends Controller
 
 
     }
     }
 
 
+
+    public function networkTimelineApi(Request $request)
+    {
+        if(!Auth::check()) {
+            return abort(403);
+        }
+
+        $this->validate($request,[
+          'page'        => 'nullable|integer|max:40',
+          'min_id'      => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
+          'max_id'      => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
+          'limit'       => 'nullable|integer|max:20'
+        ]);
+
+        $page = $request->input('page');
+        $min = $request->input('min_id');
+        $max = $request->input('max_id');
+        $limit = $request->input('limit') ?? 3;
+
+        // TODO: Use redis for timelines
+        // $timeline = Timeline::build()->local();
+        $pid = Auth::user()->profile->id;
+
+        $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
+            return Profile::whereIsPrivate(true)
+                ->orWhere('unlisted', true)
+                ->orWhere('status', '!=', null)
+                ->pluck('id');
+        });
+        $filters = UserFilter::whereUserId($pid)
+                  ->whereFilterableType('App\Profile')
+                  ->whereIn('filter_type', ['mute', 'block'])
+                  ->pluck('filterable_id')->toArray();
+        $filtered = array_merge($private->toArray(), $filters);
+
+        if($min || $max) {
+            $dir = $min ? '>' : '<';
+            $id = $min ?? $max;
+            $timeline = Status::select(
+                        'id', 
+                        'uri',
+                        'caption',
+                        'rendered',
+                        'profile_id', 
+                        'type',
+                        'in_reply_to_id',
+                        'reblog_of_id',
+                        'is_nsfw',
+                        'scope',
+                        'local',
+                        'created_at',
+                        'updated_at'
+                      )->where('id', $dir, $id)
+                      ->whereNotIn('profile_id', $filtered)
+                      ->whereNull('in_reply_to_id')
+                      ->whereNull('reblog_of_id')
+                      ->whereVisibility('public')
+                      ->orderBy('created_at', 'desc')
+                      ->limit($limit)
+                      ->get();
+        } else {
+            $timeline = Status::select(
+                        'id', 
+                        'uri',
+                        'caption',
+                        'rendered',
+                        'profile_id', 
+                        'type',
+                        'in_reply_to_id',
+                        'reblog_of_id',
+                        'is_nsfw',
+                        'scope',
+                        'local',
+                        'created_at',
+                        'updated_at'
+                      )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
+                      ->whereNotIn('profile_id', $filtered)
+                      ->whereNull('in_reply_to_id')
+                      ->whereNull('reblog_of_id')
+                      ->whereVisibility('public')
+                      ->orderBy('created_at', 'desc')
+                      ->simplePaginate($limit);
+        }
+
+        $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
+        $res = $this->fractal->createData($fractal)->toArray();
+        return response()->json($res);
+
+    }
+
     public function relationships(Request $request)
     public function relationships(Request $request)
     {
     {
         abort_if(!Auth::check(), 403);
         abort_if(!Auth::check(), 403);