ApiController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Api\BaseApiController;
  4. use App\{
  5. Follower,
  6. Like,
  7. Place,
  8. Profile,
  9. UserFilter
  10. };
  11. use Auth, Cache;
  12. use Illuminate\Support\Facades\Redis;
  13. use App\Util\Site\Config;
  14. use Illuminate\Http\Request;
  15. use App\Services\SuggestionService;
  16. class ApiController extends BaseApiController
  17. {
  18. // todo: deprecate and remove
  19. public function hydrateLikes(Request $request)
  20. {
  21. return response()->json([]);
  22. }
  23. public function siteConfiguration(Request $request)
  24. {
  25. return response()->json(Config::get());
  26. }
  27. public function userRecommendations(Request $request)
  28. {
  29. abort_if(!Auth::check(), 403);
  30. abort_if(!config('exp.rec'), 400);
  31. $id = Auth::user()->profile->id;
  32. $following = Cache::remember('profile:following:'.$id, now()->addHours(12), function() use ($id) {
  33. return Follower::whereProfileId($id)->pluck('following_id')->toArray();
  34. });
  35. array_push($following, $id);
  36. $ids = SuggestionService::get();
  37. $filters = UserFilter::whereUserId($id)
  38. ->whereFilterableType('App\Profile')
  39. ->whereIn('filter_type', ['mute', 'block'])
  40. ->pluck('filterable_id')->toArray();
  41. $following = array_merge($following, $filters);
  42. $key = config('cache.prefix').':api:local:exp:rec:'.$id;
  43. $ttl = (int) Redis::ttl($key);
  44. if($request->filled('refresh') == true && (290 > $ttl) == true) {
  45. Cache::forget('api:local:exp:rec:'.$id);
  46. }
  47. $res = Cache::remember('api:local:exp:rec:'.$id, now()->addMinutes(5), function() use($id, $following, $ids) {
  48. return Profile::select(
  49. 'id',
  50. 'username'
  51. )
  52. ->whereNotIn('id', $following)
  53. ->whereIn('id', $ids)
  54. ->whereIsPrivate(0)
  55. ->whereNull('status')
  56. ->whereNull('domain')
  57. ->inRandomOrder()
  58. ->take(3)
  59. ->get()
  60. ->map(function($item, $key) {
  61. return [
  62. 'id' => $item->id,
  63. 'avatar' => $item->avatarUrl(),
  64. 'username' => $item->username,
  65. 'message' => 'Recommended for You'
  66. ];
  67. });
  68. });
  69. return response()->json($res->all());
  70. }
  71. public function composeLocationSearch(Request $request)
  72. {
  73. abort_if(!Auth::check(), 403);
  74. $this->validate($request, [
  75. 'q' => 'required|string|max:100'
  76. ]);
  77. $q = filter_var($request->input('q'), FILTER_SANITIZE_STRING);
  78. $hash = hash('sha256', $q);
  79. $key = 'search:location:id:' . $hash;
  80. $places = Cache::remember($key, now()->addMinutes(15), function() use($q) {
  81. $q = '%' . $q . '%';
  82. return Place::where('name', 'like', $q)
  83. ->take(80)
  84. ->get()
  85. ->map(function($r) {
  86. return [
  87. 'id' => $r->id,
  88. 'name' => $r->name,
  89. 'country' => $r->country,
  90. 'url' => $r->url()
  91. ];
  92. });
  93. });
  94. return $places;
  95. }
  96. }