-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathextend.php
More file actions
executable file
·243 lines (215 loc) · 12.6 KB
/
Copy pathextend.php
File metadata and controls
executable file
·243 lines (215 loc) · 12.6 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/*
* This file is part of fof/polls.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FoF\Polls;
use Flarum\Api\Context;
use Flarum\Api\Resource;
use Flarum\Api\Schema;
use Flarum\Discussion\Discussion;
use Flarum\Extend;
use Flarum\Post\Event\Saving as PostSaving;
use Flarum\Post\Post;
use Flarum\Settings\Event\Saved as SettingsSaved;
use FoF\Polls\Api\Controllers;
return [
(new Extend\Frontend('forum'))
->js(__DIR__.'/js/dist/forum.js')
->css(__DIR__.'/resources/less/forum.less')
->route('/polls', 'fof.polls.showcase')
->route('/polls/all', 'fof.polls.list', Content\PollsDirectory::class)
->route('/polls/view/{id}', 'fof.poll.view')
->route('/polls/composer', 'fof.polls.composer')
->jsDirectory(__DIR__.'/js/dist/forum'),
(new Extend\Frontend('admin'))
->js(__DIR__.'/js/dist/admin.js')
->css(__DIR__.'/resources/less/admin.less'),
new Extend\Locales(__DIR__.'/resources/locale'),
// Image upload/delete routes (non-JSON:API endpoints)
(new Extend\Routes('api'))
->post('/polls/pollImage', 'fof.polls.upload-image', Controllers\UploadPollImageController::class)
->delete('/polls/pollImage/name/{fileName}', 'fof.polls.delete-image-name', Controllers\DeletePollImageByNameController::class)
->post('/polls/pollImage/{pollId:\d+}', 'fof.polls.upload-image-poll', Controllers\UploadPollImageController::class)
->delete('/polls/pollImage/{pollId:\d+}', 'fof.polls.delete-image-poll', Controllers\DeletePollImageController::class)
->post('/polls/pollOptionImage', 'fof.polls.upload-option-image-option', Controllers\UploadPollOptionImageController::class)
->delete('/polls/pollOptionImage/name/{fileName}', 'fof.polls.delete-option-image-name', Controllers\DeletePollImageByNameController::class)
->post('/polls/pollOptionImage/{optionId:\d+}', 'fof.polls.upload-option-image', Controllers\UploadPollOptionImageController::class)
->delete('/polls/pollOptionImage/{optionId:\d+}', 'fof.polls.delete-option-image', Controllers\DeletePollOptionImageController::class),
(new Extend\Event())
->listen(SettingsSaved::class, Listeners\ClearFormatterCache::class),
// Add poll-related attributes to Forum resource (always loaded)
(new Extend\ApiResource(Resource\ForumResource::class))
->fields(function () {
return [
Schema\Boolean::make('canStartGlobalPolls')
->get(fn ($forum, Context $context) => $context->getActor()->can('startGlobalPoll')),
Schema\Boolean::make('canUploadPollImages')
->get(fn ($forum, Context $context) => $context->getActor()->can('uploadPollImages')),
Schema\Boolean::make('canStartPollGroup')
->get(function ($forum, Context $context) {
$settings = resolve(\Flarum\Settings\SettingsRepositoryInterface::class);
return (bool) $settings->get('fof-polls.enablePollGroups', false) && $context->getActor()->can('startPollGroup');
}),
Schema\Boolean::make('canViewPollGroups')
->get(function ($forum, Context $context) {
$settings = resolve(\Flarum\Settings\SettingsRepositoryInterface::class);
return (bool) $settings->get('fof-polls.enablePollGroups', false) && $context->getActor()->can('viewPollGroups');
}),
];
}),
(new Extend\Console())
->command(Console\RefreshVoteCountCommand::class)
->command(Console\ConvertPollImagesCommand::class)
->command(Console\PublishScheduledPollsCommand::class)
->schedule(Console\PublishScheduledPollsCommand::class, Console\PublishScheduledPollsSchedule::class),
(new Extend\Policy())
->modelPolicy(Poll::class, Access\PollPolicy::class),
(new Extend\Settings())
->default('fof-polls.maxOptions', 10)
->default('fof-polls.optionsColorBlend', true)
->default('fof-polls.directory-default-sort', '-createdAt')
->default('fof-polls.enableDiscussionPolls', true)
->default('fof-polls.enableGlobalPolls', false)
->default('fof-polls.image_height', 250)
->default('fof-polls.image_width', 250)
->default('fof-polls.maxImageUploadSize', 10240)
->serializeToForum('discussionPollsEnabled', 'fof-polls.enableDiscussionPolls', 'boolval')
->serializeToForum('pollsDirectoryDefaultSort', 'fof-polls.directory-default-sort', 'strval')
->serializeToForum('globalPollsEnabled', 'fof-polls.enableGlobalPolls', 'boolval')
->serializeToForum('pollGroupsEnabled', 'fof-polls.enablePollGroups', 'boolval')
->serializeToForum('pollMaxOptions', 'fof-polls.maxOptions', 'intval')
->registerLessConfigVar('fof-polls-options-color-blend', 'fof-polls.optionsColorBlend', function ($value) {
return $value ? 'true' : 'false';
}),
(new Extend\ModelVisibility(Poll::class))
->scope(Access\ScopePollVisibility::class),
(new Extend\View())
->namespace('fof-polls', __DIR__.'/resources/views'),
(new Extend\Filesystem())
->disk('fof-polls', PollImageDisk::class),
// Resources (always registered — endpoints within handle their own access control)
new Extend\ApiResource(Api\Resource\PollResource::class),
new Extend\ApiResource(Api\Resource\PollOptionResource::class),
new Extend\ApiResource(Api\Resource\PollVoteResource::class),
new Extend\ApiResource(Api\Resource\PollGroupResource::class),
// Search drivers
(new Extend\SearchDriver(\Flarum\Search\Database\DatabaseSearchDriver::class))
->addSearcher(Poll::class, Filter\GlobalPollSearcher::class)
->addFilter(Filter\GlobalPollSearcher::class, Filter\PollIsEndedFilter::class)
->addFilter(Filter\GlobalPollSearcher::class, Filter\PollIsDraftFilter::class)
->addSearcher(PollGroup::class, Filter\PollGroupSearcher::class)
->addFilter(Filter\PollGroupSearcher::class, Filter\PollGroupHasPollsFilter::class),
// Discussion-based polls (conditionally loaded)
(new Extend\Conditional())
->whenSetting('fof-polls.enableDiscussionPolls', true, function () {
return [
(new Extend\Model(Post::class))
->hasMany('polls', Poll::class, 'post_id', 'id'),
(new Extend\Model(Discussion::class))
->hasMany('polls', Poll::class, 'post_id', 'first_post_id'),
(new Extend\Event())
->listen(PostSaving::class, Listeners\SavePollsToDatabase::class),
(new Extend\ApiResource(Resource\DiscussionResource::class))
->fields(fn () => [
Schema\Boolean::make('hasPoll')
->get(function (Discussion $discussion) {
return $discussion->relationLoaded('polls')
? $discussion->polls->isNotEmpty() // @phpstan-ignore property.notFound
: $discussion->polls()->exists(); // @phpstan-ignore method.notFound
}),
Schema\Boolean::make('canStartPoll')
->get(fn (Discussion $discussion, Context $context) => $context->getActor()->can('polls.start', $discussion)),
Schema\Arr::make('poll')
->writableOnCreate()
->visible(false)
->set(function (Discussion $discussion, ?array $value) {
Listeners\SavePollsToDatabase::$pendingPollData = $value;
}),
])
->endpoint('index', function ($endpoint) {
return $endpoint
// Batch-load the first post's polls for every discussion in the
// page in a single query. Without this, serializing the
// `firstPost.polls` include resolves the relation one post at a
// time, producing an N+1 on the discussion list (issue #124).
// Poll visibility is still enforced: the discussion list only
// contains discussions (and therefore first posts) the actor can
// already see, and discussion-scoped polls inherit that post's
// visibility (see Access\ScopePollVisibility).
// Powers the `hasPoll` attribute without a per-discussion
// `exists()` query. Narrow on purpose: nothing on
// the list serializes these rows.
->eagerLoadWhere('polls', function ($query) {
$query->select(['id', 'post_id']);
})
// No default include: the list UI reads only
// hasPoll, and including firstPost.polls forced
// every first post to be fully serialized and ran
// every poll's policy attributes — one lazy post
// fetch per poll. Explicit includers get complete,
// batch-loaded polls; the poll policies read
// poll.post.discussion, so load that chain with
// them.
->eagerLoadWhenIncluded([
'firstPost' => ['firstPost.discussion', 'firstPost.polls.post.discussion', 'firstPost.polls.myVotes'],
]);
})
->endpoint('show', function ($endpoint) {
return $endpoint->addDefaultInclude([
'firstPost.polls', 'firstPost.polls.options', 'firstPost.polls.myVotes', 'firstPost.polls.myVotes.option',
]);
}),
(new Extend\ApiResource(Resource\PostResource::class))
->fields(fn () => [
Schema\Boolean::make('canStartPoll')
->get(fn (Post $post, Context $context) => $context->getActor()->can('startPoll', $post)),
Schema\Arr::make('poll')
->writableOnCreate()
->visible(false)
->set(fn () => null),
Schema\Relationship\ToMany::make('polls')
->includable()
->type('polls'),
])
->endpoint('create', function ($endpoint) {
return $endpoint->addDefaultInclude([
'polls', 'polls.options', 'polls.myVotes', 'polls.myVotes.option',
]);
})
->endpoint('index', function ($endpoint) {
return $endpoint->addDefaultInclude([
'polls', 'polls.options', 'polls.myVotes', 'polls.myVotes.option',
]);
})
->endpoint('show', function ($endpoint) {
return $endpoint->addDefaultInclude([
'polls', 'polls.options', 'polls.myVotes', 'polls.myVotes.option',
]);
}),
(new Extend\ApiResource(Resource\ForumResource::class))
->fields(fn () => [
Schema\Boolean::make('canStartPolls')
->get(fn ($forum, Context $context) => $context->getActor()->can('discussion.polls.start')),
]),
(new Extend\Policy())
->modelPolicy(Post::class, Access\PostPolicy::class),
];
}),
// Poll groups (conditionally loaded)
(new Extend\Conditional())
->when(new Extender\IsPollGroupEnabled(), function () {
return [
(new Extend\Policy())
->modelPolicy(PollGroup::class, Access\PollGroupPolicy::class),
(new Extend\Frontend('forum'))
->route('/polls/groups/composer', 'fof.polls.groups.composer')
->route('/polls/groups', 'fof.polls.groups.list')
->route('/polls/groups/{id}', 'fof.polls.groups.view'),
];
}),
];