From 949d818bad9cb4bb5865be552c8d960dda24957c Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 25 Jan 2025 04:51:09 +0100 Subject: [PATCH] Cleanup code. --- .../Controllers/PreferencesController.php | 69 ++++++++++--------- app/Providers/AppServiceProvider.php | 14 ++++ app/Support/Cronjobs/BillWarningCronjob.php | 16 ++--- changelog.md | 2 +- resources/views/debug.twig | 2 +- resources/views/partials/debug-table.twig | 16 +++-- routes/api-noauth.php | 3 + 7 files changed, 74 insertions(+), 48 deletions(-) diff --git a/app/Http/Controllers/PreferencesController.php b/app/Http/Controllers/PreferencesController.php index 85606eb2ac..74b65c8a6c 100644 --- a/app/Http/Controllers/PreferencesController.php +++ b/app/Http/Controllers/PreferencesController.php @@ -31,6 +31,7 @@ use FireflyIII\Http\Requests\PreferencesRequest; use FireflyIII\Models\Account; use FireflyIII\Models\Preference; use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Support\Facades\Preferences; use FireflyIII\User; use Illuminate\Contracts\View\Factory; use Illuminate\Http\RedirectResponse; @@ -93,35 +94,35 @@ class PreferencesController extends Controller /** @var array $accountIds */ $accountIds = $accounts->pluck('id')->toArray(); $viewRange = app('navigation')->getViewRange(false); - $frontpageAccountsPref = app('preferences')->get('frontpageAccounts', $accountIds); + $frontpageAccountsPref = Preferences::get('frontpageAccounts', $accountIds); $frontpageAccounts = $frontpageAccountsPref->data; if (!is_array($frontpageAccounts)) { $frontpageAccounts = $accountIds; } $language = app('steam')->getLanguage(); $languages = config('firefly.languages'); - $locale = app('preferences')->get('locale', config('firefly.default_locale', 'equal'))->data; - $listPageSize = app('preferences')->get('listPageSize', 50)->data; - $darkMode = app('preferences')->get('darkMode', 'browser')->data; - $customFiscalYear = app('preferences')->get('customFiscalYear', 0)->data; - $fiscalYearStartStr = app('preferences')->get('fiscalYearStart', '01-01')->data; + $locale = Preferences::get('locale', config('firefly.default_locale', 'equal'))->data; + $listPageSize = Preferences::get('listPageSize', 50)->data; + $darkMode = Preferences::get('darkMode', 'browser')->data; + $customFiscalYear = Preferences::get('customFiscalYear', 0)->data; + $fiscalYearStartStr = Preferences::get('fiscalYearStart', '01-01')->data; $convertToNative = $this->convertToNative; if (is_array($fiscalYearStartStr)) { $fiscalYearStartStr = '01-01'; } $fiscalYearStart = sprintf('%s-%s', date('Y'), (string) $fiscalYearStartStr); - $tjOptionalFields = app('preferences')->get('transaction_journal_optional_fields', [])->data; + $tjOptionalFields = Preferences::get('transaction_journal_optional_fields', [])->data; $availableDarkModes = config('firefly.available_dark_modes'); // notifications settings - $slackUrl = app('preferences')->getEncrypted('slack_webhook_url', '')->data; - $pushoverAppToken = (string) app('preferences')->getEncrypted('pushover_app_token', '')->data; - $pushoverUserToken = (string) app('preferences')->getEncrypted('pushover_user_token', '')->data; - $ntfyServer = app('preferences')->getEncrypted('ntfy_server', 'https://ntfy.sh')->data; - $ntfyTopic = (string) app('preferences')->getEncrypted('ntfy_topic', '')->data; - $ntfyAuth = app('preferences')->get('ntfy_auth', false)->data; - $ntfyUser = app('preferences')->getEncrypted('ntfy_user', '')->data; - $ntfyPass = (string) app('preferences')->getEncrypted('ntfy_pass', '')->data; + $slackUrl = Preferences::getEncrypted('slack_webhook_url', '')->data; + $pushoverAppToken = (string) Preferences::getEncrypted('pushover_app_token', '')->data; + $pushoverUserToken = (string) Preferences::getEncrypted('pushover_user_token', '')->data; + $ntfyServer = Preferences::getEncrypted('ntfy_server', 'https://ntfy.sh')->data; + $ntfyTopic = (string) Preferences::getEncrypted('ntfy_topic', '')->data; + $ntfyAuth = Preferences::get('ntfy_auth', false)->data; + $ntfyUser = Preferences::getEncrypted('ntfy_user', '')->data; + $ntfyPass = (string) Preferences::getEncrypted('ntfy_pass', '')->data; $channels = config('notifications.channels'); $forcedAvailability = []; @@ -131,7 +132,7 @@ class PreferencesController extends Controller if (true === $info['enabled']) { $notifications[$key] = [ - 'enabled' => true === app('preferences')->get(sprintf('notification_%s', $key), true)->data, + 'enabled' => true === Preferences::get(sprintf('notification_%s', $key), true)->data, 'configurable' => $info['configurable'], ]; } @@ -221,7 +222,7 @@ class PreferencesController extends Controller foreach ($request->get('frontpageAccounts') as $id) { $frontpageAccounts[] = (int) $id; } - app('preferences')->set('frontpageAccounts', $frontpageAccounts); + Preferences::set('frontpageAccounts', $frontpageAccounts); } // extract notifications: @@ -229,15 +230,15 @@ class PreferencesController extends Controller foreach (config('notifications.notifications.user') as $key => $info) { $key = sprintf('notification_%s', $key); if (array_key_exists($key, $all)) { - app('preferences')->set($key, true); + Preferences::set($key, true); } if (!array_key_exists($key, $all)) { - app('preferences')->set($key, false); + Preferences::set($key, false); } } // view range: - app('preferences')->set('viewRange', $request->get('viewRange')); + Preferences::set('viewRange', $request->get('viewRange')); // forget session values: session()->forget('start'); session()->forget('end'); @@ -249,13 +250,13 @@ class PreferencesController extends Controller $variables = ['slack_webhook_url', 'pushover_app_token', 'pushover_user_token', 'ntfy_server', 'ntfy_topic', 'ntfy_user', 'ntfy_pass']; foreach ($variables as $variable) { if ('' === $all[$variable]) { - app('preferences')->delete($variable); + Preferences::delete($variable); } if ('' !== $all[$variable]) { - app('preferences')->setEncrypted($variable, $all[$variable]); + Preferences::setEncrypted($variable, $all[$variable]); } } - app('preferences')->set('ntfy_auth', $all['ntfy_auth'] ?? false); + Preferences::set('ntfy_auth', $all['ntfy_auth'] ?? false); } // convert native @@ -265,30 +266,30 @@ class PreferencesController extends Controller Log::debug('User sets convertToNative to true.'); event(new UserGroupChangedDefaultCurrency(auth()->user()->userGroup)); } - app('preferences')->set('convert_to_native', $convertToNative); + Preferences::set('convert_to_native', $convertToNative); // custom fiscal year $customFiscalYear = 1 === (int) $request->get('customFiscalYear'); $string = strtotime((string) $request->get('fiscalYearStart')); if (false !== $string) { $fiscalYearStart = date('m-d', $string); - app('preferences')->set('customFiscalYear', $customFiscalYear); - app('preferences')->set('fiscalYearStart', $fiscalYearStart); + Preferences::set('customFiscalYear', $customFiscalYear); + Preferences::set('fiscalYearStart', $fiscalYearStart); } // save page size: - app('preferences')->set('listPageSize', 50); + Preferences::set('listPageSize', 50); $listPageSize = (int) $request->get('listPageSize'); if ($listPageSize > 0 && $listPageSize < 1337) { - app('preferences')->set('listPageSize', $listPageSize); + Preferences::set('listPageSize', $listPageSize); } // language: /** @var Preference $currentLang */ - $currentLang = app('preferences')->get('language', 'en_US'); + $currentLang = Preferences::get('language', 'en_US'); $lang = $request->get('language'); if (array_key_exists($lang, config('firefly.languages'))) { - app('preferences')->set('language', $lang); + Preferences::set('language', $lang); } if ($currentLang->data !== $lang) { // this string is untranslated on purpose. @@ -299,7 +300,7 @@ class PreferencesController extends Controller if (!auth()->user()->hasRole('demo')) { $locale = (string) $request->get('locale'); $locale = '' === $locale ? null : $locale; - app('preferences')->set('locale', $locale); + Preferences::set('locale', $locale); } // optional fields for transactions: @@ -318,16 +319,16 @@ class PreferencesController extends Controller 'location' => array_key_exists('location', $setOptions), 'links' => array_key_exists('links', $setOptions), ]; - app('preferences')->set('transaction_journal_optional_fields', $optionalTj); + Preferences::set('transaction_journal_optional_fields', $optionalTj); // dark mode $darkMode = $request->get('darkMode') ?? 'browser'; if (in_array($darkMode, config('firefly.available_dark_modes'), true)) { - app('preferences')->set('darkMode', $darkMode); + Preferences::set('darkMode', $darkMode); } session()->flash('success', (string) trans('firefly.saved_preferences')); - app('preferences')->mark(); + Preferences::mark(); return redirect(route('preferences.index')); } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index ca3b82710a..9e2c43b3d2 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -23,6 +23,9 @@ declare(strict_types=1); namespace FireflyIII\Providers; +use Barryvdh\Debugbar\DataCollector\QueryCollector; +use Barryvdh\Debugbar\Facades\Debugbar; +use DebugBar\DebugBarException; use Illuminate\Support\Facades\Blade; use Illuminate\Support\Facades\Response; use Illuminate\Support\Facades\Route; @@ -50,6 +53,17 @@ class AppServiceProvider extends ServiceProvider $headers['X-Trace-Id'] = $uuid; } + if(config('app.debug')) { + try { + /** @var QueryCollector $collector */ + $collector = Debugbar::getCollector('queries'); + $info = $collector->collect(); + $headers['X-Debug-QueryCount'] = $info['nb_statements'] ?? 0; + } catch(DebugBarException $e) { + // ignore error. + } + } + return response() ->json($value) ->withHeaders($headers) diff --git a/app/Support/Cronjobs/BillWarningCronjob.php b/app/Support/Cronjobs/BillWarningCronjob.php index 720ca72c6a..0e711195d8 100644 --- a/app/Support/Cronjobs/BillWarningCronjob.php +++ b/app/Support/Cronjobs/BillWarningCronjob.php @@ -48,14 +48,14 @@ class BillWarningCronjob extends AbstractCronjob $diffForHumans = today(config('app.timezone'))->diffForHumans(Carbon::createFromTimestamp($lastTime), null, true); if (0 === $lastTime) { - app('log')->info('The bill warning cron-job has never fired before.'); + app('log')->info('The bill notification cron-job has never fired before.'); } // less than half a day ago: if ($lastTime > 0 && $diff <= 43200) { - app('log')->info(sprintf('It has been %s since the bill warning cron-job has fired.', $diffForHumans)); + app('log')->info(sprintf('It has been %s since the bill notification cron-job has fired.', $diffForHumans)); if (false === $this->force) { app('log')->info('The cron-job will not fire now.'); - $this->message = sprintf('It has been %s since the bill warning cron-job has fired. It will not fire now.', $diffForHumans); + $this->message = sprintf('It has been %s since the bill notification cron-job has fired. It will not fire now.', $diffForHumans); $this->jobFired = false; $this->jobErrored = false; $this->jobSucceeded = false; @@ -63,11 +63,11 @@ class BillWarningCronjob extends AbstractCronjob return; } - app('log')->info('Execution of the bill warning cron-job has been FORCED.'); + app('log')->info('Execution of the bill notification cron-job has been FORCED.'); } if ($lastTime > 0 && $diff > 43200) { - app('log')->info(sprintf('It has been %s since the bill warning cron-job has fired. It will fire now!', $diffForHumans)); + app('log')->info(sprintf('It has been %s since the bill notification cron-job has fired. It will fire now!', $diffForHumans)); } $this->fireWarnings(); @@ -77,7 +77,7 @@ class BillWarningCronjob extends AbstractCronjob private function fireWarnings(): void { - app('log')->info(sprintf('Will now fire bill warning job task for date "%s".', $this->date->format('Y-m-d H:i:s'))); + app('log')->info(sprintf('Will now fire bill notification job task for date "%s".', $this->date->format('Y-m-d H:i:s'))); /** @var WarnAboutBills $job */ $job = app(WarnAboutBills::class); @@ -89,10 +89,10 @@ class BillWarningCronjob extends AbstractCronjob $this->jobFired = true; $this->jobErrored = false; $this->jobSucceeded = true; - $this->message = 'Bill warning cron job fired successfully.'; + $this->message = 'Bill notification cron job fired successfully.'; app('fireflyconfig')->set('last_bw_job', (int) $this->date->format('U')); app('log')->info(sprintf('Marked the last time this job has run as "%s" (%d)', $this->date->format('Y-m-d H:i:s'), (int) $this->date->format('U'))); - app('log')->info('Done with bill warning cron job task.'); + app('log')->info('Done with bill notification cron job task.'); } } diff --git a/changelog.md b/changelog.md index 76b3ca40b6..801403ac19 100644 --- a/changelog.md +++ b/changelog.md @@ -7,7 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Added -- Multi-currency support. If you set `ENABLE_EXCHANGE_RATES=true` and optionally `ENABLE_EXTERNAL_RATES=true` Firefly III will try to calculate all foreign currencies back to your native currency. This is a work in progress, not all fields and all places will support this yet. Please check out the [documentation](https://docs.firefly-iii.org/explanation/financial-concepts/exchange-rates/). +- Multi-currency support. If you set `ENABLE_EXCHANGE_RATES=true` and optionally `ENABLE_EXTERNAL_RATES=true` Firefly III will have the abbility to calculate all foreign currencies back to your native currency. This is a work in progress, not all fields and all places will support this yet. Please check out the [documentation](https://docs.firefly-iii.org/explanation/financial-concepts/exchange-rates/). - Notifications support Nfty, Pushover, Slack and Discord. - Many new security related notifications. - [Issue 5523](https://github.com/firefly-iii/firefly-iii/issues/5523) (Add comment on a budget for a given month) reported by @n-serrette diff --git a/resources/views/debug.twig b/resources/views/debug.twig index 0492d0897a..d2e2872674 100644 --- a/resources/views/debug.twig +++ b/resources/views/debug.twig @@ -13,7 +13,7 @@

diff --git a/resources/views/partials/debug-table.twig b/resources/views/partials/debug-table.twig index 4b35f4bfac..4374f393b5 100644 --- a/resources/views/partials/debug-table.twig +++ b/resources/views/partials/debug-table.twig @@ -12,7 +12,7 @@ {# Firefly III version #} Firefly III - {% if FF_VERSION starts with 'develop' %}{{ FF_VERSION }}{% else %}{{ FF_VERSION }}{% endif %} / v{{ config('firefly.api_version') }} / {{ system.db_version }} (exp. {{ config('firefly.db_version') }}) + {% if FF_IS_DEVELOP %}{% endif %}{% if not FF_IS_DEVELOP %}v{% endif %}{{ FF_VERSION }} / #{{ system.db_version }} (expects #{{ config('firefly.db_version') }}) {# PHP version + settings #} @@ -83,8 +83,8 @@ {{ config('logging.level') }}, {{ config('logging.default') }} / {{ app.audit_log_channel }} - Cache driver - {{ config('cache.default') }} + Cache driver, session driver + {{ config('cache.default') }}, {{ config('session.driver') }} Default language and locale @@ -92,7 +92,7 @@ Trusted proxies - {{ config('firefly.trusted_proxies')|escape }} + {{ config('firefly.trusted_proxies')|escape }} Login provider & user guard @@ -114,6 +114,10 @@ Mailer {{ config('mail.default') }} + + Exchange rates + {% if config('cer.enabled') %}Enabled{% else %}Disabled{% endif %}, downloads {% if config('cer.download_enabled') %}enabled{% else %}disabled{% endif %} + @@ -136,6 +140,10 @@ User flags {{ user.user_flags | raw }} + + Convert to native currency? + {% if user.convert_to_native %}Enabled{% else %}Disabled{% endif %} + Session start {{ session('start') }} diff --git a/routes/api-noauth.php b/routes/api-noauth.php index 35e31c73fb..6799fa28dd 100644 --- a/routes/api-noauth.php +++ b/routes/api-noauth.php @@ -23,11 +23,14 @@ declare(strict_types=1); // Cron job API routes: +use FireflyIII\Http\Middleware\AcceptHeaders; + Route::group( [ 'namespace' => 'FireflyIII\Api\V1\Controllers\System', 'prefix' => '', 'as' => 'api.v1.cron.', + 'middleware' => [AcceptHeaders::class], ], static function (): void { Route::get('{cliToken}', ['uses' => 'CronController@cron', 'as' => 'index']);