Update admin settings, add rules

This commit is contained in:
Daniel Supernault 2021-05-13 23:45:36 -06:00
parent 73d9799a38
commit a4efbb75d8
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7
3 changed files with 117 additions and 31 deletions

View File

@ -19,6 +19,7 @@ trait AdminSettingsController
$short_description = ConfigCacheService::get('app.short_description');
$description = ConfigCacheService::get('app.description');
$types = explode(',', ConfigCacheService::get('pixelfed.media_types'));
$rules = ConfigCacheService::get('app.rules') ? json_decode(ConfigCacheService::get('app.rules'), true) : null;
$jpeg = in_array('image/jpg', $types) ? true : in_array('image/jpeg', $types);
$png = in_array('image/png', $types);
$gif = in_array('image/gif', $types);
@ -31,7 +32,8 @@ trait AdminSettingsController
'jpeg',
'png',
'gif',
'mp4'
'mp4',
'rules'
));
}
@ -50,6 +52,19 @@ trait AdminSettingsController
'type_mp4' => 'nullable',
]);
if($request->filled('rule_delete')) {
$index = (int) $request->input('rule_delete');
$rules = ConfigCacheService::get('app.rules');
$json = json_decode($rules, true);
if(!$rules || empty($json)) {
return;
}
unset($json[$index]);
$json = json_encode(array_values($json));
ConfigCacheService::put('app.rules', $json);
return 200;
}
$media_types = explode(',', config_cache('pixelfed.media_types'));
$media_types_original = $media_types;
@ -115,6 +130,18 @@ trait AdminSettingsController
}
}
if($request->filled('new_rule')) {
$rules = ConfigCacheService::get('app.rules');
$val = $request->input('new_rule');
if(!$rules) {
ConfigCacheService::put('app.rules', json_encode([$val]));
} else {
$json = json_decode($rules, true);
$json[] = $val;
ConfigCacheService::put('app.rules', json_encode(array_values($json)));
}
}
Cache::forget('api:site:configuration:_v0.2');
return redirect('/i/admin/settings');

View File

@ -957,37 +957,49 @@ class ApiV1Controller extends Controller
*/
public function instance(Request $request)
{
$res = [
'approval_required' => false,
'contact_account' => null,
'description' => config_cache('app.description'),
'email' => config('instance.email'),
'invites_enabled' => false,
'rules' => [],
'short_description' => 'Pixelfed - Photo sharing for everyone',
'languages' => ['en'],
'max_toot_chars' => (int) config('pixelfed.max_caption_length'),
'registrations' => config_cache('pixelfed.open_registration'),
'stats' => [
'user_count' => 0,
'status_count' => 0,
'domain_count' => 0
],
'thumbnail' => config('app.url') . '/img/pixelfed-icon-color.png',
'title' => config_cache('app.name'),
'uri' => config('pixelfed.domain.app'),
'urls' => [],
'version' => '2.7.2 (compatible; Pixelfed ' . config('pixelfed.version') . ')',
'environment' => [
'max_photo_size' => (int) config_cache('pixelfed.max_photo_size'),
'max_avatar_size' => (int) config('pixelfed.max_avatar_size'),
'max_caption_length' => (int) config('pixelfed.max_caption_length'),
'max_bio_length' => (int) config('pixelfed.max_bio_length'),
'max_album_length' => (int) config_cache('pixelfed.max_album_length'),
'mobile_apis' => config_cache('pixelfed.oauth_enabled')
$res = Cache::remember('api:v1:instance-data', now()->addMinutes(15), function () {
$rules = config_cache('app.rules') ? collect(json_decode(config_cache('app.rules'), true))
->map(function($rule, $key) {
$id = $key + 1;
return [
'id' => "{$id}",
'text' => $rule
];
})
->toArray() : [];
$res = [
'approval_required' => false,
'contact_account' => null,
'description' => config_cache('app.description'),
'email' => config('instance.email'),
'invites_enabled' => false,
'rules' => $rules,
'short_description' => 'Pixelfed - Photo sharing for everyone',
'languages' => ['en'],
'max_toot_chars' => (int) config('pixelfed.max_caption_length'),
'registrations' => config_cache('pixelfed.open_registration'),
'stats' => [
'user_count' => 0,
'status_count' => 0,
'domain_count' => 0
],
'thumbnail' => config('app.url') . '/img/pixelfed-icon-color.png',
'title' => config_cache('app.name'),
'uri' => config('pixelfed.domain.app'),
'urls' => [],
'version' => '2.7.2 (compatible; Pixelfed ' . config('pixelfed.version') . ')',
'environment' => [
'max_photo_size' => (int) config_cache('pixelfed.max_photo_size'),
'max_avatar_size' => (int) config('pixelfed.max_avatar_size'),
'max_caption_length' => (int) config('pixelfed.max_caption_length'),
'max_bio_length' => (int) config('pixelfed.max_bio_length'),
'max_album_length' => (int) config_cache('pixelfed.max_album_length'),
'mobile_apis' => config_cache('pixelfed.oauth_enabled')
]
];
]
];
return $res;
});
return response()->json($res);
}

View File

@ -21,6 +21,9 @@
<li class="nav-item border-none">
<a class="nav-link font-weight-bold px-4" id="users-tab" data-toggle="tab" href="#users" role="tab" aria-controls="users">Users</a>
</li>
<li class="nav-item border-none">
<a class="nav-link font-weight-bold px-4" id="rules-tab" data-toggle="tab" href="#rules" role="tab" aria-controls="rules">Rules</a>
</li>
<li class="nav-item">
<a class="nav-link font-weight-bold px-4" id="advanced-tab" data-toggle="tab" href="#advanced" role="tab" aria-controls="advanced">Advanced</a>
</li>
@ -151,6 +154,35 @@
</div>
</div>
<div class="tab-pane" id="rules" role="tabpanel" aria-labelledby="rules-tab">
<div class="border-top">
<p class="lead mt-3 py-3 text-center">Add rules that explain what is acceptable use.</p>
</div>
<div class="ml-n4 mr-n2 p-3 bg-light border-top border-bottom">
<p class="font-weight-bold text-muted">Active Rules</p>
<ol class="font-weight-bold">
@if($rules)
@foreach($rules as $rule)
<li class="mb-4">
<p class="mb-0">
{{$rule}}
</p>
<p>
<button type="button" class="btn btn-outline-danger btn-sm py-0 rule-delete" data-index="{{$loop->index}}">Delete</button>
</p>
</li>
@endforeach
@endif
</ol>
</div>
<div class="form-group mb-0">
<div class="ml-n4 mr-n2 p-3 bg-light border-top border-bottom">
<label class="font-weight-bold text-muted">Add Rule</label>
<input class="form-control" name="new_rule" placeholder="Add a new rule, we recommend being descriptive but keeping it short"/>
</div>
</div>
</div>
<div class="tab-pane" id="advanced" role="tabpanel" aria-labelledby="advanced-tab">
<div class="form-group mb-0">
<div class="ml-n4 mr-n2 p-3 bg-light border-top border-bottom">
@ -181,3 +213,18 @@
</div>
@endif
@endsection
@push('scripts')
<script type="text/javascript">
$('.rule-delete').on('click', function(e) {
if(window.confirm('Are you sure you want to delete this rule?')) {
let idx = e.target.dataset.index;
axios.post(window.location.href, {
'rule_delete': idx
}).then(res => {
$('.rule-delete[data-index="'+idx+'"]').parents().eq(1).remove();
});
}
});
</script>
@endpush