diff --git a/app/Http/Controllers/Api/Remote/ActivityProcessingController.php b/app/Http/Controllers/Api/Remote/ActivityProcessingController.php index 3b6badaa5..7806b2dbe 100644 --- a/app/Http/Controllers/Api/Remote/ActivityProcessingController.php +++ b/app/Http/Controllers/Api/Remote/ActivityProcessingController.php @@ -5,10 +5,8 @@ namespace App\Http\Controllers\Api\Remote; use Carbon\Carbon; use Illuminate\Support\Str; use App\Models\User; -use Webmozart\Assert\Assert; use App\Models\Server; use App\Models\ActivityLog; -use App\Models\ActivityLogSubject; use App\Http\Controllers\Controller; use App\Http\Requests\Api\Remote\ActivityEventRequest; @@ -71,19 +69,17 @@ class ActivityProcessingController extends Controller } foreach ($logs as $key => $data) { - Assert::isInstanceOf($server = $servers->get($key), Server::class); + $server = $servers->get($key); + assert($server instanceof Server); - $batch = []; foreach ($data as $datum) { - $id = ActivityLog::insertGetId($datum); - $batch[] = [ - 'activity_log_id' => $id, + /** @var ActivityLog $activityLog */ + $activityLog = ActivityLog::forceCreate($datum); + $activityLog->subjects()->create([ 'subject_id' => $server->id, 'subject_type' => $server->getMorphClass(), - ]; + ]); } - - ActivityLogSubject::insert($batch); } } } diff --git a/app/Models/ActivityLog.php b/app/Models/ActivityLog.php index cad17c3e6..c57c6d25b 100644 --- a/app/Models/ActivityLog.php +++ b/app/Models/ActivityLog.php @@ -120,11 +120,6 @@ class ActivityLog extends Model implements HasIcon, HasLabel return $builder->whereMorphedTo('actor', $actor); } - /** - * Returns models to be pruned. - * - * @see https://laravel.com/docs/9.x/eloquent#pruning-models - */ public function prunable(): Builder { if (is_null(config('activity.prune_days'))) { @@ -134,10 +129,6 @@ class ActivityLog extends Model implements HasIcon, HasLabel return static::where('timestamp', '<=', Carbon::now()->subDays(config('activity.prune_days'))); } - /** - * Boots the model event listeners. This will trigger an activity log event every - * time a new model is inserted which can then be captured and worked with as needed. - */ protected static function boot(): void { parent::boot(); diff --git a/app/Models/RecoveryToken.php b/app/Models/RecoveryToken.php index ba64b82f0..4b4598a93 100644 --- a/app/Models/RecoveryToken.php +++ b/app/Models/RecoveryToken.php @@ -19,7 +19,7 @@ class RecoveryToken extends Model implements Validatable use HasValidation; /** - * There are no updates to this model, only inserts and deletes. + * There are no updates to this model, only creates and deletes. */ public const UPDATED_AT = null; diff --git a/app/Services/Activity/ActivityLogService.php b/app/Services/Activity/ActivityLogService.php index 402f433c3..7cb7a214f 100644 --- a/app/Services/Activity/ActivityLogService.php +++ b/app/Services/Activity/ActivityLogService.php @@ -10,7 +10,6 @@ use Illuminate\Support\Collection; use App\Models\ActivityLog; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Request; -use App\Models\ActivityLogSubject; use App\Models\Server; use Filament\Facades\Filament; use Illuminate\Database\ConnectionInterface; @@ -236,16 +235,12 @@ class ActivityLogService $response = $this->connection->transaction(function () { $this->activity->save(); - $subjects = Collection::make($this->subjects) - ->map(fn (Model $subject) => [ - 'activity_log_id' => $this->activity->id, + foreach ($this->subjects as $subject) { + $this->activity->subjects()->forceCreate([ 'subject_id' => $subject->getKey(), 'subject_type' => $subject->getMorphClass(), - ]) - ->values() - ->toArray(); - - ActivityLogSubject::insert($subjects); + ]); + } return $this->activity; }); diff --git a/app/Services/Allocations/AssignmentService.php b/app/Services/Allocations/AssignmentService.php index 242a14872..7f52713e0 100644 --- a/app/Services/Allocations/AssignmentService.php +++ b/app/Services/Allocations/AssignmentService.php @@ -70,7 +70,7 @@ class AssignmentService throw new InvalidPortMappingException($port); } - $insertData = []; + $newAllocations = []; if (preg_match(self::PORT_RANGE_REGEX, $port, $matches)) { $block = range($matches[1], $matches[2]); @@ -83,7 +83,7 @@ class AssignmentService } foreach ($block as $unit) { - $insertData[] = [ + $newAllocations[] = [ 'node_id' => $node->id, 'ip' => $ip->__toString(), 'port' => (int) $unit, @@ -96,7 +96,7 @@ class AssignmentService throw new PortOutOfRangeException(); } - $insertData[] = [ + $newAllocations[] = [ 'node_id' => $node->id, 'ip' => $ip->__toString(), 'port' => (int) $port, @@ -105,8 +105,8 @@ class AssignmentService ]; } - foreach ($insertData as $insert) { - $allocation = Allocation::query()->create($insert); + foreach ($newAllocations as $newAllocation) { + $allocation = Allocation::query()->create($newAllocation); $ids[] = $allocation->id; } } diff --git a/app/Services/Servers/ServerCreationService.php b/app/Services/Servers/ServerCreationService.php index 26f38bbc6..542693cc8 100644 --- a/app/Services/Servers/ServerCreationService.php +++ b/app/Services/Servers/ServerCreationService.php @@ -3,7 +3,6 @@ namespace App\Services\Servers; use App\Enums\ServerState; -use App\Models\ServerVariable; use Illuminate\Http\Client\ConnectionException; use Ramsey\Uuid\Uuid; use Illuminate\Support\Arr; @@ -199,20 +198,11 @@ class ServerCreationService */ private function storeEggVariables(Server $server, Collection $variables): void { - $now = now(); - - $records = $variables->map(function ($result) use ($server, $now) { - return [ - 'server_id' => $server->id, - 'variable_id' => $result->id, - 'variable_value' => $result->value ?? '', - 'created_at' => $now, - 'updated_at' => $now, - ]; - })->toArray(); - - if (!empty($records)) { - ServerVariable::query()->insert($records); + foreach ($variables as $variable) { + $server->serverVariables()->forceCreate([ + 'variable_id' => $variable->id, + 'variable_value' => $variable->value ?? '', + ]); } } diff --git a/app/Services/Users/ToggleTwoFactorService.php b/app/Services/Users/ToggleTwoFactorService.php index 0edec54fd..434e5d3ef 100644 --- a/app/Services/Users/ToggleTwoFactorService.php +++ b/app/Services/Users/ToggleTwoFactorService.php @@ -2,9 +2,6 @@ namespace App\Services\Users; -use App\Models\RecoveryToken; -use Carbon\Carbon; -use Illuminate\Support\Str; use App\Models\User; use PragmaRX\Google2FA\Google2FA; use Illuminate\Database\ConnectionInterface; @@ -49,27 +46,14 @@ class ToggleTwoFactorService // on their account. $tokens = []; if ((!$toggleState && !$user->use_totp) || $toggleState) { - $inserts = []; + $user->recoveryTokens()->delete(); for ($i = 0; $i < 10; $i++) { - $token = Str::random(10); - - $inserts[] = [ - 'user_id' => $user->id, + $token = str_random(10); + $user->recoveryTokens()->forceCreate([ 'token' => password_hash($token, PASSWORD_DEFAULT), - // insert() won't actually set the time on the models, so make sure we do this - // manually here. - 'created_at' => Carbon::now(), - ]; - + ]); $tokens[] = $token; } - - // Before inserting any new records make sure all the old ones are deleted to avoid - // any issues or storing an unnecessary number of tokens in the database. - $user->recoveryTokens()->delete(); - - // Bulk insert the hashed tokens. - RecoveryToken::query()->insert($inserts); } $user->totp_authenticated_at = now();