Change Address::parent(),Address::children(), improved CI testing

This commit is contained in:
2023-12-13 08:41:15 +11:00
parent 541f612446
commit caa6e629f4
6 changed files with 302 additions and 213 deletions

View File

@@ -36,6 +36,7 @@ class Address extends Model
public const NODE_DOWN = 1<<7; // Down
public const NODE_POINT = 1<<8; // Point
public const NODE_UNKNOWN = 1<<15; // Unknown
public const NODE_ALL = 0xFFF; // Mask to catch all nodes
public static function boot()
{
@@ -148,105 +149,6 @@ class Address extends Model
/* RELATIONS */
/**
* Find children dependent on this record
*/
public function children()
{
// We have no session data for this address, by definition it has no children
if (! $this->session('sespass'))
return $this->hasMany(self::class,'id','void');
if (! $this->session('default')) {
switch ($this->role) {
case self::NODE_ZC:
$children = self::select('addresses.*')
->where('zone_id',$this->zone_id);
break;
case self::NODE_RC:
$children = self::select('addresses.*')
->where('zone_id',$this->zone_id)
->where('region_id',$this->region_id);
break;
case self::NODE_NC:
$children = self::select('addresses.*')
->where('zone_id',$this->zone_id)
->where('region_id',$this->region_id)
->where('host_id',$this->host_id);
break;
case self::NODE_HC:
// Identify our children.
$children = self::select('addresses.*')
->where('zone_id',$this->zone_id)
->where('region_id',$this->region_id)
->where('hub_id',$this->id);
break;
case self::NODE_ACTIVE:
case self::NODE_PVT:
case self::NODE_HOLD:
case self::NODE_DOWN:
case self::NODE_UNKNOWN:
// Identify our children.
$children = self::select('addresses.*')
->where('zone_id',$this->zone_id)
->where('region_id',$this->region_id)
->where('host_id',$this->host_id)
->where('node_id',$this->node_id)
->where('point_id','<>',0);
break;
case self::NODE_POINT:
// Points dont have children, but must return a relationship instance
return $this->hasOne(self::class,NULL,'void');
default:
throw new \Exception('Unknown role: '.serialize($this->role));
}
} else {
$children = self::select('addresses.*')
->where('zone_id',$this->zone_id);
}
// I cant have myself as a child, and have a high role than me
$children->where('id','<>',$this->id)
->where('role','>',$this->role);
// Remove any children that we have session details for (SAME AS HC)
$sessions = self::select('hubnodes.*')
->join('system_zone',['system_zone.system_id'=>'addresses.system_id','system_zone.zone_id'=>'addresses.zone_id'])
->join('addresses as hubnodes',['hubnodes.zone_id'=>'addresses.zone_id','hubnodes.id'=>'addresses.id'])
->where('addresses.zone_id',$this->zone_id)
->where('addresses.system_id','<>',$this->system_id)
->whereIN('addresses.system_id',$children->get()->pluck('system_id'));
// For each of the session, identify their children
$session_kids = collect();
foreach ($sessions->get() as $so)
$session_kids = $session_kids->merge(($x=$so->children) ? $x->pluck('id') : []);
// ZC's receive all mail, except for defined nodes, and defined hubs/hosts/rcs
return $this->hasMany(self::class,'zone_id','zone_id')
->whereIn('id',$children->get()->pluck('id')->toArray())
->whereNotIn('id',$sessions->get()->pluck('id')->toArray())
->whereNotIn('id',$session_kids->toArray())
->where('system_id','<>',$this->system_id)
->select('addresses.*')
->orderBy('region_id')
->orderBy('host_id')
->orderBy('node_id')
->orderBy('point_id')
->with(['zone.domain']);
}
public function dynamics()
{
return $this->hasMany(Dynamic::class);
@@ -291,7 +193,7 @@ class Address extends Model
}
/**
* Echoareas this address is subscribed to
* Fileareas this address is subscribed to
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
@@ -301,96 +203,6 @@ class Address extends Model
->withPivot(['subscribed']);
}
/**
* Who we send this systems mail to.
*
* @return Address|null
* @throws \Exception
*/
public function parent(): ?Address
{
// If we have session password, then we are the parent
if ($this->session('sespass'))
return $this;
// If it is our address
if (our_address()->contains($this))
return NULL;
switch ($this->role) {
// ZCs dont have parents, but we may have a default
case self::NODE_ZC:
if (($x=$this->zone->systems->where('pivot.default',TRUE))->count())
return $x->first()->match($this->zone,255)->first();
else
return NULL;
// RC
case self::NODE_RC:
$parent = self::where('zone_id',$this->zone_id)
->where('region_id',0)
->where('host_id',0)
->where('node_id',0)
->single();
break;
// Hosts
case self::NODE_NC:
// See if we have an RC
$parent = self::where('zone_id',$this->zone_id)
->where('region_id',$this->region_id)
->where('host_id',$this->region_id)
->where('node_id',0)
->single();
if (! $parent) {
// See if we have an ZC
$parent = self::where('zone_id',$this->zone_id)
->where('region_id',0)
->where('host_id',0)
->where('node_id',0)
->single();
}
break;
// Hubs
case self::NODE_HC:
// Normal Nodes
case self::NODE_ACTIVE:
case self::NODE_PVT:
case self::NODE_HOLD:
case self::NODE_DOWN:
case self::NODE_UNKNOWN:
// If we are a child of a hub, then check our hub
$parent = ($this->hub_id
? self::where('id',$this->hub_id)
: self::where('zone_id',$this->zone_id)
->where('region_id',$this->region_id)
->where('host_id',$this->host_id)
->where('role','<',self::NODE_HC))
->active()
->single();
break;
case self::NODE_POINT:
$parent = self::where('zone_id',$this->zone_id)
->where('region_id',$this->region_id)
->where('host_id',$this->host_id)
->where('node_id',$this->node_id)
->where('point_id',0)
->active()
->single();
break;
default:
throw new \Exception(sprintf('Unknown role: %s (%d)',serialize($this->role),$this->id));
}
return $parent?->parent();
}
public function system()
{
return $this->belongsTo(System::class);
@@ -466,6 +278,101 @@ class Address extends Model
/* METHODS */
/**
* Find children dependent on this record
*/
public function children(): Collection
{
// We have no session data for this address, by definition it has no children
if (! $this->session('sespass') && (! our_address()->pluck('id')->contains($this->id)))
return new Collection;
// If this system is not marked to default route for this address
if (! $this->session('default')) {
switch ($this->role) {
case self::NODE_ZC:
$children = self::select('addresses.*')
->where('zone_id',$this->zone_id);
break;
case self::NODE_RC:
$children = self::select('addresses.*')
->where('zone_id',$this->zone_id)
->where('region_id',$this->region_id);
break;
case self::NODE_NC:
$children = self::select('addresses.*')
->where('zone_id',$this->zone_id)
->where('region_id',$this->region_id)
->where('host_id',$this->host_id);
break;
case self::NODE_HC:
// Identify our children.
$children = self::select('addresses.*')
->where('zone_id',$this->zone_id)
->where('region_id',$this->region_id)
->where('hub_id',$this->id);
break;
case self::NODE_ACTIVE:
case self::NODE_PVT:
case self::NODE_HOLD:
case self::NODE_DOWN:
case self::NODE_UNKNOWN:
// Identify our children.
$children = self::select('addresses.*')
->where('zone_id',$this->zone_id)
->where('region_id',$this->region_id)
->where('host_id',$this->host_id)
->where('node_id',$this->node_id)
->where('point_id','<>',0);
break;
case self::NODE_POINT:
// Points dont have children, but must return a relationship instance
return new Collection;
default:
throw new \Exception('Unknown role: '.serialize($this->role));
}
// We route everything for this domain
} else {
$children = self::select('addresses.*')
->join('zones',['zones.id'=>'addresses.zone_id'])
->where('domain_id',$this->zone->domain_id);
}
// I cant have myself as a child, and have a high role than me
$children = $children->where('addresses.id','<>',$this->id)
->where('role','>',$this->role)
->FTNorder()
->active()
->get();
// If there are no children
if (! $children->count())
return new Collection;
// Exclude links and their children.
$exclude = collect();
foreach (our_nodes($this->zone->domain)->merge(our_address($this->zone->domain)) as $o) {
// If this address is in our list, remove it and it's children
if ($children->contains($o)) {
$exclude = $exclude->merge($o->children());
$exclude->push($o);
}
}
return $children->filter(function($item) use ($exclude) { return ! $exclude->pluck('id')->contains($item->id);});
}
/**
* Create an FTN address associated with a system
*
@@ -892,10 +799,11 @@ class Address extends Model
* Netmail waiting to be sent to this system
*
* @return Collection
* @throws \Exception
*/
public function netmailWaiting(): Collection
{
return Netmail::whereIn('tftn_id',(($x=$this->children) ? $x->pluck('id') : collect())->push($this->id))
return Netmail::whereIn('tftn_id',(($x=$this->children()) ? $x->pluck('id') : collect())->push($this->id))
->where(function($query) {
return $query->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_INTRANSIT))
->orWhereRaw(sprintf('(flags & %d) > 0',Message::FLAG_LOCAL));
@@ -920,6 +828,96 @@ class Address extends Model
->get();
}
/**
* Who we send this systems mail to.
*
* @return Address|null
* @throws \Exception
*/
public function parent(): ?Address
{
// If we have session password, then we are the parent
if ($this->session('sespass'))
return $this;
// If it is our address
if (our_address()->contains($this))
return NULL;
switch ($this->role) {
// ZCs dont have parents, but we may have a default
case self::NODE_ZC:
if (($x=$this->zone->systems->where('pivot.default',TRUE))->count())
return $x->first()->match($this->zone,255)->first();
else
return NULL;
// RC
case self::NODE_RC:
$parent = self::where('zone_id',$this->zone_id)
->where('region_id',0)
->where('host_id',0)
->where('node_id',0)
->single();
break;
// Hosts
case self::NODE_NC:
// See if we have an RC
$parent = self::where('zone_id',$this->zone_id)
->where('region_id',$this->region_id)
->where('host_id',$this->region_id)
->where('node_id',0)
->single();
if (! $parent) {
// See if we have an ZC
$parent = self::where('zone_id',$this->zone_id)
->where('region_id',0)
->where('host_id',0)
->where('node_id',0)
->single();
}
break;
// Hubs
case self::NODE_HC:
// Normal Nodes
case self::NODE_ACTIVE:
case self::NODE_PVT:
case self::NODE_HOLD:
case self::NODE_DOWN:
case self::NODE_UNKNOWN:
// If we are a child of a hub, then check our hub
$parent = ($this->hub_id
? self::where('id',$this->hub_id)
: self::where('zone_id',$this->zone_id)
->where('region_id',$this->region_id)
->where('host_id',$this->host_id)
->where('role','<',self::NODE_HC))
->active()
->single();
break;
case self::NODE_POINT:
$parent = self::where('zone_id',$this->zone_id)
->where('region_id',$this->region_id)
->where('host_id',$this->host_id)
->where('node_id',$this->node_id)
->where('point_id',0)
->active()
->single();
break;
default:
throw new \Exception(sprintf('Unknown role: %s (%d)',serialize($this->role),$this->id));
}
return $parent?->parent();
}
/**
*
* Parse a string and split it out as an FTN array