Fix FLEXIAPI-366 Send the voicemails by email

This commit is contained in:
Timothée Jaussoin 2025-10-09 15:06:30 +02:00
parent c954b21dd2
commit 146ec8facb
15 changed files with 115 additions and 24 deletions

1
cron/flexiapi.cron Normal file
View file

@ -0,0 +1 @@
* * * * * cd /opt/belledonne-communications/share/flexisip-account-manager/flexiapi/ && php artisan schedule:run >> /dev/null 2>&1

View file

@ -12,8 +12,11 @@ class AccountFile extends Model
public const VOICEMAIL_CONTENTTYPES = ['audio/opus', 'audio/wav'];
public const FILES_PATH = 'files';
protected $hidden = ['account_id', 'updated_at'];
protected $hidden = ['account_id', 'updated_at', 'sending_by_mail_at', 'sent_by_mail_at', 'sending_by_mail_tryouts'];
protected $appends = ['download_url'];
protected $casts = [
'uploaded_at' => 'datetime',
];
protected static function booted()
{
@ -24,7 +27,7 @@ class AccountFile extends Model
public function account()
{
return $this->belongsTo(Account::class);
return $this->belongsTo(Account::class)->withoutGlobalScopes();
}
public function getMaxUploadSizeAttribute(): ?int

View file

@ -0,0 +1,40 @@
<?php
namespace App\Console\Commands\Accounts;
use App\AccountFile;
use App\Mail\Voicemail;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
class SendVoicemailsEmails extends Command
{
protected $signature = 'accounts:send-voicemails-emails {--tryout}';
protected $description = 'Send the voicemail emails';
public function handle()
{
$voicemails = AccountFile::whereNotNull('uploaded_at')
->whereNull('sent_by_mail_at')
->where('sending_by_mail_tryouts', '<', is_int($this->option('tryout'))
? $this->option('tryout')
: 3)
->get();
foreach ($voicemails as $voicemail) {
$voicemail->sending_by_mail_at = Carbon::now();
$voicemail->save();
if (Mail::to(users: $voicemail->account)->send(new Voicemail($voicemail))) {
$voicemail->sent_by_mail_at = Carbon::now();
$this->info('Voicemail sent to ' . $voicemail->account->identifier);
} else {
$voicemail->sending_by_mail_tryouts++;
$this->info('Error sending voicemail to ' . $voicemail->account->identifier);
}
$voicemail->save();
}
}
}

View file

@ -5,7 +5,6 @@ namespace App\Mail;
use App\Space;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;

View file

@ -22,7 +22,6 @@ namespace App\Mail;
use App\Account;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;

View file

@ -22,7 +22,6 @@ namespace App\Mail;
use App\Account;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;

View file

@ -22,7 +22,6 @@ namespace App\Mail;
use App\Account;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;

View file

@ -20,10 +20,8 @@
namespace App\Mail;
use App\Account;
use App\ResetPasswordEmailToken;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;

View file

@ -0,0 +1,40 @@
<?php
namespace App\Mail;
use App\AccountFile;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
class Voicemail extends Mailable
{
use Queueable, SerializesModels;
public function __construct(public AccountFile $accountFile)
{
}
public function envelope(): Envelope
{
return new Envelope(
subject: $this->accountFile->account->space->name .
': ' .
__('New voice message from :sipfrom', ['sipfrom' => $this->accountFile->sip_from]),
);
}
public function content(): Content
{
return new Content(
markdown: 'mails.voicemail',
);
}
public function attachments(): array
{
return [];
}
}

View file

@ -18,6 +18,9 @@ return new class extends Migration
$table->string('content_type')->index();
$table->text('sip_from')->nullable();
$table->dateTime('uploaded_at')->nullable();
$table->dateTime('sending_by_mail_at')->nullable();
$table->integer('sending_by_mail_tryouts')->default(0);
$table->dateTime('sent_by_mail_at')->nullable();
$table->timestamps();
});
}

View file

@ -145,6 +145,7 @@
"New newsletter subscription": "Nouvelle inscription à votre newsletter",
"New Space": "Nouvel Espace",
"New user": "Nouvel utilisateur",
"New voice message from :sipfrom": "Nouveau message vocal de :sipfrom",
"Newsletter registration email address": "Addresse email d'inscription à la liste de diffusion",
"Next": "Suivant",
"No account yet?": "Pas encore de compte ?",
@ -170,6 +171,7 @@
"QR Code scanning": "Scan de QR Code",
"Realm": "Royaume",
"Recover your account using your email": "Récupérer votre compte avec votre email",
"Recorded at": "Enregistré le",
"Register": "Inscription",
"Registrar": "Registrar",
"Registration confirmed": "Confirmation de l'inscription",

View file

@ -10,7 +10,6 @@
The Clean Code ruleset contains rules that enforce a clean code base. This includes rules from SOLID and object calisthenics.
</description>
<rule ref="rulesets/cleancode.xml/ElseExpression" />
<rule ref="rulesets/controversial.xml/CamelCaseClassName" />
<rule ref="rulesets/controversial.xml/CamelCasePropertyName" />
<rule ref="rulesets/controversial.xml/CamelCaseMethodName" />

View file

@ -0,0 +1,14 @@
@extends('mails.layout')
@section('content')
# {{ __('New voice message from :sipfrom', ['sipfrom' => $accountFile->sip_from]) }}
{{ __('New voice message') }}
{{ __('From') }}: {{ $accountFile->sip_from }}
{{ __('To') }}: {{ $accountFile->account->identifier }}
{{ __('Recorded at') }}: {{ $accountFile->created_at->toDateTimeString() }}
@endsection

View file

@ -1,18 +1,8 @@
<?php
use Illuminate\Foundation\Inspiring;
use App\Console\Commands\Accounts\ClearFiles;
use App\Console\Commands\Accounts\SendVoicemailsEmails;
use Illuminate\Support\Facades\Schedule;
/*
|--------------------------------------------------------------------------
| Console Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of your Closure based console
| commands. Each Closure is bound to a command instance allowing a
| simple approach to interacting with each command's IO methods.
|
*/
Artisan::command('inspire', function () {
$this->comment(Inspiring::quote());
})->describe('Display an inspiring quote');
Schedule::command(ClearFiles::class, [7, '--apply'])->daily();
Schedule::command(SendVoicemailsEmails::class)->everyMinute();

View file

@ -54,6 +54,7 @@ cp flexiapi/composer.json "$RPM_BUILD_ROOT%{opt_dir}/flexiapi"
cp README* "$RPM_BUILD_ROOT%{opt_dir}/"
cp INSTALL* "$RPM_BUILD_ROOT%{opt_dir}/"
mkdir -p $RPM_BUILD_ROOT/etc/cron.daily
mkdir -p $RPM_BUILD_ROOT/etc/cron.d
mkdir -p $RPM_BUILD_ROOT%{apache_conf_path}
cp httpd/flexisip-account-manager.conf "$RPM_BUILD_ROOT%{apache_conf_path}/"
@ -66,6 +67,9 @@ cp httpd/flexisip-account-manager.conf "$RPM_BUILD_ROOT%{apache_conf_path}/"
chmod +x "$RPM_BUILD_ROOT/etc/cron.daily/flexiapi.redhat"
%endif
cp cron/flexiapi.cron "$RPM_BUILD_ROOT/etc/cron.d/"
chmod +x "$RPM_BUILD_ROOT/etc/cron.d/flexiapi.cron"
# POST INSTALLATION
%posttrans
@ -161,6 +165,7 @@ fi
%exclude %{opt_dir}/flexiapi/storage/
%config /etc/cron.d/flexiapi.cron
%config(noreplace) %{apache_conf_path}/flexisip-account-manager.conf
%if %{with deb}
%config(noreplace) /etc/cron.daily/flexiapi.debian