Skip to content

Commit 8c540d8

Browse files
authored
Merge pull request #618 from AaronJackson/notification-sensitivity
Notification sensitivity
2 parents 37e7143 + a281018 commit 8c540d8

7 files changed

+108
-17
lines changed

app/HMS/Entities/Role.php

+21-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace HMS\Entities;
44

5+
use App\Notifications\NotificationSensitivityInterface;
6+
use App\Notifications\NotificationSensitivityType;
57
use Doctrine\Common\Collections\ArrayCollection;
68
use HMS\Helpers\Discord;
79
use HMS\Traits\Entities\SoftDeletable;
@@ -447,9 +449,11 @@ public function routeNotificationForSlack(): false|string
447449
/**
448450
* Route notifications to the Discord channel.
449451
*
452+
* @param mixed $notification
453+
*
450454
* @return null|string
451455
*/
452-
public function routeNotificationForDiscord(): ?string
456+
public function routeNotificationForDiscord($notification): ?string
453457
{
454458
if (! config('services.discord.token')) {
455459
return null;
@@ -468,15 +472,25 @@ public function routeNotificationForDiscord(): ?string
468472

469473
$discord = app(Discord::class);
470474

471-
if ($this->getDiscordPrivateChannel()) {
472-
return $discord->findChannelByName($this->getDiscordPrivateChannel())['id'];
473-
}
475+
$privateChannel = $discord->findChannelByName($this->getDiscordPrivateChannel())['id'];
476+
$publicChannel = $discord->findChannelByName($this->getDiscordChannel())['id'];
477+
478+
// If they are null it'll cancel the notification, so it's ok
479+
// to just return the whatever is returned from the ORM.
480+
if ($notification instanceof NotificationSensitivityInterface) {
481+
switch ($notification->getDiscordSensitivity()) {
482+
case NotificationSensitivityType::PRIVATE:
483+
return $privateChannel;
484+
485+
case NotificationSensitivityType::PUBLIC:
486+
return $publicChannel;
474487

475-
if ($this->getDiscordChannel()) {
476-
return $discord->findChannelByName($this->getDiscordChannel())['id'];
488+
case NotificationSensitivityType::ANY:
489+
break;
490+
}
477491
}
478492

479-
return null;
493+
return $privateChannel ? $privateChannel : $publicChannel;
480494
}
481495

482496
/**

app/HMS/Helpers/Discord.php

+21-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace HMS\Helpers;
44

5+
use Illuminate\Support\Facades\Cache;
56
use Illuminate\Support\Facades\Log;
67
use RestCord\DiscordClient;
78

@@ -186,6 +187,25 @@ public function listGuildMembers()
186187
return $this->members;
187188
}
188189

190+
/**
191+
* Obtains an array of Discord channels.
192+
*
193+
* @return array
194+
*/
195+
private function getChannels()
196+
{
197+
// Avoid hitting redis if already in used for this instance of Discord
198+
if ($this->channels) {
199+
return $this->channels;
200+
}
201+
202+
$this->channels = Cache::remember('discord.channels', 3600, fn () => $this->client->guild->getGuildChannels([
203+
'guild.id' => $this->guildId,
204+
]));
205+
206+
return $this->channels;
207+
}
208+
189209
/**
190210
* Find a discord channel by name.
191211
*
@@ -195,13 +215,7 @@ public function listGuildMembers()
195215
*/
196216
public function findChannelByName(string $channelName)
197217
{
198-
if (! $this->channels) {
199-
$this->channels = $this->client->guild->getGuildChannels([
200-
'guild.id' => $this->guildId,
201-
]);
202-
}
203-
204-
foreach ($this->channels as $channel) {
218+
foreach ($this->getChannels() as $channel) {
205219
if ($channel['name'] == $channelName) {
206220
return $channel;
207221
}

app/Notifications/Banking/AuditResult.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Notifications\Banking;
44

5+
use App\Notifications\NotificationSensitivityInterface;
6+
use App\Notifications\NotificationSensitivityType;
57
use Carbon\Carbon;
68
use HMS\Entities\Role;
79
use HMS\Entities\User;
@@ -15,7 +17,7 @@
1517
use NotificationChannels\Discord\DiscordChannel;
1618
use NotificationChannels\Discord\DiscordMessage;
1719

18-
class AuditResult extends Notification implements ShouldQueue
20+
class AuditResult extends Notification implements ShouldQueue, NotificationSensitivityInterface
1921
{
2022
use Queueable;
2123

@@ -211,4 +213,16 @@ public function toDiscord($notifiable)
211213

212214
return (new DiscordMessage())->embed($embed);
213215
}
216+
217+
/**
218+
* Returns the sensitivity for notification routing to
219+
* Discord. e.g. whether it should go to the private or public
220+
* team channel.
221+
*
222+
* @return string
223+
*/
224+
public function getDiscordSensitivity()
225+
{
226+
return NotificationSensitivityType::PUBLIC;
227+
}
214228
}

app/Notifications/NewMemberApprovalNeeded.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use NotificationChannels\Discord\DiscordChannel;
1313
use NotificationChannels\Discord\DiscordMessage;
1414

15-
class NewMemberApprovalNeeded extends Notification implements ShouldQueue
15+
class NewMemberApprovalNeeded extends Notification implements ShouldQueue, NotificationSensitivityInterface
1616
{
1717
use Queueable;
1818

@@ -154,4 +154,16 @@ public function toDiscord($notifiable)
154154

155155
return DiscordMessage::create($message);
156156
}
157+
158+
/**
159+
* Returns the sensitivity for notification routing to
160+
* Discord. e.g. whether it should go to the private or public
161+
* team channel.
162+
*
163+
* @return string
164+
*/
165+
public function getDiscordSensitivity()
166+
{
167+
return NotificationSensitivityType::PRIVATE;
168+
}
157169
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\Notifications;
4+
5+
interface NotificationSensitivityInterface
6+
{
7+
/**
8+
* Returns the sensitivity for notification routing to
9+
* Discord. e.g. whether it should go to the private or public
10+
* team channel.
11+
*
12+
* @return string
13+
*/
14+
public function getDiscordSensitivity();
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Notifications;
4+
5+
abstract class NotificationSensitivityType
6+
{
7+
public const ANY = 'ANY';
8+
public const PUBLIC = 'PUBLIC';
9+
public const PRIVATE = 'PRIVATE';
10+
}

app/Notifications/NotifyIncommingRoleEmail.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use NotificationChannels\Discord\DiscordChannel;
1010
use NotificationChannels\Discord\DiscordMessage;
1111

12-
class NotifyIncommingRoleEmail extends Notification implements ShouldQueue
12+
class NotifyIncommingRoleEmail extends Notification implements ShouldQueue, NotificationSensitivityInterface
1313
{
1414
use Queueable;
1515

@@ -83,4 +83,16 @@ public function toDiscord($notifiable)
8383

8484
return (new DiscordMessage())->embed($embed);
8585
}
86+
87+
/**
88+
* Returns the sensitivity for notification routing to
89+
* Discord. e.g. whether it should go to the private or public
90+
* team channel.
91+
*
92+
* @return string
93+
*/
94+
public function getDiscordSensitivity()
95+
{
96+
return NotificationSensitivityType::PRIVATE;
97+
}
8698
}

0 commit comments

Comments
 (0)