Skip to content

Firefly III: a personal finances manager

Fix #3974

James Cole048e5eeb3131
diff --git a/app/Http/Controllers/Account/EditController.php b/app/Http/Controllers/Account/EditController.php
index 576f9fb867..85655d0ed1 100644
--- a/app/Http/Controllers/Account/EditController.php
+++ b/app/Http/Controllers/Account/EditController.php
@@ -186,7 +186,6 @@ class EditController extends Controller
$this->repository->update($account, $data);
$request->session()->flash('success', (string) trans('firefly.updated_account', ['name' => $account->name]));
- app('preferences')->mark();
// store new attachment(s):
$files = $request->hasFile('attachments') ? $request->file('attachments') : null;
@@ -209,6 +208,7 @@ class EditController extends Controller
$redirect = redirect(route('accounts.edit', [$account->id]))->withInput(['return_to_edit' => 1]);
}
+ app('preferences')->mark();
return $redirect;
}
diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php
index 5b33937b6e..4b9e9cc4a4 100644
--- a/app/Http/Controllers/HomeController.php
+++ b/app/Http/Controllers/HomeController.php
@@ -119,7 +119,7 @@ class HomeController extends Controller
}
$subTitle = (string) trans('firefly.welcome_back');
$transactions = [];
- $frontPage = app('preferences')->get('frontPageAccounts', $repository->getAccountsByType([AccountType::ASSET])->pluck('id')->toArray());
+ $frontPage = app('preferences')->getFresh('frontPageAccounts', $repository->getAccountsByType([AccountType::ASSET])->pluck('id')->toArray());
/** @var Carbon $start */
$start = session('start', Carbon::now()->startOfMonth());
/** @var Carbon $end */
@@ -128,6 +128,8 @@ class HomeController extends Controller
$accounts = $repository->getAccountsById($frontPage->data);
$today = today(config('app.timezone'));
+ Log::debug('Frontpage accounts are ', $frontPage->data);
+
/** @var BillRepositoryInterface $billRepository */
$billRepository = app(BillRepositoryInterface::class);
$billCount = $billRepository->getBills()->count();
diff --git a/app/Services/Internal/Update/AccountUpdateService.php b/app/Services/Internal/Update/AccountUpdateService.php
index ae8c43b479..b26e7ebb5f 100644
--- a/app/Services/Internal/Update/AccountUpdateService.php
+++ b/app/Services/Internal/Update/AccountUpdateService.php
@@ -196,7 +196,9 @@ class AccountUpdateService
*/
private function updatePreferences(Account $account, array $data): void
{
+ Log::debug(sprintf('Now in updatePreferences(#%d)', $account->id));
if (array_key_exists('active', $data) && (false === $data['active'] || 0 === $data['active'])) {
+ Log::debug('Account was marked as inactive.');
$preference = app('preferences')->getForUser($account->user, 'frontpageAccounts');
if (null !== $preference) {
$removeAccountId = (int)$account->id;
@@ -210,8 +212,13 @@ class AccountUpdateService
);
Log::debug('Left with accounts', array_values($filtered));
app('preferences')->setForUser($account->user, 'frontpageAccounts', array_values($filtered));
+ app('preferences')->forget($account->user, 'frontpageAccounts');
+ return;
}
+ Log::debug("Found no frontpageAccounts preference, do nothing.");
+ return;
}
+ Log::debug('Account was not marked as inactive, do nothing.');
}
/**
diff --git a/app/Support/Preferences.php b/app/Support/Preferences.php
index 9e7deb6168..ddbef3d157 100644
--- a/app/Support/Preferences.php
+++ b/app/Support/Preferences.php
@@ -76,10 +76,6 @@ class Preferences
*/
public function findByName(string $name): Collection
{
- if ('testing' === config('app.env')) {
- Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
- }
-
return Preference::where('name', $name)->get();
}
@@ -91,9 +87,6 @@ class Preferences
*/
public function get(string $name, $default = null): ?Preference
{
- if ('testing' === config('app.env')) {
- Log::warning(sprintf('%s("%s") should NOT be called in the TEST environment!', __METHOD__, $name));
- }
/** @var User $user */
$user = auth()->user();
if (null === $user) {
@@ -106,6 +99,26 @@ class Preferences
return $this->getForUser($user, $name, $default);
}
+ /**
+ * @param string $name
+ * @param mixed $default
+ *
+ * @return \FireflyIII\Models\Preference|null
+ */
+ public function getFresh(string $name, $default = null): ?Preference
+ {
+ /** @var User $user */
+ $user = auth()->user();
+ if (null === $user) {
+ $preference = new Preference;
+ $preference->data = $default;
+
+ return $preference;
+ }
+
+ return $this->getFreshForUser($user, $name, $default);
+ }
+
/**
* @param \FireflyIII\User $user
* @param array $list
@@ -114,9 +127,6 @@ class Preferences
*/
public function getArrayForUser(User $user, array $list): array
{
- if ('testing' === config('app.env')) {
- Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
- }
$result = [];
$preferences = Preference::where('user_id', $user->id)->whereIn('name', $list)->get(['id', 'name', 'data']);
/** @var Preference $preference */
@@ -133,9 +143,9 @@ class Preferences
}
/**
- * @param User $user
- * @param string $name
- * @param null|string $default
+ * @param User $user
+ * @param string $name
+ * @param null|string $default
*
* @return \FireflyIII\Models\Preference|null
*/
@@ -143,9 +153,46 @@ class Preferences
{
$fullName = sprintf('preference%s%s', $user->id, $name);
if (Cache::has($fullName)) {
+ Log::debug(sprintf('Retrieved preference "%s" from cache ("%s").', $name, $fullName));
+
return Cache::get($fullName);
}
+ Log::debug(sprintf('Retrieved preference "%s" FRESH.', $name));
+ $preference = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data', 'updated_at', 'created_at']);
+ if (null !== $preference && null === $preference->data) {
+ try {
+ $preference->delete();
+ } catch (Exception $e) {
+ Log::debug(sprintf('Could not delete preference #%d: %s', $preference->id, $e->getMessage()));
+ }
+ $preference = null;
+ }
+
+ if (null !== $preference) {
+ Cache::forever($fullName, $preference);
+
+ return $preference;
+ }
+ // no preference found and default is null:
+ if (null === $default) {
+ // return NULL
+ return null;
+ }
+ return $this->setForUser($user, $name, $default);
+ }
+
+ /**
+ * @param User $user
+ * @param string $name
+ * @param null|string $default
+ *
+ * @return \FireflyIII\Models\Preference|null
+ */
+ public function getFreshForUser(User $user, string $name, $default = null): ?Preference
+ {
+ $fullName = sprintf('preference%s%s', $user->id, $name);
+ Log::debug(sprintf('Retrieved preference "%s" FRESH.', $name));
$preference = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data', 'updated_at', 'created_at']);
if (null !== $preference && null === $preference->data) {
try {
@@ -175,9 +222,6 @@ class Preferences
*/
public function lastActivity(): string
{
- if ('testing' === config('app.env')) {
- Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
- }
$lastActivity = microtime();
$preference = $this->get('lastActivity', microtime());
@@ -221,6 +265,18 @@ class Preferences
return $this->setForUser(auth()->user(), $name, $value);
}
+ /**
+ * @param User $user
+ * @param string $name
+ */
+ public function forget(User $user, string $name): void
+ {
+ $key = sprintf('preference%s%s', $user->id, $name);
+ Log::debug(sprintf('Going to forget key "%s"', $key));
+ Cache::forget($key);
+ Cache::put($key, '', 5);
+ }
+
/**
* @param \FireflyIII\User $user
* @param string $name
@@ -251,7 +307,7 @@ class Preferences
if (null !== $pref) {
$pref->data = $value;
$pref->save();
-
+ Log::debug(sprintf('Saved new value under existing preference object. "%s"', $fullName));
Cache::forever($fullName, $pref);
return $pref;
@@ -263,7 +319,7 @@ class Preferences
$pref->user()->associate($user);
$pref->save();
-
+ Log::debug(sprintf('Saved new value under new preference object. "%s"', $fullName));
Cache::forever($fullName, $pref);
return $pref;