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