ApiController.php 2.8 KB

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