Add Discord allowed mentions configuration
This commit is contained in:
@@ -21,6 +21,11 @@ class DeliverNotificationAction implements ShouldQueue
|
||||
|
||||
private const int DISCORD_CONTENT_LIMIT = 2000;
|
||||
|
||||
/**
|
||||
* @var list<string>
|
||||
*/
|
||||
private const array DISCORD_ALLOWED_MENTION_TYPES = ['everyone', 'roles', 'users'];
|
||||
|
||||
public int $tries = 3;
|
||||
|
||||
public int $timeout = 15;
|
||||
@@ -280,11 +285,53 @@ class DeliverNotificationAction implements ShouldQueue
|
||||
->post($notificationAction->delivery_url, [
|
||||
'content' => $message,
|
||||
'allowed_mentions' => [
|
||||
'parse' => [],
|
||||
'parse' => $this->discordAllowedMentions($notificationAction),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
private function discordAllowedMentions(NotificationAction $notificationAction): array
|
||||
{
|
||||
$configuredAllowedMentions = data_get(
|
||||
$notificationAction->channel_configuration,
|
||||
'allowed_mentions',
|
||||
[],
|
||||
);
|
||||
|
||||
if (! is_array($configuredAllowedMentions)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$allowedMentions = [];
|
||||
|
||||
foreach ($configuredAllowedMentions as $allowedMention) {
|
||||
if (! is_string($allowedMention)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$allowedMention = Str::lower(trim($allowedMention));
|
||||
|
||||
if ($allowedMention === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! in_array($allowedMention, self::DISCORD_ALLOWED_MENTION_TYPES, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (in_array($allowedMention, $allowedMentions, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$allowedMentions[] = $allowedMention;
|
||||
}
|
||||
|
||||
return $allowedMentions;
|
||||
}
|
||||
|
||||
private function sendWebhookRequest(NotificationAction $notificationAction): Response
|
||||
{
|
||||
return Http::acceptJson()
|
||||
|
||||
@@ -10,6 +10,11 @@ use Illuminate\Validation\Rule;
|
||||
|
||||
class StoreProjectNotificationActionRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* @var list<string>
|
||||
*/
|
||||
private const array DISCORD_ALLOWED_MENTION_TYPES = ['everyone', 'roles', 'users'];
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
@@ -29,6 +34,9 @@ class StoreProjectNotificationActionRequest extends FormRequest
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'event_type' => ['required', 'string', Rule::in($this->eventTypes())],
|
||||
'channel' => ['required', 'string', Rule::in($this->channels())],
|
||||
'channel_configuration' => ['nullable', 'array'],
|
||||
'channel_configuration.allowed_mentions' => ['nullable', 'array'],
|
||||
'channel_configuration.allowed_mentions.*' => $this->allowedMentionRules(),
|
||||
'enabled' => ['required', 'boolean'],
|
||||
'delivery_url' => ['required', 'string', 'max:2048', 'url'],
|
||||
'message_template' => $this->messageTemplateRules(),
|
||||
@@ -68,4 +76,16 @@ class StoreProjectNotificationActionRequest extends FormRequest
|
||||
|
||||
return ['nullable', 'string', 'max:2000'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, ValidationRule|string>
|
||||
*/
|
||||
private function allowedMentionRules(): array
|
||||
{
|
||||
return [
|
||||
'nullable',
|
||||
'string',
|
||||
Rule::in(self::DISCORD_ALLOWED_MENTION_TYPES),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,11 @@ use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdateProjectNotificationActionRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* @var list<string>
|
||||
*/
|
||||
private const array DISCORD_ALLOWED_MENTION_TYPES = ['everyone', 'roles', 'users'];
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
@@ -29,6 +34,9 @@ class UpdateProjectNotificationActionRequest extends FormRequest
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'event_type' => ['required', 'string', Rule::in($this->eventTypes())],
|
||||
'channel' => ['required', 'string', Rule::in($this->channels())],
|
||||
'channel_configuration' => ['nullable', 'array'],
|
||||
'channel_configuration.allowed_mentions' => ['nullable', 'array'],
|
||||
'channel_configuration.allowed_mentions.*' => $this->allowedMentionRules(),
|
||||
'enabled' => ['required', 'boolean'],
|
||||
'delivery_url' => ['nullable', 'string', 'max:2048', 'url'],
|
||||
'message_template' => $this->messageTemplateRules(),
|
||||
@@ -68,4 +76,16 @@ class UpdateProjectNotificationActionRequest extends FormRequest
|
||||
|
||||
return ['nullable', 'string', 'max:2000'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, ValidationRule|string>
|
||||
*/
|
||||
private function allowedMentionRules(): array
|
||||
{
|
||||
return [
|
||||
'nullable',
|
||||
'string',
|
||||
Rule::in(self::DISCORD_ALLOWED_MENTION_TYPES),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user