Apply Mentions everywhere (#595)
* variables and mentions * fix lint * add missing changes * fix tests * update quilly, fix bugs * fix lint * apply fixes * apply fixes * Fix MentionParser * Apply Mentions everywhere * Fix MentionParserTest * Small refactoring * Fixing quill import issues * Polished email integration, added customer sender mail * Add missing changes * improve migration command --------- Co-authored-by: Frank <csskfaves@gmail.com> Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Integrations\Handlers;
|
||||
|
||||
use App\Open\MentionParser;
|
||||
use App\Service\Forms\FormSubmissionFormatter;
|
||||
use Illuminate\Support\Arr;
|
||||
use Vinkla\Hashids\Facades\Hashids;
|
||||
@@ -32,6 +33,9 @@ class DiscordIntegration extends AbstractIntegrationHandler
|
||||
|
||||
protected function getWebhookData(): array
|
||||
{
|
||||
$formatter = (new FormSubmissionFormatter($this->form, $this->submissionData))->outputStringsOnly();
|
||||
$formattedData = $formatter->getFieldsWithValue();
|
||||
|
||||
$settings = (array) $this->integrationData ?? [];
|
||||
$externalLinks = [];
|
||||
if (Arr::get($settings, 'link_open_form', true)) {
|
||||
@@ -50,8 +54,7 @@ class DiscordIntegration extends AbstractIntegrationHandler
|
||||
$blocks = [];
|
||||
if (Arr::get($settings, 'include_submission_data', true)) {
|
||||
$submissionString = '';
|
||||
$formatter = (new FormSubmissionFormatter($this->form, $this->submissionData))->outputStringsOnly();
|
||||
foreach ($formatter->getFieldsWithValue() as $field) {
|
||||
foreach ($formattedData as $field) {
|
||||
$tmpVal = is_array($field['value']) ? implode(',', $field['value']) : $field['value'];
|
||||
$submissionString .= '**' . ucfirst($field['name']) . '**: ' . $tmpVal . "\n";
|
||||
}
|
||||
@@ -80,8 +83,9 @@ class DiscordIntegration extends AbstractIntegrationHandler
|
||||
];
|
||||
}
|
||||
|
||||
$message = Arr::get($settings, 'message', 'New form submission');
|
||||
return [
|
||||
'content' => 'New submission for your form **' . $this->form->title . '**',
|
||||
'content' => (new MentionParser($message, $formattedData))->parse(),
|
||||
'tts' => false,
|
||||
'username' => config('app.name'),
|
||||
'avatar_url' => asset('img/logo.png'),
|
||||
|
||||
@@ -2,24 +2,51 @@
|
||||
|
||||
namespace App\Integrations\Handlers;
|
||||
|
||||
use App\Rules\OneEmailPerLine;
|
||||
use App\Notifications\Forms\FormEmailNotification;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use App\Notifications\Forms\FormSubmissionNotification;
|
||||
use App\Open\MentionParser;
|
||||
use App\Service\Forms\FormSubmissionFormatter;
|
||||
|
||||
class EmailIntegration extends AbstractEmailIntegrationHandler
|
||||
{
|
||||
public const RISKY_USERS_LIMIT = 120;
|
||||
|
||||
public static function getValidationRules(): array
|
||||
{
|
||||
return [
|
||||
'notification_emails' => ['required', new OneEmailPerLine()],
|
||||
'notification_reply_to' => 'email|nullable',
|
||||
'send_to' => 'required',
|
||||
'sender_name' => 'required',
|
||||
'sender_email' => 'email|nullable',
|
||||
'subject' => 'required',
|
||||
'email_content' => 'required',
|
||||
'include_submission_data' => 'boolean',
|
||||
'include_hidden_fields_submission_data' => ['nullable', 'boolean'],
|
||||
'reply_to' => 'nullable',
|
||||
];
|
||||
}
|
||||
|
||||
protected function shouldRun(): bool
|
||||
{
|
||||
return $this->integrationData->notification_emails && parent::shouldRun();
|
||||
return $this->integrationData->send_to && parent::shouldRun() && !$this->riskLimitReached();
|
||||
}
|
||||
|
||||
// To avoid phishing abuse we limit this feature for risky users
|
||||
private function riskLimitReached(): bool
|
||||
{
|
||||
// This is a per-workspace limit for risky workspaces
|
||||
if ($this->form->workspace->is_risky) {
|
||||
if ($this->form->workspace->submissions_count >= self::RISKY_USERS_LIMIT) {
|
||||
Log::error('!!!DANGER!!! Dangerous user detected! Attempting many email sending.', [
|
||||
'form_id' => $this->form->id,
|
||||
'workspace_id' => $this->form->workspace->id,
|
||||
]);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
@@ -28,19 +55,27 @@ class EmailIntegration extends AbstractEmailIntegrationHandler
|
||||
return;
|
||||
}
|
||||
|
||||
$subscribers = collect(preg_split("/\r\n|\n|\r/", $this->integrationData->notification_emails))
|
||||
if ($this->form->is_pro) { // For Send to field Mentions are Pro feature
|
||||
$formatter = (new FormSubmissionFormatter($this->form, $this->submissionData))->outputStringsOnly();
|
||||
$parser = new MentionParser($this->integrationData->send_to, $formatter->getFieldsWithValue());
|
||||
$sendTo = $parser->parse();
|
||||
} else {
|
||||
$sendTo = $this->integrationData->send_to;
|
||||
}
|
||||
|
||||
$recipients = collect(preg_split("/\r\n|\n|\r/", $sendTo))
|
||||
->filter(function ($email) {
|
||||
return filter_var($email, FILTER_VALIDATE_EMAIL);
|
||||
});
|
||||
Log::debug('Sending email notification', [
|
||||
'recipients' => $subscribers->toArray(),
|
||||
'recipients' => $recipients->toArray(),
|
||||
'form_id' => $this->form->id,
|
||||
'form_slug' => $this->form->slug,
|
||||
'mailer' => $this->mailer
|
||||
]);
|
||||
$subscribers->each(function ($subscriber) {
|
||||
$recipients->each(function ($subscriber) {
|
||||
Notification::route('mail', $subscriber)->notify(
|
||||
new FormSubmissionNotification($this->event, $this->integrationData, $this->mailer)
|
||||
new FormEmailNotification($this->event, $this->integrationData, $this->mailer)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Integrations\Handlers;
|
||||
|
||||
use App\Open\MentionParser;
|
||||
use App\Service\Forms\FormSubmissionFormatter;
|
||||
use Illuminate\Support\Arr;
|
||||
use Vinkla\Hashids\Facades\Hashids;
|
||||
@@ -32,6 +33,9 @@ class SlackIntegration extends AbstractIntegrationHandler
|
||||
|
||||
protected function getWebhookData(): array
|
||||
{
|
||||
$formatter = (new FormSubmissionFormatter($this->form, $this->submissionData))->outputStringsOnly();
|
||||
$formattedData = $formatter->getFieldsWithValue();
|
||||
|
||||
$settings = (array) $this->integrationData ?? [];
|
||||
$externalLinks = [];
|
||||
if (Arr::get($settings, 'link_open_form', true)) {
|
||||
@@ -46,20 +50,20 @@ class SlackIntegration extends AbstractIntegrationHandler
|
||||
$externalLinks[] = '*<' . $this->form->share_url . '?submission_id=' . $submissionId . '|✍️ ' . $this->form->editable_submissions_button_text . '>*';
|
||||
}
|
||||
|
||||
$message = Arr::get($settings, 'message', 'New form submission');
|
||||
$blocks = [
|
||||
[
|
||||
'type' => 'section',
|
||||
'text' => [
|
||||
'type' => 'mrkdwn',
|
||||
'text' => 'New submission for your form *' . $this->form->title . '*',
|
||||
'text' => (new MentionParser($message, $formattedData))->parse(),
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
if (Arr::get($settings, 'include_submission_data', true)) {
|
||||
$submissionString = '';
|
||||
$formatter = (new FormSubmissionFormatter($this->form, $this->submissionData))->outputStringsOnly();
|
||||
foreach ($formatter->getFieldsWithValue() as $field) {
|
||||
foreach ($formattedData as $field) {
|
||||
$tmpVal = is_array($field['value']) ? implode(',', $field['value']) : $field['value'];
|
||||
$submissionString .= '>*' . ucfirst($field['name']) . '*: ' . $tmpVal . " \n";
|
||||
}
|
||||
|
||||
@@ -1,113 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Integrations\Handlers;
|
||||
|
||||
use App\Mail\Forms\SubmissionConfirmationMail;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Stevebauman\Purify\Facades\Purify;
|
||||
|
||||
/**
|
||||
* Sends a confirmation to form respondant that form was submitted
|
||||
*/
|
||||
class SubmissionConfirmationIntegration extends AbstractEmailIntegrationHandler
|
||||
{
|
||||
public const RISKY_USERS_LIMIT = 120;
|
||||
|
||||
public static function getValidationRules(): array
|
||||
{
|
||||
return [
|
||||
'respondent_email' => [
|
||||
'required',
|
||||
'boolean',
|
||||
function ($attribute, $value, $fail) {
|
||||
if ($value !== true) {
|
||||
$fail('Need at least 1 email field.');
|
||||
}
|
||||
},
|
||||
],
|
||||
'confirmation_reply_to' => 'email|nullable',
|
||||
'notification_sender' => 'required',
|
||||
'notification_subject' => 'required',
|
||||
'notification_body' => 'required',
|
||||
'notifications_include_submission' => 'boolean'
|
||||
];
|
||||
}
|
||||
|
||||
protected function shouldRun(): bool
|
||||
{
|
||||
return !(!$this->form->is_pro) && parent::shouldRun() && !$this->riskLimitReached();
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
if (!$this->shouldRun()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$email = $this->getRespondentEmail();
|
||||
if (!$email) {
|
||||
return;
|
||||
}
|
||||
|
||||
Log::info('Sending submission confirmation', [
|
||||
'recipient' => $email,
|
||||
'form_id' => $this->form->id,
|
||||
'form_slug' => $this->form->slug,
|
||||
'mailer' => $this->mailer
|
||||
]);
|
||||
Mail::mailer($this->mailer)->to($email)->send(new SubmissionConfirmationMail($this->event, $this->integrationData));
|
||||
}
|
||||
|
||||
private function getRespondentEmail()
|
||||
{
|
||||
// Make sure we only have one email field in the form
|
||||
$emailFields = collect($this->form->properties)->filter(function ($field) {
|
||||
$hidden = $field['hidden'] ?? false;
|
||||
|
||||
return !$hidden && $field['type'] == 'email';
|
||||
});
|
||||
if ($emailFields->count() != 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isset($this->submissionData[$emailFields->first()['id']])) {
|
||||
$email = $this->submissionData[$emailFields->first()['id']];
|
||||
if ($this->validateEmail($email)) {
|
||||
return $email;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// To avoid phishing abuse we limit this feature for risky users
|
||||
private function riskLimitReached(): bool
|
||||
{
|
||||
// This is a per-workspace limit for risky workspaces
|
||||
if ($this->form->workspace->is_risky) {
|
||||
if ($this->form->workspace->submissions_count >= self::RISKY_USERS_LIMIT) {
|
||||
Log::error('!!!DANGER!!! Dangerous user detected! Attempting many email sending.', [
|
||||
'form_id' => $this->form->id,
|
||||
'workspace_id' => $this->form->workspace->id,
|
||||
]);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function validateEmail($email): bool
|
||||
{
|
||||
return (bool)filter_var($email, FILTER_VALIDATE_EMAIL);
|
||||
}
|
||||
|
||||
public static function formatData(array $data): array
|
||||
{
|
||||
return array_merge(parent::formatData($data), [
|
||||
'notification_body' => Purify::clean($data['notification_body'] ?? ''),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user