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 104 additions and 25 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;

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,5 @@
{
"/js/components.js": "/js/components.js?id=c28daccae770fb4beb51",
"/js/components.js": "/js/components.js?id=1830e28a5b89d62c516d",
"/js/app.js": "/js/app.js?id=763d01bb175be69c8ad3",
"/css/app.css": "/css/app.css?id=0f11643b1635ebda5e1b",
"/js/timeline.js": "/js/timeline.js?id=415bfde862ab8c5b4548",

View File

@ -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 = $('<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);
}
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');
}
});
}
}
}
</script>

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