Cleanup code.

This commit is contained in:
James Cole 2025-01-25 04:51:09 +01:00
parent a12e200a0a
commit 949d818bad
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
7 changed files with 74 additions and 48 deletions

View File

@ -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<int, int> $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'));
}

View File

@ -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)

View File

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

View File

@ -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

View File

@ -13,7 +13,7 @@
</p>
<textarea rows="30" cols="100" name="debug_info" id="debug_info"
style="font-family:Menlo, Monaco, Consolas, monospace;font-size:8pt;">
Debug information generated at {{ now }} for Firefly III version **v{{ FF_VERSION }}**.
Debug information generated at {{ now }} for Firefly III version **{% if not FF_IS_DEVELOP %}v{% endif %}{{ FF_VERSION }}**.
{{ table|escape('html') }}
</textarea>

View File

@ -12,7 +12,7 @@
{# Firefly III version #}
<tr>
<td>Firefly III</td>
<td>{% 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') }})
<td>{% if FF_IS_DEVELOP %}<!-- .Z9JBCmw64Zkx1pQw -->{% endif %}{% if not FF_IS_DEVELOP %}v{% endif %}{{ FF_VERSION }} / <span>#</span>{{ system.db_version }} (expects <span>#</span>{{ config('firefly.db_version') }})
</td>
</tr>
{# PHP version + settings #}
@ -83,8 +83,8 @@
<td>{{ config('logging.level') }}, {{ config('logging.default') }} / {{ app.audit_log_channel }}</td>
</tr>
<tr>
<td>Cache driver</td>
<td>{{ config('cache.default') }}</td>
<td>Cache driver, session driver</td>
<td>{{ config('cache.default') }}, {{ config('session.driver') }}</td>
</tr>
<tr>
<td>Default language and locale</td>
@ -92,7 +92,7 @@
</tr>
<tr>
<td>Trusted proxies</td>
<td>{{ config('firefly.trusted_proxies')|escape }}</td>
<td><code>{{ config('firefly.trusted_proxies')|escape }}</code></td>
</tr>
<tr>
<td>Login provider & user guard</td>
@ -114,6 +114,10 @@
<td>Mailer</td>
<td>{{ config('mail.default') }}</td>
</tr>
<tr>
<td>Exchange rates</td>
<td>{% if config('cer.enabled') %}Enabled{% else %}Disabled{% endif %}, downloads {% if config('cer.download_enabled') %}enabled{% else %}disabled{% endif %}</td>
</tr>
</tbody>
</table>
@ -136,6 +140,10 @@
<td>User flags</td>
<td>{{ user.user_flags | raw }}</td>
</tr>
<tr>
<td>Convert to native currency?</td>
<td>{% if user.convert_to_native %}Enabled{% else %}Disabled{% endif %}</td>
</tr>
<tr>
<td>Session start</td>
<td>{{ session('start') }}</td>

View File

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