Skip to content

Commit 4706d70

Browse files
committed
feat: select own post permission
1 parent b4d6406 commit 4706d70

File tree

7 files changed

+37
-25
lines changed

7 files changed

+37
-25
lines changed

js/src/admin/components/BestAnswerSettingsPage.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ export default class BestAnswerSettingsPage extends ExtensionPage {
3131
</div>
3232
<h3>{app.translator.trans('fof-best-answer.admin.settings.label.general')}</h3>
3333
<div className="Section">
34-
{this.buildSettingComponent({
35-
type: 'boolean',
36-
setting: 'fof-best-answer.allow_select_own_post',
37-
label: app.translator.trans('fof-best-answer.admin.settings.allow_select_own_post'),
38-
help: app.translator.trans('fof-best-answer.admin.settings.allow_select_own_post_help'),
39-
})}
4034
{this.buildSettingComponent({
4135
type: 'boolean',
4236
setting: 'fof-best-answer.use_alternative_ui',

js/src/admin/extend.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import app from 'flarum/admin/app';
12
import Extend from 'flarum/common/extenders';
23
import BestAnswerSettingsPage from './components/BestAnswerSettingsPage';
34
import commonExtend from '../common/extend';
@@ -22,5 +23,13 @@ export default [
2223
permission: 'discussion.selectBestAnswerNotOwnDiscussion',
2324
}),
2425
'reply'
26+
)
27+
.permission(
28+
() => ({
29+
icon : 'fas fa-check',
30+
label: app.translator.trans('fof-best-answer.admin.permissions.allow_select_own_post'),
31+
permission: 'discussion.fof-best-answer.allow_select_own_post'
32+
}),
33+
'reply'
2534
),
2635
];

js/src/forum/addBestAnswerAction.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ export default function addBestAnswerAction() {
1313
return post.isHidden() || post.number() === 1 || !discussion.canSelectBestAnswer() || !app.session.user;
1414
};
1515

16-
const blockSelectOwnPost = (post: Post): boolean => {
17-
const user = post.user();
18-
return !app.forum.attribute<boolean>('canSelectBestAnswerOwnPost') && user !== false && user.id() === app.session.user?.id();
19-
};
20-
2116
const isThisBestAnswer = (discussion: Discussion, post: Post): boolean => {
2217
const bAPost = discussion.bestAnswerPost();
2318
const hasBestAnswer = discussion.hasBestAnswer();
@@ -72,7 +67,7 @@ export default function addBestAnswerAction() {
7267

7368
if (post.contentType() !== 'comment') return;
7469

75-
if (ineligible(discussion, post) || blockSelectOwnPost(post) || !app.current.matches(DiscussionPage)) return;
70+
if (ineligible(discussion, post) || !app.current.matches(DiscussionPage)) return;
7671

7772
items.add(
7873
'bestAnswer',
@@ -101,7 +96,7 @@ export default function addBestAnswerAction() {
10196

10297
post.pushAttributes({ isBestAnswer });
10398

104-
if (ineligible(discussion, post) || blockSelectOwnPost(post) || !app.current.matches(DiscussionPage)) return;
99+
if (ineligible(discussion, post) || !app.current.matches(DiscussionPage)) return;
105100

106101
items.add(
107102
'bestAnswer',

resources/locale/en.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ fof-best-answer:
33
permissions:
44
best_answer: Select Best Answer (own Discussion)
55
best_answer_not_own_discussion: Select Best Answer (not own Discussion)
6+
allow_select_own_post: Select own post as Best Answer
67
settings:
78
label:
89
tags: Best Answer Tags
910
general: General
1011
reminders: Reminders
1112
advanced: Advanced
1213
reminders_notice: For reminders to function, you must have set up the Flarum scheduler correctly.
13-
allow_select_own_post: Select own post
14-
allow_select_own_post_help: Allow a user to select their own post as a best answer to a discussion
1514
show_max_lines_label: Max lines to show in post preview
1615
show_max_lines_help: Set to 0 to disable. If a post is longer than the configured amount of lines, it will be truncated in the post preview with a fade out effect.
1716
select_best_answer_reminder_days: Reminder frequency

src/Api/ForumAttributes.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace FoF\BestAnswer\Api;
1313

14+
use Flarum\Api\Context;
1415
use Flarum\Api\Schema;
1516
use Flarum\Settings\SettingsRepositoryInterface;
1617

@@ -24,9 +25,6 @@ public function __construct(
2425
public function __invoke(): array
2526
{
2627
return [
27-
Schema\Boolean::make('canSelectBestAnswerOwnPost')
28-
->get(fn () => (bool) $this->settings->get('fof-best-answer.allow_select_own_post')),
29-
3028
Schema\Boolean::make('bestAnswerDiscussionSidebarJumpButton')
3129
->get(fn () => (bool) $this->settings->get('fof-best-answer.discussion_sidebar_jump_button')),
3230

src/Repository/BestAnswerRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function canSelectPostAsBestAnswer(User $user, Post $post): bool
5050
}
5151

5252
if ($user->id === $post->user_id) {
53-
return (bool) $this->settings->get('fof-best-answer.allow_select_own_post');
53+
return $user->can('discussion.fof-best-answer.allow_select_own_post', $post->discussion);
5454
}
5555

5656
return true;

tests/integration/api/SetBestAnswerTest.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function setUp(): void
5959
],
6060
'group_permission' => [
6161
['group_id' => 4, 'permission' => 'discussion.selectBestAnswerNotOwnDiscussion', 'created_at' => Carbon::now()],
62+
['group_id' => 4, 'permission' => 'discussion.fof-best-answer.allow_select_own_post', 'created_at' => Carbon::now()]
6263
],
6364
'group_user' => [
6465
['user_id' => 4, 'group_id' => 4],
@@ -166,20 +167,36 @@ public function user_without_permission_cannot_set_best_answer(int $userId)
166167
$this->assertEquals(403, $response->getStatusCode());
167168
}
168169

170+
public static function unauthorizedUsersOwnPostProvider(): array
171+
{
172+
return [
173+
[2],
174+
[3],
175+
];
176+
}
177+
169178
#[Test]
170-
public function user_cannot_set_own_post_as_best_answer_if_not_permitted()
179+
#[DataProvider('unauthorizedUsersOwnPostProvider')]
180+
public function user_cannot_set_own_post_as_best_answer_if_not_permitted(int $userId)
171181
{
172-
$response = $this->setBestAnswer(3, 5, 2);
182+
$response = $this->setBestAnswer($userId, 5, 2);
173183

174184
$this->assertEquals(403, $response->getStatusCode());
175185
}
176186

177-
#[Test]
178-
public function user_can_set_own_post_as_best_answer_if_permitted()
187+
public static function permittedUsersOwnPostProvider(): array
179188
{
180-
$this->setting('fof-best-answer.allow_select_own_post', true);
189+
return [
190+
[1],
191+
[4],
192+
];
193+
}
181194

182-
$response = $this->setBestAnswer(3, 5, 2);
195+
#[Test]
196+
#[DataProvider('permittedUsersOwnPostProvider')]
197+
public function user_can_set_own_post_as_best_answer_if_permitted(int $userId)
198+
{
199+
$response = $this->setBestAnswer($userId, 5, 2);
183200

184201
$this->assertEquals(200, $response->getStatusCode());
185202

0 commit comments

Comments
 (0)