浏览代码

Update ApiController

Daniel Supernault 6 年之前
父节点
当前提交
0b1c03e5bf
共有 1 个文件被更改,包括 44 次插入1 次删除
  1. 44 1
      app/Http/Controllers/ApiController.php

+ 44 - 1
app/Http/Controllers/ApiController.php

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