mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-09 16:24:51 +00:00
Update AccountService, add dynamic user settings methods
This commit is contained in:
parent
c4d3851afe
commit
2aa73c1ffa
2 changed files with 91 additions and 8 deletions
|
@ -5,6 +5,7 @@ namespace App\Services;
|
|||
use Cache;
|
||||
use App\Profile;
|
||||
use App\Status;
|
||||
use App\UserSetting;
|
||||
use App\Transformer\Api\AccountTransformer;
|
||||
use League\Fractal;
|
||||
use League\Fractal\Serializer\ArraySerializer;
|
||||
|
@ -17,14 +18,7 @@ class AccountService
|
|||
|
||||
public static function get($id, $softFail = false)
|
||||
{
|
||||
if($id > PHP_INT_MAX || $id < 1) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$key = self::CACHE_KEY . $id;
|
||||
$ttl = now()->addHours(12);
|
||||
|
||||
return Cache::remember($key, $ttl, function() use($id, $softFail) {
|
||||
return Cache::remember(self::CACHE_KEY . $id, 43200, function() use($id, $softFail) {
|
||||
$fractal = new Fractal\Manager();
|
||||
$fractal->setSerializer(new ArraySerializer());
|
||||
$profile = Profile::find($id);
|
||||
|
@ -44,6 +38,63 @@ class AccountService
|
|||
return Cache::forget(self::CACHE_KEY . $id);
|
||||
}
|
||||
|
||||
public static function settings($id)
|
||||
{
|
||||
$settings = UserSetting::whereUserId($id)->first();
|
||||
if(!$settings) {
|
||||
return self::defaultSettings();
|
||||
}
|
||||
return collect($settings)
|
||||
->filter(function($item, $key) {
|
||||
return in_array($key, array_keys(self::defaultSettings())) == true;
|
||||
})
|
||||
->map(function($item, $key) {
|
||||
if($key == 'compose_settings') {
|
||||
$cs = self::defaultSettings()['compose_settings'];
|
||||
return array_merge($cs, $item ?? []);
|
||||
}
|
||||
|
||||
if($key == 'other') {
|
||||
$other = self::defaultSettings()['other'];
|
||||
return array_merge($other, $item ?? []);
|
||||
}
|
||||
return $item;
|
||||
});
|
||||
}
|
||||
|
||||
public static function canEmbed($id)
|
||||
{
|
||||
return self::settings($id)['other']['disable_embeds'] == false;
|
||||
}
|
||||
|
||||
public static function defaultSettings()
|
||||
{
|
||||
return [
|
||||
'crawlable' => true,
|
||||
'public_dm' => false,
|
||||
'reduce_motion' => false,
|
||||
'high_contrast_mode' => false,
|
||||
'video_autoplay' => false,
|
||||
'show_profile_follower_count' => true,
|
||||
'show_profile_following_count' => true,
|
||||
'compose_settings' => [
|
||||
'default_scope' => 'public',
|
||||
'default_license' => 1,
|
||||
'media_descriptions' => false
|
||||
],
|
||||
'other' => [
|
||||
'advanced_atom' => false,
|
||||
'disable_embeds' => false,
|
||||
'mutual_mention_notifications' => false,
|
||||
'hide_collections' => false,
|
||||
'hide_like_counts' => false,
|
||||
'hide_groups' => false,
|
||||
'hide_stories' => false,
|
||||
'disable_cw' => false,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public static function syncPostCount($id)
|
||||
{
|
||||
$profile = Profile::find($id);
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddMoreSettingsToUserSettingsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('user_settings', function (Blueprint $table) {
|
||||
$table->json('other')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('user_settings', function (Blueprint $table) {
|
||||
$table->dropColumn('other');
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue