Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

refactor(notifications): standardize getRecipients method signatures

Andras Bacsaie7f32a1c443a
diff --git a/app/Models/InstanceSettings.php b/app/Models/InstanceSettings.php
index 5b89bb401c..ac95bb8a9e 100644
--- a/app/Models/InstanceSettings.php
+++ b/app/Models/InstanceSettings.php
@@ -3,16 +3,12 @@
namespace App\Models;
use App\Jobs\PullHelperImageJob;
-use App\Notifications\Channels\SendsEmail;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
-use Illuminate\Notifications\Notifiable;
use Spatie\Url\Url;
-class InstanceSettings extends Model implements SendsEmail
+class InstanceSettings extends Model
{
- use Notifiable;
-
protected $guarded = [];
protected $casts = [
@@ -92,15 +88,15 @@ class InstanceSettings extends Model implements SendsEmail
return InstanceSettings::findOrFail(0);
}
- public function getRecipients($notification)
- {
- $recipients = data_get($notification, 'emails', null);
- if (is_null($recipients) || $recipients === '') {
- return [];
- }
+ // public function getRecipients($notification)
+ // {
+ // $recipients = data_get($notification, 'emails', null);
+ // if (is_null($recipients) || $recipients === '') {
+ // return [];
+ // }
- return explode(',', $recipients);
- }
+ // return explode(',', $recipients);
+ // }
public function getTitleDisplayName(): string
{
diff --git a/app/Models/Team.php b/app/Models/Team.php
index 6796b22ad7..d36f8c1ab1 100644
--- a/app/Models/Team.php
+++ b/app/Models/Team.php
@@ -163,14 +163,17 @@ class Team extends Model implements SendsDiscord, SendsEmail, SendsPushover, Sen
];
}
- public function getRecipients($notification)
+ public function getRecipients(): array
{
- $recipients = data_get($notification, 'emails', null);
- if (is_null($recipients)) {
- return $this->members()->pluck('email')->toArray();
+ $recipients = $this->members()->pluck('email')->toArray();
+ $validatedEmails = array_filter($recipients, function ($email) {
+ return filter_var($email, FILTER_VALIDATE_EMAIL);
+ });
+ if (is_null($validatedEmails)) {
+ return [];
}
- return explode(',', $recipients);
+ return array_values($validatedEmails);
}
public function isAnyNotificationEnabled()
diff --git a/app/Models/User.php b/app/Models/User.php
index f59f506fc9..f9515ad093 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -116,9 +116,9 @@ class User extends Authenticatable implements SendsEmail
return $this->belongsToMany(Team::class)->withPivot('role');
}
- public function getRecipients($notification)
+ public function getRecipients(): array
{
- return $this->email;
+ return [$this->email];
}
public function sendVerificationEmail()
diff --git a/app/Notifications/Channels/EmailChannel.php b/app/Notifications/Channels/EmailChannel.php
index 215fae4ea8..2a18a2d343 100644
--- a/app/Notifications/Channels/EmailChannel.php
+++ b/app/Notifications/Channels/EmailChannel.php
@@ -13,7 +13,7 @@ class EmailChannel
{
try {
$this->bootConfigs($notifiable);
- $recipients = $notifiable->getRecipients($notification);
+ $recipients = $notifiable->getRecipients();
if (count($recipients) === 0) {
throw new Exception('No email recipients found');
}
diff --git a/app/Notifications/Channels/SendsEmail.php b/app/Notifications/Channels/SendsEmail.php
index 3adc6d0a21..7039a30665 100644
--- a/app/Notifications/Channels/SendsEmail.php
+++ b/app/Notifications/Channels/SendsEmail.php
@@ -4,5 +4,5 @@ namespace App\Notifications\Channels;
interface SendsEmail
{
- public function getRecipients($notification);
+ public function getRecipients(): array;
}
diff --git a/app/Notifications/Notification.php b/app/Notifications/Notification.php
new file mode 100644
index 0000000000..d37716a8b4
--- /dev/null
+++ b/app/Notifications/Notification.php
@@ -0,0 +1,22 @@
+<?php
+
+namespace Illuminate\Notifications;
+
+use App\Notifications\Channels\SendsEmail;
+use App\Notifications\Dto\DiscordMessage;
+use App\Notifications\Dto\PushoverMessage;
+use App\Notifications\Dto\SlackMessage;
+use Illuminate\Notifications\Messages\MailMessage;
+
+interface Notification
+{
+ public function toMail(SendsEmail $notifiable): MailMessage;
+
+ public function toPushover(): PushoverMessage;
+
+ public function toDiscord(): DiscordMessage;
+
+ public function toSlack(): SlackMessage;
+
+ public function toTelegram();
+}