1
0

ApiController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Api\BaseApiController;
  4. use App\{
  5. Follower,
  6. Like,
  7. Profile,
  8. UserFilter
  9. };
  10. use Auth, Cache, Redis;
  11. use Illuminate\Http\Request;
  12. use App\Services\SuggestionService;
  13. class ApiController extends BaseApiController
  14. {
  15. // todo: deprecate and remove
  16. public function hydrateLikes(Request $request)
  17. {
  18. return response()->json([]);
  19. }
  20. public function siteConfiguration(Request $request)
  21. {
  22. $res = Cache::remember('api:site:configuration', now()->addMinutes(30), function() {
  23. return [
  24. 'uploader' => [
  25. 'max_photo_size' => config('pixelfed.max_photo_size'),
  26. 'max_caption_length' => config('pixelfed.max_caption_length'),
  27. 'album_limit' => config('pixelfed.max_album_length'),
  28. 'image_quality' => config('pixelfed.image_quality'),
  29. 'optimize_image' => config('pixelfed.optimize_image'),
  30. 'optimize_video' => config('pixelfed.optimize_video'),
  31. 'media_types' => config('pixelfed.media_types'),
  32. 'enforce_account_limit' => config('pixelfed.enforce_account_limit')
  33. ],
  34. 'activitypub' => [
  35. 'enabled' => config('pixelfed.activitypub_enabled'),
  36. 'remote_follow' => config('pixelfed.remote_follow_enabled')
  37. ],
  38. 'ab' => [
  39. 'lc' => config('exp.lc'),
  40. 'rec' => config('exp.rec'),
  41. ],
  42. ];
  43. });
  44. return response()->json($res);
  45. }
  46. public function userRecommendations(Request $request)
  47. {
  48. abort_if(!Auth::check(), 403);
  49. abort_if(!config('exp.rec'), 400);
  50. $id = Auth::user()->profile->id;
  51. $following = Cache::remember('profile:following:'.$id, now()->addHours(12), function() use ($id) {
  52. return Follower::whereProfileId($id)->pluck('following_id')->toArray();
  53. });
  54. array_push($following, $id);
  55. $ids = SuggestionService::get();
  56. $filters = UserFilter::whereUserId($id)
  57. ->whereFilterableType('App\Profile')
  58. ->whereIn('filter_type', ['mute', 'block'])
  59. ->pluck('filterable_id')->toArray();
  60. $following = array_merge($following, $filters);
  61. $key = config('cache.prefix').':api:local:exp:rec:'.$id;
  62. $ttl = (int) Redis::ttl($key);
  63. if($request->filled('refresh') == true && (290 > $ttl) == true) {
  64. Cache::forget('api:local:exp:rec:'.$id);
  65. }
  66. $res = Cache::remember('api:local:exp:rec:'.$id, now()->addMinutes(5), function() use($id, $following, $ids) {
  67. return Profile::select(
  68. 'id',
  69. 'username'
  70. )
  71. ->whereNotIn('id', $following)
  72. ->whereIn('id', $ids)
  73. ->whereIsPrivate(0)
  74. ->whereNull('status')
  75. ->whereNull('domain')
  76. ->inRandomOrder()
  77. ->take(3)
  78. ->get()
  79. ->map(function($item, $key) {
  80. return [
  81. 'id' => $item->id,
  82. 'avatar' => $item->avatarUrl(),
  83. 'username' => $item->username,
  84. 'message' => 'Recommended for You'
  85. ];
  86. });
  87. });
  88. return response()->json($res->all());
  89. }
  90. }