clrghouz/app/Models/Echomail.php

200 lines
4.3 KiB
PHP
Raw Normal View History

2019-04-27 13:57:39 +00:00
<?php
namespace App\Models;
2021-09-06 13:39:32 +00:00
use Carbon\Carbon;
2022-01-01 05:59:35 +00:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
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;
use Rennokki\QueryCache\Traits\QueryCacheable;
2019-04-27 13:57:39 +00:00
use App\Classes\FTN\Message;
use App\Interfaces\Packet;
2022-01-01 05:59:35 +00:00
use App\Traits\{EncodeUTF8,MsgID};
2021-09-06 13:39:32 +00:00
final class Echomail extends Model implements Packet
2019-04-27 13:57:39 +00:00
{
use SoftDeletes,EncodeUTF8,MsgID,QueryCacheable;
2021-08-11 13:45:30 +00:00
2021-09-06 13:39:32 +00:00
private const LOGKEY = 'ME-';
2022-01-01 05:59:35 +00:00
private array $set_seenby = [];
private array $set_path = [];
private ?string $set_packet = NULL;
private bool $no_export = FALSE;
2022-01-01 05:59:35 +00:00
protected $casts = [
'kludges' => 'json',
'rogue_seenby' => 'json',
'rogue_path' => 'json',
];
2021-09-14 13:14:13 +00:00
2021-08-11 13:45:30 +00:00
private const cast_utf8 = [
'to',
'from',
2021-08-26 12:32:32 +00:00
'subject',
'msg',
'origin',
'tearline',
'tagline',
2021-08-11 13:45:30 +00:00
];
2019-04-27 13:57:39 +00:00
protected $dates = ['datetime'];
2022-01-01 05:59:35 +00:00
public function __set($key, $value)
{
switch ($key) {
case 'no_export':
2022-01-01 05:59:35 +00:00
case 'set_path':
case 'set_packet':
case 'set_seenby':
$this->{$key} = $value;
break;
default:
parent::__set($key,$value);
}
}
2021-09-06 13:39:32 +00:00
public static function boot()
{
parent::boot();
// @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) {
Log::alert(sprintf('%s:- Message has no echo area, not exporting',self::LOGKEY,$model->id));
2021-09-06 13:39:32 +00:00
return;
}
2022-01-01 05:59:35 +00:00
// Save the seenby
foreach ($model->set_seenby as $aoid) {
DB::insert('INSERT INTO echomail_seenby (echomail_id,address_id,packet) VALUES (?,?,?)',[
$model->id,
$aoid,
$model->set_packet,
]);
2021-09-06 13:39:32 +00:00
}
2022-01-01 05:59:35 +00:00
// Save the Path
$ppoid = NULL;
foreach ($model->set_path as $aoid) {
$po = DB::select('INSERT INTO echomail_path (echomail_id,address_id,parent_id) VALUES (?,?,?) RETURNING id',[
$model->id,
$aoid,
$ppoid,
2021-09-06 13:39:32 +00:00
]);
2022-01-01 05:59:35 +00:00
$ppoid = $po[0]->id;
2021-09-06 13:39:32 +00:00
}
2022-01-01 05:59:35 +00:00
// See if we need to export this message.
$exportto = $model->echoarea->addresses->pluck('id')->diff($model->set_seenby);
if ($exportto->count()) {
if ($model->no_export) {
Log::debug(sprintf('%s:- NOT processing exporting of message by configuration [%s] to [%s]',self::LOGKEY,$model->id,$exportto->join(',')));
return;
}
2022-01-01 05:59:35 +00:00
Log::debug(sprintf('%s:- Exporting message [%s] to [%s]',self::LOGKEY,$model->id,$exportto->join(',')));
// Save the seenby for the exported systems
$export_at = Carbon::now();
foreach ($exportto as $aoid) {
DB::insert('INSERT INTO echomail_seenby (echomail_id,address_id,export_at) VALUES (?,?,?)',[
$model->id,
$aoid,
$export_at,
]);
}
}
2021-09-06 13:39:32 +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);
}
public function fftn()
2019-04-27 13:57:39 +00:00
{
2022-01-01 05:59:35 +00:00
return $this->belongsTo(Address::class)
->withTrashed();
2019-04-27 13:57:39 +00:00
}
2022-01-01 05:59:35 +00:00
public function seenby()
2021-08-29 01:48:27 +00:00
{
2022-01-01 05:59:35 +00:00
return $this->belongsToMany(Address::class,'echomail_seenby')
->ftnOrder();
2021-08-29 01:48:27 +00:00
}
2022-01-01 05:59:35 +00:00
public function path()
2021-08-29 01:48:27 +00:00
{
2022-01-01 05:59:35 +00:00
return $this->belongsToMany(Address::class,'echomail_path');
2021-08-29 01:48:27 +00:00
}
/* METHODS */
2021-08-11 13:45:30 +00:00
public function jsonSerialize(): array
{
return $this->encode();
}
/**
* 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));
// @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;
$o->flags = $this->flags;
2021-09-14 13:14:13 +00:00
if ($this->kludges)
$o->kludge = $this->kludges;
2022-01-01 05:59:35 +00:00
$o->kludge->put('dbid',$this->id);
$o->msgid = $this->msgid;
2022-01-01 05:59:35 +00:00
if ($this->replyid)
$o->replyid = $this->replyid;
$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;
return $o;
2019-04-27 13:57:39 +00:00
}
}