clrghouz/app/Console/Commands/Areafix/Rescan.php

114 lines
3.5 KiB
PHP
Raw Normal View History

2023-12-01 07:14:51 +00:00
<?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 : FTN Address}'
.' {area : Echoarea Tag}'
.' {days? : Limit to messages authored days ago}'
.' {--R|export : Re-export previously sent messages }';
2023-12-01 07:14:51 +00:00
/**
* The console command description.
*
* @var string
*/
protected $description = 'Resend some echomail to a node';
/**
* Execute the console command.
*
* @return int
2023-12-01 07:14:51 +00:00
* @throws \Exception
*/
public function handle(): int
2023-12-01 07:14:51 +00:00
{
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->can_read($ao->security))
throw new \Exception(sprintf('FTN [%s] doesnt have permission to receive [%s]',$ao->ftn,$eao->name));
2023-12-01 07:14:51 +00:00
foreach (Echomail::select(['id','datetime'])
2023-12-01 07:14:51 +00:00
->where('echoarea_id',$eao->id)
->when(
$this->argument('days'),
fn($query)=>$query->where('datetime','>=',
Carbon::now()
->subDays($this->argument('days'))
->startOfDay())
)
2024-04-16 12:30:41 +00:00
->orderBy('datetime')
2023-12-01 07:14:51 +00:00
->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] MSG (%s) dated (%s) to [%s]',$eo->id,$eo->msgid ?: '*NO MSGID*',$eo->datetime->format('Y-m-d H:i:s'),$ao->ftn3d));
2023-12-01 07:14:51 +00:00
} else {
$export = $eo->seenby->where('id',$ao->id)->pop();
if ($export) {
// Echomail is pending export
if ($export->pivot->export_at && is_null($export->pivot->sent_at) && is_null($export->pivot->sent_pkt)) {
$this->warn(sprintf('Not exporting [%d] MSG (%s) dated (%s) already queued for [%s]',$eo->id,$eo->msgid ?: '*NO MSGID*',$eo->datetime->format('Y-m-d H:i:s'),$ao->ftn3d));
// Echomail has been exported
} elseif ($this->option('export')) {
$eo->seenby()->updateExistingPivot($ao,['export_at'=>Carbon::now(),'sent_at'=>NULL,'sent_pkt'=>NULL]);
2023-12-01 07:14:51 +00:00
$this->info(sprintf('Re-exported [%d] MSG (%s) dated (%s) to [%s]',$eo->id,$eo->msgid ?: '*NO MSGID*',$eo->datetime,$ao->ftn3d));
} else {
$this->info(sprintf('Not resending previously sent message [%d], MSGID (%s) - sent in Pkt [%s] on [%s]',
$eo->id,
$eo->msgid ?: '* NO MSGID*',
$export->pivot->sent_pkt ?: '-',
$export->pivot->sent_at ?: '-',
));
}
2023-12-01 07:14:51 +00:00
// Echomail has not been exported
} else {
$eo->seenby()->attach($ao,['export_at'=>Carbon::now(),'sent_at'=>NULL,'sent_pkt'=>NULL]);
2023-12-01 07:14:51 +00:00
$this->info(sprintf('Exported [%d] to [%s]',$eo->id,$ao->ftn3d));
}
}
}
return self::SUCCESS;
}
}