Skip to content

Commit

Permalink
perf: reduce number of queries (monicahq/chandler#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Sep 14, 2022
1 parent 1fa8db7 commit ef2496c
Show file tree
Hide file tree
Showing 34 changed files with 210 additions and 178 deletions.
2 changes: 1 addition & 1 deletion app/Actions/Jetstream/DeleteUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function delete($user)
{
$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::user()->id,
'author_id' => Auth::id(),
'user_id' => $user->id,
];

Expand Down
26 changes: 26 additions & 0 deletions app/Helpers/ContactImportantDateHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Helpers;

use App\Models\ContactImportantDateType;
use App\Models\Vault;
use Illuminate\Support\Facades\Cache;

class ContactImportantDateHelper
{
/**
* Get the important date type from the vault.
*
* @return null|ContactImportantDateType
*/
public static function getImportantDateType(string $type, int $vault_id): ?ContactImportantDateType
{
return Cache::store('array')->remember("ImportantDateType:{$vault_id}:{$type}", 5,
fn () => ContactImportantDateType::where([
'vault_id' => $vault_id,
'internal_type' => $type,
])
->first()
);
}
}
4 changes: 3 additions & 1 deletion app/Helpers/UserHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\User;
use App\Models\Vault;
use Illuminate\Support\Facades\Cache;

class UserHelper
{
Expand All @@ -15,7 +16,8 @@ class UserHelper
*/
public static function getInformationAboutContact(User $user, Vault $vault): ?array
{
$contact = $user->getContactInVault($vault);
$contact = Cache::store('array')->remember("InformationAboutContact:{$user->id}:{$vault->id}", 5, fn () => $user->getContactInVault($vault)
);

if (! $contact) {
return null;
Expand Down
13 changes: 10 additions & 3 deletions app/Helpers/VaultHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\User;
use App\Models\Vault;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class VaultHelper
Expand Down Expand Up @@ -46,9 +47,15 @@ public static function getPermissionFriendlyName(int $permission): string
*/
public static function getPermission(User $user, Vault $vault): ?int
{
$permission = DB::table('user_vault')->where('vault_id', $vault->id)
->where('user_id', $user->id)
->select('permission')->first();
$permission = Cache::store('array')->remember("Permission:{$user->id}:{$vault->id}", 5,
fn () => DB::table('user_vault')
->where([
'vault_id' => $vault->id,
'user_id' => $user->id,
])
->select('permission')
->first()
);

if (! $permission) {
return null;
Expand Down
8 changes: 3 additions & 5 deletions app/Http/Middleware/CheckAdministratorPrivilege.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ class CheckAdministratorPrivilege
*/
public function handle(Request $request, Closure $next)
{
if (Auth::user()->is_account_administrator) {
return $next($request);
} else {
abort(401);
}
abort_if(! Auth::user()->is_account_administrator, 401);

return $next($request);
}
}
15 changes: 7 additions & 8 deletions app/Http/Middleware/CheckContactAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ public function handle(Request $request, Closure $next)
$requestedVaultId = $request->route()->parameter('vault');
$requestedContactId = $request->route()->parameter('contact');

$exists = DB::table('contacts')->where('vault_id', $requestedVaultId)
->where('id', $requestedContactId)
->count() > 0;
$exists = DB::table('contacts')->where([
'vault_id' => $requestedVaultId,
'id' => $requestedContactId,
])->exists();

if ($exists) {
return $next($request);
} else {
abort(401);
}
abort_if(! $exists, 401);

return $next($request);
}
}
15 changes: 7 additions & 8 deletions app/Http/Middleware/CheckVaultAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ public function handle(Request $request, Closure $next)
{
$requestedVaultId = $request->route()->parameter('vault');

$exists = DB::table('user_vault')->where('user_id', Auth::user()->id)
->where('vault_id', $requestedVaultId)
->count() > 0;
$exists = DB::table('user_vault')->where([
'user_id' => Auth::id(),
'vault_id' => $requestedVaultId,
])->exists();

if ($exists) {
return $next($request);
} else {
abort(401);
}
abort_if(! $exists, 401);

return $next($request);
}
}
17 changes: 8 additions & 9 deletions app/Http/Middleware/CheckVaultPermissionAtLeastEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ public function handle(Request $request, Closure $next)
{
$requestedVaultId = $request->route()->parameter('vault');

$exists = DB::table('user_vault')->where('vault_id', $requestedVaultId)
->where('user_id', Auth::user()->id)
->where('permission', '<=', 200)
->count() > 0;
$exists = DB::table('user_vault')->where([
['vault_id', '=', $requestedVaultId],
['user_id', '=', Auth::id()],
['permission', '<=', 200],
])->exists();

if ($exists) {
return $next($request);
} else {
abort(401);
}
abort_if(! $exists, 401);

return $next($request);
}
}
17 changes: 8 additions & 9 deletions app/Http/Middleware/CheckVaultPermissionAtLeastManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ public function handle(Request $request, Closure $next)
{
$requestedVaultId = $request->route()->parameter('vault');

$exists = DB::table('user_vault')->where('vault_id', $requestedVaultId)
->where('user_id', Auth::user()->id)
->where('permission', '<=', 100)
->count() > 0;
$exists = DB::table('user_vault')->where([
['vault_id', '=', $requestedVaultId],
['user_id', '=', Auth::id()],
['permission', '<=', 100],
])->exists();

if ($exists) {
return $next($request);
} else {
abort(401);
}
abort_if(! $exists, 401);

return $next($request);
}
}
13 changes: 1 addition & 12 deletions app/Http/Middleware/TrustProxies.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TrustProxies extends Middleware
*
* @var array<string>|string|null
*/
protected $proxies = '*';
protected $proxies;

/**
* The headers that should be used to detect proxies.
Expand All @@ -25,15 +25,4 @@ class TrustProxies extends Middleware
Request::HEADER_X_FORWARDED_PORT |
Request::HEADER_X_FORWARDED_PROTO |
Request::HEADER_X_FORWARDED_AWS_ELB;

/**
* Get the trusted proxies.
*
* @return array|string|null
* @codeCoverageIgnore
*/
protected function proxies()
{
return config('app.trust_proxies');
}
}
9 changes: 4 additions & 5 deletions app/Models/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use App\Helpers\AvatarHelper;
use App\Helpers\ContactImportantDateHelper;
use App\Helpers\ImportantDateHelper;
use App\Helpers\NameHelper;
use App\Helpers\ScoutHelper;
Expand Down Expand Up @@ -213,7 +214,7 @@ public function notes(): HasMany
*
* @return HasMany
*/
public function dates(): HasMany
public function importantDates(): HasMany
{
return $this->hasMany(ContactImportantDate::class);
}
Expand Down Expand Up @@ -362,15 +363,13 @@ protected function age(): Attribute
{
return Attribute::make(
get: function ($value) {
$type = ContactImportantDateType::where('vault_id', $this->vault_id)
->where('internal_type', ContactImportantDate::TYPE_BIRTHDATE)
->first();
$type = ContactImportantDateHelper::getImportantDateType(ContactImportantDate::TYPE_BIRTHDATE, $this->vault_id);

if (! $type) {
return null;
}

$birthdate = $this->dates()
$birthdate = $this->importantDates
->where('contact_important_date_type_id', $type->id)
->first();

Expand Down
12 changes: 0 additions & 12 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,6 @@

'cipher' => 'AES-256-CBC',

/*
|--------------------------------------------------------------------------
| Trust proxies
|--------------------------------------------------------------------------
|
| List of trusted proxies.
| Example: set it to '*' to allow any proxy.
|
*/

'trust_proxies' => env('APP_TRUSTED_PROXIES'),

/*
|--------------------------------------------------------------------------
| Autoloaded Service Providers
Expand Down
21 changes: 21 additions & 0 deletions config/trustedproxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Application trusted proxies
|--------------------------------------------------------------------------
|
| Set trusted proxy IP addresses.
| Both IPv4 and IPv6 addresses are supported, along with CIDR notation.
|
| The "*" character is syntactic sugar within TrustedProxy to trust any
| proxy that connects directly to your server, a requirement when you
| cannot know the address of your proxy (e.g. if using ELB or similar).
|
*/

'proxies' => env('APP_TRUSTED_PROXIES', null),

];
19 changes: 9 additions & 10 deletions domains/Contact/ManageContact/Services/ToggleFavoriteContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,26 @@ private function validate(): void

private function toggle(): void
{
$contact = [
'contact_id' => $this->data['contact_id'],
'vault_id' => $this->data['vault_id'],
'user_id' => $this->data['author_id'],
];

$exists = DB::table('contact_vault_user')
->where('contact_id', $this->data['contact_id'])
->where('vault_id', $this->data['vault_id'])
->where('user_id', $this->data['author_id'])
->where($contact)
->first();

if ($exists) {
$this->isFavorite = $exists->is_favorite;

DB::table('contact_vault_user')
->where('contact_id', $this->data['contact_id'])
->where('vault_id', $this->data['vault_id'])
->where('user_id', $this->data['author_id'])
->where($contact)
->update(['is_favorite' => ! $this->isFavorite]);
} else {
$this->isFavorite = true;

DB::table('contact_vault_user')->insert([
'contact_id' => $this->data['contact_id'],
'vault_id' => $this->data['vault_id'],
'user_id' => $this->data['author_id'],
DB::table('contact_vault_user')->insert($contact + [
'is_favorite' => true,
'number_of_views' => 1,
]);
Expand Down
21 changes: 10 additions & 11 deletions domains/Contact/ManageContact/Services/UpdateContactView.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,22 @@ public function execute(array $data): void

private function updateView(): void
{
$contact = [
'contact_id' => $this->data['contact_id'],
'vault_id' => $this->data['vault_id'],
'user_id' => $this->data['author_id'],
];

$exists = DB::table('contact_vault_user')
->where('contact_id', $this->data['contact_id'])
->where('vault_id', $this->data['vault_id'])
->where('user_id', $this->data['author_id'])
->first();
->where($contact)
->exists();

if ($exists) {
DB::table('contact_vault_user')
->where('contact_id', $this->data['contact_id'])
->where('vault_id', $this->data['vault_id'])
->where('user_id', $this->data['author_id'])
->where($contact)
->increment('number_of_views');
} else {
DB::table('contact_vault_user')->insert([
'contact_id' => $this->data['contact_id'],
'vault_id' => $this->data['vault_id'],
'user_id' => $this->data['author_id'],
DB::table('contact_vault_user')->insert($contact + [
'number_of_views' => 1,
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ public function store(Request $request, int $vaultId)
public function show(Request $request, int $vaultId, int $contactId)
{
$vault = Vault::findOrFail($vaultId);
$contact = Contact::with('gender')
->with('pronoun')
->with('notes')
->with('dates')
->with('vault')
$contact = Contact::with([
'gender',
'pronoun',
'notes',
'importantDates',
'vault',
])
->findOrFail($contactId);

if (! $contact->template_id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ class ContactPageController extends Controller
public function show(Request $request, int $vaultId, int $contactId, string $slug)
{
$vault = Vault::findOrFail($vaultId);
$contact = Contact::with('gender')
->with('pronoun')
->with('notes')
->with('dates')
->with('vault')
$contact = Contact::with([
'gender',
'pronoun',
'notes',
'importantDates',
'vault',
])
->findOrFail($contactId);

if (! $contact->template_id) {
Expand Down
Loading

0 comments on commit ef2496c

Please sign in to comment.