1
0

PixelfedDirectoryController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\ConfigCache;
  5. use Storage;
  6. use App\Services\AccountService;
  7. use App\Services\StatusService;
  8. use Illuminate\Support\Str;
  9. class PixelfedDirectoryController extends Controller
  10. {
  11. public function get(Request $request)
  12. {
  13. if(!$request->filled('sk')) {
  14. abort(404);
  15. }
  16. if(!config_cache('pixelfed.directory.submission-key')) {
  17. abort(404);
  18. }
  19. if(!hash_equals(config_cache('pixelfed.directory.submission-key'), $request->input('sk'))) {
  20. abort(403);
  21. }
  22. $res = $this->buildListing();
  23. return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  24. }
  25. public function buildListing()
  26. {
  27. $res = config_cache('pixelfed.directory');
  28. if($res) {
  29. $res = is_string($res) ? json_decode($res, true) : $res;
  30. }
  31. $res['_domain'] = config_cache('pixelfed.domain.app');
  32. $res['_sk'] = config_cache('pixelfed.directory.submission-key');
  33. $res['_ts'] = config_cache('pixelfed.directory.submission-ts');
  34. $res['version'] = config_cache('pixelfed.version');
  35. if(empty($res['summary'])) {
  36. $summary = ConfigCache::whereK('app.short_description')->pluck('v');
  37. $res['summary'] = $summary ? $summary[0] : null;
  38. }
  39. if(isset($res['admin'])) {
  40. $res['admin'] = AccountService::get($res['admin'], true);
  41. }
  42. if(isset($res['banner_image']) && !empty($res['banner_image'])) {
  43. $res['banner_image'] = url(Storage::url($res['banner_image']));
  44. }
  45. if(isset($res['favourite_posts'])) {
  46. $res['favourite_posts'] = collect($res['favourite_posts'])->map(function($id) {
  47. return StatusService::get($id);
  48. })
  49. ->filter(function($post) {
  50. return $post && isset($post['account']);
  51. })
  52. ->map(function($post) {
  53. return [
  54. 'avatar' => $post['account']['avatar'],
  55. 'display_name' => $post['account']['display_name'],
  56. 'username' => $post['account']['username'],
  57. 'media' => $post['media_attachments'][0]['url'],
  58. 'url' => $post['url']
  59. ];
  60. })
  61. ->values();
  62. }
  63. $guidelines = ConfigCache::whereK('app.rules')->first();
  64. if($guidelines) {
  65. $res['community_guidelines'] = json_decode($guidelines->v, true);
  66. }
  67. $openRegistration = ConfigCache::whereK('pixelfed.open_registration')->first();
  68. if($openRegistration) {
  69. $res['open_registration'] = (bool) $openRegistration;
  70. }
  71. $oauthEnabled = ConfigCache::whereK('pixelfed.oauth_enabled')->first();
  72. if($oauthEnabled) {
  73. $keys = file_exists(storage_path('oauth-public.key')) && file_exists(storage_path('oauth-private.key'));
  74. $res['oauth_enabled'] = (bool) $oauthEnabled && $keys;
  75. }
  76. $activityPubEnabled = ConfigCache::whereK('federation.activitypub.enabled')->first();
  77. if($activityPubEnabled) {
  78. $res['activitypub_enabled'] = (bool) $activityPubEnabled;
  79. }
  80. $res['feature_config'] = [
  81. 'media_types' => Str::of(config_cache('pixelfed.media_types'))->explode(','),
  82. 'image_quality' => config_cache('pixelfed.image_quality'),
  83. 'optimize_image' => config_cache('pixelfed.optimize_image'),
  84. 'max_photo_size' => config_cache('pixelfed.max_photo_size'),
  85. 'max_caption_length' => config_cache('pixelfed.max_caption_length'),
  86. 'max_altext_length' => config_cache('pixelfed.max_altext_length'),
  87. 'enforce_account_limit' => config_cache('pixelfed.enforce_account_limit'),
  88. 'max_account_size' => config_cache('pixelfed.max_account_size'),
  89. 'max_album_length' => config_cache('pixelfed.max_album_length'),
  90. 'account_deletion' => config_cache('pixelfed.account_deletion'),
  91. ];
  92. $res['is_eligible'] = $this->validVal($res, 'admin') &&
  93. $this->validVal($res, 'summary', null, 10) &&
  94. $this->validVal($res, 'favourite_posts', 3) &&
  95. $this->validVal($res, 'contact_email') &&
  96. $this->validVal($res, 'privacy_pledge') &&
  97. $this->validVal($res, 'location');
  98. if(config_cache('pixelfed.directory.testimonials')) {
  99. $res['testimonials'] = collect(json_decode(config_cache('pixelfed.directory.testimonials'), true))
  100. ->map(function($testimonial) {
  101. $profile = AccountService::get($testimonial['profile_id']);
  102. return [
  103. 'profile' => [
  104. 'username' => $profile['username'],
  105. 'display_name' => $profile['display_name'],
  106. 'avatar' => $profile['avatar'],
  107. 'created_at' => $profile['created_at']
  108. ],
  109. 'body' => $testimonial['body']
  110. ];
  111. });
  112. }
  113. $res['features_enabled'] = [
  114. 'stories' => (bool) config_cache('instance.stories.enabled')
  115. ];
  116. $res['stats'] = [
  117. 'user_count' => \App\User::count(),
  118. 'post_count' => \App\Status::whereNull('uri')->count(),
  119. ];
  120. $res['primary_locale'] = config('app.locale');
  121. $hash = hash('sha256', json_encode($res));
  122. $res['_hash'] = $hash;
  123. ksort($res);
  124. return $res;
  125. }
  126. protected function validVal($res, $val, $count = false, $minLen = false)
  127. {
  128. if(!isset($res[$val])) {
  129. return false;
  130. }
  131. if($count) {
  132. return count($res[$val]) >= $count;
  133. }
  134. if($minLen) {
  135. return strlen($res[$val]) >= $minLen;
  136. }
  137. return $res[$val];
  138. }
  139. }