2019-04-27 13:57:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2021-09-06 13:39:32 +00:00
|
|
|
use Carbon\Carbon;
|
2021-08-29 01:48:27 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2021-08-29 14:44:20 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2021-09-06 13:39:32 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2021-07-30 14:35:52 +00:00
|
|
|
use Jenssegers\Mongodb\Eloquent\Model;
|
|
|
|
use Jenssegers\Mongodb\Eloquent\SoftDeletes;
|
2019-04-27 13:57:39 +00:00
|
|
|
|
2021-07-30 14:35:52 +00:00
|
|
|
use App\Classes\FTN\Message;
|
|
|
|
use App\Interfaces\Packet;
|
2021-08-11 13:45:30 +00:00
|
|
|
use App\Traits\{EncodeUTF8,MsgID,UseMongo};
|
2021-07-30 14:35:52 +00:00
|
|
|
|
2021-09-06 13:39:32 +00:00
|
|
|
final class Echomail extends Model implements Packet
|
2019-04-27 13:57:39 +00:00
|
|
|
{
|
2021-08-11 13:45:30 +00:00
|
|
|
use SoftDeletes,MsgID,UseMongo,EncodeUTF8;
|
|
|
|
|
2021-09-06 13:39:32 +00:00
|
|
|
private const LOGKEY = 'ME-';
|
|
|
|
|
2021-08-11 13:45:30 +00:00
|
|
|
protected $collection = FALSE;
|
|
|
|
|
2021-09-14 13:14:13 +00:00
|
|
|
protected $casts = [ 'kludges' => 'json' ];
|
|
|
|
|
2021-08-11 13:45:30 +00:00
|
|
|
private const cast_utf8 = [
|
2021-12-01 11:45:51 +00:00
|
|
|
'to',
|
|
|
|
'from',
|
2021-08-26 12:32:32 +00:00
|
|
|
'subject',
|
|
|
|
'msg',
|
2021-09-12 13:06:17 +00:00
|
|
|
'origin',
|
|
|
|
'tearline',
|
|
|
|
'tagline',
|
2021-08-11 13:45:30 +00:00
|
|
|
];
|
2019-04-27 13:57:39 +00:00
|
|
|
|
2021-07-30 14:35:52 +00:00
|
|
|
protected $dates = ['datetime'];
|
|
|
|
|
2021-09-06 13:39:32 +00:00
|
|
|
public static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
2021-11-29 10:12:44 +00:00
|
|
|
// @todo if the message is updated with new SEEN-BY's from another route, we'll delete the pending export for systems (if there is one)
|
2021-09-06 13:39:32 +00:00
|
|
|
static::created(function($model) {
|
|
|
|
if (! $model->echoarea_id) {
|
2021-12-29 02:44:27 +00:00
|
|
|
Log::alert(sprintf('%s:- Message has no echo area, not exporting',self::LOGKEY,$model->id));
|
2021-09-06 13:39:32 +00:00
|
|
|
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()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-11-21 05:53:56 +00:00
|
|
|
$model->seenby = $model->seenby->merge($export_ao)->pluck('id')->toArray();
|
2021-09-06 13:39:32 +00:00
|
|
|
$model->save();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-30 14:35:52 +00:00
|
|
|
/* RELATIONS */
|
2019-04-27 13:57:39 +00:00
|
|
|
|
2021-09-06 13:39:32 +00:00
|
|
|
public function echoarea()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Echoarea::class);
|
|
|
|
}
|
|
|
|
|
2021-07-30 14:35:52 +00:00
|
|
|
public function fftn()
|
2019-04-27 13:57:39 +00:00
|
|
|
{
|
2021-07-30 14:35:52 +00:00
|
|
|
return $this
|
|
|
|
->setConnection('pgsql')
|
|
|
|
->belongsTo(Address::class)
|
|
|
|
->withTrashed();
|
2019-04-27 13:57:39 +00:00
|
|
|
}
|
|
|
|
|
2021-08-29 01:48:27 +00:00
|
|
|
/* ATTRIBUTES */
|
|
|
|
|
2021-10-07 12:32:05 +00:00
|
|
|
public function getKludgesAttribute(?string $value): Collection
|
2021-09-14 13:14:13 +00:00
|
|
|
{
|
|
|
|
return collect($this->castAttribute('kludges',$value));
|
|
|
|
}
|
|
|
|
|
2021-09-06 13:39:32 +00:00
|
|
|
public function getPathAttribute(?array $value): Collection
|
2021-08-29 01:48:27 +00:00
|
|
|
{
|
2021-09-06 13:39:32 +00:00
|
|
|
if (is_null($value))
|
|
|
|
return collect();
|
|
|
|
|
2021-08-29 14:44:20 +00:00
|
|
|
return Address::whereIn('id',$value)
|
|
|
|
->orderBy(DB::raw(sprintf("position (id::text in '(%s)')",join(',',$value))))
|
2021-09-06 13:39:32 +00:00
|
|
|
->get();
|
2021-08-29 01:48:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 13:39:32 +00:00
|
|
|
public function getSeenByAttribute(?array $value): Collection
|
2021-08-29 01:48:27 +00:00
|
|
|
{
|
2021-09-06 13:39:32 +00:00
|
|
|
if (is_null($value))
|
|
|
|
return collect();
|
|
|
|
|
|
|
|
return Address::whereIn('id',$value)->get();
|
2021-08-29 01:48:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 14:35:52 +00:00
|
|
|
/* METHODS */
|
|
|
|
|
2021-08-11 13:45:30 +00:00
|
|
|
public function jsonSerialize(): array
|
|
|
|
{
|
|
|
|
return $this->encode();
|
|
|
|
}
|
|
|
|
|
2021-07-30 14:35:52 +00:00
|
|
|
/**
|
|
|
|
* Return this model as a packet
|
|
|
|
*/
|
|
|
|
public function packet(Address $ao): Message
|
2019-04-27 13:57:39 +00:00
|
|
|
{
|
2021-09-06 13:39:32 +00:00
|
|
|
Log::debug(sprintf('%s:Bundling [%s]',self::LOGKEY,$this->id));
|
|
|
|
|
2021-07-30 14:35:52 +00:00
|
|
|
// @todo Dont bundle mail to nodes that have been disabled, or addresses that have been deleted
|
|
|
|
$o = new Message;
|
|
|
|
|
|
|
|
$o->header = [
|
|
|
|
'onode' => $this->fftn->node_id,
|
|
|
|
'dnode' => $ao->node_id,
|
|
|
|
'onet' => $this->fftn->host_id,
|
|
|
|
'dnet' => $ao->host_id,
|
|
|
|
'flags' => 0, // @todo?
|
|
|
|
'cost' => 0,
|
|
|
|
'date'=>$this->datetime->format('d M y H:i:s'),
|
|
|
|
];
|
|
|
|
|
|
|
|
$o->tzutc = $this->datetime->utcOffset($this->tzoffset)->getOffsetString('');
|
|
|
|
$o->user_to = $this->to;
|
|
|
|
$o->user_from = $this->from;
|
|
|
|
$o->subject = $this->subject;
|
2021-09-06 13:39:32 +00:00
|
|
|
$o->echoarea = $this->echoarea->name;
|
2021-07-30 14:35:52 +00:00
|
|
|
$o->flags = $this->flags;
|
|
|
|
|
2021-09-14 13:14:13 +00:00
|
|
|
if ($this->kludges)
|
|
|
|
$o->kludge = $this->kludges;
|
|
|
|
|
2021-07-30 14:35:52 +00:00
|
|
|
$o->kludge->put('mid',$this->id);
|
|
|
|
|
|
|
|
$o->msgid = $this->msgid;
|
|
|
|
if ($this->reply)
|
|
|
|
$o->reply = $this->reply;
|
|
|
|
|
|
|
|
$o->message = $this->msg;
|
|
|
|
|
|
|
|
if ($this->tagline)
|
|
|
|
$o->tagline = $this->tagline;
|
|
|
|
|
|
|
|
if ($this->tearline)
|
|
|
|
$o->tearline = $this->tearline;
|
|
|
|
|
|
|
|
if ($this->origin)
|
|
|
|
$o->origin = $this->origin;
|
|
|
|
|
|
|
|
// @todo SEENBY
|
|
|
|
// @todo PATH
|
|
|
|
|
2021-09-06 13:39:32 +00:00
|
|
|
$o->packed = TRUE;
|
|
|
|
|
2021-07-30 14:35:52 +00:00
|
|
|
return $o;
|
2019-04-27 13:57:39 +00:00
|
|
|
}
|
|
|
|
}
|