Also support Discord.

This commit is contained in:
James Cole
2023-08-15 13:52:30 +02:00
parent 33a841b831
commit 00de78b6f1
17 changed files with 200 additions and 33 deletions

View File

@@ -26,6 +26,7 @@ namespace FireflyIII\Handlers\Events\Model;
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray; use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
use FireflyIII\Events\Model\Rule\RuleActionFailedOnObject; use FireflyIII\Events\Model\Rule\RuleActionFailedOnObject;
use FireflyIII\Notifications\User\RuleActionFailed; use FireflyIII\Notifications\User\RuleActionFailed;
use FireflyIII\Support\Facades\Preferences;
use Illuminate\Support\Facades\Notification; use Illuminate\Support\Facades\Notification;
/** /**
@@ -40,12 +41,16 @@ class RuleHandler
*/ */
public function ruleActionFailedOnArray(RuleActionFailedOnArray $event): void public function ruleActionFailedOnArray(RuleActionFailedOnArray $event): void
{ {
app('log')->debug('Now in ruleActionFailed');
$ruleAction = $event->ruleAction; $ruleAction = $event->ruleAction;
$rule = $ruleAction->rule; $rule = $ruleAction->rule;
$journal = $event->journal; $preference = Preferences::getForUser($rule->user, 'notification_rule_action_failures', true)->data;
$error = $event->error; if (false === $preference) {
$user = $ruleAction->rule->user; return;
}
app('log')->debug('Now in ruleActionFailedOnArray');
$journal = $event->journal;
$error = $event->error;
$user = $ruleAction->rule->user;
$mainMessage = trans('rules.main_message', ['rule' => $rule->title, 'action' => $ruleAction->action_type, 'group' => $journal['transaction_group_id'], 'error' => $error]); $mainMessage = trans('rules.main_message', ['rule' => $rule->title, 'action' => $ruleAction->action_type, 'group' => $journal['transaction_group_id'], 'error' => $error]);
$groupTitle = $journal['description'] ?? ''; $groupTitle = $journal['description'] ?? '';
@@ -65,12 +70,16 @@ class RuleHandler
*/ */
public function ruleActionFailedOnObject(RuleActionFailedOnObject $event): void public function ruleActionFailedOnObject(RuleActionFailedOnObject $event): void
{ {
app('log')->debug('Now in ruleActionFailed');
$ruleAction = $event->ruleAction; $ruleAction = $event->ruleAction;
$rule = $ruleAction->rule; $rule = $ruleAction->rule;
$journal = $event->journal; $preference = Preferences::getForUser($rule->user, 'notification_rule_action_failures', true)->data;
$error = $event->error; if (false === $preference) {
$user = $ruleAction->rule->user; return;
}
app('log')->debug('Now in ruleActionFailedOnObject');
$journal = $event->journal;
$error = $event->error;
$user = $ruleAction->rule->user;
$mainMessage = trans('rules.main_message', ['rule' => $rule->title, 'action' => $ruleAction->action_type, 'group' => $journal->transaction_group_id, 'error' => $error]); $mainMessage = trans('rules.main_message', ['rule' => $rule->title, 'action' => $ruleAction->action_type, 'group' => $journal->transaction_group_id, 'error' => $error]);
$groupTitle = $journal->description ?? ''; $groupTitle = $journal->description ?? '';

View File

@@ -27,6 +27,7 @@ use FireflyIII\Events\AdminRequestedTestMessage;
use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Middleware\IsDemoUser; use FireflyIII\Http\Middleware\IsDemoUser;
use FireflyIII\Support\Facades\FireflyConfig; use FireflyIII\Support\Facades\FireflyConfig;
use FireflyIII\Support\Notifications\UrlValidator;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
@@ -99,7 +100,7 @@ class HomeController extends Controller
if ('' === $url) { if ('' === $url) {
FireflyConfig::delete('slack_webhook_url'); FireflyConfig::delete('slack_webhook_url');
} }
if (str_starts_with($url, 'https://hooks.slack.com/services/')) { if (UrlValidator::isValidWebhookURL($url)) {
FireflyConfig::set('slack_webhook_url', $url); FireflyConfig::set('slack_webhook_url', $url);
} }

View File

@@ -28,6 +28,7 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType; use FireflyIII\Models\AccountType;
use FireflyIII\Models\Preference; use FireflyIII\Models\Preference;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Support\Notifications\UrlValidator;
use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@@ -207,7 +208,7 @@ class PreferencesController extends Controller
// slack URL: // slack URL:
if (!auth()->user()->hasRole('demo')) { if (!auth()->user()->hasRole('demo')) {
$url = (string)$request->get('slackUrl'); $url = (string)$request->get('slackUrl');
if (str_starts_with($url, 'https://hooks.slack.com/services/')) { if (UrlValidator::isValidWebhookURL($url)) {
app('preferences')->set('slack_webhook_url', $url); app('preferences')->set('slack_webhook_url', $url);
} }
if ('' === $url) { if ('' === $url) {

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Notifications\Admin; namespace FireflyIII\Notifications\Admin;
use FireflyIII\Support\Notifications\UrlValidator;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\MailMessage;
@@ -99,9 +100,9 @@ class TestNotification extends Notification
public function via($notifiable) public function via($notifiable)
{ {
/** @var User|null $user */ /** @var User|null $user */
$user = auth()->user(); $user = auth()->user();
$slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data; $slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data;
if (str_starts_with($slackUrl, 'https://hooks.slack.com/services/')) { if (UrlValidator::isValidWebhookURL($slackUrl)) {
return ['mail', 'slack']; return ['mail', 'slack'];
} }
return ['mail']; return ['mail'];

View File

@@ -25,6 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Notifications\Admin; namespace FireflyIII\Notifications\Admin;
use FireflyIII\Models\InvitedUser; use FireflyIII\Models\InvitedUser;
use FireflyIII\Support\Notifications\UrlValidator;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\MailMessage;
@@ -102,9 +103,9 @@ class UserInvitation extends Notification
public function via($notifiable) public function via($notifiable)
{ {
/** @var User|null $user */ /** @var User|null $user */
$user = auth()->user(); $user = auth()->user();
$slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data; $slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data;
if (str_starts_with($slackUrl, 'https://hooks.slack.com/services/')) { if (UrlValidator::isValidWebhookURL($slackUrl)) {
return ['mail', 'slack']; return ['mail', 'slack'];
} }
return ['mail']; return ['mail'];

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Notifications\Admin; namespace FireflyIII\Notifications\Admin;
use FireflyIII\Support\Notifications\UrlValidator;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\MailMessage;
@@ -99,9 +100,9 @@ class UserRegistration extends Notification
public function via($notifiable) public function via($notifiable)
{ {
/** @var User|null $user */ /** @var User|null $user */
$user = auth()->user(); $user = auth()->user();
$slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data; $slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data;
if (str_starts_with($slackUrl, 'https://hooks.slack.com/services/')) { if (UrlValidator::isValidWebhookURL($slackUrl)) {
return ['mail', 'slack']; return ['mail', 'slack'];
} }
return ['mail']; return ['mail'];

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Notifications\Admin; namespace FireflyIII\Notifications\Admin;
use FireflyIII\Support\Notifications\UrlValidator;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\MailMessage;
@@ -105,7 +106,7 @@ class VersionCheckResult extends Notification
/** @var User|null $user */ /** @var User|null $user */
$user = auth()->user(); $user = auth()->user();
$slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data; $slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data;
if (str_starts_with($slackUrl, 'https://hooks.slack.com/services/')) { if (UrlValidator::isValidWebhookURL($slackUrl)) {
return ['mail', 'slack']; return ['mail', 'slack'];
} }
return ['mail']; return ['mail'];

View File

@@ -25,6 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Notifications\User; namespace FireflyIII\Notifications\User;
use FireflyIII\Models\Bill; use FireflyIII\Models\Bill;
use FireflyIII\Support\Notifications\UrlValidator;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\MailMessage;
@@ -38,8 +39,8 @@ class BillReminder extends Notification
{ {
use Queueable; use Queueable;
private Bill $bill; private Bill $bill;
private int $diff; private int $diff;
private string $field; private string $field;
/** /**
@@ -49,9 +50,9 @@ class BillReminder extends Notification
*/ */
public function __construct(Bill $bill, string $field, int $diff) public function __construct(Bill $bill, string $field, int $diff)
{ {
$this->bill = $bill; $this->bill = $bill;
$this->field = $field; $this->field = $field;
$this->diff = $diff; $this->diff = $diff;
} }
/** /**
@@ -101,7 +102,7 @@ class BillReminder extends Notification
$message = (string)trans(sprintf('email.bill_warning_subject_now_%s', $this->field), ['diff' => $this->diff, 'name' => $this->bill->name]); $message = (string)trans(sprintf('email.bill_warning_subject_now_%s', $this->field), ['diff' => $this->diff, 'name' => $this->bill->name]);
} }
$bill = $this->bill; $bill = $this->bill;
$url = route('bills.show', [$bill->id]); $url = route('bills.show', [$bill->id]);
return (new SlackMessage()) return (new SlackMessage())
->warning() ->warning()
->attachment(function ($attachment) use ($bill, $url) { ->attachment(function ($attachment) use ($bill, $url) {
@@ -120,9 +121,9 @@ class BillReminder extends Notification
public function via($notifiable) public function via($notifiable)
{ {
/** @var User|null $user */ /** @var User|null $user */
$user = auth()->user(); $user = auth()->user();
$slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data; $slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data;
if (str_starts_with($slackUrl, 'https://hooks.slack.com/services/')) { if (UrlValidator::isValidWebhookURL($slackUrl)) {
return ['mail', 'slack']; return ['mail', 'slack'];
} }
return ['mail']; return ['mail'];

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Notifications\User; namespace FireflyIII\Notifications\User;
use FireflyIII\Support\Notifications\UrlValidator;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\MailMessage;
@@ -42,9 +43,7 @@ class NewAccessToken extends Notification
* *
* @return void * @return void
*/ */
public function __construct() public function __construct() {}
{
}
/** /**
* Get the array representation of the notification. * Get the array representation of the notification.
@@ -96,9 +95,9 @@ class NewAccessToken extends Notification
public function via($notifiable) public function via($notifiable)
{ {
/** @var User|null $user */ /** @var User|null $user */
$user = auth()->user(); $user = auth()->user();
$slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data; $slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data;
if (str_starts_with($slackUrl, 'https://hooks.slack.com/services/')) { if (UrlValidator::isValidWebhookURL($slackUrl)) {
return ['mail', 'slack']; return ['mail', 'slack'];
} }
return ['mail']; return ['mail'];

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Notifications\User; namespace FireflyIII\Notifications\User;
use FireflyIII\Support\Notifications\UrlValidator;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\SlackMessage; use Illuminate\Notifications\Messages\SlackMessage;
@@ -106,7 +107,7 @@ class RuleActionFailed extends Notification
/** @var User|null $user */ /** @var User|null $user */
$user = auth()->user(); $user = auth()->user();
$slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data; $slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data;
if (str_starts_with($slackUrl, 'https://hooks.slack.com/services/')) { if (UrlValidator::isValidWebhookURL($slackUrl)) {
app('log')->debug('Will send ruleActionFailed through Slack!'); app('log')->debug('Will send ruleActionFailed through Slack!');
return ['slack']; return ['slack'];
} }

View File

@@ -25,6 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Notifications\User; namespace FireflyIII\Notifications\User;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Support\Notifications\UrlValidator;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\MailMessage;
@@ -124,9 +125,9 @@ class UserLogin extends Notification
public function via($notifiable) public function via($notifiable)
{ {
/** @var User|null $user */ /** @var User|null $user */
$user = auth()->user(); $user = auth()->user();
$slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data; $slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data;
if (str_starts_with($slackUrl, 'https://hooks.slack.com/services/')) { if (UrlValidator::isValidWebhookURL($slackUrl)) {
return ['mail', 'slack']; return ['mail', 'slack'];
} }
return ['mail']; return ['mail'];

View File

@@ -0,0 +1,39 @@
<?php
/*
* UrlValidator.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Support\Notifications;
/**
* Class UrlValidator
*/
class UrlValidator
{
/**
* @param string $url
*
* @return bool
*/
public static function isValidWebhookURL(string $url): bool
{
return str_starts_with($url, 'https://hooks.slack.com/services/') || str_starts_with($url, 'https://discord.com/api/webhooks/');
}
}

View File

@@ -148,7 +148,7 @@ return [
'update_minimum_age' => 7, 'update_minimum_age' => 7,
// notifications // notifications
'available_notifications' => ['bill_reminder', 'new_access_token', 'transaction_creation', 'user_login'], 'available_notifications' => ['bill_reminder', 'new_access_token', 'transaction_creation', 'user_login', 'rule_action_failures'],
'admin_notifications' => ['admin_new_reg', 'user_new_reg', 'new_version', 'invite_created', 'invite_redeemed'], 'admin_notifications' => ['admin_new_reg', 'user_new_reg', 'new_version', 'invite_created', 'invite_redeemed'],
// enabled languages // enabled languages

View File

@@ -29,6 +29,7 @@ import axios from 'axios';
import store from "store"; import store from "store";
import observePlugin from 'store/plugins/observe'; import observePlugin from 'store/plugins/observe';
import Alpine from "alpinejs"; import Alpine from "alpinejs";
import * as bootstrap from 'bootstrap'
store.addPlugin(observePlugin); store.addPlugin(observePlugin);
window.store = store; window.store = store;

View File

@@ -0,0 +1,72 @@
/*
* create.js
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import '../../boot/bootstrap.js';
import dates from '../../pages/shared/dates.js';
import {createEmptySplit} from "./shared/create-empty-split.js";
import formatMoney from "../../util/format-money.js";
let transactions = function () {
return {
count: 0,
totalAmount: 0,
entries: [],
init() {
this.entries.push(createEmptySplit());
console.log('Ik ben init hoera');
},
addSplit() {
this.entries.push(createEmptySplit());
},
removeSplit(index) {
this.entries.splice(index, 1);
// fall back to index 0
const triggerFirstTabEl = document.querySelector('#split-0-tab')
triggerFirstTabEl.click();
//bootstrap.Tab.getInstance(triggerFirstTabEl).show() // Select first tab
},
formattedTotalAmount() {
return formatMoney(this.totalAmount, 'EUR');
}
}
}
let comps = {transactions, dates};
function loadPage() {
Object.keys(comps).forEach(comp => {
console.log(`Loading page component "${comp}"`);
let data = comps[comp]();
Alpine.data(comp, () => data);
});
Alpine.start();
}
// wait for load until bootstrapped event is received.
document.addEventListener('firefly-iii-bootstrapped', () => {
console.log('Loaded through event listener.');
loadPage();
});
// or is bootstrapped before event is triggered.
if (window.bootstrapped) {
console.log('Loaded through window variable.');
loadPage();
}

View File

@@ -0,0 +1,36 @@
/*
* create-empty-split.js
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
function getAccount() {
return {
id: '',
name: '',
};
}
export function createEmptySplit() {
return {
description: 'I am descr',
amount: '',
source_account: getAccount(),
destination_account: getAccount(),
};
}

View File

@@ -1355,6 +1355,7 @@ return [
'pref_notification_new_access_token' => 'Alert when a new API access token is created', 'pref_notification_new_access_token' => 'Alert when a new API access token is created',
'pref_notification_transaction_creation' => 'Alert when a transaction is created automatically', 'pref_notification_transaction_creation' => 'Alert when a transaction is created automatically',
'pref_notification_user_login' => 'Alert when you login from a new location', 'pref_notification_user_login' => 'Alert when you login from a new location',
'pref_notification_rule_action_failures' => 'Alert when rule actions fail to execute (Slack or Discord only)',
'pref_notifications' => 'Notifications', 'pref_notifications' => 'Notifications',
'pref_notifications_help' => 'Indicate if these are notifications you would like to get. Some notifications may contain sensitive financial information.', 'pref_notifications_help' => 'Indicate if these are notifications you would like to get. Some notifications may contain sensitive financial information.',
'slack_webhook_url' => 'Slack Webhook URL', 'slack_webhook_url' => 'Slack Webhook URL',
@@ -2291,6 +2292,7 @@ return [
'created_tag' => 'Tag ":tag" has been created!', 'created_tag' => 'Tag ":tag" has been created!',
'transaction_journal_information' => 'Transaction information', 'transaction_journal_information' => 'Transaction information',
'transaction_journal_amount' => 'Amount information',
'transaction_journal_meta' => 'Meta information', 'transaction_journal_meta' => 'Meta information',
'transaction_journal_more' => 'More information', 'transaction_journal_more' => 'More information',
'basic_journal_information' => 'Basic transaction information', 'basic_journal_information' => 'Basic transaction information',