From 60ea0afc9840599de28242673557d61faf6e2efa Mon Sep 17 00:00:00 2001 From: Deon George Date: Tue, 5 Nov 2024 00:28:34 +1100 Subject: [PATCH] Minor optimisations when editing and viewing domains --- app/Jobs/AddressIdle.php | 2 +- app/Models/Domain.php | 78 +++++++++++++----------- app/Models/System.php | 12 +++- resources/views/domain/addedit.blade.php | 58 +++++++++--------- resources/views/domain/view.blade.php | 10 ++- resources/views/system/addedit.blade.php | 4 +- 6 files changed, 91 insertions(+), 73 deletions(-) diff --git a/app/Jobs/AddressIdle.php b/app/Jobs/AddressIdle.php index 8c9a80d..02c5d4d 100644 --- a/app/Jobs/AddressIdle.php +++ b/app/Jobs/AddressIdle.php @@ -186,7 +186,7 @@ class AddressIdle implements ShouldQueue } if ($result->count()) - Notification::route('echomail',$this->do->nodestatusarea)->notify(new AbsentNodes($result)); + Notification::route('echomail',$this->do->nodestatus_echoarea)->notify(new AbsentNodes($result)); } private function old(Domain $do,int $days,int $flags=0,Address $ao=NULL): Collection diff --git a/app/Models/Domain.php b/app/Models/Domain.php index e4c211a..91c92f8 100644 --- a/app/Models/Domain.php +++ b/app/Models/Domain.php @@ -11,9 +11,16 @@ use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; -use App\Casts\CompressedStringOrNull; +use App\Models\Casts\CompressedStringOrNull; use App\Traits\{QueryCacheableConfig,ScopeActive}; +/** + * This represents an FTN Domain. + * + * We are either a member of the domain (because we have an AKA in it) or NOT. + * + * The assumption is, if we are a member of the domain, we receive/send mail to an uplink and possibly downlinks + */ class Domain extends Model { use HasFactory,QueryCacheableConfig,ScopeActive; @@ -28,14 +35,16 @@ class Domain extends Model /* SCOPES */ /** - * Only query active records + * A domain is public (visible), if the user is an admin or, the domain is marked public) */ public function scopePublic($query) { $user = Auth::user(); return $query - ->when(((! $user) || (! $user->isAdmin())),function($query) { return $query->where('public',TRUE)->active(); }); + ->active() + ->when((! $user) || (! $user->isAdmin()), + fn($query)=>$query->where('public',TRUE)); } /* RELATIONS */ @@ -43,13 +52,13 @@ class Domain extends Model public function echoareas() { return $this->hasMany(Echoarea::class) - ->orderBY('name'); + ->orderBy('name'); } public function fileareas() { return $this->hasMany(Filearea::class) - ->orderBY('name'); + ->orderBy('name'); } public function nodelist_filearea() @@ -57,7 +66,7 @@ class Domain extends Model return $this->belongsTo(Filearea::class); } - public function nodestatusarea() + public function nodestatus_echoarea() { return $this->belongsTo(Echoarea::class,'nodestatus_id'); } @@ -65,29 +74,37 @@ class Domain extends Model public function zones() { return $this->hasMany(Zone::class) - ->select(['id','zone_id','domain_id','active']); + ->select(['id','zone_id','domain_id','active']) + ->orderBy('zone_id'); } /* ATTRIBUTES */ + /** + * We can accept applications if we have an address in the domain + * + * @return bool + * @throws \Exception + */ public function getCanAcceptAppAttribute(): bool { - return ($x=our_address($this)) - && $x->count() - && $this->active + return $this->isManaged() && $this->accept_app - && Auth::id() - && $this->userHasSystemsNotInNet(Auth::user())->count(); + && Auth::id(); } public function getHomePageAttribute(?string $value): string { - //0xFD2FB528 return $this->castAttribute('homepage',$value) ?: 'No available information at the moment.'; } /* METHODS */ + /** + * Show count of messages by day/week/month/all stats for each echoarea in this domain + * + * @return Collection + */ public function echoarea_stats(): Collection { return Cache::remember(md5(sprintf('%d-%s',$this->id,__METHOD__)),self::CACHE_TIME,function() { @@ -110,6 +127,12 @@ class Domain extends Model }); } + /** + * Show daily total of messages by echoarea in this domain + * + * @param Collection|NULL $systems + * @return Collection + */ public function echoarea_total_daily(Collection $systems=NULL): Collection { return Cache::remember(md5(sprintf('%d-%s',$this->id,$systems?->pluck('id')->join(','))),self::CACHE_TIME,function() use ($systems) { @@ -120,7 +143,7 @@ class Domain extends Model ->join('echomails',['echomails.echoarea_id'=>'echoareas.id']) ->where('domains.id',$this->id) ->where('echomails.datetime','>=',Carbon::now()->subMonths(self::STATS_MONTHS)->startOfMonth()) - ->when($systems?->count(),function($query) use ($systems) { return $query->whereIn('echomails.fftn_id',$systems->pluck('addresses')->flatten()->pluck('id')); }) + ->when($systems?->count(),fn($query)=>$query->whereIn('echomails.fftn_id',$systems->pluck('addresses')->flatten()->pluck('id'))) ->groupBy(['echoareas.name','echoareas.show','date']) ->orderBy('echoareas.name') ->orderBy('date') @@ -128,6 +151,9 @@ class Domain extends Model }); } + /** + * Show count of files by week/month/all status for each filearea in this domain + */ public function filearea_stats() { return Cache::remember(md5(sprintf('%d-%s',$this->id,__METHOD__)),self::CACHE_TIME,function() { @@ -157,28 +183,6 @@ class Domain extends Model */ public function isManaged(): bool { - return ($x=our_address()) - && $x->pluck('zone.domain') - ->pluck('id') - ->contains($this->id); - } - - /** - * Work out which of the users systems are not in this domain - * - * @param User $o - * @return Collection - */ - public function userHasSystemsNotInNet(User $o): Collection - { - $o->load('systems.akas.zone'); - - $result = collect(); - foreach ($o->systems->filter(function($item) { return $item->active; }) as $so) { - if (! $so->akas->pluck('zone')->unique('domain_id')->pluck('domain_id')->contains($this->id)) - $result->push($so); - } - - return $result; + return our_address($this)->count() > 0; } } \ No newline at end of file diff --git a/app/Models/System.php b/app/Models/System.php index bb03562..0ff1b92 100644 --- a/app/Models/System.php +++ b/app/Models/System.php @@ -115,9 +115,10 @@ class System extends Model public function sessions() { return $this->belongsToMany(Zone::class) - ->select(['id','zones.zone_id','domain_id','active']) + ->select(['zones.id','zones.zone_id','domain_id','zones.active']) + ->join('domains',['domains.id'=>'zones.domain_id']) ->withPivot(['sespass','pktpass','ticpass','fixpass','zt_ipv4','zt_ipv6','default']) - ->dontCache(); + ->orderBy('domains.name'); } /** @@ -301,6 +302,13 @@ class System extends Model } } + public function inDomain(Domain $o): bool + { + return $this->addresses + ->filter(fn($item)=>$item->zone->domain_id === $o->id) + ->count() > 0; + } + /** * Return the system's address in the same zone * This function can filter based on the address type needed. diff --git a/resources/views/domain/addedit.blade.php b/resources/views/domain/addedit.blade.php index a51b9ee..4870e53 100644 --- a/resources/views/domain/addedit.blade.php +++ b/resources/views/domain/addedit.blade.php @@ -1,14 +1,12 @@ +@use(App\Models\Echoarea) + @extends('layouts.app') @section('htmlheader_title') @if($o->exists) Update @else Add @endif Domain @endsection -@php -use App\Models\Echoarea; -@endphp - @section('content')
@csrf @@ -107,16 +105,18 @@ use App\Models\Echoarea;
- -
-
- accept_app))checked @endif> - + @if ($o->isManaged()) + +
+
+ accept_app))checked @endif> + - accept_app))checked @endif> - + accept_app))checked @endif> + +
-
+ @endif
@if ($o->nodelist_filename) @@ -136,22 +136,24 @@ use App\Models\Echoarea;
- -
- - - - @error('nodestatus_id') - {{ $message }} - @enderror - - Add a NEW Echoarea. This echoarea is used to send node status messages. -
+ @if ($o->isManaged()) + +
+ + + + @error('nodestatus_id') + {{ $message }} + @enderror + + Add a NEW Echoarea. This echoarea is used to send node status messages. +
+ @endif
diff --git a/resources/views/domain/view.blade.php b/resources/views/domain/view.blade.php index 5d31822..ddd35de 100644 --- a/resources/views/domain/view.blade.php +++ b/resources/views/domain/view.blade.php @@ -1,3 +1,4 @@ +@use(App\Models\Address) @use(Illuminate\Mail\Markdown) @@ -176,7 +177,7 @@ - @if ($o->can_accept_app) + @if ($o->can_accept_app && ($x=Auth::user()->systems->filter(fn($item)=>$item->address && (! $item->inDomain($o))))->count())

@@ -184,7 +185,10 @@
-

Your system(s) {!! $o->userHasSystemsNotInNet(Auth::user())->pluck('name')->join(', ') !!} can join this network.

+

Your system(s) {!! $x->pluck('name')->sort()->join(', ') !!} can join this network.

+ @if (($x=Auth::user()->systems->filter(fn($item)=>(! $item->address) && (! $item->inDomain($o))))->count()) +

Your other systems(s) {!! $x->pluck('name')->sort()->join(', ') !!} cant use this process, because they are missing network details.

+ @endif

If you want to join it/them to this network, make sure:

@@ -206,7 +210,7 @@
  • Enjoy!
  • - + <-- NOT READY YET

    diff --git a/resources/views/system/addedit.blade.php b/resources/views/system/addedit.blade.php index 1e61cf0..397f54e 100644 --- a/resources/views/system/addedit.blade.php +++ b/resources/views/system/addedit.blade.php @@ -1,8 +1,8 @@ - @use(App\Models\Address) @use(App\Models\Echomail) @use(App\Models\File) @use(App\Models\Netmail) + @extends('layouts.app') @@ -212,7 +212,7 @@ - @foreach ($o->sessions->sortBy('zone_id') as $oo) + @foreach ($o->sessions as $oo) {{ $oo->zone_id }}@{{ $oo->domain->name }}