Update AdminSettingsController, add AdminSettingsService

This commit is contained in:
Daniel Supernault 2024-03-18 05:55:38 -06:00
parent ac1f074889
commit dcc5f416ef
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1
2 changed files with 249 additions and 34 deletions

View file

@ -9,14 +9,13 @@ use App\Profile;
use App\Services\AccountService; use App\Services\AccountService;
use App\Services\AdminSettingsService; use App\Services\AdminSettingsService;
use App\Services\ConfigCacheService; use App\Services\ConfigCacheService;
use App\Services\FilesystemService;
use App\User; use App\User;
use App\Util\Site\Config; use App\Util\Site\Config;
use Artisan; use Artisan;
use Cache; use Cache;
use DB; use DB;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Services\Internal\BeagleService;
use App\Services\FilesystemService;
trait AdminSettingsController trait AdminSettingsController
{ {
@ -74,7 +73,7 @@ trait AdminSettingsController
'admin_account_id' => 'nullable', 'admin_account_id' => 'nullable',
'regs' => 'required|in:open,filtered,closed', 'regs' => 'required|in:open,filtered,closed',
'account_migration' => 'nullable', 'account_migration' => 'nullable',
'rule_delete' => 'sometimes' 'rule_delete' => 'sometimes',
]); ]);
$orb = false; $orb = false;
@ -347,7 +346,7 @@ trait AdminSettingsController
public function settingsApiRulesAdd(Request $request) public function settingsApiRulesAdd(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'rule' => 'required|string|min:5|max:1000' 'rule' => 'required|string|min:5|max:1000',
]); ]);
$rules = ConfigCacheService::get('app.rules'); $rules = ConfigCacheService::get('app.rules');
@ -367,6 +366,7 @@ trait AdminSettingsController
Cache::forget('api:v1:instance-data-response-v1'); Cache::forget('api:v1:instance-data-response-v1');
Cache::forget('api:v2:instance-data-response-v2'); Cache::forget('api:v2:instance-data-response-v2');
Config::refresh(); Config::refresh();
return [$val]; return [$val];
} }
@ -437,6 +437,7 @@ trait AdminSettingsController
} }
} }
ConfigCacheService::put('account.autofollow_usernames', implode(',', $names)); ConfigCacheService::put('account.autofollow_usernames', implode(',', $names));
return response()->json(['accounts' => array_values($names)]); return response()->json(['accounts' => array_values($names)]);
} }
@ -453,12 +454,22 @@ trait AdminSettingsController
$names = explode(',', $existing); $names = explode(',', $existing);
} }
$p = Profile::whereUsername($username)->whereNotNull('user_id')->first(); if ($existing && count($names)) {
if (count($names) >= 5) {
return response()->json(['message' => 'You can only add up to 5 accounts to be autofollowed.'], 400);
}
if (in_array(strtolower($username), array_map('strtolower', $names))) {
return response()->json(['message' => 'User already exists, please try again.'], 400);
}
}
$p = User::whereUsername($username)->whereNull('status')->first();
if (! $p || in_array($p->username, $names)) { if (! $p || in_array($p->username, $names)) {
abort(404); abort(404);
} }
array_push($names, strtolower($p->username)); array_push($names, $p->username);
ConfigCacheService::put('account.autofollow_usernames', implode(',', $names)); ConfigCacheService::put('account.autofollow_usernames', implode(',', $names));
return response()->json(['accounts' => array_values($names)]); return response()->json(['accounts' => array_values($names)]);
} }
@ -555,6 +566,7 @@ trait AdminSettingsController
Cache::forget('api:v2:instance-data-response-v2'); Cache::forget('api:v2:instance-data-response-v2');
Cache::forget('api:v1:instance-data:contact'); Cache::forget('api:v1:instance-data:contact');
Config::refresh(); Config::refresh();
return $request->all(); return $request->all();
} }
@ -652,6 +664,7 @@ trait AdminSettingsController
Cache::forget('api:v1:instance-data-response-v1'); Cache::forget('api:v1:instance-data-response-v1');
Cache::forget('api:v2:instance-data-response-v2'); Cache::forget('api:v2:instance-data-response-v2');
Config::refresh(); Config::refresh();
return $res; return $res;
} }
@ -717,6 +730,7 @@ trait AdminSettingsController
Cache::forget('api:v1:instance-data-response-v1'); Cache::forget('api:v1:instance-data-response-v1');
Cache::forget('api:v2:instance-data-response-v2'); Cache::forget('api:v2:instance-data-response-v2');
Config::refresh(); Config::refresh();
return $res; return $res;
} }
@ -726,11 +740,43 @@ trait AdminSettingsController
'require_email_verification' => 'required', 'require_email_verification' => 'required',
'enforce_account_limit' => 'required', 'enforce_account_limit' => 'required',
'admin_autofollow' => 'required', 'admin_autofollow' => 'required',
'admin_autofollow_accounts' => 'sometimes',
'max_user_blocks' => 'required', 'max_user_blocks' => 'required',
'max_user_mutes' => 'required', 'max_user_mutes' => 'required',
'max_domain_blocks' => 'required', 'max_domain_blocks' => 'required',
]); ]);
$adminAutofollow = $request->boolean('admin_autofollow');
$adminAutofollowAccounts = $request->input('admin_autofollow_accounts');
if ($adminAutofollow) {
if ($request->filled('admin_autofollow_accounts')) {
$names = [];
$existing = config_cache('account.autofollow_usernames');
if ($existing) {
$names = explode(',', $existing);
foreach (array_map('strtolower', $adminAutofollowAccounts) as $afc) {
if (in_array(strtolower($afc), array_map('strtolower', $names))) {
continue;
}
$names[] = $afc;
}
} else {
$names = $adminAutofollowAccounts;
}
if (! $names || count($names) == 0) {
return response()->json(['message' => 'You need to assign autofollow accounts before you can enable it.'], 400);
}
if (count($names) > 5) {
return response()->json(['message' => 'You can only add up to 5 accounts to be autofollowed.'.json_encode($names)], 400);
}
$autofollows = User::whereIn('username', $names)->whereNull('status')->pluck('username');
$adminAutofollowAccounts = $autofollows->implode(',');
ConfigCacheService::put('account.autofollow_usernames', $adminAutofollowAccounts);
} else {
return response()->json(['message' => 'You need to assign autofollow accounts before you can enable it.'], 400);
}
}
ConfigCacheService::put('pixelfed.enforce_email_verification', $request->boolean('require_email_verification')); ConfigCacheService::put('pixelfed.enforce_email_verification', $request->boolean('require_email_verification'));
ConfigCacheService::put('pixelfed.enforce_account_limit', $request->boolean('enforce_account_limit')); ConfigCacheService::put('pixelfed.enforce_account_limit', $request->boolean('enforce_account_limit'));
ConfigCacheService::put('account.autofollow', $request->boolean('admin_autofollow')); ConfigCacheService::put('account.autofollow', $request->boolean('admin_autofollow'));
@ -741,6 +787,7 @@ trait AdminSettingsController
'require_email_verification' => $request->boolean('require_email_verification'), 'require_email_verification' => $request->boolean('require_email_verification'),
'enforce_account_limit' => $request->boolean('enforce_account_limit'), 'enforce_account_limit' => $request->boolean('enforce_account_limit'),
'admin_autofollow' => $request->boolean('admin_autofollow'), 'admin_autofollow' => $request->boolean('admin_autofollow'),
'admin_autofollow_accounts' => $adminAutofollowAccounts,
'max_user_blocks' => $request->input('max_user_blocks'), 'max_user_blocks' => $request->input('max_user_blocks'),
'max_user_mutes' => $request->input('max_user_mutes'), 'max_user_mutes' => $request->input('max_user_mutes'),
'max_domain_blocks' => $request->input('max_domain_blocks'), 'max_domain_blocks' => $request->input('max_domain_blocks'),
@ -749,6 +796,7 @@ trait AdminSettingsController
Cache::forget('api:v1:instance-data-response-v1'); Cache::forget('api:v1:instance-data-response-v1');
Cache::forget('api:v2:instance-data-response-v2'); Cache::forget('api:v2:instance-data-response-v2');
Config::refresh(); Config::refresh();
return $res; return $res;
} }
@ -829,6 +877,7 @@ trait AdminSettingsController
Cache::forget('api:v1:instance-data-response-v1'); Cache::forget('api:v1:instance-data-response-v1');
Cache::forget('api:v2:instance-data-response-v2'); Cache::forget('api:v2:instance-data-response-v2');
Config::refresh(); Config::refresh();
return $res; return $res;
} }
} }

View file

@ -0,0 +1,166 @@
<?php
namespace App\Services;
use App\Services\Internal\BeagleService;
use App\User;
use Illuminate\Support\Str;
class AdminSettingsService
{
public static function getAll()
{
return [
'features' => self::getFeatures(),
'landing' => self::getLanding(),
'branding' => self::getBranding(),
'media' => self::getMedia(),
'rules' => self::getRules(),
'suggested_rules' => self::getSuggestedRules(),
'users' => self::getUsers(),
'posts' => self::getPosts(),
'platform' => self::getPlatform(),
'storage' => self::getStorage(),
];
}
public static function getFeatures()
{
$cloud_storage = (bool) config_cache('pixelfed.cloud_storage');
$cloud_disk = config('filesystems.cloud');
$cloud_ready = ! empty(config('filesystems.disks.'.$cloud_disk.'.key')) && ! empty(config('filesystems.disks.'.$cloud_disk.'.secret'));
$openReg = (bool) config_cache('pixelfed.open_registration');
$curOnboarding = (bool) config_cache('instance.curated_registration.enabled');
$regState = $openReg ? 'open' : ($curOnboarding ? 'filtered' : 'closed');
return [
'registration_status' => $regState,
'cloud_storage' => $cloud_ready && $cloud_storage,
'activitypub_enabled' => (bool) config_cache('federation.activitypub.enabled'),
'account_migration' => (bool) config_cache('federation.migration'),
'mobile_apis' => (bool) config_cache('pixelfed.oauth_enabled'),
'stories' => (bool) config_cache('instance.stories.enabled'),
'instagram_import' => (bool) config_cache('pixelfed.import.instagram.enabled'),
'autospam_enabled' => (bool) config_cache('pixelfed.bouncer.enabled'),
];
}
public static function getLanding()
{
$availableAdmins = User::whereIsAdmin(true)->get();
$currentAdmin = config_cache('instance.admin.pid');
return [
'admins' => $availableAdmins,
'current_admin' => $currentAdmin,
'show_directory' => (bool) config_cache('instance.landing.show_directory'),
'show_explore' => (bool) config_cache('instance.landing.show_explore'),
];
}
public static function getBranding()
{
return [
'name' => config_cache('app.name'),
'short_description' => config_cache('app.short_description'),
'long_description' => config_cache('app.description'),
];
}
public static function getMedia()
{
return [
'max_photo_size' => config_cache('pixelfed.max_photo_size'),
'max_album_length' => config_cache('pixelfed.max_album_length'),
'image_quality' => config_cache('pixelfed.image_quality'),
'media_types' => config_cache('pixelfed.media_types'),
'optimize_image' => (bool) config_cache('pixelfed.optimize_image'),
'optimize_video' => (bool) config_cache('pixelfed.optimize_video'),
];
}
public static function getRules()
{
return config_cache('app.rules') ? json_decode(config_cache('app.rules'), true) : [];
}
public static function getSuggestedRules()
{
return BeagleService::getDefaultRules();
}
public static function getUsers()
{
$autoFollow = config_cache('account.autofollow_usernames');
if (strlen($autoFollow) >= 2) {
$autoFollow = explode(',', $autoFollow);
} else {
$autoFollow = [];
}
return [
'require_email_verification' => (bool) config_cache('pixelfed.enforce_email_verification'),
'enforce_account_limit' => (bool) config_cache('pixelfed.enforce_account_limit'),
'max_account_size' => config_cache('pixelfed.max_account_size'),
'admin_autofollow' => (bool) config_cache('account.autofollow'),
'admin_autofollow_accounts' => $autoFollow,
'max_user_blocks' => (int) config_cache('instance.user_filters.max_user_blocks'),
'max_user_mutes' => (int) config_cache('instance.user_filters.max_user_mutes'),
'max_domain_blocks' => (int) config_cache('instance.user_filters.max_domain_blocks'),
];
}
public static function getPosts()
{
return [
'max_caption_length' => config_cache('pixelfed.max_caption_length'),
'max_altext_length' => config_cache('pixelfed.max_altext_length'),
];
}
public static function getPlatform()
{
return [
'allow_app_registration' => (bool) config_cache('pixelfed.allow_app_registration'),
'app_registration_rate_limit_attempts' => config_cache('pixelfed.app_registration_rate_limit_attempts'),
'app_registration_rate_limit_decay' => config_cache('pixelfed.app_registration_rate_limit_decay'),
'app_registration_confirm_rate_limit_attempts' => config_cache('pixelfed.app_registration_confirm_rate_limit_attempts'),
'app_registration_confirm_rate_limit_decay' => config_cache('pixelfed.app_registration_confirm_rate_limit_decay'),
'allow_post_embeds' => (bool) config_cache('instance.embed.post'),
'allow_profile_embeds' => (bool) config_cache('instance.embed.profile'),
'captcha_enabled' => (bool) config_cache('captcha.enabled'),
'captcha_on_login' => (bool) config_cache('captcha.active.login'),
'captcha_on_register' => (bool) config_cache('captcha.active.register'),
'captcha_secret' => Str::of(config_cache('captcha.secret'))->mask('*', 4, -4),
'captcha_sitekey' => Str::of(config_cache('captcha.sitekey'))->mask('*', 4, -4),
'custom_emoji_enabled' => (bool) config_cache('federation.custom_emoji.enabled'),
];
}
public static function getStorage()
{
$cloud_storage = (bool) config_cache('pixelfed.cloud_storage');
$cloud_disk = config('filesystems.cloud');
$cloud_ready = ! empty(config('filesystems.disks.'.$cloud_disk.'.key')) && ! empty(config('filesystems.disks.'.$cloud_disk.'.secret'));
$primaryDisk = (bool) $cloud_ready && $cloud_storage;
$pkey = 'filesystems.disks.'.$cloud_disk.'.';
$disk = [
'driver' => $cloud_disk,
'key' => Str::of(config_cache($pkey.'key'))->mask('*', 0, -2),
'secret' => Str::of(config_cache($pkey.'secret'))->mask('*', 0, -2),
'region' => config_cache($pkey.'region'),
'bucket' => config_cache($pkey.'bucket'),
'visibility' => config_cache($pkey.'visibility'),
'endpoint' => config_cache($pkey.'endpoint'),
'url' => config_cache($pkey.'url'),
'use_path_style_endpoint' => config_cache($pkey.'use_path_style_endpoint'),
];
return [
'primary_disk' => $primaryDisk ? 'cloud' : 'local',
'cloud_ready' => (bool) $cloud_ready,
'cloud_disk' => $cloud_disk,
'disk_config' => $disk,
];
}
}