Update ApiController

This commit is contained in:
Daniel Supernault 2019-04-28 17:48:53 -06:00
parent ec21ad374f
commit 0b1c03e5bf
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -3,10 +3,14 @@
namespace App\Http\Controllers;
use App\Http\Controllers\Api\BaseApiController;
use App\Like;
use App\{
Like,
Profile
};
use Auth;
use Cache;
use Illuminate\Http\Request;
use App\Services\SuggestionService;
class ApiController extends BaseApiController
{
@ -47,4 +51,43 @@ class ApiController extends BaseApiController
return response()->json($res);
}
public function userRecommendations(Request $request)
{
abort_if(!Auth::check(), 403);
abort_if(!config('exp.rec'), 400);
$id = Auth::user()->profile->id;
$following = Cache::get('profile:following:'.$id, []);
$ids = SuggestionService::get();
$res = Cache::remember('api:local:exp:rec:'.$id, now()->addMinutes(5), function() use($id, $following, $ids) {
array_push($following, $id);
return Profile::select(
'id',
'username'
)
->whereNotIn('id', $following)
->whereIn('id', $ids)
->whereIsPrivate(0)
->whereNull('status')
->whereNull('domain')
->inRandomOrder()
->take(4)
->get()
->map(function($item, $key) {
return [
'id' => $item->id,
'avatar' => $item->avatarUrl(),
'username' => $item->username,
'message' => 'Recommended for You'
];
});
});
return response()->json($res->all());
}
}