Browse Source

Update PublicApiController, add AccountService

Daniel Supernault 5 years ago
parent
commit
5ebd2c8a1b
1 changed files with 29 additions and 113 deletions
  1. 29 113
      app/Http/Controllers/PublicApiController.php

+ 29 - 113
app/Http/Controllers/PublicApiController.php

@@ -22,7 +22,11 @@ use App\Transformer\Api\{
     RelationshipTransformer,
     StatusTransformer,
 };
-use App\Services\UserFilterService;
+use App\Services\{
+    AccountService,
+    PublicTimelineService,
+    UserFilterService
+};
 use App\Jobs\StatusPipeline\NewStatusPipeline;
 use League\Fractal\Serializer\ArraySerializer;
 use League\Fractal\Pagination\IlluminatePaginatorAdapter;
@@ -38,17 +42,12 @@ class PublicApiController extends Controller
         $this->fractal->setSerializer(new ArraySerializer());
     }
 
-    protected function getUserData()
+    protected function getUserData($user)
     {
-    	if(false == Auth::check()) {
+    	if(!$user) {
     		return [];
     	} else {
-	        $profile = Auth::user()->profile;
-            if($profile->status) {
-                return [];
-            }
-	        $user = new Fractal\Resource\Item($profile, new AccountTransformer());
-        	return $this->fractal->createData($user)->toArray();
+            return AccountService::get($user->profile_id);
     	}
     }
 
@@ -90,7 +89,7 @@ class PublicApiController extends Controller
         $item = new Fractal\Resource\Item($status, new StatusTransformer());
         $res = [
         	'status' => $this->fractal->createData($item)->toArray(),
-        	'user' => $this->getUserData(),
+        	'user' => $this->getUserData($request->user()),
             'likes' => $this->getLikes($status),
             'shares' => $this->getShares($status),
             'reactions' => [
@@ -235,12 +234,13 @@ class PublicApiController extends Controller
         $max = $request->input('max_id');
         $limit = $request->input('limit') ?? 3;
 
-        // $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
-        //     return Profile::whereIsPrivate(true)
-        //         ->orWhere('unlisted', true)
-        //         ->orWhere('status', '!=', null)
-        //         ->pluck('id');
-        // });
+        $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
+            return Profile::whereIsPrivate(true)
+                ->orWhere('unlisted', true)
+                ->orWhere('status', '!=', null)
+                ->pluck('id')
+                ->toArray();
+        });
 
         // if(Auth::check()) {
         //     // $pid = Auth::user()->profile->id;
@@ -255,7 +255,17 @@ class PublicApiController extends Controller
         //     $filtered = [];
         // }
 
-        $filtered = Auth::check() ? UserFilterService::filters(Auth::user()->profile_id) : [];
+        $filtered = Auth::check() ? array_merge($private, UserFilterService::filters(Auth::user()->profile_id)) : [];
+        if($max == 0) {
+            $res = PublicTimelineService::count();
+            if($res == 0) {
+                PublicTimelineService::warmCache();
+                $res = PublicTimelineService::get(0,4);
+            } else {
+                $res = PublicTimelineService::get(0,4);
+            }
+            return response($res);
+        } 
 
         if($min || $max) {
             $dir = $min ? '>' : '<';
@@ -439,98 +449,7 @@ 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',
-                        'reply_count',
-                        'comments_disabled',
-                        'created_at',
-                        'updated_at'
-                      )->where('id', $dir, $id)
-                      ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
-                      ->whereNotIn('profile_id', $filtered)
-                      ->whereNotNull('uri')
-                      ->whereNull('in_reply_to_id')
-                      ->whereNull('reblog_of_id')
-                      ->whereVisibility('public')
-                      ->latest()
-                      ->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',
-                        'reply_count',
-                        'comments_disabled',
-                        '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')
-                      ->whereNotNull('uri')
-                      ->whereVisibility('public')
-                      ->latest()
-                      ->simplePaginate($limit);
-        }
-
-        $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
-        $res = $this->fractal->createData($fractal)->toArray();
-        return response()->json($res);
-
+        return response()->json([]);
     }
 
     public function relationships(Request $request)
@@ -555,10 +474,7 @@ class PublicApiController extends Controller
 
     public function account(Request $request, $id)
     {
-        $profile = Profile::whereNull('status')->findOrFail($id);
-        $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
-        $res = $this->fractal->createData($resource)->toArray();
-
+        $res = AccountService::get($id);
         return response()->json($res);
     }