pixelfed/app/Http/Controllers/PixelfedDirectoryController.php

178 lines
6.4 KiB
PHP
Raw Permalink Normal View History

2022-11-14 03:11:07 +00:00
<?php
namespace App\Http\Controllers;
use App\Models\ConfigCache;
use App\Services\AccountService;
use App\Services\InstanceService;
2022-11-14 03:11:07 +00:00
use App\Services\StatusService;
use App\User;
use Cache;
use Illuminate\Http\Request;
2022-11-14 03:11:07 +00:00
use Illuminate\Support\Str;
use Storage;
2022-11-14 03:11:07 +00:00
class PixelfedDirectoryController extends Controller
{
public function get(Request $request)
{
if (! $request->filled('sk')) {
2022-11-14 03:11:07 +00:00
abort(404);
}
if (! config_cache('pixelfed.directory.submission-key')) {
2022-11-14 03:11:07 +00:00
abort(404);
}
if (! hash_equals(config_cache('pixelfed.directory.submission-key'), $request->input('sk'))) {
2022-11-14 03:11:07 +00:00
abort(403);
}
$res = $this->buildListing();
return response()->json($res, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
2022-11-14 03:11:07 +00:00
}
public function buildListing()
{
$res = config_cache('pixelfed.directory');
if ($res) {
2022-11-14 03:11:07 +00:00
$res = is_string($res) ? json_decode($res, true) : $res;
}
$res['_domain'] = config_cache('pixelfed.domain.app');
$res['_sk'] = config_cache('pixelfed.directory.submission-key');
$res['_ts'] = config_cache('pixelfed.directory.submission-ts');
$res['version'] = config_cache('pixelfed.version');
if (empty($res['summary'])) {
2022-11-14 03:11:07 +00:00
$summary = ConfigCache::whereK('app.short_description')->pluck('v');
$res['summary'] = $summary ? $summary[0] : null;
}
if (isset($res['admin'])) {
2022-11-14 03:11:07 +00:00
$res['admin'] = AccountService::get($res['admin'], true);
}
if (isset($res['banner_image']) && ! empty($res['banner_image'])) {
2022-11-14 03:11:07 +00:00
$res['banner_image'] = url(Storage::url($res['banner_image']));
}
if (isset($res['favourite_posts'])) {
$res['favourite_posts'] = collect($res['favourite_posts'])->map(function ($id) {
2022-11-14 03:11:07 +00:00
return StatusService::get($id);
})
->filter(function ($post) {
return $post && isset($post['account']);
})
->map(function ($post) {
return [
'avatar' => $post['account']['avatar'],
'display_name' => $post['account']['display_name'],
'username' => $post['account']['username'],
'media' => $post['media_attachments'][0]['url'],
'url' => $post['url'],
];
})
->values();
2022-11-14 03:11:07 +00:00
}
$guidelines = ConfigCache::whereK('app.rules')->first();
if ($guidelines) {
2022-11-14 03:11:07 +00:00
$res['community_guidelines'] = json_decode($guidelines->v, true);
}
$openRegistration = (bool) config_cache('pixelfed.open_registration');
$res['open_registration'] = $openRegistration;
$curatedOnboarding = (bool) config_cache('instance.curated_registration.enabled');
$res['curated_onboarding'] = $curatedOnboarding;
2022-11-14 03:11:07 +00:00
$oauthEnabled = ConfigCache::whereK('pixelfed.oauth_enabled')->first();
if ($oauthEnabled) {
$keys = (file_exists(storage_path('oauth-public.key')) || config_cache('passport.public_key')) &&
(file_exists(storage_path('oauth-private.key')) || config_cache('passport.private_key'));
2022-11-14 03:11:07 +00:00
$res['oauth_enabled'] = (bool) $oauthEnabled && $keys;
}
$activityPubEnabled = ConfigCache::whereK('federation.activitypub.enabled')->first();
if ($activityPubEnabled) {
2022-11-14 03:11:07 +00:00
$res['activitypub_enabled'] = (bool) $activityPubEnabled;
}
$res['feature_config'] = [
'media_types' => Str::of(config_cache('pixelfed.media_types'))->explode(','),
'image_quality' => config_cache('pixelfed.image_quality'),
'optimize_image' => (bool) config_cache('pixelfed.optimize_image'),
2022-11-14 03:11:07 +00:00
'max_photo_size' => config_cache('pixelfed.max_photo_size'),
'max_caption_length' => config_cache('pixelfed.max_caption_length'),
'max_altext_length' => config_cache('pixelfed.max_altext_length'),
'enforce_account_limit' => (bool) config_cache('pixelfed.enforce_account_limit'),
2022-11-14 03:11:07 +00:00
'max_account_size' => config_cache('pixelfed.max_account_size'),
'max_album_length' => config_cache('pixelfed.max_album_length'),
'account_deletion' => (bool) config_cache('pixelfed.account_deletion'),
2022-11-14 03:11:07 +00:00
];
$res['is_eligible'] = $this->validVal($res, 'admin') &&
$this->validVal($res, 'summary', null, 10) &&
$this->validVal($res, 'favourite_posts', 3) &&
$this->validVal($res, 'contact_email') &&
$this->validVal($res, 'privacy_pledge') &&
$this->validVal($res, 'location');
if (config_cache('pixelfed.directory.testimonials')) {
2022-11-14 03:11:07 +00:00
$res['testimonials'] = collect(json_decode(config_cache('pixelfed.directory.testimonials'), true))
->map(function ($testimonial) {
2022-11-14 03:11:07 +00:00
$profile = AccountService::get($testimonial['profile_id']);
2022-11-14 03:11:07 +00:00
return [
'profile' => [
'username' => $profile['username'],
'display_name' => $profile['display_name'],
'avatar' => $profile['avatar'],
'created_at' => $profile['created_at'],
2022-11-14 03:11:07 +00:00
],
'body' => $testimonial['body'],
2022-11-14 03:11:07 +00:00
];
});
}
$res['features_enabled'] = [
'stories' => (bool) config_cache('instance.stories.enabled'),
2022-11-14 03:11:07 +00:00
];
$statusesCount = InstanceService::totalLocalStatuses();
$usersCount = Cache::remember('api:nodeinfo:users', 43200, function () {
return User::count();
});
2022-11-14 03:11:07 +00:00
$res['stats'] = [
'user_count' => (int) $usersCount,
'post_count' => (int) $statusesCount,
2022-11-14 03:11:07 +00:00
];
$res['primary_locale'] = config('app.locale');
$hash = hash('sha256', json_encode($res));
$res['_hash'] = $hash;
ksort($res);
return $res;
}
protected function validVal($res, $val, $count = false, $minLen = false)
{
if (! isset($res[$val])) {
2022-11-14 03:11:07 +00:00
return false;
}
if ($count) {
2022-11-14 03:11:07 +00:00
return count($res[$val]) >= $count;
}
if ($minLen) {
2022-11-14 03:11:07 +00:00
return strlen($res[$val]) >= $minLen;
}
return $res[$val];
}
}