diff --git a/app/Services/AccountService.php b/app/Services/AccountService.php index 7042b3c94..b57361069 100644 --- a/app/Services/AccountService.php +++ b/app/Services/AccountService.php @@ -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); diff --git a/database/migrations/2021_11_06_100552_add_more_settings_to_user_settings_table.php b/database/migrations/2021_11_06_100552_add_more_settings_to_user_settings_table.php new file mode 100644 index 000000000..00af978f2 --- /dev/null +++ b/database/migrations/2021_11_06_100552_add_more_settings_to_user_settings_table.php @@ -0,0 +1,32 @@ +json('other')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('user_settings', function (Blueprint $table) { + $table->dropColumn('other'); + }); + } +}