clrghouz/app/Traits/MessageAttributes.php

184 lines
4.5 KiB
PHP

<?php
/**
* Common Attributes used by message packets (and thus their Models)
*/
namespace App\Traits;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Validator as ValidatorResult;
use App\Classes\FTN\Message;
use App\Models\Address;
trait MessageAttributes
{
use EncodeUTF8;
// Items we need to set when creating()
public Collection $set;
// Validation Errors
public ?ValidatorResult $errors = NULL;
private const cast_utf8 = [
'to',
'from',
'subject',
'msg',
'msg_src',
'origin',
'tearline',
'tagline',
];
public function __construct()
{
parent::__construct();
// Init
$this->set = collect();
}
/* ATTRIBUTES */
public function getContentAttribute(): string
{
if ($this->msg_src)
return $this->msg_src."\r";
// If we have a msg_src attribute, we'll use that
$result = $this->msg."\r\r";
if ($this->tagline)
$result .= sprintf("%s\r",$this->tagline);
if ($this->tearline)
$result .= sprintf("%s\r",$this->tearline);
if ($this->origin)
$result .= sprintf("%s",$this->origin);
return rtrim($result,"\r")."\r";
}
public function getDateAttribute(): Carbon
{
return $this->datetime->utcOffset($this->tzoffset);
}
public function getOriginAttribute(string $val=NULL): ?string
{
// If $val is not set, then it may be an unsaved object
return ((! $this->exists) && $this->set->has('set_origin'))
? sprintf(' * Origin: %s',$this->set->get('set_origin'))
: $val;
}
public function getTaglineAttribute(string $val=NULL): ?string
{
// If $val is not set, then it may be an unsaved object
return ((! $this->exists) && $this->set->has('set_tagline'))
? sprintf('... %s',$this->set->get('set_tagline'))
: $val;
}
public function getTearlineAttribute(string $val=NULL): ?string
{
// If $val is not set, then it may be an unsaved object
return ((! $this->exists) && $this->set->has('set_tearline'))
? sprintf('--- %s',$this->set->get('set_tearline'))
: $val;
}
/* METHODS */
/**
* Return an array of flag descriptions
*
* @return Collection
*
* http://ftsc.org/docs/fsc-0001.000
* AttributeWord bit meaning
* --- --------------------
* 0 + Private
* 1 + s Crash
* 2 Recd
* 3 Sent
* 4 + FileAttached
* 5 InTransit
* 6 Orphan
* 7 KillSent
* 8 Local
* 9 s HoldForPickup
* 10 + unused
* 11 s FileRequest
* 12 + s ReturnReceiptRequest
* 13 + s IsReturnReceipt
* 14 + s AuditRequest
* 15 s FileUpdateReq
*
* s - this bit is supported by SEAdog only
* + - this bit is not zeroed before packeting
*/
public function flags(): Collection
{
return collect([
'private' => $this->isFlagSet(Message::FLAG_PRIVATE),
'crash' => $this->isFlagSet(Message::FLAG_CRASH),
'recd' => $this->isFlagSet(Message::FLAG_RECD),
'sent' => $this->isFlagSet(Message::FLAG_SENT),
'fileattach' => $this->isFlagSet(Message::FLAG_FILEATTACH),
'intransit' => $this->isFlagSet(Message::FLAG_INTRANSIT),
'orphan' => $this->isFlagSet(Message::FLAG_ORPHAN),
'killsent' => $this->isFlagSet(Message::FLAG_KILLSENT),
'local' => $this->isFlagSet(Message::FLAG_LOCAL),
'hold' => $this->isFlagSet(Message::FLAG_HOLD),
'unused-10' => $this->isFlagSet(Message::FLAG_UNUSED_10),
'filereq' => $this->isFlagSet(Message::FLAG_FREQ),
'receipt-req' => $this->isFlagSet(Message::FLAG_RETRECEIPT),
'receipt' => $this->isFlagSet(Message::FLAG_ISRETRECEIPT),
'audit' => $this->isFlagSet(Message::FLAG_AUDITREQ),
'fileupdate' => $this->isFlagSet(Message::FLAG_FILEUPDATEREQ),
])->filter();
}
private function isFlagSet($flag): bool
{
return ($this->flags & $flag);
}
/**
* Return this model as a packet
*/
public function packet(Address $ao): Message
{
Log::debug(sprintf('%s:+ Bundling [%s]',self::LOGKEY,$this->id),['type'=>get_class($this)]);
// For netmails, our tftn is the next hop
$this->tftn = $ao;
// @todo Dont bundle mail to nodes that have been disabled, or addresses that have been deleted
return Message::packMessage($this);
}
/**
* Return our path in order
*
* @param string $display
* @param int|NULL $start
* @return Collection
*/
public function pathorder(string $display='ftn2d',int $start=NULL): Collection
{
$result = collect();
if ($x=$this->path->firstWhere('pivot.parent_id',$start)) {
$result->push($x->$display);
$result->push($this->pathorder($display,$x->pivot->id));
}
return $result->flatten()->filter();
}
}