Update PixelfedDirectoryController, fix boolean cast bug

This commit is contained in:
Daniel Supernault 2024-03-11 23:43:23 -06:00
parent 8a0c456edc
commit f08aab2231
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1

View file

@ -2,37 +2,38 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\ConfigCache; use App\Models\ConfigCache;
use Storage;
use App\Services\AccountService; use App\Services\AccountService;
use App\Services\StatusService; use App\Services\StatusService;
use Illuminate\Http\Request;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Storage;
class PixelfedDirectoryController extends Controller class PixelfedDirectoryController extends Controller
{ {
public function get(Request $request) public function get(Request $request)
{ {
if(!$request->filled('sk')) { if (! $request->filled('sk')) {
abort(404); abort(404);
} }
if(!config_cache('pixelfed.directory.submission-key')) { if (! config_cache('pixelfed.directory.submission-key')) {
abort(404); abort(404);
} }
if(!hash_equals(config_cache('pixelfed.directory.submission-key'), $request->input('sk'))) { if (! hash_equals(config_cache('pixelfed.directory.submission-key'), $request->input('sk'))) {
abort(403); abort(403);
} }
$res = $this->buildListing(); $res = $this->buildListing();
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
return response()->json($res, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
} }
public function buildListing() public function buildListing()
{ {
$res = config_cache('pixelfed.directory'); $res = config_cache('pixelfed.directory');
if($res) { if ($res) {
$res = is_string($res) ? json_decode($res, true) : $res; $res = is_string($res) ? json_decode($res, true) : $res;
} }
@ -41,40 +42,40 @@ class PixelfedDirectoryController extends Controller
$res['_ts'] = config_cache('pixelfed.directory.submission-ts'); $res['_ts'] = config_cache('pixelfed.directory.submission-ts');
$res['version'] = config_cache('pixelfed.version'); $res['version'] = config_cache('pixelfed.version');
if(empty($res['summary'])) { if (empty($res['summary'])) {
$summary = ConfigCache::whereK('app.short_description')->pluck('v'); $summary = ConfigCache::whereK('app.short_description')->pluck('v');
$res['summary'] = $summary ? $summary[0] : null; $res['summary'] = $summary ? $summary[0] : null;
} }
if(isset($res['admin'])) { if (isset($res['admin'])) {
$res['admin'] = AccountService::get($res['admin'], true); $res['admin'] = AccountService::get($res['admin'], true);
} }
if(isset($res['banner_image']) && !empty($res['banner_image'])) { if (isset($res['banner_image']) && ! empty($res['banner_image'])) {
$res['banner_image'] = url(Storage::url($res['banner_image'])); $res['banner_image'] = url(Storage::url($res['banner_image']));
} }
if(isset($res['favourite_posts'])) { if (isset($res['favourite_posts'])) {
$res['favourite_posts'] = collect($res['favourite_posts'])->map(function($id) { $res['favourite_posts'] = collect($res['favourite_posts'])->map(function ($id) {
return StatusService::get($id); return StatusService::get($id);
}) })
->filter(function($post) { ->filter(function ($post) {
return $post && isset($post['account']); return $post && isset($post['account']);
}) })
->map(function($post) { ->map(function ($post) {
return [ return [
'avatar' => $post['account']['avatar'], 'avatar' => $post['account']['avatar'],
'display_name' => $post['account']['display_name'], 'display_name' => $post['account']['display_name'],
'username' => $post['account']['username'], 'username' => $post['account']['username'],
'media' => $post['media_attachments'][0]['url'], 'media' => $post['media_attachments'][0]['url'],
'url' => $post['url'] 'url' => $post['url'],
]; ];
}) })
->values(); ->values();
} }
$guidelines = ConfigCache::whereK('app.rules')->first(); $guidelines = ConfigCache::whereK('app.rules')->first();
if($guidelines) { if ($guidelines) {
$res['community_guidelines'] = json_decode($guidelines->v, true); $res['community_guidelines'] = json_decode($guidelines->v, true);
} }
@ -85,27 +86,27 @@ class PixelfedDirectoryController extends Controller
$res['curated_onboarding'] = $curatedOnboarding; $res['curated_onboarding'] = $curatedOnboarding;
$oauthEnabled = ConfigCache::whereK('pixelfed.oauth_enabled')->first(); $oauthEnabled = ConfigCache::whereK('pixelfed.oauth_enabled')->first();
if($oauthEnabled) { if ($oauthEnabled) {
$keys = file_exists(storage_path('oauth-public.key')) && file_exists(storage_path('oauth-private.key')); $keys = file_exists(storage_path('oauth-public.key')) && file_exists(storage_path('oauth-private.key'));
$res['oauth_enabled'] = (bool) $oauthEnabled && $keys; $res['oauth_enabled'] = (bool) $oauthEnabled && $keys;
} }
$activityPubEnabled = ConfigCache::whereK('federation.activitypub.enabled')->first(); $activityPubEnabled = ConfigCache::whereK('federation.activitypub.enabled')->first();
if($activityPubEnabled) { if ($activityPubEnabled) {
$res['activitypub_enabled'] = (bool) $activityPubEnabled; $res['activitypub_enabled'] = (bool) $activityPubEnabled;
} }
$res['feature_config'] = [ $res['feature_config'] = [
'media_types' => Str::of(config_cache('pixelfed.media_types'))->explode(','), 'media_types' => Str::of(config_cache('pixelfed.media_types'))->explode(','),
'image_quality' => config_cache('pixelfed.image_quality'), 'image_quality' => config_cache('pixelfed.image_quality'),
'optimize_image' => config_cache('pixelfed.optimize_image'), 'optimize_image' => (bool) config_cache('pixelfed.optimize_image'),
'max_photo_size' => config_cache('pixelfed.max_photo_size'), 'max_photo_size' => config_cache('pixelfed.max_photo_size'),
'max_caption_length' => config_cache('pixelfed.max_caption_length'), 'max_caption_length' => config_cache('pixelfed.max_caption_length'),
'max_altext_length' => config_cache('pixelfed.max_altext_length'), 'max_altext_length' => config_cache('pixelfed.max_altext_length'),
'enforce_account_limit' => config_cache('pixelfed.enforce_account_limit'), 'enforce_account_limit' => (bool) config_cache('pixelfed.enforce_account_limit'),
'max_account_size' => config_cache('pixelfed.max_account_size'), 'max_account_size' => config_cache('pixelfed.max_account_size'),
'max_album_length' => config_cache('pixelfed.max_album_length'), 'max_album_length' => config_cache('pixelfed.max_album_length'),
'account_deletion' => config_cache('pixelfed.account_deletion'), 'account_deletion' => (bool) config_cache('pixelfed.account_deletion'),
]; ];
$res['is_eligible'] = $this->validVal($res, 'admin') && $res['is_eligible'] = $this->validVal($res, 'admin') &&
@ -115,24 +116,25 @@ class PixelfedDirectoryController extends Controller
$this->validVal($res, 'privacy_pledge') && $this->validVal($res, 'privacy_pledge') &&
$this->validVal($res, 'location'); $this->validVal($res, 'location');
if(config_cache('pixelfed.directory.testimonials')) { if (config_cache('pixelfed.directory.testimonials')) {
$res['testimonials'] = collect(json_decode(config_cache('pixelfed.directory.testimonials'), true)) $res['testimonials'] = collect(json_decode(config_cache('pixelfed.directory.testimonials'), true))
->map(function($testimonial) { ->map(function ($testimonial) {
$profile = AccountService::get($testimonial['profile_id']); $profile = AccountService::get($testimonial['profile_id']);
return [ return [
'profile' => [ 'profile' => [
'username' => $profile['username'], 'username' => $profile['username'],
'display_name' => $profile['display_name'], 'display_name' => $profile['display_name'],
'avatar' => $profile['avatar'], 'avatar' => $profile['avatar'],
'created_at' => $profile['created_at'] 'created_at' => $profile['created_at'],
], ],
'body' => $testimonial['body'] 'body' => $testimonial['body'],
]; ];
}); });
} }
$res['features_enabled'] = [ $res['features_enabled'] = [
'stories' => (bool) config_cache('instance.stories.enabled') 'stories' => (bool) config_cache('instance.stories.enabled'),
]; ];
$res['stats'] = [ $res['stats'] = [
@ -150,19 +152,18 @@ class PixelfedDirectoryController extends Controller
protected function validVal($res, $val, $count = false, $minLen = false) protected function validVal($res, $val, $count = false, $minLen = false)
{ {
if(!isset($res[$val])) { if (! isset($res[$val])) {
return false; return false;
} }
if($count) { if ($count) {
return count($res[$val]) >= $count; return count($res[$val]) >= $count;
} }
if($minLen) { if ($minLen) {
return strlen($res[$val]) >= $minLen; return strlen($res[$val]) >= $minLen;
} }
return $res[$val]; return $res[$val];
} }
} }