Skip to content

Commit

Permalink
feat: simplify tag configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Nov 24, 2023
1 parent 3ee9d85 commit cbe11bc
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 180 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [workflow_dispatch, push, pull_request]

jobs:
run:
uses: flarum/framework/.github/workflows/REUSABLE_backend.yml@main
uses: flarum/framework/.github/workflows/REUSABLE_backend.yml@1.x
with:
enable_backend_testing: true
enable_phpstan: true
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
{
"name": "IanM",
"homepage": "https://discuss.flarum.org/u/ianm",
"email": "ian@flarum.org",
"role": "Developer"
}
],
Expand Down
14 changes: 7 additions & 7 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
use Flarum\Api\Controller\UpdateDiscussionController;
use Flarum\Api\Serializer;
use Flarum\Discussion\Discussion;
use Flarum\Discussion\Event\Saving;
use Flarum\Discussion\Event\Saving as DiscussionSaving;
use Flarum\Discussion\Filter\DiscussionFilterer;
use Flarum\Discussion\Search\DiscussionSearcher;
use Flarum\Extend;
use Flarum\Post\Post;
use Flarum\Settings\Event\Saving as SettingsSaving;
use Flarum\Tags\Api\Serializer\TagSerializer;
use Flarum\Tags\Event\Creating as TagCreating;
use Flarum\Tags\Event\Saving as TagSaving;
Expand All @@ -40,9 +41,6 @@

new Extend\Locales(__DIR__.'/resources/locale'),

(new Extend\Routes('api'))
->post('/fof/best-answer/enable', 'fof-best-answer.enable-tags-features', Api\Controller\FeatureEnableController::class),

(new Extend\Model(Discussion::class))
->belongsTo('bestAnswerPost', Post::class, 'best_answer_post_id')
->belongsTo('bestAnswerUser', User::class, 'best_answer_user_id')
Expand All @@ -62,11 +60,12 @@
->cast('best_answer_count', 'int'),

(new Extend\Event())
->listen(Saving::class, Listeners\SaveBestAnswerToDatabase::class)
->listen(DiscussionSaving::class, Listeners\SaveBestAnswerToDatabase::class)
->listen(BestAnswerSet::class, Listeners\QueueNotificationJobs::class)
->listen(TagCreating::class, Listeners\TagCreating::class)
->listen(TagSaving::class, Listeners\TagEditing::class)
->subscribe(Listeners\RecalculateBestAnswerCounts::class),
->subscribe(Listeners\RecalculateBestAnswerCounts::class)
->listen(SettingsSaving::class, Listeners\SaveTagSettings::class),

(new Extend\Notification())
->type(Notification\SelectBestAnswerBlueprint::class, Serializer\BasicDiscussionSerializer::class, ['alert', 'email'])
Expand Down Expand Up @@ -96,7 +95,8 @@
->serializeToForum('fof-best-answer.show_max_lines', 'fof-best-answer.show_max_lines', 'intVal')
->default('fof-best-answer.schedule_on_one_server', false)
->default('fof-best-answer.stop_overnight', false)
->default('fof-best-answer.store_log_output', false),
->default('fof-best-answer.store_log_output', false)
->default('fof-best-answer.enabled-tags', '[]'),

(new Extend\ApiController(ShowDiscussionController::class))
->addInclude(['bestAnswerPost', 'bestAnswerUser'])
Expand Down
50 changes: 0 additions & 50 deletions js/src/admin/addQnAToTagsModal.js

This file was deleted.

49 changes: 30 additions & 19 deletions js/src/admin/components/BestAnswerSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,36 @@ export default class BestAnswerSettings extends ExtensionPage {
<div className="BestAnswerSettingsPage">
<div className="Form">
<div className="Introduction">
<p>
{app.translator.trans('fof-best-answer.admin.settings.introduction', {
a: <Link href={app.route('extension', { id: 'flarum-tags' })} />,
})}
</p>
<div className="ButtonGroup">
<Button className="Button" onclick={this.enableAllTags.bind(this)} loading={this.loading} disabled={this.loading} icon="fas fa-check">
{app.translator.trans('fof-best-answer.admin.settings.enable_all_tags_button')}
</Button>
<Button
className="Button"
onclick={this.enableAllReminders.bind(this)}
loading={this.loading}
disabled={this.loading}
icon="fas fa-stopwatch"
>
{app.translator.trans('fof-best-answer.admin.settings.enable_all_tags_reminder_button')}
</Button>
</div>
<h3>{app.translator.trans('fof-best-answer.admin.settings.label.tags')}</h3>
<p className="helpText">{app.translator.trans('fof-best-answer.admin.settings.tags_info')}</p>
{this.buildSettingComponent({
type: 'flarum-tags.select-tags',
setting: 'fof-best-answer.enabled-tags',
label: app.translator.trans('fof-best-answer.admin.settings.enabled_tags_label'),
help: app.translator.trans('fof-best-answer.admin.settings.enabled_tags_help'),
options: {
requireParentTag: false,
limits: {
max: {
secondary: 0,
},
},
},
})}
{this.buildSettingComponent({
type: 'flarum-tags.select-tags',
setting: 'fof-best-answer.remind-tags',
label: app.translator.trans('fof-best-answer.admin.settings.remind_tags_label'),
help: app.translator.trans('fof-best-answer.admin.settings.remind_tags_help'),
options: {
requireParentTag: false,
limits: {
max: {
secondary: 0,
},
},
},
})}
</div>
<hr />
<div className="GeneralPreferences">
Expand Down
3 changes: 3 additions & 0 deletions js/src/admin/extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import commonExtend from '../common/extend';

export default [...commonExtend];
4 changes: 2 additions & 2 deletions js/src/admin/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import app from 'flarum/admin/app';
import addQnAToTagsModal from './addQnAToTagsModal';
import BestAnswerSettings from './components/BestAnswerSettings';
import addBestAnswerCountSort from '../common/addBestAnswerCountSort';

export { default as extend } from './extend';

app.initializers.add(
'fof-best-answer',
() => {
Expand All @@ -26,7 +27,6 @@ app.initializers.add(
'reply'
);

addQnAToTagsModal();
addBestAnswerCountSort();
},
5
Expand Down
8 changes: 8 additions & 0 deletions js/src/common/extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Extend from 'flarum/common/extenders';
import Tag from 'flarum/tags/models/Tag';

export default [
new Extend.Model(Tag) //
.attribute<boolean>('isQnA')
.attribute<boolean>('reminders'),
];
17 changes: 17 additions & 0 deletions js/src/forum/extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Discussion from 'flarum/common/models/Discussion';
import commonExtend from '../common/extend';
import Extend from 'flarum/common/extenders';
import Post from 'flarum/common/models/Post';
import User from 'flarum/common/models/User';
import Model from 'flarum/common/Model';

export default [
...commonExtend,

new Extend.Model(Discussion) //
.hasOne<Post>('bestAnswerPost')
.hasOne<User>('bestAnswerUser')
.attribute<boolean>('hasBestAnswer')
.attribute<boolean>('canSelectBestAnswer')
.attribute('bestAnswerSetAt', Model.transformDate),
];
File renamed without changes.
21 changes: 5 additions & 16 deletions js/src/forum/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import app from 'flarum/forum/app';
import { extend } from 'flarum/common/extend';
import Discussion from 'flarum/common/models/Discussion';
import Tag from 'flarum/tags/models/Tag';
import Model from 'flarum/common/Model';
import IndexPage from 'flarum/forum/components/IndexPage';
import Dropdown from 'flarum/common/components/Dropdown';
import Button from 'flarum/common/components/Button';
Expand All @@ -15,23 +12,15 @@ import addBestAnswerView from './addBestAnswerView';
import addAnsweredBadge from './addAnsweredBadge';
import AwardedBestAnswerNotification from './components/AwardedBestAnswerNotification';
import BestAnswerInDiscussionNotification from './components/BestAnswerInDiscussionNotification';
import extendNotifications from './extend/extendNotifications';
import extendNotifications from './extenders/extendNotifications';
import addBestAnswerCountToUsers from './addBestAnswerCountToUsers';
import addBestAnswerCountSort from '../common/addBestAnswerCountSort';

export * from './components';

app.initializers.add('fof/best-answer', () => {
Discussion.prototype.bestAnswerPost = Model.hasOne('bestAnswerPost');
Discussion.prototype.bestAnswerUser = Model.hasOne('bestAnswerUser');
Discussion.prototype.hasBestAnswer = Model.attribute('hasBestAnswer');
Discussion.prototype.canSelectBestAnswer = Model.attribute('canSelectBestAnswer');
Discussion.prototype.bestAnswerSetAt = Model.attribute('bestAnswerSetAt', Model.transformDate);

if (app.initializers.has('flarum-tags')) {
Tag.prototype.isQnA = Model.attribute('isQnA');
}
export { default as extend } from './extend';

app.initializers.add('fof/best-answer', () => {
app.notificationComponents.selectBestAnswer = SelectBestAnswerNotification;
app.notificationComponents.awardedBestAnswer = AwardedBestAnswerNotification;
app.notificationComponents.bestAnswerInDiscussion = BestAnswerInDiscussionNotification;
Expand All @@ -55,7 +44,7 @@ app.initializers.add('fof/best-answer', () => {
canStartDiscussion ? 'fof-best-answer.forum.index.ask_question' : 'fof-best-answer.forum.index.cannot_ask_question'
);

items.replace('startDiscussion', cta);
items.setContent('startDiscussion', cta);
});

extend(IndexPage.prototype, 'viewItems', function (items) {
Expand Down Expand Up @@ -134,7 +123,7 @@ app.initializers.add('fof/best-answer', () => {

this.attrs.titlePlaceholder = app.translator.trans('fof-best-answer.forum.composer.titlePlaceholder');

items.replace(
items.setContent(
'discussionTitle',
<h3>
<input
Expand Down
60 changes: 60 additions & 0 deletions migrations/2023_11_23_000000_create_enabled_tags_setting_key.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of fof/best-answer.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Illuminate\Database\Schema\Builder;

$remindersKey = 'fof-best-answer.remind-tags';
$qnaKey = 'fof-best-answer.enabled-tags';

return [
'up' => function (Builder $schema) use ($remindersKey, $qnaKey) {
$reminderTagIds = $schema->getConnection()
->table('tags')
->where('qna_reminders', true)
->pluck('id')
->map(function ($id) {
return (string) $id; // Convert each ID to string
});

$qnaTagIds = $schema->getConnection()
->table('tags')
->where('is_qna', true)
->pluck('id')
->map(function ($id) {
return (string) $id; // Convert each ID to string
});

// Convert the arrays to JSON strings
$reminderTagIdsJson = json_encode($reminderTagIds->all());
$qnaTagIdsJson = json_encode($qnaTagIds->all());

$schema->getConnection()
->table('settings')
->insertOrIgnore([
'key' => $remindersKey,
'value' => $reminderTagIdsJson,
]);

$schema->getConnection()
->table('settings')
->insertOrIgnore([
'key' => $qnaKey,
'value' => $qnaTagIdsJson,
]);
},
'down' => function (Builder $schema) use ($remindersKey, $qnaKey) {
// Delete the settings keys
$schema->getConnection()
->table('settings')
->whereIn('key', [$remindersKey, $qnaKey])
->delete();
},
];
9 changes: 5 additions & 4 deletions resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ fof-best-answer:
best_answer_not_own_discussion: Select Best Answer (not own Discussion)
settings:
label:
tags: Best Answer Tags
general: General
reminders: Reminders
advanced: Advanced
reminders_notice: For reminders to function, you must have set up the Flarum scheduler correctly.
allow_select_own_post: Select own post
allow_select_own_post_help: Allow a user to select their own post as a best answer to a discussion
enable_all_tags_button: Enable all tags for Best Answers
enable_all_tags_reminder_button: Enable all tags for Best Answer reminders
show_max_lines_label: Max lines to show in post preview
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.
select_best_answer_reminder_days: Reminder frequency
Expand All @@ -29,8 +28,10 @@ fof-best-answer:
use_alt_ui: Alternative layout
use_alt_ui_help: Best answer controls in post footer
documentation: Documentation
introduction: |
Enable which tags are to support Best Answers in <code><a>flarum/tags</a></code>, and optionally which tag(s) should support reminders. When a tag is Best Answer enabled, assign permissions to each user group that may set answers (own discussion, any discussion). Permissions may be assigned globally (for any Best Answer anabled tag), or on a per tag basis.
enabled_tags_label: Enabled Tags
remind_tags_label: Reminder Tags
tags_info: |
When a tag is Best Answer enabled, assign permissions to each user group that may set answers (own discussion, any discussion). Permissions may be assigned globally (for any Best Answer anabled tag), or on a per tag basis.
edit_tag:
qna_label: Enable Best Answers to be set in this tag
reminders: Send reminders to set a Best Answer for discussions in this tag
Expand Down
Loading

0 comments on commit cbe11bc

Please sign in to comment.