framework/extensions/nicknames/extend.php
2020-11-30 20:48:10 -05:00

48 lines
1.3 KiB
PHP

<?php
/*
* This file is part of flarum/nickname.
*
* Copyright (c) 2020 Flarum.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace Flarum\Nicknames;
use Flarum\Api\Serializer\UserSerializer;
use Flarum\Event\ConfigureUserGambits;
use Flarum\Extend;
use Flarum\User\Event\Saving;
use Flarum\User\User;
use Flarum\User\UserValidator;
return [
(new Extend\Frontend('forum'))
->js(__DIR__.'/js/dist/forum.js'),
(new Extend\Frontend('admin'))
->js(__DIR__.'/js/dist/admin.js'),
(new Extend\User())
->displayNameDriver('nickname', NicknameDriver::class),
(new Extend\ModelUrl(User::class))
->addSlugDriver('idOnly', IdOnlyUserSlugDriver::class),
(new Extend\Event())
->listen(Saving::class, SaveNicknameToDatabase::class)
->listen(ConfigureUserGambits::class, SetUserNicknameGambit::class),
(new Extend\ApiSerializer(UserSerializer::class))
->attribute('canEditOwnNickname', function($serializer, $user) {
$actor = $serializer->getActor();
return $actor->id === $user->id && $serializer->getActor()->can('editOwnNickname', $user);
}),
(new Extend\Validator(UserValidator::class))
->configure(AddNicknameValidation::class),
];