Echomail export

This commit is contained in:
Deon George 2021-09-06 23:39:32 +10:00
parent 8306f4c3a3
commit dbbfe46cb9
13 changed files with 163 additions and 62 deletions

View File

@ -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;

View File

@ -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()) {

View File

@ -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));
}
}
/**

View File

@ -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

View File

@ -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)
{

View File

@ -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()
{

View File

@ -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;
}
/**

View File

@ -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 */

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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');
});
}
/**

View File

@ -0,0 +1,39 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class EchomailExport extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('address_echomail', function (Blueprint $table) {
$table->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');
}
}

View File

@ -32,12 +32,12 @@
<div class="row pb-2">
<div class="col-8">
SEENBY: <br><strong class="highlight">{!! $msg->seenby->join('</strong>, <strong class="highlight">') !!}</strong>
SEENBY: <br><strong class="highlight">{!! $msg->seenby->pluck('ftn2d')->join('</strong>, <strong class="highlight">') !!}</strong>
</div>
</div>
<div class="row pb-2">
<div class="col-8">
PATH: <br><strong class="highlight">{!! $msg->path->join('</strong> -> <strong class="highlight">') !!}</strong>
PATH: <br><strong class="highlight">{!! $msg->path->pluck('ftn3d')->join('</strong> -> <strong class="highlight">') !!}</strong>
</div>
</div>