ApiController.php 3.2 KB

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