Add filters for avatar

This commit is contained in:
Pig Fang 2019-12-22 17:22:05 +08:00
parent 3e249a9902
commit 591b9969aa
3 changed files with 32 additions and 14 deletions

View File

@ -2,6 +2,7 @@
namespace App\Http\View\Composers;
use App\Services\Filter;
use Illuminate\Http\Request;
use Illuminate\View\View;
@ -10,20 +11,25 @@ class UserMenuComposer
/** @var Request */
protected $request;
public function __construct(Request $request)
/** @var Filter */
protected $filter;
public function __construct(Request $request, Filter $filter)
{
$this->request = $request;
$this->filter = $filter;
}
public function compose(View $view)
{
$user = auth()->user();
$email = base64_encode($user->email);
$avatar = $user->avatar;
$avatar = $this->filter->apply(
'user_avatar',
url('avatar/25/'.$email.'.png?tid='.$user->avatar),
[$user]
);
$view->with([
'user' => $user,
'avatar' => url('avatar/25/'.$email.'.png?tid='.$avatar),
]);
$view->with(compact('user', 'avatar'));
}
}

View File

@ -2,6 +2,7 @@
namespace App\Http\View\Composers;
use App\Services\Filter;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\View\View;
@ -10,15 +11,23 @@ class UserPanelComposer
/** @var Dispatcher */
protected $dispatcher;
public function __construct(Dispatcher $dispatcher)
/** @var Filter */
protected $filter;
public function __construct(Dispatcher $dispatcher, Filter $filter)
{
$this->dispatcher = $dispatcher;
$this->filter = $filter;
}
public function compose(View $view)
{
$user = auth()->user();
$avatar = url('avatar/45/'.base64_encode($user->email).'.png?tid='.$user->avatar);
$avatar = $this->filter->apply(
'user_avatar',
url('avatar/45/'.base64_encode($user->email).'.png?tid='.$user->avatar),
[$user]
);
$badges = [];
if (auth()->user()->isAdmin()) {
@ -26,10 +35,6 @@ class UserPanelComposer
}
$this->dispatcher->dispatch(new \App\Events\RenderingBadges($badges));
$view->with([
'user' => $user,
'avatar' => $avatar,
'badges' => $badges,
]);
$view->with(compact('user', 'avatar', 'badges'));
}
}

View File

@ -40,7 +40,8 @@ class UserControllerTest extends TestCase
$filter = Fakes\Filter::fake();
$user = factory(User::class)->create();
factory(\App\Models\Player::class)->create(['uid' => $user->uid]);
$uid = $user->uid;
factory(\App\Models\Player::class)->create(['uid' => $uid]);
$this->actingAs($user)
->get('/user')
@ -48,6 +49,12 @@ class UserControllerTest extends TestCase
->assertSee((new Parsedown())->text(option_localized('announcement')))
->assertSee((string) $user->score);
$filter->assertApplied('grid:user.index');
$filter->assertApplied('user_avatar', function ($url, $user) use ($uid) {
$this->assertTrue(Str::endsWith($url, 'tid=0'));
$this->assertEquals($uid, $user->uid);
return true;
});
$unverified = factory(User::class)->create(['verified' => false]);
$this->actingAs($unverified)