Skip to content

Commit

Permalink
refactor(mails): reduce duplicated entries on mails module
Browse files Browse the repository at this point in the history
  • Loading branch information
warlof committed Nov 1, 2019
1 parent ed2c7cc commit 0f1ddab
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
20 changes: 11 additions & 9 deletions src/Repositories/Character/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace Seat\Services\Repositories\Character;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Seat\Eveapi\Models\Mail\MailHeader;

Expand Down Expand Up @@ -127,21 +128,22 @@ public function getCharacterMailTimeline(int $message_id = null)
// Get the User for permissions and affiliation
// checks
$user = auth()->user();
$messages = MailHeader::with('recipients', 'body');
$messages = MailHeader::with('body', 'recipients', 'recipients.entity', 'sender');

// If a user is not a super user, only return their own mail and those
// which they are affiliated to to receive.
if (! $user->hasSuperUser()) {

$messages = $messages->where(function ($query) use ($user) {
$messages = $messages->whereHas('recipients', function ($sub_query) {
// retrieve authenticated user permissions map
$character_map = collect(Arr::get(auth()->user()->getAffiliationMap(), 'char'));

// If the user has any affiliations and can
// list those characters, add them
if ($user->has('character.mail', false))
$query = $query->whereIn('character_id', array_keys($user->getAffiliationMap()['char']));
// collect only character which has either the requested permission or wildcard
$characters_ids = $character_map->filter(function ($permissions, $key) {
return in_array('character.*', $permissions) || in_array('character.mail', $permissions);
})->keys();

// Add mail owned by *this* character
$query->orWhere('character_id', $user->id);
$sub_query->whereIn('recipient_id', $characters_ids);
});
}

Expand All @@ -150,7 +152,7 @@ public function getCharacterMailTimeline(int $message_id = null)
return $messages->where('mail_id', $message_id)
->first();

return $messages->select('mail_id', 'subject', 'from', 'timestamp', 'labels', 'is_read')
return $messages->select('mail_id', 'subject', 'from', 'timestamp')
->orderBy('timestamp', 'desc')
->distinct()
->paginate(25);
Expand Down
33 changes: 11 additions & 22 deletions src/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace Seat\Services\Search;

use Illuminate\Support\Arr;
use Seat\Eveapi\Models\Assets\CharacterAsset;
use Seat\Eveapi\Models\Character\CharacterSkill;
use Seat\Eveapi\Models\Mail\MailHeader;
Expand Down Expand Up @@ -64,34 +65,22 @@ public function doSearchCharacterMail()
// checks
$user = auth()->user();

$messages = MailHeader::with('body', 'recipients', 'sender', 'character')
->select('timestamp', 'from', 'subject', 'mail_headers.mail_id', 'character_id');
$messages = MailHeader::with('body', 'recipients', 'recipients.entity', 'sender')
->select('timestamp', 'from', 'subject', 'mail_headers.mail_id');

// If the user is a super user, return all
if (! $user->hasSuperUser()) {

$messages = $messages->where(function ($query) use ($user) {
$messages = $messages->whereHas('recipients', function ($sub_query) {
// retrieve authenticated user permissions map
$character_map = collect(Arr::get(auth()->user()->getAffiliationMap(), 'char'));

// If the user has any affiliations and can
// list those characters, add them
// also include all attached characters
// collect only character which has either the requested permission or wildcard
$characters_ids = $character_map->filter(function ($permissions, $key) {
return in_array('character.*', $permissions) || in_array('character.mail', $permissions);
})->keys();

$map = $user->getAffiliationMap();
$character_maps = [];

foreach ($map['char'] as $character_id => $permissions) {
if (in_array('character.*', $permissions))
$character_maps[] = $character_id;
if (in_array('character.mail', $permissions))
$character_maps[] = $character_id;
}

$query = $query->orWhereIn('character_id', $character_maps)
->orWhereIn('from', $character_maps);

$query = $query->orWhereHas('recipients', function ($sub_query) use ($character_maps) {
$sub_query->whereIn('recipient_id', $character_maps);
});
$sub_query->whereIn('recipient_id', $characters_ids);
});
}

Expand Down

0 comments on commit 0f1ddab

Please sign in to comment.