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 8595fd9 commit de8f37f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/Config/web.jobnames.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@
\Seat\Eveapi\Jobs\Killmails\Character\Detail::class,
],
'mail' => [
\Seat\Eveapi\Jobs\Mail\Headers::class,
\Seat\Eveapi\Jobs\Mail\Bodies::class,
\Seat\Eveapi\Jobs\Mail\Labels::class,
\Seat\Eveapi\Jobs\Mail\Mails::class,
\Seat\Eveapi\Jobs\Mail\MailingLists::class,
],
'market' => \Seat\Eveapi\Jobs\Market\Character\Orders::class,
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/Character/MailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
use Seat\Services\Repositories\Character\Mail;
use Seat\Web\Http\Controllers\Controller;
use Seat\Web\Http\DataTables\Character\Intel\MailDataTable;
use Seat\Web\Http\DataTables\Scopes\CharacterScope;
use Seat\Web\Http\DataTables\Scopes\CharacterMailScope;
use Seat\Web\Models\User;

/**
Expand All @@ -49,7 +49,7 @@ public function index(int $character_id, MailDataTable $dataTable)
$characters = (User::find($character_id))->group->users;

return $dataTable
->addScope(new CharacterScope('character.mail', $character_id, request()->input('characters', [])))
->addScope(new CharacterMailScope($character_id, request()->input('characters', [])))
->render('web::character.mail', compact('characters'));

}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/DataTables/Character/Intel/MailDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function ajax()
return view('web::partials.date', ['datetime' => $row->timestamp]);
})
->editColumn('action', function ($row) {
return view('web::common.mails.buttons.read', ['character_id' => $row->character_id, 'mail_id' => $row->mail_id]);
return view('web::common.mails.buttons.read', ['character_id' => request()->character_id, 'mail_id' => $row->mail_id]);
})
->editColumn('sender.name', function ($row) {
switch ($row->sender->category) {
Expand Down
87 changes: 87 additions & 0 deletions src/Http/DataTables/Scopes/CharacterMailScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

/*
* This file is part of SeAT
*
* Copyright (C) 2015, 2016, 2017, 2018, 2019 Leon Jacobs
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

namespace Seat\Web\Http\DataTables\Scopes;

use Illuminate\Support\Arr;
use Yajra\DataTables\Contracts\DataTableScope;

/**
* Class CharacterMailScope.
*
* This is a specific scope for character mails table.
* It will restrict returned data based on both requested character ID and granted access.
*
* @package Seat\Web\Http\DataTables\Scopes
*/
class CharacterMailScope implements DataTableScope
{
/**
* @var array
*/
private $character_ids = [];

/**
* CharacterMailScope constructor.
*
* @param int $character_id
* @param array|null $character_ids
*/
public function __construct(int $character_id, ?array $character_ids)
{
if (is_null($character_ids))
$character_ids = [];

$this->character_ids = array_merge([$character_id], $character_ids);
}

/**
* Apply a query scope.
*
* @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query
* @return mixed
*/
public function apply($query)
{
return $query->whereHas('recipients', function ($sub_query) {

$character_ids = [];
$map = Arr::get(auth()->user()->getAffiliationMap(), 'char');

// in case user is super, apply filter over all requested characters
if (auth()->user()->hasSuperUser()) {
$character_ids = $this->character_ids;
}

// otherwise, determine to which character the user has access and include them in applied filters
// reject each other
if (empty($character_ids)) {
foreach ($this->character_ids as $character_id) {
if (in_array(Arr::has($map, (int) $character_id), ['character.*', 'character.mail']))
$character_ids[] = (int) $character_id;
}
}

return $sub_query->whereIn('recipient_id', $character_ids);
});
}
}
5 changes: 2 additions & 3 deletions src/resources/views/character/mail-timeline.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@

@foreach($message->recipients->where('recipient_type', 'alliance') as $recipient)

{!! img('alliance', $recipient->recipient_id, 64, ['class' => 'img-circle eve-icon small-icon'], false) !!}
<span class="id-to-name" data-id="{{ $recipient->recipient_id }}">{{ trans('web::seat.unknown') }}</span>
@include('web::partials.alliance', ['alliance' => $recipient->entity])

@endforeach

Expand All @@ -61,7 +60,7 @@

@foreach($message->recipients->where('recipient_type', 'corporation') as $recipient)

@include('web::partials.corporation', ['corporation' => $CorporationInfo::find($recipient->recipient_id) ?: $recipient->recipient_id, 'character_id' => $message->character_id])
@include('web::partials.corporation', ['corporation' => $recipient->entity])

@endforeach

Expand Down
3 changes: 1 addition & 2 deletions src/resources/views/search/components/mail.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
{data: 'body.body', name: 'body.body'},
{data: 'tocounts', name: 'tocounts', searchable: false, sortable: false},
{data: 'read', name: 'read', searchable: false, sortable: false},
{data: 'recipients', name: 'recipients', visible: false},
{data: 'character.name', name: 'character.name', visible: false}
{data: 'recipients', name: 'recipients', visible: false}
],
drawCallback: function () {
$('[data-toggle="tooltip"]').tooltip();
Expand Down

0 comments on commit de8f37f

Please sign in to comment.