Echomail export

This commit is contained in:
Deon George
2021-09-06 23:39:32 +10:00
parent 8306f4c3a3
commit dbbfe46cb9
13 changed files with 163 additions and 62 deletions

View File

@@ -2,8 +2,10 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Jenssegers\Mongodb\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\SoftDeletes;
@@ -11,10 +13,12 @@ use App\Classes\FTN\Message;
use App\Interfaces\Packet;
use App\Traits\{EncodeUTF8,MsgID,UseMongo};
class Echomail extends Model implements Packet
final class Echomail extends Model implements Packet
{
use SoftDeletes,MsgID,UseMongo,EncodeUTF8;
private const LOGKEY = 'ME-';
protected $collection = FALSE;
private const cast_utf8 = [
@@ -24,8 +28,49 @@ class Echomail extends Model implements Packet
protected $dates = ['datetime'];
public static function boot()
{
parent::boot();
static::created(function($model) {
if (! $model->echoarea_id) {
Log::alert(sprintf('%s:- Message has no echo area, no exporting',self::LOGKEY,$model->id));
return;
}
// See if we need to export this message.
$exportto = $model->echoarea->addresses->pluck('system')->diff($model->seenby->pluck('system'));
$export_ao = collect();
foreach ($model->echoarea->domain->zones as $zo) {
foreach ($exportto as $so) {
$export_ao = $export_ao->merge($so->match($zo));
}
}
// Add to export
foreach ($export_ao as $ao) {
Log::info(sprintf('%s:- Exporting message [%s] to [%s]',self::LOGKEY,$model->id,$ao->ftn));
DB::table('address_echomail')->insert([
'address_id'=>$ao->id,
'echomail_id'=>$model->id,
'export_date'=>Carbon::now()
]);
}
$model->seenby = $model->seenby->merge($export_ao);
$model->save();
});
}
/* RELATIONS */
public function echoarea()
{
return $this->belongsTo(Echoarea::class);
}
public function fftn()
{
return $this
@@ -36,17 +81,22 @@ class Echomail extends Model implements Packet
/* ATTRIBUTES */
public function getPathAttribute($value): Collection
public function getPathAttribute(?array $value): Collection
{
if (is_null($value))
return collect();
return Address::whereIn('id',$value)
->orderBy(DB::raw(sprintf("position (id::text in '(%s)')",join(',',$value))))
->get()
->pluck('ftn3d');
->get();
}
public function getSeenByAttribute($value): Collection
public function getSeenByAttribute(?array $value): Collection
{
return Address::whereIn('id',$value)->get()->pluck('ftn2d');
if (is_null($value))
return collect();
return Address::whereIn('id',$value)->get();
}
/* METHODS */
@@ -61,6 +111,8 @@ class Echomail extends Model implements Packet
*/
public function packet(Address $ao): Message
{
Log::debug(sprintf('%s:Bundling [%s]',self::LOGKEY,$this->id));
// @todo Dont bundle mail to nodes that have been disabled, or addresses that have been deleted
$o = new Message;
@@ -78,7 +130,7 @@ class Echomail extends Model implements Packet
$o->user_to = $this->to;
$o->user_from = $this->from;
$o->subject = $this->subject;
$o->echoarea = $this->echoarea;
$o->echoarea = $this->echoarea->name;
$o->flags = $this->flags;
$o->kludge->put('mid',$this->id);
@@ -101,6 +153,8 @@ class Echomail extends Model implements Packet
// @todo SEENBY
// @todo PATH
$o->packed = TRUE;
return $o;
}
}