From dbbfe46cb9e136c2f2291c0342887120246d5c2a Mon Sep 17 00:00:00 2001 From: Deon George Date: Mon, 6 Sep 2021 23:39:32 +1000 Subject: [PATCH] Echomail export --- app/Classes/FTN/Process/Test.php | 20 +++--- app/Classes/File/Receive.php | 8 +-- app/Classes/File/Send.php | 7 ++ app/Console/Commands/PacketProcess.php | 2 +- app/Http/Controllers/SystemController.php | 1 + .../{PacketProcess.php => MessageProcess.php} | 4 +- app/Models/Address.php | 30 +++++--- app/Models/Echoarea.php | 4 +- app/Models/Echomail.php | 68 +++++++++++++++++-- app/Models/System.php | 22 +++--- .../migrations/2021_08_10_130352_echomail.php | 16 ----- .../2021_08_22_110731_echomail_export.php | 39 +++++++++++ resources/views/widgets/message.blade.php | 4 +- 13 files changed, 163 insertions(+), 62 deletions(-) rename app/Jobs/{PacketProcess.php => MessageProcess.php} (98%) create mode 100644 database/migrations/2021_08_22_110731_echomail_export.php diff --git a/app/Classes/FTN/Process/Test.php b/app/Classes/FTN/Process/Test.php index 3e8d76a..5d211a7 100644 --- a/app/Classes/FTN/Process/Test.php +++ b/app/Classes/FTN/Process/Test.php @@ -7,7 +7,7 @@ use Carbon\CarbonInterface; use Illuminate\Support\Facades\Log; use App\Classes\FTN\{Message,Process}; -use App\Models\{Echomail,Setup}; +use App\Models\{Echoarea,Echomail,Setup}; /** * Process messages to Test @@ -16,6 +16,8 @@ use App\Models\{Echomail,Setup}; */ final class Test extends Process { + private const LOGKEY = 'RT-'; + private static array $logo = [ 'Ú¿ÚÄ¿ÚÄ¿Ú¿', ' ³ ³ÄÙÀÄ¿ ³ ', @@ -29,12 +31,12 @@ final class Test extends Process if ((strtolower($msg->user_to) !== 'all') || ! in_array(strtolower($msg->subject),self::testing)) return FALSE; - Log::info(sprintf('Processing TEST message from (%s) [%s]',$msg->user_from,$msg->fftn)); + Log::info(sprintf('%s:- Processing TEST message from (%s) [%s]',self::LOGKEY,$msg->user_from,$msg->fftn)); $ftns = Setup::findOrFail(config('app.id'))->system->match($msg->fftn_o->zone)->first(); $reply = sprintf("Your test was received here on %s and it looks like you sent it on %s. If that is correct, then it took %s to get here.\r", - $msg->date->toDateTimeString(), - Carbon::now()->toDateTimeString(), + $msg->date->utc()->toDateTimeString(), + Carbon::now()->utc()->toDateTimeString(), $msg->date->diffForHumans(['parts'=>3,'syntax'=>CarbonInterface::DIFF_ABSOLUTE]) ); @@ -45,23 +47,25 @@ final class Test extends Process $reply .= sprintf("SUBJECT: %s\r",$msg->subject); $reply .= $msg->message."\r"; $reply .= "------------------------------ CONTROL LINES ------------------------------\r"; - $reply .= sprintf("DATE: %s\r",$msg->date->format('Y-m-d H:i:s')); + $reply .= sprintf("DATE: %s\r",$msg->date->utc()->format('Y-m-d H:i:s')); $reply .= sprintf("MSGID: %s\r",$msg->msgid); foreach ($msg->kludge as $k=>$v) - $reply .= sprintf("@%s: %s\n",strtoupper($k),$v); + $reply .= sprintf("@%s: %s\r",strtoupper($k),$v); foreach ($msg->via as $via) - $reply .= sprintf("VIA: %s\n",$via); + $reply .= sprintf("VIA: %s\r",$via); $reply .= "------------------------------ END MESSAGE ------------------------------\r"; + $eo = Echoarea::where('name',$msg->echoarea)->single(); + $o = new Echomail; $o->to = $msg->user_from; $o->from = Setup::PRODUCT_NAME; $o->subject = 'Test Reply'; $o->datetime = Carbon::now(); $o->tzoffset = $o->datetime->utcOffset(); - $o->echoarea = $msg->echoarea; + $o->echoarea_id = $eo?->id; $o->reply = $msg->msgid; $o->fftn_id = $ftns->id; diff --git a/app/Classes/File/Receive.php b/app/Classes/File/Receive.php index 33b525d..123c05e 100644 --- a/app/Classes/File/Receive.php +++ b/app/Classes/File/Receive.php @@ -10,7 +10,7 @@ use Symfony\Component\HttpFoundation\File\Exception\FileException; use App\Classes\FTN\InvalidPacketException; use App\Classes\FTN\Packet; -use App\Jobs\PacketProcess; +use App\Jobs\MessageProcess; use App\Models\Address; /** @@ -128,7 +128,7 @@ final class Receive extends Item // Check the packet password if ($this->ao->session('pktpass') !== $po->password) { - Log::error(sprintf('%s: ! Packet from [%s] with password [%s] is invalid (%s)',self::LOGKEY,$this->ao->ftn,$po->password)); + Log::error(sprintf('%s: ! Packet from [%s] with password [%s] is invalid.',self::LOGKEY,$this->ao->ftn,$po->password)); // @todo Generate message to system advising invalid password - that message should be sent without a packet password! break; } @@ -145,9 +145,9 @@ final class Receive extends Item // Dispatch job. if ($queue) - PacketProcess::dispatch($msg); + MessageProcess::dispatch($msg); else - PacketProcess::dispatchSync($msg); + MessageProcess::dispatchSync($msg); } if ($po->errors->count()) { diff --git a/app/Classes/File/Send.php b/app/Classes/File/Send.php index d67833a..cba62f2 100644 --- a/app/Classes/File/Send.php +++ b/app/Classes/File/Send.php @@ -236,6 +236,13 @@ final class Send extends Item $this->packets->push(new Mail($x,self::I_SEND)); } + + // Echomail + if ($x=$ao->getEchomail()) { + Log::debug(sprintf('%s: - Echomail(s) added for sending to [%s]',self::LOGKEY,$ao->ftn)); + + $this->packets->push(new Mail($x,self::I_SEND)); + } } /** diff --git a/app/Console/Commands/PacketProcess.php b/app/Console/Commands/PacketProcess.php index 05c347d..e2da1fe 100644 --- a/app/Console/Commands/PacketProcess.php +++ b/app/Console/Commands/PacketProcess.php @@ -6,7 +6,7 @@ use Illuminate\Console\Command; use Symfony\Component\HttpFoundation\File\File; use App\Classes\FTN\Packet; -use App\Jobs\PacketProcess as Job; +use App\Jobs\MessageProcess as Job; use App\Models\Zone; class PacketProcess extends Command diff --git a/app/Http/Controllers/SystemController.php b/app/Http/Controllers/SystemController.php index 1740138..51710d8 100644 --- a/app/Http/Controllers/SystemController.php +++ b/app/Http/Controllers/SystemController.php @@ -326,6 +326,7 @@ class SystemController extends Controller * * @param Request $request * @param System $o + * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse */ public function echoareas(Request $request,System $o) { diff --git a/app/Jobs/PacketProcess.php b/app/Jobs/MessageProcess.php similarity index 98% rename from app/Jobs/PacketProcess.php rename to app/Jobs/MessageProcess.php index a4a22f5..30487a7 100644 --- a/app/Jobs/PacketProcess.php +++ b/app/Jobs/MessageProcess.php @@ -12,7 +12,7 @@ use Illuminate\Support\Facades\Log; use App\Classes\FTN\{Message,Process}; use App\Models\{Echoarea,Echomail,Netmail,Setup}; -class PacketProcess implements ShouldQueue +class MessageProcess implements ShouldQueue { private const LOGKEY = 'PP-'; @@ -27,7 +27,7 @@ class PacketProcess implements ShouldQueue } /** - * When calling PacketProcess - we assume that the packet is from a valid source + * When calling MessageProcess - we assume that the packet is from a valid source */ public function handle() { diff --git a/app/Models/Address.php b/app/Models/Address.php index 4f7aac1..84efa93 100644 --- a/app/Models/Address.php +++ b/app/Models/Address.php @@ -7,14 +7,15 @@ use Exception; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Support\Facades\DB; use App\Classes\FTN\Packet; use App\Http\Controllers\DomainController; -use App\Traits\ScopeActive; +use App\Traits\{ScopeActive,UsePostgres}; class Address extends Model { - use ScopeActive,SoftDeletes; + use ScopeActive,SoftDeletes,UsePostgres; /* SCOPES */ @@ -321,17 +322,28 @@ class Address extends Model */ public function getEchomail(): ?Packet { - if (($x=Echomail::select('*') //where('tftn_id',$this->id) - ->where(function($q) { - return $q->whereNull('sent') - ->orWhere('sent',FALSE); - })) + $pkt = NULL; + + $echomail = DB::table('address_echomail') + ->select('echomail_id') + ->where('address_id',$this->id) + ->whereNull('sent_date') + ->get(); + + if (($x=Echomail::select('*') + ->whereIn('_id',$echomail->pluck('echomail_id'))) ->count()) { - return $this->getPacket($x->get()); + $pkt = $this->getPacket($x->get()); + + DB::table('address_echomail') + ->whereIn('echomail_id',$echomail->pluck('echomail_id')) + ->where('address_id',$this->id) + ->whereNull('sent_date') + ->update(['sent_date'=>Carbon::now()]); } - return NULL; + return $pkt; } /** diff --git a/app/Models/Echoarea.php b/app/Models/Echoarea.php index 81a732f..2c6b353 100644 --- a/app/Models/Echoarea.php +++ b/app/Models/Echoarea.php @@ -5,11 +5,11 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; -use App\Traits\ScopeActive; +use App\Traits\{ScopeActive,UsePostgres}; class Echoarea extends Model { - use SoftDeletes,ScopeActive; + use SoftDeletes,ScopeActive,UsePostgres; /* RELATIONS */ diff --git a/app/Models/Echomail.php b/app/Models/Echomail.php index bcb836d..f9e17ec 100644 --- a/app/Models/Echomail.php +++ b/app/Models/Echomail.php @@ -2,8 +2,10 @@ namespace App\Models; +use Carbon\Carbon; use Illuminate\Support\Collection; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; use Jenssegers\Mongodb\Eloquent\Model; use Jenssegers\Mongodb\Eloquent\SoftDeletes; @@ -11,10 +13,12 @@ use App\Classes\FTN\Message; use App\Interfaces\Packet; use App\Traits\{EncodeUTF8,MsgID,UseMongo}; -class Echomail extends Model implements Packet +final class Echomail extends Model implements Packet { use SoftDeletes,MsgID,UseMongo,EncodeUTF8; + private const LOGKEY = 'ME-'; + protected $collection = FALSE; private const cast_utf8 = [ @@ -24,8 +28,49 @@ class Echomail extends Model implements Packet protected $dates = ['datetime']; + public static function boot() + { + parent::boot(); + + static::created(function($model) { + if (! $model->echoarea_id) { + Log::alert(sprintf('%s:- Message has no echo area, no exporting',self::LOGKEY,$model->id)); + 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() + ]); + } + + $model->seenby = $model->seenby->merge($export_ao); + $model->save(); + }); + } + /* RELATIONS */ + public function echoarea() + { + return $this->belongsTo(Echoarea::class); + } + public function fftn() { return $this @@ -36,17 +81,22 @@ class Echomail extends Model implements Packet /* ATTRIBUTES */ - public function getPathAttribute($value): Collection + public function getPathAttribute(?array $value): Collection { + if (is_null($value)) + return collect(); + return Address::whereIn('id',$value) ->orderBy(DB::raw(sprintf("position (id::text in '(%s)')",join(',',$value)))) - ->get() - ->pluck('ftn3d'); + ->get(); } - public function getSeenByAttribute($value): Collection + public function getSeenByAttribute(?array $value): Collection { - return Address::whereIn('id',$value)->get()->pluck('ftn2d'); + if (is_null($value)) + return collect(); + + return Address::whereIn('id',$value)->get(); } /* METHODS */ @@ -61,6 +111,8 @@ class Echomail extends Model implements Packet */ public function packet(Address $ao): Message { + 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; @@ -78,7 +130,7 @@ class Echomail extends Model implements Packet $o->user_to = $this->to; $o->user_from = $this->from; $o->subject = $this->subject; - $o->echoarea = $this->echoarea; + $o->echoarea = $this->echoarea->name; $o->flags = $this->flags; $o->kludge->put('mid',$this->id); @@ -101,6 +153,8 @@ class Echomail extends Model implements Packet // @todo SEENBY // @todo PATH + $o->packed = TRUE; + return $o; } } \ No newline at end of file diff --git a/app/Models/System.php b/app/Models/System.php index 8b1ceef..3ce9f16 100644 --- a/app/Models/System.php +++ b/app/Models/System.php @@ -80,17 +80,6 @@ class System extends Model ->where('addresses.system_id',$this->id); } - /** - * Return the system's address in the same zone - * - * @param Zone $o - * @return Collection - */ - public function match(Zone $o): Collection - { - return $this->addresses->where('zone_id',$o->id); - } - /** * Return the system name, or role name for the zone * @@ -115,4 +104,15 @@ class System extends Model return $this->name; } } + + /** + * Return the system's address in the same zone + * + * @param Zone $o + * @return Collection + */ + public function match(Zone $o): Collection + { + return $this->addresses->where('zone_id',$o->id); + } } \ No newline at end of file diff --git a/database/migrations/2021_08_10_130352_echomail.php b/database/migrations/2021_08_10_130352_echomail.php index f1b6bc9..074d960 100644 --- a/database/migrations/2021_08_10_130352_echomail.php +++ b/database/migrations/2021_08_10_130352_echomail.php @@ -60,22 +60,6 @@ class Echomail extends Migration $table->dateTime('subscribed'); }); - - Schema::create('address_echomail', function (Blueprint $table) { - $table->integer('echomail_id'); - $table->string('packet'); - - $table->integer('address_id'); - $table->foreign('address_id')->references('id')->on('addresses'); - }); - - Schema::create('address_file', function (Blueprint $table) { - $table->integer('filearea_id'); - $table->boolean('sent'); - - $table->integer('address_id'); - $table->foreign('address_id')->references('id')->on('addresses'); - }); } /** diff --git a/database/migrations/2021_08_22_110731_echomail_export.php b/database/migrations/2021_08_22_110731_echomail_export.php new file mode 100644 index 0000000..c375007 --- /dev/null +++ b/database/migrations/2021_08_22_110731_echomail_export.php @@ -0,0 +1,39 @@ +string('echomail_id'); + + $table->integer('address_id'); + $table->foreign('address_id')->references('id')->on('addresses'); + + $table->datetime('export_date'); + $table->datetime('sent_date')->nullable(); + + $table->unique(['address_id','echomail_id']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('address_echomail'); + } +} diff --git a/resources/views/widgets/message.blade.php b/resources/views/widgets/message.blade.php index 155e28f..f238e94 100644 --- a/resources/views/widgets/message.blade.php +++ b/resources/views/widgets/message.blade.php @@ -32,12 +32,12 @@
- SEENBY:
{!! $msg->seenby->join(', ') !!} + SEENBY:
{!! $msg->seenby->pluck('ftn2d')->join(', ') !!}
- PATH:
{!! $msg->path->join(' -> ') !!} + PATH:
{!! $msg->path->pluck('ftn3d')->join(' -> ') !!}
\ No newline at end of file