Implemented CLI areafix:rescan

This commit is contained in:
Deon George 2023-12-01 18:14:51 +11:00
parent 049b2c7204
commit 8f3d77b04d
2 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,92 @@
<?php
namespace App\Console\Commands\Areafix;
use Carbon\Carbon;
use Illuminate\Console\Command;
use App\Models\{Address,Echoarea,Echomail};
class Rescan extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'areafix:rescan {ftn} {area} {days?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Resend some echomail to a node';
/**
* Execute the console command.
*
* @return mixed
* @throws \Exception
*/
public function handle()
{
if (($this->argument('days')) && (! is_numeric($this->argument('days'))))
throw new \Exception('Days must be numeric: '.$this->argument('days'));
$ao = Address::findFtn($this->argument('ftn'));
if (! $ao)
throw new \Exception('FTN not found: '.$this->argument('ftn'));
// Check that the area belongs to the domain for the FTN
if (! $this->argument('area'))
throw new \Exception('Areaname is required');
$eao = Echoarea::where('name',$this->argument('area'))->singleOrFail();
if ($eao->domain_id !== $ao->zone->domain_id)
throw new \Exception(sprintf('Echo area [%s] is not in domain [%s] for FTN [%s]',$eao->name,$ao->zone->domain->name,$ao->ftn));
// Check that the user is subscribed
if (! $ao->echoareas->contains($eao->id))
throw new \Exception(sprintf('FTN [%s] is not subscribed to [%s]',$ao->ftn,$eao->name));
// Check that an FTN can read the area
if (! $eao->sec_read || ($ao->security < $eao->sec_read))
throw new \Exception(sprintf('FTN [%s] doesnt have permission to received [%s]',$ao->ftn,$eao->name));
foreach (Echomail::select('id')
->where('echoarea_id',$eao->id)
->when($this->argument('days'),function($query) {
return $query->where('created_at','>=',Carbon::now()->subDays($this->argument('days'))->startOfDay());
})
->cursor() as $eo) {
// Echomail hasnt been exported before
if (! $eo->seenby->count()) {
$eo->seenby()->attach($ao->id,['export_at'=>Carbon::now()]);
$this->info(sprintf('Exported [%d] to [%s]',$eo->id,$ao->ftn3d));
} else {
$export = $eo->seenby->where('id',$ao->id)->pop();
// Echomail is pending export
if ($export && $export->pivot->export_at && is_null($export->pivot->sent_at) && is_null($export->pivot->sent_pkt)) {
$this->warn(sprintf('Not exporting [%d] already queued for [%s]',$eo->id,$ao->ftn3d));
// Echomail has been exported
} elseif ($export) {
$eo->seenby()->updateExistingPivot($ao,['export_at'=>Carbon::now(),'sent_at'=>NULL,'sent_pkt'=>NULL]);
$this->info(sprintf('Re-exported [%d] to [%s]',$eo->id,$ao->ftn3d));
// Echomail has not been exported
} else {
$eo->seenby()->attach($ao,['export_at'=>Carbon::now(),'sent_at'=>NULL,'sent_pkt'=>NULL]);
$this->info(sprintf('Exported [%d] to [%s]',$eo->id,$ao->ftn3d));
}
}
}
return self::SUCCESS;
}
}

View File

@ -104,6 +104,7 @@ final class Echomail extends Model implements Packet
$rogue = collect(); $rogue = collect();
// @todo move the parseAddress processing into Message::class, and our address to the seenby (and thus no need to add it when we export)
// Parse SEEN-BY // Parse SEEN-BY
if ($model->set_seenby->count()) if ($model->set_seenby->count())
$seenby = self::parseAddresses('seenby',$model->set_seenby,$model->fftn->zone,$rogue); $seenby = self::parseAddresses('seenby',$model->set_seenby,$model->fftn->zone,$rogue);
@ -183,6 +184,7 @@ final class Echomail extends Model implements Packet
public function seenby() public function seenby()
{ {
return $this->belongsToMany(Address::class,'echomail_seenby') return $this->belongsToMany(Address::class,'echomail_seenby')
->withPivot(['export_at','sent_at','sent_pkt'])
->FTN2DOrder(); ->FTN2DOrder();
} }