Cast empty Collections to null, Cast strings to zstd compressed strings, add msg_src to echomails processed, fix duplicate seenbys

This commit is contained in:
Deon George 2022-10-30 23:42:30 +11:00
parent 31db017a0d
commit da85e85774
5 changed files with 100 additions and 43 deletions

View File

@ -0,0 +1,38 @@
<?php
namespace App\Casts;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
class CollectionOrNull implements CastsAttributes
{
/**
* Cast the given value.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return Collection
*/
public function get($model, string $key, $value, array $attributes): Collection
{
return collect(json_decode($value, true));
}
/**
* Prepare the given value for storage.
*
* @param Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return string|null
*/
public function set($model, string $key, $value, array $attributes): ?string
{
return ($value->count()) ? json_encode($value) : NULL;
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace App\Casts;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
class CompressedString implements CastsAttributes
{
/**
* Cast the given value.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return string
*/
public function get($model, string $key, $value, array $attributes): string
{
return zstd_uncompress(base64_decode($value));
}
/**
* Prepare the given value for storage.
*
* @param Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return string|null
*/
public function set($model, string $key, $value, array $attributes): ?string
{
return $value ? base64_encode(zstd_compress($value)) : NULL;
}
}

View File

@ -214,13 +214,15 @@ class MessageProcess implements ShouldQueue
$o->fftn_id = ($x=$this->msg->fboss_o) ? $x->id : NULL; // @todo This should be the node that originated the message - but since that node is not in the DB it would be null $o->fftn_id = ($x=$this->msg->fboss_o) ? $x->id : NULL; // @todo This should be the node that originated the message - but since that node is not in the DB it would be null
$o->echoarea_id = $ea->id; $o->echoarea_id = $ea->id;
$o->msgid = $this->msg->msgid; $o->msgid = $this->msg->msgid;
$o->replyid = $this->msg->replyid;
$o->msg = $this->msg->message_src."\r"; $o->msg = $this->msg->message_src."\r";
$o->msg_src = $this->msg->message_src;
$o->msg_crc = md5($this->msg->message); $o->msg_crc = md5($this->msg->message);
$o->rogue_seenby = $this->msg->rogue_seenby; $o->rogue_seenby = $this->msg->rogue_seenby;
$o->rogue_path = $this->msg->rogue_path; $o->rogue_path = $this->msg->rogue_path;
$o->set_path = $this->msg->pathaddress->toArray(); $o->set_path = $this->msg->pathaddress;
$o->set_seenby = $this->msg->seenaddress->toArray(); $o->set_seenby = $this->msg->seenaddress;
$o->save(); $o->save();

View File

@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use App\Casts\CompressedString;
use App\Traits\{QueryCacheableConfig,ScopeActive}; use App\Traits\{QueryCacheableConfig,ScopeActive};
class Domain extends Model class Domain extends Model
@ -17,6 +18,10 @@ class Domain extends Model
private const CACHE_TIME = 3600; private const CACHE_TIME = 3600;
private const STATS_MONTHS = 6; private const STATS_MONTHS = 6;
protected $casts = [
'homepage' => CompressedString::class,
];
/* SCOPES */ /* SCOPES */
/** /**
@ -49,12 +54,7 @@ class Domain extends Model
public function getHomePageAttribute(string $value): string public function getHomePageAttribute(string $value): string
{ {
//0xFD2FB528 //0xFD2FB528
return $value ? zstd_uncompress(base64_decode($value)) : 'No available information at the moment.'; return $this->castAttribute('homepage',$value) ?: 'No available information at the moment.';
}
public function setHomePageAttribute(string $value): void
{
$this->attributes['homepage'] = base64_encode(zstd_compress($value,9));
} }
/* METHODS */ /* METHODS */

View File

@ -10,6 +10,7 @@ use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Rennokki\QueryCache\Traits\QueryCacheable; use Rennokki\QueryCache\Traits\QueryCacheable;
use App\Casts\{CollectionOrNull,CompressedString};
use App\Classes\FTN\Message; use App\Classes\FTN\Message;
use App\Interfaces\Packet; use App\Interfaces\Packet;
use App\Traits\{EncodeUTF8,MsgID}; use App\Traits\{EncodeUTF8,MsgID};
@ -19,15 +20,17 @@ final class Echomail extends Model implements Packet
use SoftDeletes,EncodeUTF8,MsgID,QueryCacheable; use SoftDeletes,EncodeUTF8,MsgID,QueryCacheable;
private const LOGKEY = 'ME-'; private const LOGKEY = 'ME-';
private array $set_seenby = []; private Collection $set_seenby;
private array $set_path = []; private Collection $set_path;
private ?string $set_packet = NULL; private ?string $set_packet = NULL;
private bool $no_export = FALSE; private bool $no_export = FALSE;
protected $casts = [ protected $casts = [
'kludges' => 'json', 'kludges' => CollectionOrNull::class,
'rogue_seenby' => 'json', 'rogue_seenby' => CollectionOrNull::class,
'rogue_path' => 'json', 'rogue_path' => CollectionOrNull::class,
'msg' => CompressedString::class,
'msg_src' => CompressedString::class,
]; ];
private const cast_utf8 = [ private const cast_utf8 = [
@ -70,20 +73,16 @@ final class Echomail extends Model implements Packet
} }
// Our address // Our address
$ftns = Setup::findOrFail(config('app.id'))->system->match($model->fftn->zone,Address::NODE_ACTIVE|Address::NODE_PVT|Address::NODE_HOLD); $ftns = Setup::findOrFail(config('app.id'))
->system
->match($model->fftn->zone,Address::NODE_ACTIVE|Address::NODE_PVT|Address::NODE_HOLD);
// Add our address to the seenby; // Add our address to the seenby;
$model->set_seenby = array_merge($model->set_seenby,$ftns->pluck('id')->toArray()); $model->set_seenby = $model->set_seenby->merge($ftns->pluck('id'))->unique();
$model->set_path = array_merge($model->set_path,$ftns->pluck('id')->toArray()); $model->set_path = $model->set_path->merge($ftns->pluck('id'));
// Save the seenby // Save the seenby
foreach ($model->set_seenby as $aoid) { $model->seenby()->syncWithPivotValues($model->set_seenby,['packet'=>$model->set_packet]);
DB::insert('INSERT INTO echomail_seenby (echomail_id,address_id,packet) VALUES (?,?,?)',[
$model->id,
$aoid,
$model->set_packet,
]);
}
// Save the Path // Save the Path
$ppoid = NULL; $ppoid = NULL;
@ -109,14 +108,7 @@ final class Echomail extends Model implements Packet
Log::debug(sprintf('%s:- Exporting message [%s] to [%s]',self::LOGKEY,$model->id,$exportto->join(','))); Log::debug(sprintf('%s:- Exporting message [%s] to [%s]',self::LOGKEY,$model->id,$exportto->join(',')));
// Save the seenby for the exported systems // Save the seenby for the exported systems
$export_at = Carbon::now(); $model->seenby()->syncWithPivotValues($exportto,['export_at'=>Carbon::now()],FALSE);
foreach ($exportto as $aoid) {
DB::insert('INSERT INTO echomail_seenby (echomail_id,address_id,export_at) VALUES (?,?,?)',[
$model->id,
$aoid,
$export_at,
]);
}
} }
}); });
} }
@ -146,18 +138,6 @@ final class Echomail extends Model implements Packet
->withPivot(['id','parent_id']); ->withPivot(['id','parent_id']);
} }
/* ATTRIBUTES */
public function getMsgAttribute($value): ?string
{
return $value ? zstd_uncompress(base64_decode($value)) : NULL;
}
public function getMsgSrcAttribute($value): ?string
{
return $value ? zstd_uncompress(base64_decode($value)) : NULL;
}
/* METHODS */ /* METHODS */
public function jsonSerialize(): array public function jsonSerialize(): array