1
0
Daniel Supernault 6 жил өмнө
parent
commit
1ca76b3a3c

+ 78 - 0
app/Http/Controllers/InternalApiController.php

@@ -121,6 +121,7 @@ class InternalApiController extends Controller
         return response()->json($notifications, 200, [], JSON_PRETTY_PRINT);
         return response()->json($notifications, 200, [], JSON_PRETTY_PRINT);
     }
     }
 
 
+    // deprecated
     public function discover(Request $request)
     public function discover(Request $request)
     {
     {
         $profile = Auth::user()->profile;
         $profile = Auth::user()->profile;
@@ -176,6 +177,83 @@ class InternalApiController extends Controller
         ];
         ];
         return response()->json($res, 200, [], JSON_PRETTY_PRINT);
         return response()->json($res, 200, [], JSON_PRETTY_PRINT);
     }
     }
+
+    public function discoverPeople(Request $request)
+    {
+        $profile = Auth::user()->profile;
+        $pid = $profile->id;
+        $following = Cache::remember('feature:discover:following:'.$pid, 60, function() use ($pid) {
+            return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
+        });
+        $filters = Cache::remember("user:filter:list:$pid", 60, function() use($pid) {
+            return UserFilter::whereUserId($pid)
+            ->whereFilterableType('App\Profile')
+            ->whereIn('filter_type', ['mute', 'block'])
+            ->pluck('filterable_id')->toArray();
+        });
+        $following = array_merge($following, $filters);
+
+        $people = Profile::select('id', 'name', 'username')
+            ->with('avatar')
+            ->orderByRaw('rand()')
+            ->whereHas('statuses')
+            ->whereNull('domain')
+            ->whereNotIn('id', $following)
+            ->whereIsPrivate(false)
+            ->take(3)
+            ->get();
+
+        $res = [
+            'people' => $people->map(function($profile) {
+                return [
+                    'id'    => $profile->id,
+                    'avatar' => $profile->avatarUrl(),
+                    'name' => $profile->name,
+                    'username' => $profile->username,
+                    'url'   => $profile->url(),
+                ];
+            })
+        ];
+        return response()->json($res, 200, [], JSON_PRETTY_PRINT);
+    }
+
+    public function discoverPosts(Request $request)
+    {
+        $profile = Auth::user()->profile;
+        $pid = $profile->id;
+        $following = Cache::remember('feature:discover:following:'.$pid, 60, function() use ($pid) {
+            return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
+        });
+        $filters = Cache::remember("user:filter:list:$pid", 60, function() use($pid) {
+            return UserFilter::whereUserId($pid)
+            ->whereFilterableType('App\Profile')
+            ->whereIn('filter_type', ['mute', 'block'])
+            ->pluck('filterable_id')->toArray();
+        });
+        $following = array_merge($following, $filters);
+
+        $posts = Status::select('id', 'caption', 'profile_id')
+              ->whereNull('in_reply_to_id')
+              ->whereNull('reblog_of_id')
+              ->whereIsNsfw(false)
+              ->whereVisibility('public')
+              ->whereNotIn('profile_id', $following)
+              ->with('media')
+              ->orderBy('created_at', 'desc')
+              ->take(21)
+              ->get();
+
+        $res = [
+            'posts' => $posts->map(function($post) {
+                return [
+                    'url' => $post->url(),
+                    'thumb' => $post->thumb(),
+                ];
+            })
+        ];
+        return response()->json($res);
+    }
+
     public function directMessage(Request $request, $profileId, $threadId)
     public function directMessage(Request $request, $profileId, $threadId)
     {
     {
         $profile = Auth::user()->profile;
         $profile = Auth::user()->profile;

BIN
public/js/components.js


BIN
public/mix-manifest.json


+ 22 - 23
resources/assets/js/components/DiscoverComponent.vue

@@ -55,11 +55,11 @@ export default {
 		}
 		}
 	},
 	},
 	mounted() {
 	mounted() {
-    this.slowTimeout();
     this.fetchData();
     this.fetchData();
 	},
 	},
 
 
 	methods: {
 	methods: {
+    
     followUser(id, event) {
     followUser(id, event) {
       axios.post('/i/follow', {
       axios.post('/i/follow', {
         item: id
         item: id
@@ -75,31 +75,30 @@ export default {
         );
         );
       });
       });
     },
     },
+
 		fetchData() {
 		fetchData() {
-			axios.get('/api/v2/discover')
-			.then((res) => {
-				let data = res.data;
-				this.people = data.people;
-				this.posts = data.posts;
+      axios.get('/api/v2/discover/people')
+      .then((res) => {
+        let data = res.data;
+        this.people = data.people;
+
+        if(this.people.length > 1) {
+          $('.section-people .loader').hide();
+          $('.section-people .row.d-none').removeClass('d-none');
+        }
+      });
 
 
-				if(this.people.length > 1) {
-					$('.section-people .loader').hide();
-					$('.section-people .row.d-none').removeClass('d-none');
-				}
+      axios.get('/api/v2/discover/posts')
+      .then((res) => {
+        let data = res.data;
+        this.posts = data.posts;
 
 
-				if(this.posts.length > 1) {
-					$('.section-explore .loader').hide();
-					$('.section-explore .row.d-none').removeClass('d-none');
-				}
-			});
-		},
-    slowTimeout() {
-      setTimeout(function() {
-        let el = $('<p>').addClass('font-weight-bold').text('This is taking longer than expected to load. Please try reloading the page if this does not load after 30 seconds.');
-        $('.section-people .loader').append(el);
-        $('.section-explore .loader').append(el);
-      }, 5000);
-    }
+        if(this.posts.length > 1) {
+          $('.section-explore .loader').hide();
+          $('.section-explore .row.d-none').removeClass('d-none');
+        }
+      });
+		}
 	}
 	}
 }
 }
 </script>
 </script>

+ 2 - 0
routes/web.php

@@ -46,6 +46,8 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact
             Route::get('notifications', 'InternalApiController@notifications');
             Route::get('notifications', 'InternalApiController@notifications');
             Route::post('notifications', 'InternalApiController@notificationMarkAllRead');
             Route::post('notifications', 'InternalApiController@notificationMarkAllRead');
             Route::get('discover', 'InternalApiController@discover');
             Route::get('discover', 'InternalApiController@discover');
+            Route::get('discover/people', 'InternalApiController@discoverPeople');
+            Route::get('discover/posts', 'InternalApiController@discoverPosts');
             Route::get('profile/{username}/status/{postid}', 'PublicApiController@status');
             Route::get('profile/{username}/status/{postid}', 'PublicApiController@status');
             Route::get('comments/{username}/status/{postId}', 'PublicApiController@statusComments');
             Route::get('comments/{username}/status/{postId}', 'PublicApiController@statusComments');
         });
         });