Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
austinkregel committed May 20, 2024
1 parent eea3cbb commit 7e42c48
Show file tree
Hide file tree
Showing 42 changed files with 1,609 additions and 154 deletions.
60 changes: 60 additions & 0 deletions app/Actions/Spork/ApplyTagToModelAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace App\Actions\Spork;

use App\Actions\Spork\CustomAction;
use App\Contracts\ActionInterface;
use App\Models\Credential;
use App\Models\Domain;
use App\Models\ExternalRssFeed;
use App\Models\Finance\Account;
use App\Models\Finance\Transaction;
use App\Models\Page;
use App\Models\Person;
use App\Models\Research;
use App\Models\Tag;
use App\Services\Factories\DomainServiceFactory;
use App\Services\Factories\RegistrarServiceFactory;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Http\Request;

class ApplyTagToModelAction extends CustomAction implements ActionInterface
{
public function __construct(
$name = 'Apply Tag',
$slug = 'apply-tag'
) {
parent::__construct($name, $slug, models: [
Domain::class,
Credential::class,
ExternalRssFeed::class,
Transaction::class,
Research::class,
Person::class,
Account::class,
Page::class,
]);
}

public function __invoke(Dispatcher $dispatcher, Request $request)
{
$request->validate([
'items' => 'required|array',
'tag' => 'required|string',
]);
}

public function fields(): array
{
return [
'tag' => [
'type' => 'select',
'label' => 'Tag',
'required' => true,
'options' => Tag::all(),
],
];
}
}
3 changes: 2 additions & 1 deletion app/Actions/Spork/SyncDataFromCredential.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

namespace App\Actions\Spork;

use App\Contracts\ActionInterface;
use App\Jobs\FetchResourcesFromCredential;
use App\Models\Credential;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Http\Request;

class SyncDataFromCredential extends CustomAction
class SyncDataFromCredential extends CustomAction implements ActionInterface
{
public function __construct($name = 'Sync Data From Credential', $slug = 'sync-data-from-credential')
{
Expand Down
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Jobs\FetchResourcesFromCredentials;
use App\Jobs\News\UpdateAllFeeds;
use App\Jobs\Notifications\BuildSummaryNotificationJob;
use App\Jobs\SyncJiraTicketsJob;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
Expand All @@ -20,6 +21,7 @@ protected function schedule(Schedule $schedule): void
$schedule->job(SyncJiraTicketsJob::class)->daily();
$schedule->job(UpdateAllFeeds::class)->everyFifteenMinutes();
$schedule->job(FetchResourcesFromCredentials::class)->hourly();
$schedule->job(BuildSummaryNotificationJob::class)->dailyAt('13:00');
$schedule->command('operations:queue')->everyFiveMinutes();
}

Expand Down
15 changes: 10 additions & 5 deletions app/Contracts/Services/ServerServiceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@ public function findAllServers(): array;

public function removeServerKey($identifier): void;

public function deleteServer(int $identifier): void;
public function deleteServer(int|string $identifier): void;

public function powerOnServer(int $identifier): void;
public function powerOnServer(int|string $identifier): void;

public function powerOffServer(int $identifier): void;
public function powerOffServer(int|string $identifier): void;

public function shutdownServer(int $identifier): void;
public function shutdownServer(int|string $identifier): void;

public function rebootServer(int $identifier): void;
public function rebootServer(int|string $identifier): void;

public function findAllSshkeys(): array;

// public function changeMemory(int|string $identifier, int $memory): void;
// public function changeCpu(int|string $identifier, int $vcpu, int $cores, int $threads): void;
// public function changeDisk(int|string $identifier, int $disk_capacity): void;

}
1 change: 1 addition & 0 deletions app/Http/Controllers/Spork/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function __invoke()

$query->whereHas('tags', fn ($q) => $q->where('name->en', 'news'));
})
->distinct(['headline'])
->orderByDesc('last_modified')
->paginate(request('news_limit', 15), ['*'], 'news_page', request('news_page', 1))),

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Spork/PagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public function __invoke()

public function create()
{
return Inertia::render('Pages/Create', []);
return Inertia::render('Pages/BlueprintEditor', []);
}
}
21 changes: 7 additions & 14 deletions app/Http/Controllers/Spork/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace App\Http\Controllers\Spork;

use App\Services\Code;
use Illuminate\Notifications\Notification;
use Inertia\Inertia;

class SettingsController
Expand All @@ -17,20 +19,11 @@ public function __invoke()
'settings' => new class()
{
},
'files' => collect((new \Illuminate\Filesystem\Filesystem())->directories(app_path()))
->map(fn ($directory) => [
'name' => basename($directory),
'file_path' => base64_encode($directory),
'is_directory' => true,
])
->concat(
collect((new \Illuminate\Filesystem\Filesystem())->files(app_path()))
->map(fn (\SplFileInfo $file) => [
'name' => $file->getFilename(),
'file_path' => base64_encode($file->getPathname()),
'is_directory' => false,
])
),
'notifications' =>
array_map(
fn ($class) => $class,
Code::instancesOf(Notification::class)->getClasses(),
)

]);
}
Expand Down
12 changes: 6 additions & 6 deletions app/Jobs/CloudflareSyncAndPurgeJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Domain;
use App\Services\Factories\DomainServiceFactory;
use App\Services\Factories\RegistrarServiceFactory;
use Illuminate\Database\Eloquent\Model;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class CloudflareSyncAndPurgeJob extends AbstractSyncDomainResource
Expand Down Expand Up @@ -35,10 +36,10 @@ public function sync(): void
{
$page = 1;
$registrarService = (new RegistrarServiceFactory)->make($this->credential);

$dispatcher = Model::getEventDispatcher();
Model::unsetEventDispatcher();
do {
$domains = $this->service->getDomains(100, $page++);

foreach ($domains as $domain) {
// In order for domain jobs to be able to run, we need the domain to exist from a registrar.
$localDomain = Domain::where('name', $domain['domain'])->first();
Expand Down Expand Up @@ -68,21 +69,20 @@ public function sync(): void
continue;
}
$dnsResults = $this->service->getDns($localDomain->cloudflare_id);

foreach ($dnsResults as $dnsRecord) {
$localDomain->records()->firstOrCreate([
'record_id' => $dnsRecord['id'],
'type' => $dnsRecord['type'],
'name' => $dnsRecord['name'],
], [
'record_id' => $dnsRecord['id'],
'ttl' => $dnsRecord['ttl'],
'value' => $dnsRecord['content'],
], [
'priority' => $dnsRecord['priority'],
'proxied_through_cloudflare' => $dnsRecord['proxied_through_cloudflare'],
]);
}

}
} while ($domains->hasMorePages());
Model::setEventDispatcher($dispatcher);
}
}
76 changes: 76 additions & 0 deletions app/Jobs/Notifications/BuildSummaryNotificationJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace App\Jobs\Notifications;

use App\Contracts\Services\News\NewsServiceContract;
use App\Contracts\Services\WeatherServiceContract;
use App\Models\Article;
use App\Models\Finance\Transaction;
use App\Models\Person;
use App\Models\User;
use App\Notifications\Daily\SummaryNotification;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class BuildSummaryNotificationJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function handle(
WeatherServiceContract $weatherService,
): void
{
$now = Carbon::now();
$nowLocal = Carbon::now('America/Detroit');
$page = 1;
$headlines = Article::query()
->limit(10)
->distinct('headline')
->where('last_modified', '>=', $nowLocal->copy()->startOfDay())
->inRandomOrder()
->get();

do {
$userPaginator = User::query()
->paginate(
1,
['*'],
'page',
$page++
);

/** @var User $user */
foreach ($userPaginator->items() as $user) {
/** @var Person $person */
$person = $user->person();
if (empty($person)) {
continue;
}

$weather = null;

if (!empty($person->primary_address)) {
$weatherResponse = $weatherService->query($person->primary_address);
$weather = collect($weatherResponse)->first();
}

$user->notify(new SummaryNotification(
$weather,
$headlines->toArray(),
Transaction::query()
->whereIn('account_id', $user->accounts()->pluck('account_id'))
->where('date', '<=', $nowLocal->copy()->addDay()->endOfDay())
->where('date', '>=', $nowLocal->copy()->subDays(7)->startOfDay())
->where('name', 'not like', '%Transfer%')
->orderBy('date', 'desc')
->get()
->groupBy('date'),
$user->accounts
));
}
} while ($userPaginator->hasMorePages());
}
}
3 changes: 3 additions & 0 deletions app/Models/DomainRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class DomainRecord extends Model implements Crud, ModelQuery
'tags',
'value',
'timeout',
'record_id',
'proxied_through_cloudflare',
'priority',
];

public $dispatchesEvents = [
Expand Down
5 changes: 5 additions & 0 deletions app/Models/ExternalRssFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Models;

use App\Actions\Spork\Domains\ApplyTagToModelAction;
use App\Events\Models\ExternalRssFeed\ExternalRssFeedCreated;
use App\Events\Models\ExternalRssFeed\ExternalRssFeedCreating;
use App\Events\Models\ExternalRssFeed\ExternalRssFeedDeleted;
Expand All @@ -22,6 +23,10 @@ class ExternalRssFeed extends Model implements Crud, Taggable
use HasTags;
use LogsActivity;

public $actions = [
ApplyTagToModelAction::class
];

public $fillable = [
'uuid',
'url',
Expand Down
20 changes: 20 additions & 0 deletions app/Notifications/AbstractNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use NotificationChannels\Webhook\WebhookChannel;

abstract class AbstractNotification extends Notification
{
use Queueable;

public static function getNotificationOptions(): array
{
return [];
}
}
54 changes: 54 additions & 0 deletions app/Notifications/Daily/SummaryNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Notifications\Daily;

use App\Forecast;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class SummaryNotification extends Notification
{
use Queueable;

public function __construct(
// weather forecast near a user
// article headlines
// Transactions from a period,
// domain expirations
public ?Forecast $weather,
public ?array $articles,
public Collection $transactions,
public Collection $accounts,
)
{
//
}

public function via(object $notifiable): array
{
return ['mail'];
}

public function toMail(object $notifiable): MailMessage
{
$message = (new MailMessage);
$message->view(
'emails.daily.summary',
[
'weather' => $this->weather,
'articles' => $this->articles,
'transactions' => $this->transactions,
'accounts' => $this->accounts,
]
);
return $message;
}

public function toArray(object $notifiable): array
{
return [];
}
}
Loading

0 comments on commit 7e42c48

Please sign in to comment.