From da85e85774cf964a2633f5f7477b09ab86a8e8c1 Mon Sep 17 00:00:00 2001 From: Deon George Date: Sun, 30 Oct 2022 23:42:30 +1100 Subject: [PATCH] Cast empty Collections to null, Cast strings to zstd compressed strings, add msg_src to echomails processed, fix duplicate seenbys --- app/Casts/CollectionOrNull.php | 38 ++++++++++++++++++++++++++ app/Casts/CompressedString.php | 37 +++++++++++++++++++++++++ app/Jobs/MessageProcess.php | 6 ++-- app/Models/Domain.php | 12 ++++---- app/Models/Echomail.php | 50 ++++++++++------------------------ 5 files changed, 100 insertions(+), 43 deletions(-) create mode 100644 app/Casts/CollectionOrNull.php create mode 100644 app/Casts/CompressedString.php diff --git a/app/Casts/CollectionOrNull.php b/app/Casts/CollectionOrNull.php new file mode 100644 index 0000000..ab1c04c --- /dev/null +++ b/app/Casts/CollectionOrNull.php @@ -0,0 +1,38 @@ +count()) ? json_encode($value) : NULL; + } +} \ No newline at end of file diff --git a/app/Casts/CompressedString.php b/app/Casts/CompressedString.php new file mode 100644 index 0000000..0e89e53 --- /dev/null +++ b/app/Casts/CompressedString.php @@ -0,0 +1,37 @@ +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->msgid = $this->msg->msgid; + $o->replyid = $this->msg->replyid; $o->msg = $this->msg->message_src."\r"; + $o->msg_src = $this->msg->message_src; $o->msg_crc = md5($this->msg->message); $o->rogue_seenby = $this->msg->rogue_seenby; $o->rogue_path = $this->msg->rogue_path; - $o->set_path = $this->msg->pathaddress->toArray(); - $o->set_seenby = $this->msg->seenaddress->toArray(); + $o->set_path = $this->msg->pathaddress; + $o->set_seenby = $this->msg->seenaddress; $o->save(); diff --git a/app/Models/Domain.php b/app/Models/Domain.php index aab3fa3..f514802 100644 --- a/app/Models/Domain.php +++ b/app/Models/Domain.php @@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; use Illuminate\Support\Facades\DB; +use App\Casts\CompressedString; use App\Traits\{QueryCacheableConfig,ScopeActive}; class Domain extends Model @@ -17,6 +18,10 @@ class Domain extends Model private const CACHE_TIME = 3600; private const STATS_MONTHS = 6; + protected $casts = [ + 'homepage' => CompressedString::class, + ]; + /* SCOPES */ /** @@ -49,12 +54,7 @@ class Domain extends Model public function getHomePageAttribute(string $value): string { //0xFD2FB528 - return $value ? zstd_uncompress(base64_decode($value)) : 'No available information at the moment.'; - } - - public function setHomePageAttribute(string $value): void - { - $this->attributes['homepage'] = base64_encode(zstd_compress($value,9)); + return $this->castAttribute('homepage',$value) ?: 'No available information at the moment.'; } /* METHODS */ diff --git a/app/Models/Echomail.php b/app/Models/Echomail.php index 74558bc..8d5c0c0 100644 --- a/app/Models/Echomail.php +++ b/app/Models/Echomail.php @@ -10,6 +10,7 @@ use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use Rennokki\QueryCache\Traits\QueryCacheable; +use App\Casts\{CollectionOrNull,CompressedString}; use App\Classes\FTN\Message; use App\Interfaces\Packet; use App\Traits\{EncodeUTF8,MsgID}; @@ -19,15 +20,17 @@ final class Echomail extends Model implements Packet use SoftDeletes,EncodeUTF8,MsgID,QueryCacheable; private const LOGKEY = 'ME-'; - private array $set_seenby = []; - private array $set_path = []; + private Collection $set_seenby; + private Collection $set_path; private ?string $set_packet = NULL; private bool $no_export = FALSE; protected $casts = [ - 'kludges' => 'json', - 'rogue_seenby' => 'json', - 'rogue_path' => 'json', + 'kludges' => CollectionOrNull::class, + 'rogue_seenby' => CollectionOrNull::class, + 'rogue_path' => CollectionOrNull::class, + 'msg' => CompressedString::class, + 'msg_src' => CompressedString::class, ]; private const cast_utf8 = [ @@ -70,20 +73,16 @@ final class Echomail extends Model implements Packet } // 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; - $model->set_seenby = array_merge($model->set_seenby,$ftns->pluck('id')->toArray()); - $model->set_path = array_merge($model->set_path,$ftns->pluck('id')->toArray()); + $model->set_seenby = $model->set_seenby->merge($ftns->pluck('id'))->unique(); + $model->set_path = $model->set_path->merge($ftns->pluck('id')); // 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, - ]); - } + $model->seenby()->syncWithPivotValues($model->set_seenby,['packet'=>$model->set_packet]); // Save the Path $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(','))); // 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, - ]); - } + $model->seenby()->syncWithPivotValues($exportto,['export_at'=>Carbon::now()],FALSE); } }); } @@ -146,18 +138,6 @@ final class Echomail extends Model implements Packet ->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 */ public function jsonSerialize(): array