-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
extend.php
85 lines (70 loc) · 3.75 KB
/
extend.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/*
* This file is part of fof/username-request.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FoF\UserRequest;
use Flarum\Api\Controller\ListUsersController;
use Flarum\Api\Controller\ShowForumController;
use Flarum\Api\Controller\ShowUserController;
use Flarum\Api\Serializer;
use Flarum\Extend;
use Flarum\User\User;
use FoF\UserRequest\Api\Controller;
use FoF\UserRequest\Api\Serializer\RequestSerializer;
return [
(new Extend\Frontend('forum'))
->js(__DIR__.'/js/dist/forum.js')
->route('/u/{username}/history', 'username.history.view')
->route('/username-requests', 'username.request.view'),
(new Extend\Frontend('admin'))
->js(__DIR__.'/js/dist/admin.js'),
(new Extend\Routes('api'))
->get('/username-requests', 'username.request.index', Controller\ListRequestsController::class)
->post('/username-requests', 'username.request.create', Controller\CreateRequestController::class)
->patch('/username-requests/{id}', 'username.request.act', Controller\ActOnRequestController::class)
->delete('/username-requests/{id}', 'username.request.delete', Controller\DeleteRequestController::class),
(new Extend\Model(User::class))
->cast('username_history', 'string')
->hasMany('nameChangeRequests', UsernameRequest::class, 'user_id')
->relationship('lastNicknameRequest', function ($user) {
return $user->hasOne(UsernameRequest::class, 'user_id', null)->where('for_nickname', true);
})
->relationship('lastUsernameRequest', function ($user) {
return $user->hasOne(UsernameRequest::class, 'user_id', null)->where('for_nickname', false);
}),
new Extend\Locales(__DIR__.'/resources/locale'),
(new Extend\ApiSerializer(Serializer\UserSerializer::class))
->attribute('usernameHistory', function (Serializer\UserSerializer $serializer, User $user) {
return $user->username_history ? json_decode($user->username_history) : null;
})
->hasOne('lastNicknameRequest', RequestSerializer::class)
->hasOne('lastUsernameRequest', RequestSerializer::class),
(new Extend\ApiSerializer(Serializer\ForumSerializer::class))
->attribute('canRequestUsername', function (Serializer\ForumSerializer $serializer) {
return $serializer->getActor()->hasPermission('user.requestUsername');
})
->attribute('canRequestNickname', function (Serializer\ForumSerializer $serializer) {
return $serializer->getActor()->hasPermission('user.requestNickname');
})
->attribute('passwordlessSignUp', function (Serializer\ForumSerializer $serializer) {
return !$serializer->getActor()->isGuest() && $serializer->getActor()->loginProviders()->count() > 0;
})
->hasMany('username_requests', RequestSerializer::class),
(new Extend\ApiController(ShowForumController::class))
->addInclude(['username_requests', 'username_requests.user'])
->prepareDataForSerialization(AddUsernameRequests::class),
(new Extend\ApiController(ListUsersController::class))
->addInclude(['lastNicknameRequest', 'lastUsernameRequest']),
(new Extend\ApiController(ShowUserController::class))
->addInclude(['lastNicknameRequest', 'lastUsernameRequest']),
(new Extend\Notification())
->type(Notification\RequestApprovedBlueprint::class, Api\Serializer\RequestSerializer::class, ['email'])
->type(Notification\RequestRejectedBlueprint::class, Api\Serializer\RequestSerializer::class, ['email']),
(new Extend\View())
->namespace('fof-username-request', __DIR__.'/resources/views'),
];