Update Discover

This commit is contained in:
Daniel Supernault 2018-11-27 02:17:27 -07:00
parent 7494d97bbd
commit 1ca76b3a3c
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
5 changed files with 102 additions and 23 deletions

View file

@ -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;

Binary file not shown.

Binary file not shown.

View file

@ -55,11 +55,11 @@ export default {
}
},
mounted() {
this.slowTimeout();
this.fetchData();
},
methods: {
followUser(id, event) {
axios.post('/i/follow', {
item: id
@ -75,30 +75,29 @@ export default {
);
});
},
fetchData() {
axios.get('/api/v2/discover')
axios.get('/api/v2/discover/people')
.then((res) => {
let data = res.data;
this.people = data.people;
this.posts = data.posts;
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);
}
}
}

View file

@ -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');
});