1
0
Эх сурвалжийг харах

Update AccountService, add dynamic user settings methods

Daniel Supernault 3 жил өмнө
parent
commit
2aa73c1ffa

+ 59 - 8
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);

+ 32 - 0
database/migrations/2021_11_06_100552_add_more_settings_to_user_settings_table.php

@@ -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');
+		});
+	}
+}