Notification & Integrations refactoring (#346)

* Integrations Refactoring - WIP

* integrations list & edit - WIP

* Fix integration store binding issue

* integrations refactor - WIP

* Form integration - WIP

* Form integration Edit - WIP

* Integration Refactor - Slack - WIP

* Integration Refactor - Discord - WIP

* Integration Refactor - Webhook - WIP

* Integration Refactor - Send Submission Confirmation - WIP

* Integration Refactor - Backend handler - WIP

* Form Integration Status field

* Integration Refactor - Backend SubmissionConfirmation - WIP

* IntegrationMigration Command

* skip confirmation email test case

* Small refactoring

* FormIntegration status active/inactive

* formIntegrationData to integrationData

* Rename file name with Integration suffix for integration realted files

* Loader on form integrations

* WIP

* form integration test case

* WIP

* Added Integration card - working on refactoring

* change location for IntegrationCard and update package file

* Form Integration Create/Edit in single Modal

* Remove integration extra pages

* crisp_help_page_slug for integration json

* integration logic as collapse

* UI improvements

* WIP

* Trying to debug vue devtools

* WIP for integrations

* getIntegrationHandler change namespace name

* useForm for integration fields + validation structure

* Integration Test case & apply validation rules

* Apply useform changes to integration other files

* validation rules for FormNotificationsMessageActions fields

* Zapier integration as coming soon

* Update FormCleaner

* set default settings for confirmation integration

* WIP

* Finish validation for all integrations

* Updated purify, added integration formatData

* Fix testcase

* Ran pint; working on integration errors

* Handle integration events

* command for Delete Old Integration Events

* Display Past Events in Modal

* on Integration event create with status error send email to form creator

* Polish styling

* Minor improvements

* Finish badge and integration status

* Fix tests and add an integration event test

* Lint

---------

Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
formsdev
2024-03-28 22:44:30 +05:30
committed by GitHub
parent d9996e0d9d
commit 6f61faa9ef
84 changed files with 6121 additions and 2205 deletions

View File

@@ -0,0 +1,34 @@
<?php
namespace App\Console\Commands;
use App\Models\Integration\FormIntegrationsEvent;
use Illuminate\Console\Command;
class CleanIntegrationEvents extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'forms:integration-events-cleanup';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete Old Integration Events';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$response = FormIntegrationsEvent::where('created_at', '<', now()->subDays(14))->delete();
$this->line($response . ' Events Deleted');
}
}

View File

@@ -0,0 +1,107 @@
<?php
namespace App\Console\Commands;
use App\Models\Forms\Form;
use App\Models\Integration\FormIntegration;
use Illuminate\Console\Command;
class IntegrationMigration extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'forms:integration-migration';
/**
* The console command description.
*
* @var string
*/
protected $description = 'One Time Only -- Refactor integration';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
Form::chunk(
100,
function ($forms) {
foreach ($forms as $form) {
$this->line('Process For Form: ' . $form->id . ' - ' . $form->slug);
// Email
if ($form->notifies && $form->notification_emails) {
$this->createFormIntegration($form, 'email', [
'notification_reply_to' => $form->notification_settings->notification_reply_to,
'notification_emails' => $form->notification_emails
]);
}
// Submission Confirmation
if ($form->send_submission_confirmation) {
$this->createFormIntegration($form, 'submission_confirmation', [
'confirmation_reply_to' => $form->notification_settings->confirmation_reply_to,
'notification_sender' => $form->notification_sender,
'notification_subject' => $form->notification_subject,
'notification_body' => $form->notification_body,
'notifications_include_submission' => $form->notifications_include_submission,
]);
}
// Slack
if ($form->slack_webhook_url) {
$slackData = $form->notification_settings->slack;
$this->createFormIntegration($form, 'slack', [
'slack_webhook_url' => $form->slack_webhook_url,
'include_submission_data' => $slackData->include_submission_data ?? true,
'link_open_form' => $slackData->link_open_form ?? true,
'link_edit_form' => $slackData->link_edit_form ?? true,
'views_submissions_count' => $slackData->views_submissions_count ?? true,
'link_edit_submission' => $slackData->link_edit_submission ?? true
]);
}
// Discord
if ($form->discord_webhook_url) {
$discordData = $form->notification_settings->discord;
$this->createFormIntegration($form, 'discord', [
'discord_webhook_url' => $form->discord_webhook_url,
'include_submission_data' => $discordData->include_submission_data ?? true,
'link_open_form' => $discordData->link_open_form ?? true,
'link_edit_form' => $discordData->link_edit_form ?? true,
'views_submissions_count' => $discordData->views_submissions_count ?? true,
'link_edit_submission' => $discordData->link_edit_submission ?? true
]);
}
// Webhook
if ($form->webhook_url) {
$this->createFormIntegration($form, 'webhook', [
'webhook_url' => $form->webhook_url
]);
}
}
}
);
$this->line('Migration Done');
}
private function createFormIntegration(Form $form, $integration_id, $data = [])
{
$this->line('Form Integration Create: ' . $integration_id);
return FormIntegration::create([
'form_id' => $form->id,
'status' => FormIntegration::STATUS_ACTIVE,
'integration_id' => $integration_id,
'data' => $data,
'logic' => []
]);
}
}

View File

@@ -24,6 +24,7 @@ class Kernel extends ConsoleKernel
protected function schedule(Schedule $schedule)
{
$schedule->command('forms:database-cleanup')->hourly();
$schedule->command('forms:integration-events-cleanup')->daily();
}
/**
@@ -33,7 +34,7 @@ class Kernel extends ConsoleKernel
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
$this->load(__DIR__ . '/Commands');
require base_path('routes/console.php');
}

View File

@@ -0,0 +1,25 @@
<?php
namespace App\Events\Models;
use App\Models\Integration\FormIntegrationsEvent;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class FormIntegrationsEventCreated
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(public FormIntegrationsEvent $formIntegrationsEvent)
{
//
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace App\Http\Controllers\Forms\Integration;
use App\Http\Controllers\Controller;
use App\Http\Requests\Integration\FormIntegrationsRequest;
use App\Models\Forms\Form;
use App\Models\Integration\FormIntegration;
class FormIntegrationsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index(string $id)
{
$form = Form::findOrFail((int)$id);
$this->authorize('view', $form);
return FormIntegration::where('form_id', $form->id)->get();
}
public function create(FormIntegrationsRequest $request, string $id)
{
$form = Form::findOrFail((int)$id);
$this->authorize('update', $form);
$formIntegration = FormIntegration::create(
array_merge([
'form_id' => $form->id,
], $request->toIntegrationData())
);
return $this->success([
'message' => 'Form Integration was created.',
'form_integration' => $formIntegration
]);
}
public function update(FormIntegrationsRequest $request, string $id, string $integrationid)
{
$form = Form::findOrFail((int)$id);
$this->authorize('update', $form);
$formIntegration = FormIntegration::findOrFail((int)$integrationid);
$formIntegration->update($request->toIntegrationData());
return $this->success([
'message' => 'Form Integration was updated.',
'form_integration' => $formIntegration
]);
}
public function destroy(string $id, string $integrationid)
{
$form = Form::findOrFail((int)$id);
$this->authorize('update', $form);
$formIntegration = FormIntegration::findOrFail((int)$integrationid);
$formIntegration->delete();
return $this->success([
'message' => 'Form Integration was deleted.'
]);
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Http\Controllers\Forms\Integration;
use App\Http\Controllers\Controller;
use App\Http\Resources\FormIntegrationsEventResource;
use App\Models\Forms\Form;
use App\Models\Integration\FormIntegrationsEvent;
class FormIntegrationsEventController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index(string $id, string $integrationid)
{
$form = Form::findOrFail((int)$id);
$this->authorize('view', $form);
return FormIntegrationsEventResource::collection(
FormIntegrationsEvent::where('integration_id', (int)$integrationid)->orderByDesc('created_at')->get()
);
}
}

View File

@@ -0,0 +1,84 @@
<?php
namespace App\Http\Requests\Integration;
use App\Models\Integration\FormIntegration;
use App\Rules\IntegrationLogicRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
class FormIntegrationsRequest extends FormRequest
{
public array $integrationRules = [];
private ?string $integrationClassName = null;
public function __construct(Request $request)
{
if ($request->integration_id) {
// Load integration class, and get rules
$integration = FormIntegration::getIntegration($request->integration_id);
if ($integration && isset($integration['file_name']) && class_exists(
'App\Service\Forms\Integrations\\' . $integration['file_name']
)) {
$this->integrationClassName = 'App\Service\Forms\Integrations\\' . $integration['file_name'];
$this->loadIntegrationRules();
return;
}
throw new \Exception('Unknown Integration!');
}
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return array_merge([
'integration_id' => ['required', Rule::in(array_keys(FormIntegration::getAllIntegrations()))],
'settings' => 'present|array',
'status' => 'required|boolean',
'logic' => [new IntegrationLogicRule()]
], $this->integrationRules);
}
/**
* Give the validated fields a better "human-readable" name
*
* @return array
*/
public function attributes()
{
$fields = [];
foreach ($this->rules() as $key => $value) {
$fields[$key] = Str::of($key)
->replace('settings.', '')
->headline();
}
return $fields;
}
private function loadIntegrationRules()
{
foreach ($this->integrationClassName::getValidationRules() as $key => $value) {
$this->integrationRules['settings.' . $key] = $value;
}
}
public function toIntegrationData(): array
{
return $this->integrationClassName::formatData([
'status' => ($this->validated(
'status'
)) ? FormIntegration::STATUS_ACTIVE : FormIntegration::STATUS_INACTIVE,
'integration_id' => $this->validated('integration_id'),
'data' => $this->validated('settings') ?? [],
'logic' => $this->validated('logic') ?? []
]);
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class FormIntegrationsEventResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
return [
'date' => date('Y-m-d H:i', strtotime($this->created_at)),
'status' => ucfirst($this->status),
'data' => $this->data
];
}
}

View File

@@ -15,6 +15,7 @@ class FailedWebhookListener
*/
public function handle(WebhookCallFailedEvent $event)
{
ray('in faieled', $event);
// Notify form owner
if ($event->meta['type'] == 'form_submission') {
$event->meta['form']->creator->notify(new FailedWebhookNotification($event));

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Listeners\Forms;
use App\Events\Models\FormIntegrationsEventCreated;
use App\Mail\Forms\FormIntegrationsEventCreationConfirmationMail;
use App\Models\Integration\FormIntegrationsEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Mail;
class FormIntegrationsEventListener implements ShouldQueue
{
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle(FormIntegrationsEventCreated $event)
{
if ($event->formIntegrationsEvent->status === FormIntegrationsEvent::STATUS_ERROR) {
$form = $event->formIntegrationsEvent->integration->form;
Mail::to($form->creator)->send(new FormIntegrationsEventCreationConfirmationMail($event->formIntegrationsEvent));
}
}
}

View File

@@ -3,12 +3,10 @@
namespace App\Listeners\Forms;
use App\Events\Forms\FormSubmitted;
use App\Models\Forms\Form;
use App\Notifications\Forms\FormSubmissionNotification;
use App\Service\Forms\Webhooks\WebhookHandlerProvider;
use App\Models\Integration\FormIntegration;
use App\Service\Forms\Integrations\AbstractIntegrationHandler;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Notification;
class NotifyFormSubmission implements ShouldQueue
{
@@ -22,48 +20,32 @@ class NotifyFormSubmission implements ShouldQueue
*/
public function handle(FormSubmitted $event)
{
$this->sendEmailNotifications($event);
$formIntegrations = FormIntegration::where([['form_id', $event->form->id], ['status', FormIntegration::STATUS_ACTIVE]])->get();
foreach ($formIntegrations as $formIntegration) {
ray($formIntegration, $formIntegration->integration_id);
$this->getIntegrationHandler(
$event,
$formIntegration
)->run();
}
/* $this->sendEmailNotifications($event);
$this->sendWebhookNotification($event, WebhookHandlerProvider::SIMPLE_WEBHOOK_PROVIDER);
$this->sendWebhookNotification($event, WebhookHandlerProvider::SLACK_PROVIDER);
$this->sendWebhookNotification($event, WebhookHandlerProvider::DISCORD_PROVIDER);
foreach ($event->form->zappierHooks as $hook) {
$hook->triggerHook($event->data);
}
*/
}
private function sendWebhookNotification(FormSubmitted $event, string $provider)
public static function getIntegrationHandler(FormSubmitted $event, FormIntegration $formIntegration): AbstractIntegrationHandler
{
WebhookHandlerProvider::getProvider(
$event->form,
$event->data,
$provider
)->handle();
}
/**
* Sends an email to each email address in the form's notification_emails field
*
* @return void
*/
private function sendEmailNotifications(FormSubmitted $event)
{
if (! $event->form->is_pro || ! $event->form->notifies) {
return;
$integration = FormIntegration::getIntegration($formIntegration->integration_id);
if ($integration && isset($integration['file_name']) && class_exists('App\Service\Forms\Integrations\\' . $integration['file_name'])) {
$className = 'App\Service\Forms\Integrations\\' . $integration['file_name'];
return new $className($event, $formIntegration, $integration);
}
$subscribers = collect(preg_split("/\r\n|\n|\r/", $event->form->notification_emails))->filter(function (
$email
) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
});
\Log::debug('Sending email notification', [
'recipients' => $subscribers->toArray(),
'form_id' => $event->form->id,
'form_slug' => $event->form->slug,
]);
$subscribers->each(function ($subscriber) use ($event) {
Notification::route('mail', $subscriber)->notify(new FormSubmissionNotification($event));
});
throw new \Exception('Unknown Integration!');
}
}

View File

@@ -1,94 +0,0 @@
<?php
namespace App\Listeners\Forms;
use App\Events\Forms\FormSubmitted;
use App\Mail\Forms\SubmissionConfirmationMail;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Mail;
/**
* Sends a confirmation to form respondant that form was submitted
*
* Class SubmissionConfirmation
*/
class SubmissionConfirmation implements ShouldQueue
{
use InteractsWithQueue;
public const RISKY_USERS_LIMIT = 120;
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle(FormSubmitted $event)
{
if (
! $event->form->is_pro ||
! $event->form->send_submission_confirmation ||
$this->riskLimitReached($event) // To avoid phishing abuse we limit this feature for risky users
) {
return;
}
$email = $this->getRespondentEmail($event);
if (! $email) {
return;
}
\Log::info('Sending submission confirmation', [
'recipient' => $email,
'form_id' => $event->form->id,
'form_slug' => $event->form->slug,
]);
Mail::to($email)->send(new SubmissionConfirmationMail($event));
}
private function getRespondentEmail(FormSubmitted $event)
{
// Make sure we only have one email field in the form
$emailFields = collect($event->form->properties)->filter(function ($field) {
$hidden = $field['hidden'] ?? false;
return ! $hidden && $field['type'] == 'email';
});
if ($emailFields->count() != 1) {
return null;
}
if (isset($event->data[$emailFields->first()['id']])) {
$email = $event->data[$emailFields->first()['id']];
if ($this->validateEmail($email)) {
return $email;
}
}
return null;
}
private function riskLimitReached(FormSubmitted $event): bool
{
// This is a per-workspace limit for risky workspaces
if ($event->form->workspace->is_risky) {
if ($event->form->workspace->submissions_count >= self::RISKY_USERS_LIMIT) {
\Log::error('!!!DANGER!!! Dangerous user detected! Attempting many email sending.', [
'form_id' => $event->form->id,
'workspace_id' => $event->form->workspace->id,
]);
return true;
}
}
return false;
}
public static function validateEmail($email): bool
{
return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace App\Mail\Forms;
use App\Mail\OpenFormMail;
use App\Models\Integration\FormIntegration;
use App\Models\Integration\FormIntegrationsEvent;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\SerializesModels;
class FormIntegrationsEventCreationConfirmationMail extends OpenFormMail implements ShouldQueue
{
use Queueable;
use SerializesModels;
public $formIntegration;
public $form;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(public FormIntegrationsEvent $formIntegrationsEvent)
{
$this->formIntegration = $formIntegrationsEvent->integration;
$this->form = $this->formIntegration->form;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$integration = FormIntegration::getIntegration($this->formIntegration->integration_id);
return $this
->markdown('mail.form.integrations-event-created', [
'form' => $this->form,
'integration_name' => $integration['name'] ?? '',
'error' => json_encode($this->formIntegrationsEvent->data)
])->subject("Integration issue with your form: '" . $this->form->title . "'");
}
}

View File

@@ -8,7 +8,6 @@ use App\Service\Forms\FormSubmissionFormatter;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Vinkla\Hashids\Facades\Hashids;
@@ -22,7 +21,7 @@ class SubmissionConfirmationMail extends OpenFormMail implements ShouldQueue
*
* @return void
*/
public function __construct(private FormSubmitted $event)
public function __construct(private FormSubmitted $event, private $integrationData)
{
}
@@ -42,11 +41,12 @@ class SubmissionConfirmationMail extends OpenFormMail implements ShouldQueue
return $this
->replyTo($this->getReplyToEmail($form->creator->email))
->from($this->getFromEmail(), $form->notification_sender)
->subject($form->notification_subject)
->from($this->getFromEmail(), $this->integrationData->notification_sender)
->subject($this->integrationData->notification_subject)
->markdown('mail.form.confirmation-submission-notification', [
'fields' => $formatter->getFieldsWithValue(),
'form' => $form,
'integrationData' => $this->integrationData,
'noBranding' => $form->no_branding,
'submission_id' => (isset($this->event->data['submission_id']) && $this->event->data['submission_id']) ? Hashids::encode($this->event->data['submission_id']) : null,
]);
@@ -56,12 +56,12 @@ class SubmissionConfirmationMail extends OpenFormMail implements ShouldQueue
{
$originalFromAddress = Str::of(config('mail.from.address'))->explode('@');
return $originalFromAddress->first().'+'.time().'@'.$originalFromAddress->last();
return $originalFromAddress->first() . '+' . time() . '@' . $originalFromAddress->last();
}
private function getReplyToEmail($default)
{
$replyTo = Arr::get((array) $this->event->form->notification_settings, 'confirmation_reply_to', null);
$replyTo = $this->integrationData->confirmation_reply_to ?? null;
if ($replyTo && filter_var($replyTo, FILTER_VALIDATE_EMAIL)) {
return $replyTo;

View File

@@ -0,0 +1,51 @@
<?php
namespace App\Models\Integration;
use App\Models\Forms\Form;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class FormIntegration extends Model
{
use HasFactory;
public const STATUS_ACTIVE = 'active';
public const STATUS_INACTIVE = 'inactive';
protected $fillable = [
'form_id',
'status',
'integration_id',
'logic',
'data',
'oauth_id'
];
protected $casts = [
'data' => 'object',
'logic' => 'object'
];
/**
* Relationships
*/
public function form()
{
return $this->belongsTo(Form::class);
}
public function events()
{
return $this->hasMany(FormIntegrationsEvent::class, 'integration_id');
}
public static function getAllIntegrations()
{
return json_decode(file_get_contents(resource_path('data/forms/integrations.json')), true);
}
public static function getIntegration($key)
{
return self::getAllIntegrations()[$key] ?? null;
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Models\Integration;
use App\Events\Models\FormIntegrationsEventCreated;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class FormIntegrationsEvent extends Model
{
use HasFactory;
public const STATUS_SUCCESS = 'success';
public const STATUS_ERROR = 'error';
protected $fillable = [
'integration_id',
'status',
'data'
];
protected $casts = [
'data' => 'object'
];
/**
* The event map for the model.
*
* @var array
*/
protected $dispatchesEvents = [
'created' => FormIntegrationsEventCreated::class,
];
public function integration()
{
return $this->belongsTo(FormIntegration::class, 'integration_id');
}
}

View File

@@ -3,7 +3,7 @@
namespace App\Models\Integration;
use App\Models\Forms\Form;
use App\Service\Forms\Webhooks\WebhookHandlerProvider;
use App\Service\Forms\Integrations\WebhookHandlerProvider;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

View File

@@ -8,7 +8,6 @@ use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class FormSubmissionNotification extends Notification implements ShouldQueue
@@ -22,7 +21,7 @@ class FormSubmissionNotification extends Notification implements ShouldQueue
*
* @return void
*/
public function __construct(FormSubmitted $event)
public function __construct(FormSubmitted $event, private $integrationData)
{
$this->event = $event;
}
@@ -55,7 +54,7 @@ class FormSubmissionNotification extends Notification implements ShouldQueue
return (new MailMessage())
->replyTo($this->getReplyToEmail($notifiable->routes['mail']))
->from($this->getFromEmail(), config('app.name'))
->subject('New form submission for "'.$this->event->form->title.'"')
->subject('New form submission for "' . $this->event->form->title . '"')
->markdown('mail.form.submission-notification', [
'fields' => $formatter->getFieldsWithValue(),
'form' => $this->event->form,
@@ -66,12 +65,12 @@ class FormSubmissionNotification extends Notification implements ShouldQueue
{
$originalFromAddress = Str::of(config('mail.from.address'))->explode('@');
return $originalFromAddress->first().'+'.time().'@'.$originalFromAddress->last();
return $originalFromAddress->first() . '+' . time() . '@' . $originalFromAddress->last();
}
private function getReplyToEmail($default)
{
$replyTo = Arr::get((array) $this->event->form->notification_settings, 'notification_reply_to', null);
$replyTo = $this->integrationData->notification_reply_to ?? null;
if ($replyTo && $this->validateEmail($replyTo)) {
return $replyTo;
}
@@ -85,7 +84,7 @@ class FormSubmissionNotification extends Notification implements ShouldQueue
$emailFields = collect($this->event->form->properties)->filter(function ($field) {
$hidden = $field['hidden'] ?? false;
return ! $hidden && $field['type'] == 'email';
return !$hidden && $field['type'] == 'email';
});
if ($emailFields->count() != 1) {
return null;

View File

@@ -4,14 +4,14 @@ namespace App\Providers;
use App\Events\Forms\FormSubmitted;
use App\Events\Models\FormCreated;
use App\Events\Models\FormIntegrationsEventCreated;
use App\Listeners\FailedWebhookListener;
use App\Listeners\Forms\FormCreationConfirmation;
use App\Listeners\Forms\FormIntegrationsEventListener;
use App\Listeners\Forms\NotifyFormSubmission;
use App\Listeners\Forms\SubmissionConfirmation;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
use Spatie\WebhookServer\Events\WebhookCallFailedEvent;
class EventServiceProvider extends ServiceProvider
@@ -29,12 +29,14 @@ class EventServiceProvider extends ServiceProvider
FormCreationConfirmation::class,
],
FormSubmitted::class => [
NotifyFormSubmission::class,
SubmissionConfirmation::class,
NotifyFormSubmission::class
],
WebhookCallFailedEvent::class => [
FailedWebhookListener::class,
],
FormIntegrationsEventCreated::class => [
FormIntegrationsEventListener::class,
],
];
/**

View File

@@ -0,0 +1,186 @@
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\Rule;
class IntegrationLogicRule implements DataAwareRule, Rule
{
private $isConditionCorrect = true;
private $conditionErrors = [];
private $field = [];
private $data = [];
private function checkBaseCondition($condition)
{
if (!isset($condition['value'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition body';
return;
}
if (!isset($condition['value']['property_meta'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition property';
return;
}
if (!isset($condition['value']['property_meta']['type'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition property type';
return;
}
if (!isset($condition['value']['operator'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition operator';
return;
}
if (!isset($condition['value']['value'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition value';
return;
}
$typeField = $condition['value']['property_meta']['type'];
$operator = $condition['value']['operator'];
$value = $condition['value']['value'];
if (!isset(FormPropertyLogicRule::CONDITION_MAPPING[$typeField])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'configuration not found for condition type';
return;
}
if (!isset(FormPropertyLogicRule::CONDITION_MAPPING[$typeField]['comparators'][$operator])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'configuration not found for condition operator';
return;
}
$type = FormPropertyLogicRule::CONDITION_MAPPING[$typeField]['comparators'][$operator]['expected_type'];
if (is_array($type)) {
$foundCorrectType = false;
foreach ($type as $subtype) {
if ($this->valueHasCorrectType($subtype, $value)) {
$foundCorrectType = true;
}
}
if (!$foundCorrectType) {
$this->isConditionCorrect = false;
}
} else {
if (!$this->valueHasCorrectType($type, $value)) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'wrong type of condition value';
}
}
}
private function valueHasCorrectType($type, $value)
{
if (
($type === 'string' && gettype($value) !== 'string') ||
($type === 'boolean' && !is_bool($value)) ||
($type === 'number' && !is_numeric($value)) ||
($type === 'object' && !is_array($value))
) {
return false;
}
return true;
}
private function checkConditions($conditions)
{
if (array_key_exists('operatorIdentifier', $conditions)) {
if (($conditions['operatorIdentifier'] !== 'and') && ($conditions['operatorIdentifier'] !== 'or')) {
$this->conditionErrors[] = 'missing operator';
$this->isConditionCorrect = false;
return;
}
if (isset($conditions['operatorIdentifier']['children'])) {
$this->conditionErrors[] = 'extra condition';
$this->isConditionCorrect = false;
return;
}
if (!is_array($conditions['children'])) {
$this->conditionErrors[] = 'wrong sub-condition type';
$this->isConditionCorrect = false;
return;
}
foreach ($conditions['children'] as &$child) {
$this->checkConditions($child);
}
} elseif (isset($conditions['identifier'])) {
$this->checkBaseCondition($conditions);
}
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
if (isset($value)) {
$this->checkConditions($value);
}
return $this->isConditionCorrect;
}
/**
* Get the validation error message.
*/
public function message()
{
$message = null;
if (!$this->isConditionCorrect) {
$message = 'The logic conditions are not complete.';
}
if (count($this->conditionErrors) > 0) {
return $message . ' Error detail(s): ' . implode(', ', $this->conditionErrors);
}
return $message;
}
/**
* Set the data under validation.
*
* @param array $data
* @return $this
*/
public function setData($data)
{
$this->data = $data;
$this->isConditionCorrect = true;
$this->conditionErrors = [];
return $this;
}
}

View File

@@ -28,12 +28,8 @@ class FormCleaner
private array $customKeys = ['seo_meta'];
private array $formDefaults = [
'notifies' => false,
'no_branding' => false,
'webhook_url' => null,
'database_fields_update' => null,
'slack_webhook_url' => null,
'discord_webhook_url' => null,
'editable_submissions' => false,
'custom_code' => null,
'seo_meta' => [],
@@ -46,12 +42,8 @@ class FormCleaner
private array $cleaningMessages = [
// For form
'notifies' => 'Email notification were disabled.',
'no_branding' => 'OpenForm branding is not hidden.',
'webhook_url' => 'Webhook disabled.',
'database_fields_update' => 'Form submission will only create new records (no updates).',
'slack_webhook_url' => 'Slack webhook disabled.',
'discord_webhook_url' => 'Discord webhook disabled.',
'editable_submissions' => 'Users will not be able to edit their submissions.',
'custom_code' => 'Custom code was disabled',
'seo_meta' => 'Custom SEO was disabled',
@@ -126,7 +118,7 @@ class FormCleaner
*/
public function simulateCleaning(Workspace $workspace): FormCleaner
{
if (! $this->isPro($workspace)) {
if (!$this->isPro($workspace)) {
$this->data = $this->removeProFeatures($this->data, true);
}
@@ -141,7 +133,7 @@ class FormCleaner
*/
public function performCleaning(Workspace $workspace): FormCleaner
{
if (! $this->isPro($workspace)) {
if (!$this->isPro($workspace)) {
$this->data = $this->removeProFeatures($this->data);
}
@@ -217,14 +209,14 @@ class FormCleaner
$formVal = (($formVal === 0 || $formVal === '0') ? false : $formVal);
$formVal = (($formVal === 1 || $formVal === '1') ? true : $formVal);
if (! is_null($formVal) && $formVal !== $value) {
if (! isset($this->cleanings['form'])) {
if (!is_null($formVal) && $formVal !== $value) {
if (!isset($this->cleanings['form'])) {
$this->cleanings['form'] = [];
}
$this->cleanings['form'][] = $key;
// If not a simulation, do the cleaning
if (! $simulation) {
if (!$simulation) {
Arr::set($data, $key, $value);
}
}
@@ -236,7 +228,7 @@ class FormCleaner
foreach ($defaults as $key => $value) {
if (isset($data[$key]) && Arr::get($data, $key) !== $value) {
$this->cleanings[$data['name']][] = $key;
if (! $simulation) {
if (!$simulation) {
Arr::set($data, $key, $value);
}
}

View File

@@ -0,0 +1,131 @@
<?php
namespace App\Service\Forms\Integrations;
use App\Models\Integration\FormIntegration;
use App\Events\Forms\FormSubmitted;
use App\Models\Integration\FormIntegrationsEvent;
use App\Service\Forms\FormSubmissionFormatter;
use App\Service\Forms\FormLogicConditionChecker;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;
use Vinkla\Hashids\Facades\Hashids;
abstract class AbstractIntegrationHandler
{
protected $form = null;
protected $submissionData = null;
protected $integrationData = null;
public function __construct(
protected FormSubmitted $event,
protected FormIntegration $formIntegration,
protected array $integration
) {
$this->form = $event->form;
$this->submissionData = $event->data;
$this->integrationData = $formIntegration->data;
}
protected function getProviderName(): string
{
return $this->integration['name'] ?? '';
}
protected function logicConditionsMet(): bool
{
if (!$this->formIntegration->logic) {
return true;
}
return FormLogicConditionChecker::conditionsMet(
json_decode(json_encode($this->formIntegration->logic), true),
$this->submissionData
);
}
protected function shouldRun(): bool
{
return $this->logicConditionsMet();
}
protected function getWebhookUrl(): ?string
{
return '';
}
/**
* Default webhook payload. Can be changed in child classes.
*/
protected function getWebhookData(): array
{
$formatter = (new FormSubmissionFormatter($this->form, $this->submissionData))
->useSignedUrlForFiles()
->showHiddenFields();
$formattedData = [];
foreach ($formatter->getFieldsWithValue() as $field) {
$formattedData[$field['name']] = $field['value'];
}
$data = [
'form_title' => $this->form->title,
'form_slug' => $this->form->slug,
'submission' => $formattedData,
];
if ($this->form->is_pro && $this->form->editable_submissions) {
$data['edit_link'] = $this->form->share_url . '?submission_id=' . Hashids::encode(
$this->submissionData['submission_id']
);
}
return $data;
}
final public function run(): void
{
try {
$this->handle();
$this->formIntegration->events()->create([
'status' => FormIntegrationsEvent::STATUS_SUCCESS,
]);
} catch (\Exception $e) {
$this->formIntegration->events()->create([
'status' => FormIntegrationsEvent::STATUS_ERROR,
'data' => $this->extractEventDataFromException($e),
]);
}
}
/**
* Default handle. Can be changed in child classes.
*/
public function handle(): void
{
if (!$this->shouldRun()) {
return;
}
Http::throw()->post($this->getWebhookUrl(), $this->getWebhookData());
}
abstract public static function getValidationRules(): array;
public static function formatData(array $data): array
{
return $data;
}
public function extractEventDataFromException(\Exception $e): array
{
if ($e instanceof RequestException) {
return [
'message' => $e->getMessage(),
'response' => $e->response->json(),
'status' => $e->response->status(),
];
}
return [
'message' => $e->getMessage()
];
}
}

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Service\Forms\Webhooks;
namespace App\Service\Forms\Integrations;
use App\Models\Forms\Form;
use App\Service\Forms\FormSubmissionFormatter;

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Service\Forms\Webhooks;
namespace App\Service\Forms\Integrations;
use App\Service\Forms\FormSubmissionFormatter;
use Illuminate\Support\Arr;

View File

@@ -0,0 +1,91 @@
<?php
namespace App\Service\Forms\Integrations;
use App\Service\Forms\FormSubmissionFormatter;
use Illuminate\Support\Arr;
use Vinkla\Hashids\Facades\Hashids;
class DiscordIntegration extends AbstractIntegrationHandler
{
public static function getValidationRules(): array
{
return [
'discord_webhook_url' => 'required|url|starts_with:https://discord.com/api/webhooks',
'include_submission_data' => 'boolean',
'link_open_form' => 'boolean',
'link_edit_form' => 'boolean',
'views_submissions_count' => 'boolean',
'link_edit_submission' => 'boolean'
];
}
protected function getWebhookUrl(): ?string
{
return $this->integrationData->discord_webhook_url;
}
protected function shouldRun(): bool
{
return !is_null($this->getWebhookUrl()) && $this->form->is_pro && parent::shouldRun();
}
protected function getWebhookData(): array
{
$settings = (array) $this->integrationData ?? [];
$externalLinks = [];
if (Arr::get($settings, 'link_open_form', true)) {
$externalLinks[] = '[**🔗 Open Form**](' . $this->form->share_url . ')';
}
if (Arr::get($settings, 'link_edit_form', true)) {
$editFormURL = front_url('forms/' . $this->form->slug . '/show');
$externalLinks[] = '[**✍️ Edit Form**](' . $editFormURL . ')';
}
if (Arr::get($settings, 'link_edit_submission', true) && $this->form->editable_submissions) {
$submissionId = Hashids::encode($this->submissionData['submission_id']);
$externalLinks[] = '[**✍️ ' . $this->form->editable_submissions_button_text . '**](' . $this->form->share_url . '?submission_id=' . $submissionId . ')';
}
$color = hexdec(str_replace('#', '', $this->form->color));
$blocks = [];
if (Arr::get($settings, 'include_submission_data', true)) {
$submissionString = '';
$formatter = (new FormSubmissionFormatter($this->form, $this->submissionData))->outputStringsOnly();
foreach ($formatter->getFieldsWithValue() as $field) {
$tmpVal = is_array($field['value']) ? implode(',', $field['value']) : $field['value'];
$submissionString .= '**' . ucfirst($field['name']) . '**: ' . $tmpVal . "\n";
}
$blocks[] = [
'type' => 'rich',
'color' => $color,
'description' => $submissionString,
];
}
if (Arr::get($settings, 'views_submissions_count', true)) {
$countString = '**👀 Views**: ' . (string) $this->form->views_count . " \n";
$countString .= '**🖊️ Submissions**: ' . (string) $this->form->submissions_count;
$blocks[] = [
'type' => 'rich',
'color' => $color,
'description' => $countString,
];
}
if (count($externalLinks) > 0) {
$blocks[] = [
'type' => 'rich',
'color' => $color,
'description' => implode(' - ', $externalLinks),
];
}
return [
'content' => 'New submission for your form **' . $this->form->title . '**',
'tts' => false,
'username' => config('app.name'),
'avatar_url' => asset('img/logo.png'),
'embeds' => $blocks,
];
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace App\Service\Forms\Integrations;
use App\Rules\OneEmailPerLine;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;
use App\Notifications\Forms\FormSubmissionNotification;
class EmailIntegration extends AbstractIntegrationHandler
{
public static function getValidationRules(): array
{
return [
'notification_emails' => ['required', new OneEmailPerLine()],
'notification_reply_to' => 'email|nullable',
];
}
protected function shouldRun(): bool
{
return !(!$this->form->is_pro || !$this->integrationData->notification_emails) && parent::shouldRun();
}
public function handle(): void
{
if (!$this->shouldRun()) {
return;
}
$subscribers = collect(preg_split("/\r\n|\n|\r/", $this->integrationData->notification_emails))
->filter(function ($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
});
Log::debug('Sending email notification', [
'recipients' => $subscribers->toArray(),
'form_id' => $this->form->id,
'form_slug' => $this->form->slug,
]);
$subscribers->each(function ($subscriber) {
Notification::route('mail', $subscriber)->notify(
new FormSubmissionNotification($this->event, $this->integrationData)
);
});
}
}

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Service\Forms\Webhooks;
namespace App\Service\Forms\Integrations;
class SimpleWebhookHandler extends AbstractWebhookHandler
{

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Service\Forms\Webhooks;
namespace App\Service\Forms\Integrations;
use App\Service\Forms\FormSubmissionFormatter;
use Illuminate\Support\Arr;

View File

@@ -0,0 +1,101 @@
<?php
namespace App\Service\Forms\Integrations;
use App\Service\Forms\FormSubmissionFormatter;
use Illuminate\Support\Arr;
use Vinkla\Hashids\Facades\Hashids;
class SlackIntegration extends AbstractIntegrationHandler
{
public static function getValidationRules(): array
{
return [
'slack_webhook_url' => 'required|url|starts_with:https://hooks.slack.com/',
'include_submission_data' => 'boolean',
'link_open_form' => 'boolean',
'link_edit_form' => 'boolean',
'views_submissions_count' => 'boolean',
'link_edit_submission' => 'boolean'
];
}
protected function getWebhookUrl(): ?string
{
return $this->integrationData->slack_webhook_url;
}
protected function shouldRun(): bool
{
return !is_null($this->getWebhookUrl()) && $this->form->is_pro && parent::shouldRun();
}
protected function getWebhookData(): array
{
$settings = (array) $this->integrationData ?? [];
$externalLinks = [];
if (Arr::get($settings, 'link_open_form', true)) {
$externalLinks[] = '*<' . $this->form->share_url . '|🔗 Open Form>*';
}
if (Arr::get($settings, 'link_edit_form', true)) {
$editFormURL = front_url('forms/' . $this->form->slug . '/show');
$externalLinks[] = '*<' . $editFormURL . '|✍️ Edit Form>*';
}
if (Arr::get($settings, 'link_edit_submission', true) && $this->form->editable_submissions) {
$submissionId = Hashids::encode($this->submissionData['submission_id']);
$externalLinks[] = '*<' . $this->form->share_url . '?submission_id=' . $submissionId . '|✍️ ' . $this->form->editable_submissions_button_text . '>*';
}
$blocks = [
[
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => 'New submission for your form *' . $this->form->title . '*',
],
],
];
if (Arr::get($settings, 'include_submission_data', true)) {
$submissionString = '';
$formatter = (new FormSubmissionFormatter($this->form, $this->submissionData))->outputStringsOnly();
foreach ($formatter->getFieldsWithValue() as $field) {
$tmpVal = is_array($field['value']) ? implode(',', $field['value']) : $field['value'];
$submissionString .= '>*' . ucfirst($field['name']) . '*: ' . $tmpVal . " \n";
}
$blocks[] = [
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => $submissionString,
],
];
}
if (Arr::get($settings, 'views_submissions_count', true)) {
$countString = '*👀 Views*: ' . (string) $this->form->views_count . " \n";
$countString .= '*🖊️ Submissions*: ' . (string) $this->form->submissions_count;
$blocks[] = [
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => $countString,
],
];
}
if (count($externalLinks) > 0) {
$blocks[] = [
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => implode(' ', $externalLinks),
],
];
}
return [
'blocks' => $blocks,
];
}
}

View File

@@ -0,0 +1,103 @@
<?php
namespace App\Service\Forms\Integrations;
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 AbstractIntegrationHandler
{
public const RISKY_USERS_LIMIT = 120;
public static function getValidationRules(): array
{
return [
'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,
]);
Mail::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'] ?? ''),
]);
}
}

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Service\Forms\Webhooks;
namespace App\Service\Forms\Integrations;
use App\Models\Forms\Form;

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Service\Forms\Integrations;
class WebhookIntegration extends AbstractIntegrationHandler
{
public static function getValidationRules(): array
{
return [
'webhook_url' => 'required|url'
];
}
protected function getWebhookUrl(): ?string
{
return $this->integrationData->webhook_url;
}
protected function shouldRun(): bool
{
return !is_null($this->getWebhookUrl()) && parent::shouldRun();
}
}

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Service\Forms\Webhooks;
namespace App\Service\Forms\Integrations;
use App\Models\Forms\Form;