From 1ca76b3a3c52a91e145e4e5791903e1d496310c5 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Tue, 27 Nov 2018 02:17:27 -0700 Subject: [PATCH] Update Discover --- .../Controllers/InternalApiController.php | 78 ++++++++++++++++++ public/js/components.js | Bin 424511 -> 424267 bytes public/mix-manifest.json | Bin 321 -> 321 bytes .../js/components/DiscoverComponent.vue | 45 +++++----- routes/web.php | 2 + 5 files changed, 102 insertions(+), 23 deletions(-) diff --git a/app/Http/Controllers/InternalApiController.php b/app/Http/Controllers/InternalApiController.php index c07c3a5dd..ae8a45ef3 100644 --- a/app/Http/Controllers/InternalApiController.php +++ b/app/Http/Controllers/InternalApiController.php @@ -121,6 +121,7 @@ class InternalApiController extends Controller return response()->json($notifications, 200, [], JSON_PRETTY_PRINT); } + // deprecated public function discover(Request $request) { $profile = Auth::user()->profile; @@ -176,6 +177,83 @@ class InternalApiController extends Controller ]; 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) { $profile = Auth::user()->profile; diff --git a/public/js/components.js b/public/js/components.js index 83390dd025cd03ed9fca8e7d5e8a60a13a8d5584..71a2c8f6a387e28e15dccb9bc02af39f1f61aa39 100644 GIT binary patch delta 183 zcmdo0Me_6~$qnZjH|ulDu}rV8VCCJM%b&)`pWC@XK0YUX4qDfy|z3VHb@ zU`+~%X(d1djSUovQ5vxj|ZzNm6Q(so_M|KLDE@3UvSg diff --git a/resources/assets/js/components/DiscoverComponent.vue b/resources/assets/js/components/DiscoverComponent.vue index 04182c37d..5d292fd1c 100644 --- a/resources/assets/js/components/DiscoverComponent.vue +++ b/resources/assets/js/components/DiscoverComponent.vue @@ -55,11 +55,11 @@ export default { } }, mounted() { - this.slowTimeout(); this.fetchData(); }, methods: { + followUser(id, event) { axios.post('/i/follow', { item: id @@ -75,31 +75,30 @@ export default { ); }); }, + 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'); + } + }); - if(this.posts.length > 1) { - $('.section-explore .loader').hide(); - $('.section-explore .row.d-none').removeClass('d-none'); - } - }); - }, - slowTimeout() { - setTimeout(function() { - let el = $('

').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); - } + 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'); + } + }); + } } } \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index c6cd71e6b..7747eb802 100644 --- a/routes/web.php +++ b/routes/web.php @@ -46,6 +46,8 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact Route::get('notifications', 'InternalApiController@notifications'); Route::post('notifications', 'InternalApiController@notificationMarkAllRead'); 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('comments/{username}/status/{postId}', 'PublicApiController@statusComments'); });