Browse Source

Update SettingsController, fix double json encoding and cache settings for 7 days

Daniel Supernault 2 years ago
parent
commit
4514ab1dbe

+ 1 - 1
app/Http/Controllers/SettingsController.php

@@ -303,7 +303,7 @@ class SettingsController extends Controller
 		}
 		}
 
 
 		if($changed) {
 		if($changed) {
-			$setting->compose_settings = json_encode($compose);
+			$setting->compose_settings = $compose;
 			$setting->save();
 			$setting->save();
 			Cache::forget('profile:compose:settings:' . $request->user()->id);
 			Cache::forget('profile:compose:settings:' . $request->user()->id);
 		}
 		}

+ 21 - 19
app/Services/AccountService.php

@@ -74,27 +74,29 @@ class AccountService
 
 
 	public static function settings($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'];
-				$ms = is_array($item) ? $item : [];
-				return array_merge($cs, $ms);
+		return Cache::remember('profile:compose:settings:' . $id, 604800, function() use($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'];
+					$ms = is_array($item) ? $item : [];
+					return array_merge($cs, $ms);
+				}
 
 
-			if($key == 'other') {
-				$other =  self::defaultSettings()['other'];
-				$mo = is_array($item) ? $item : [];
-				return array_merge($other, $mo);
-			}
-			return $item;
+				if($key == 'other') {
+					$other =  self::defaultSettings()['other'];
+					$mo = is_array($item) ? $item : [];
+					return array_merge($other, $mo);
+				}
+				return $item;
+			});
 		});
 		});
 	}
 	}
 
 

+ 38 - 0
database/migrations/2022_09_19_093029_fix_double_json_encoded_settings_in_usersettings_table.php

@@ -0,0 +1,38 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+use App\UserSetting;
+
+class FixDoubleJsonEncodedSettingsInUsersettingsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        UserSetting::whereNotNull('compose_settings')
+        ->chunk(50, function($settings) {
+            foreach($settings as $userSetting) {
+                if(is_array($userSetting->compose_settings)) {
+                    continue;
+                }
+                $userSetting->compose_settings = json_decode($userSetting->compose_settings);
+                $userSetting->save();
+            }
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+
+    }
+}