pixelfed/app/Http/Controllers/Admin/AdminDirectoryController.php

455 lines
18 KiB
PHP
Raw Permalink Normal View History

2022-11-14 03:11:07 +00:00
<?php
namespace App\Http\Controllers\Admin;
2024-03-12 09:55:51 +00:00
use App\Http\Controllers\PixelfedDirectoryController;
2022-11-14 03:11:07 +00:00
use App\Models\ConfigCache;
use App\Services\AccountService;
use App\Services\ConfigCacheService;
use App\Services\StatusService;
2024-03-12 09:55:51 +00:00
use App\Status;
use App\User;
use Cache;
2022-11-14 03:11:07 +00:00
use Illuminate\Http\Request;
2024-03-12 09:55:51 +00:00
use Illuminate\Support\Facades\Http;
2022-11-14 03:11:07 +00:00
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
2024-03-12 09:55:51 +00:00
use Illuminate\Support\Str;
use League\ISO3166\ISO3166;
2022-11-14 03:11:07 +00:00
trait AdminDirectoryController
{
public function directoryHome(Request $request)
{
return view('admin.directory.home');
}
public function directoryInitialData(Request $request)
{
$res = [];
$res['countries'] = collect((new ISO3166)->all())->pluck('name');
$res['admins'] = User::whereIsAdmin(true)
->where('2fa_enabled', true)
2024-03-12 09:55:51 +00:00
->get()->map(function ($user) {
return [
'uid' => (string) $user->id,
'pid' => (string) $user->profile_id,
'username' => $user->username,
'created_at' => $user->created_at,
];
});
2022-11-14 03:11:07 +00:00
$config = ConfigCache::whereK('pixelfed.directory')->first();
2024-03-12 09:55:51 +00:00
if ($config) {
2022-11-14 03:11:07 +00:00
$data = $config->v ? json_decode($config->v, true) : [];
$res = array_merge($res, $data);
}
2024-03-12 09:55:51 +00:00
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;
}
2024-03-12 09:55:51 +00:00
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']));
}
2024-03-12 09:55:51 +00:00
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);
})
2024-03-12 09:55:51 +00:00
->filter(function ($post) {
return $post && isset($post['account']);
})
->values();
2022-11-14 03:11:07 +00:00
}
$res['community_guidelines'] = config_cache('app.rules') ? json_decode(config_cache('app.rules'), true) : [];
$res['curated_onboarding'] = (bool) config_cache('instance.curated_registration.enabled');
2022-11-14 03:11:07 +00:00
$res['open_registration'] = (bool) config_cache('pixelfed.open_registration');
$res['oauth_enabled'] = (bool) config_cache('pixelfed.oauth_enabled') && file_exists(storage_path('oauth-public.key')) && file_exists(storage_path('oauth-private.key'));
$res['activitypub_enabled'] = (bool) config_cache('federation.activitypub.enabled');
$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
];
2024-03-12 09:55:51 +00:00
if (config_cache('pixelfed.directory.testimonials')) {
$testimonials = collect(json_decode(config_cache('pixelfed.directory.testimonials'), true))
->map(function ($t) {
2022-11-14 03:11:07 +00:00
return [
'profile' => AccountService::get($t['profile_id']),
2024-03-12 09:55:51 +00:00
'body' => $t['body'],
2022-11-14 03:11:07 +00:00
];
});
$res['testimonials'] = $testimonials;
}
$validator = Validator::make($res['feature_config'], [
'media_types' => [
'required',
2024-03-12 09:55:51 +00:00
function ($attribute, $value, $fail) {
if (! in_array('image/jpeg', $value->toArray()) || ! in_array('image/png', $value->toArray())) {
2022-11-14 03:11:07 +00:00
$fail('You must enable image/jpeg and image/png support.');
}
},
],
'image_quality' => 'required_if:optimize_image,true|integer|min:75|max:100',
'max_altext_length' => 'required|integer|min:1000|max:5000',
'max_photo_size' => 'required|integer|min:15000|max:100000',
'max_account_size' => 'required_if:enforce_account_limit,true|integer|min:1000000',
'max_album_length' => 'required|integer|min:4|max:20',
'account_deletion' => 'required|accepted',
2024-03-12 09:55:51 +00:00
'max_caption_length' => 'required|integer|min:500|max:10000',
2022-11-14 03:11:07 +00:00
]);
$res['requirements_validator'] = $validator->errors();
$res['is_eligible'] = ($res['open_registration'] || $res['curated_onboarding']) &&
2022-11-14 03:11:07 +00:00
$res['oauth_enabled'] &&
$res['activitypub_enabled'] &&
count($res['requirements_validator']) === 0 &&
$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');
$res['has_submitted'] = config_cache('pixelfed.directory.has_submitted') ?? false;
$res['synced'] = config_cache('pixelfed.directory.is_synced') ?? false;
$res['latest_response'] = config_cache('pixelfed.directory.latest_response') ?? null;
$path = base_path('resources/lang');
$langs = collect([]);
foreach (new \DirectoryIterator($path) as $io) {
$name = $io->getFilename();
$skip = ['vendor'];
2024-03-12 09:55:51 +00:00
if ($io->isDot() || in_array($name, $skip)) {
2022-11-14 03:11:07 +00:00
continue;
}
2024-03-12 09:55:51 +00:00
if ($io->isDir()) {
2022-11-14 03:11:07 +00:00
$langs->push(['code' => $name, 'name' => locale_get_display_name($name)]);
}
}
$res['available_languages'] = $langs->sortBy('name')->values();
$res['primary_locale'] = config('app.locale');
$submissionState = Http::withoutVerifying()
2024-03-12 09:55:51 +00:00
->post('https://pixelfed.org/api/v1/directory/check-submission', [
'domain' => config('pixelfed.domain.app'),
]);
2022-11-14 03:11:07 +00:00
$res['submission_state'] = $submissionState->json();
2024-03-12 09:55:51 +00:00
2022-11-14 03:11:07 +00:00
return $res;
}
protected function validVal($res, $val, $count = false, $minLen = false)
{
2024-03-12 09:55:51 +00:00
if (! isset($res[$val])) {
2022-11-14 03:11:07 +00:00
return false;
}
2024-03-12 09:55:51 +00:00
if ($count) {
2022-11-14 03:11:07 +00:00
return count($res[$val]) >= $count;
}
2024-03-12 09:55:51 +00:00
if ($minLen) {
2022-11-14 03:11:07 +00:00
return strlen($res[$val]) >= $minLen;
}
return $res[$val];
}
public function directoryStore(Request $request)
{
$this->validate($request, [
'location' => 'string|min:1|max:53',
'summary' => 'string|nullable|max:140',
'admin_uid' => 'sometimes|nullable',
'contact_email' => 'sometimes|nullable|email:rfc,dns',
'favourite_posts' => 'array|max:12',
'favourite_posts.*' => 'distinct',
'privacy_pledge' => 'sometimes',
2024-03-12 09:55:51 +00:00
'banner_image' => 'sometimes|mimes:jpg,png|dimensions:width=1920,height:1080|max:5000',
2022-11-14 03:11:07 +00:00
]);
$config = ConfigCache::firstOrNew([
2024-03-12 09:55:51 +00:00
'k' => 'pixelfed.directory',
2022-11-14 03:11:07 +00:00
]);
$res = $config->v ? json_decode($config->v, true) : [];
$res['summary'] = strip_tags($request->input('summary'));
$res['favourite_posts'] = $request->input('favourite_posts');
$res['admin'] = (string) $request->input('admin_uid');
$res['contact_email'] = $request->input('contact_email');
$res['privacy_pledge'] = (bool) $request->input('privacy_pledge');
2024-03-12 09:55:51 +00:00
if ($request->filled('location')) {
2022-11-14 03:11:07 +00:00
$exists = (new ISO3166)->name($request->location);
2024-03-12 09:55:51 +00:00
if ($exists) {
2022-11-14 03:11:07 +00:00
$res['location'] = $request->input('location');
}
}
2024-03-12 09:55:51 +00:00
if ($request->hasFile('banner_image')) {
2022-11-14 03:11:07 +00:00
collect(Storage::files('public/headers'))
2024-03-12 09:55:51 +00:00
->filter(function ($name) {
$protected = [
'public/headers/.gitignore',
'public/headers/default.jpg',
'public/headers/missing.png',
];
return ! in_array($name, $protected);
})
->each(function ($name) {
Storage::delete($name);
});
$path = $request->file('banner_image')->storePublicly('public/headers');
2022-11-14 03:11:07 +00:00
$res['banner_image'] = $path;
ConfigCacheService::put('app.banner_image', url(Storage::url($path)));
Cache::forget('api:v1:instance-data-response-v1');
}
$config->v = json_encode($res);
$config->save();
ConfigCacheService::put('pixelfed.directory', $config->v);
$updated = json_decode($config->v, true);
2024-03-12 09:55:51 +00:00
if (isset($updated['banner_image'])) {
2022-11-14 03:11:07 +00:00
$updated['banner_image'] = url(Storage::url($updated['banner_image']));
}
2024-03-12 09:55:51 +00:00
2022-11-14 03:11:07 +00:00
return $updated;
}
public function directoryHandleServerSubmission(Request $request)
{
$reqs = [];
$reqs['feature_config'] = [
'open_registration' => (bool) config_cache('pixelfed.open_registration'),
'curated_onboarding' => (bool) config_cache('instance.curated_registration.enabled'),
2022-11-14 03:11:07 +00:00
'activitypub_enabled' => config_cache('federation.activitypub.enabled'),
2024-03-12 09:55:51 +00:00
'oauth_enabled' => (bool) config_cache('pixelfed.oauth_enabled'),
2022-11-14 03:11:07 +00:00
'media_types' => Str::of(config_cache('pixelfed.media_types'))->explode(','),
'image_quality' => config_cache('pixelfed.image_quality'),
'optimize_image' => config_cache('pixelfed.optimize_image'),
'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' => config_cache('pixelfed.enforce_account_limit'),
'max_account_size' => config_cache('pixelfed.max_account_size'),
'max_album_length' => config_cache('pixelfed.max_album_length'),
'account_deletion' => config_cache('pixelfed.account_deletion'),
];
$validator = Validator::make($reqs['feature_config'], [
'open_registration' => 'required_unless:curated_onboarding,true',
'curated_onboarding' => 'required_unless:open_registration,true',
2022-11-14 03:11:07 +00:00
'activitypub_enabled' => 'required|accepted',
'oauth_enabled' => 'required|accepted',
'media_types' => [
'required',
2024-03-12 09:55:51 +00:00
function ($attribute, $value, $fail) {
if (! in_array('image/jpeg', $value->toArray()) || ! in_array('image/png', $value->toArray())) {
2022-11-14 03:11:07 +00:00
$fail('You must enable image/jpeg and image/png support.');
}
},
],
'image_quality' => 'required_if:optimize_image,true|integer|min:75|max:100',
'max_altext_length' => 'required|integer|min:1000|max:5000',
'max_photo_size' => 'required|integer|min:15000|max:100000',
'max_account_size' => 'required_if:enforce_account_limit,true|integer|min:1000000',
'max_album_length' => 'required|integer|min:4|max:20',
'account_deletion' => 'required|accepted',
2024-03-12 09:55:51 +00:00
'max_caption_length' => 'required|integer|min:500|max:10000',
2022-11-14 03:11:07 +00:00
]);
2024-03-12 09:55:51 +00:00
if (! $validator->validate()) {
2022-11-14 03:11:07 +00:00
return response()->json($validator->errors(), 422);
}
ConfigCacheService::put('pixelfed.directory.submission-key', Str::random(random_int(40, 69)));
ConfigCacheService::put('pixelfed.directory.submission-ts', now());
$data = (new PixelfedDirectoryController())->buildListing();
$res = Http::withoutVerifying()->post('https://pixelfed.org/api/v1/directory/submission', $data);
2024-03-12 09:55:51 +00:00
2022-11-14 03:11:07 +00:00
return 200;
}
public function directoryDeleteBannerImage(Request $request)
{
$bannerImage = ConfigCache::whereK('app.banner_image')->first();
$directory = ConfigCache::whereK('pixelfed.directory')->first();
2024-03-12 09:55:51 +00:00
if (! $bannerImage && ! $directory || empty($directory->v)) {
2022-11-14 03:11:07 +00:00
return;
}
$directoryArr = json_decode($directory->v, true);
$path = isset($directoryArr['banner_image']) ? $directoryArr['banner_image'] : false;
$protected = [
'public/headers/.gitignore',
'public/headers/default.jpg',
2024-03-12 09:55:51 +00:00
'public/headers/missing.png',
2022-11-14 03:11:07 +00:00
];
2024-03-12 09:55:51 +00:00
if (! $path || in_array($path, $protected)) {
2022-11-14 03:11:07 +00:00
return;
}
2024-03-12 09:55:51 +00:00
if (Storage::exists($directoryArr['banner_image'])) {
2022-11-14 03:11:07 +00:00
Storage::delete($directoryArr['banner_image']);
}
$directoryArr['banner_image'] = 'public/headers/default.jpg';
$directory->v = $directoryArr;
$directory->save();
$bannerImage->v = url(Storage::url('public/headers/default.jpg'));
$bannerImage->save();
Cache::forget('api:v1:instance-data-response-v1');
ConfigCacheService::put('pixelfed.directory', $directory);
2024-03-12 09:55:51 +00:00
2022-11-14 03:11:07 +00:00
return $bannerImage->v;
}
public function directoryGetPopularPosts(Request $request)
{
2024-03-12 09:55:51 +00:00
$ids = Cache::remember('admin:api:popular_posts', 86400, function () {
2022-11-14 03:11:07 +00:00
return Status::whereLocal(true)
->whereScope('public')
->whereType('photo')
->whereNull(['in_reply_to_id', 'reblog_of_id'])
->orderByDesc('likes_count')
->take(50)
->pluck('id');
});
2024-03-12 09:55:51 +00:00
$res = $ids->map(function ($id) {
2022-11-14 03:11:07 +00:00
return StatusService::get($id);
})
2024-03-12 09:55:51 +00:00
->filter(function ($post) {
return $post && isset($post['account']);
})
->values();
2022-11-14 03:11:07 +00:00
2024-03-12 09:55:51 +00:00
return response()->json($res, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
2022-11-14 03:11:07 +00:00
}
public function directoryGetAddPostByIdSearch(Request $request)
{
$this->validate($request, [
2024-03-12 09:55:51 +00:00
'q' => 'required|integer',
2022-11-14 03:11:07 +00:00
]);
$id = $request->input('q');
$status = Status::whereLocal(true)
->whereType('photo')
->whereNull(['in_reply_to_id', 'reblog_of_id'])
->findOrFail($id);
$res = StatusService::get($status->id);
return $res;
}
public function directoryDeleteTestimonial(Request $request)
{
$this->validate($request, [
'profile_id' => 'required',
]);
$profile_id = $request->input('profile_id');
$testimonials = ConfigCache::whereK('pixelfed.directory.testimonials')->firstOrFail();
$existing = collect(json_decode($testimonials->v, true))
2024-03-12 09:55:51 +00:00
->filter(function ($t) use ($profile_id) {
2022-11-14 03:11:07 +00:00
return $t['profile_id'] !== $profile_id;
})
->values();
ConfigCacheService::put('pixelfed.directory.testimonials', $existing);
2024-03-12 09:55:51 +00:00
2022-11-14 03:11:07 +00:00
return $existing;
}
public function directorySaveTestimonial(Request $request)
{
$this->validate($request, [
'username' => 'required',
2024-03-12 09:55:51 +00:00
'body' => 'required|string|min:5|max:500',
2022-11-14 03:11:07 +00:00
]);
$user = User::whereUsername($request->input('username'))->whereNull('status')->firstOrFail();
$configCache = ConfigCache::firstOrCreate([
2024-03-12 09:55:51 +00:00
'k' => 'pixelfed.directory.testimonials',
2022-11-14 03:11:07 +00:00
]);
$testimonials = $configCache->v ? collect(json_decode($configCache->v, true)) : collect([]);
abort_if($testimonials->contains('profile_id', $user->profile_id), 422, 'Testimonial already exists');
abort_if($testimonials->count() == 10, 422, 'You can only have 10 active testimonials');
$testimonials->push([
'profile_id' => (string) $user->profile_id,
'username' => $request->input('username'),
2024-03-12 09:55:51 +00:00
'body' => $request->input('body'),
2022-11-14 03:11:07 +00:00
]);
$configCache->v = json_encode($testimonials->toArray());
$configCache->save();
ConfigCacheService::put('pixelfed.directory.testimonials', $configCache->v);
$res = [
'profile' => AccountService::get($user->profile_id),
2024-03-12 09:55:51 +00:00
'body' => $request->input('body'),
2022-11-14 03:11:07 +00:00
];
2024-03-12 09:55:51 +00:00
2022-11-14 03:11:07 +00:00
return $res;
}
public function directoryUpdateTestimonial(Request $request)
{
$this->validate($request, [
'profile_id' => 'required',
2024-03-12 09:55:51 +00:00
'body' => 'required|string|min:5|max:500',
2022-11-14 03:11:07 +00:00
]);
$profile_id = $request->input('profile_id');
$body = $request->input('body');
$user = User::whereProfileId($profile_id)->firstOrFail();
$configCache = ConfigCache::firstOrCreate([
2024-03-12 09:55:51 +00:00
'k' => 'pixelfed.directory.testimonials',
2022-11-14 03:11:07 +00:00
]);
$testimonials = $configCache->v ? collect(json_decode($configCache->v, true)) : collect([]);
2024-03-12 09:55:51 +00:00
$updated = $testimonials->map(function ($t) use ($profile_id, $body) {
if ($t['profile_id'] == $profile_id) {
2022-11-14 03:11:07 +00:00
$t['body'] = $body;
}
2024-03-12 09:55:51 +00:00
2022-11-14 03:11:07 +00:00
return $t;
})
2024-03-12 09:55:51 +00:00
->values();
2022-11-14 03:11:07 +00:00
$configCache->v = json_encode($updated);
$configCache->save();
ConfigCacheService::put('pixelfed.directory.testimonials', $configCache->v);
return $updated;
}
}