1
0

ApiController.php 3.2 KB

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