Skip to content

Commit

Permalink
allow extending solution search query, limit. setting to display disc… (
Browse files Browse the repository at this point in the history
#99)

* allow extending solution search query, limit. setting to display discussion tag in search results

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
imorland and StyleCIBot authored Sep 26, 2024
1 parent b24223b commit 082b595
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 8 deletions.
11 changes: 5 additions & 6 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,12 @@
->default('fof-best-answer.store_log_output', false)
->default('fof-best-answer.enabled-tags', '[]')
->default('fof-best-answer.search.solution_search', true)
->default('fof-best-answer.search.display_tags', true)
->default('fof-best-answer.discussion_sidebar_jump_button', false)
->serializeToForum('canSelectBestAnswerOwnPost', 'fof-best-answer.allow_select_own_post', 'boolVal')
->serializeToForum('useAlternativeBestAnswerUi', 'fof-best-answer.use_alternative_ui', 'boolVal')
->serializeToForum('showBestAnswerFilterUi', 'fof-best-answer.show_filter_dropdown', 'boolVal')
->serializeToForum('fof-best-answer.show_max_lines', 'fof-best-answer.show_max_lines', 'intVal')
->serializeToForum('solutionSearchEnabled', 'fof-best-answer.search.solution_search', 'boolVal')
->serializeToForum('bestAnswerDiscussionSidebarJumpButton', 'fof-best-answer.discussion_sidebar_jump_button', 'boolVal'),
->serializeToForum('fof-best-answer.show_max_lines', 'fof-best-answer.show_max_lines', 'intVal'),

(new Extend\ApiSerializer(Serializer\ForumSerializer::class))
->attributes(ForumAttributes::class),

(new Extend\ApiController(ShowDiscussionController::class))
->addInclude(['bestAnswerPost', 'bestAnswerUser'])
Expand Down
6 changes: 6 additions & 0 deletions js/src/admin/components/BestAnswerSettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ export default class BestAnswerSettingsPage extends ExtensionPage {
label: app.translator.trans('fof-best-answer.admin.settings.solution_search'),
help: app.translator.trans('fof-best-answer.admin.settings.solution_search_help'),
})}
{this.buildSettingComponent({
type: 'boolean',
setting: 'fof-best-answer.search.display_tags',
label: app.translator.trans('fof-best-answer.admin.settings.display_tags'),
help: app.translator.trans('fof-best-answer.admin.settings.display_tags_help'),
})}
</div>
<h3>{app.translator.trans('fof-best-answer.admin.settings.label.reminders')}</h3>
<p className="helpText">
Expand Down
3 changes: 2 additions & 1 deletion js/src/forum/components/SolutionSearchItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export default class SolutionSearchItem extends Component<SolutionSearchItemAttr
viewItems(): ItemList<Mithril.Children> {
const items = new ItemList<Mithril.Children>();

items.add('tags', <div className="SolutionSearchResult-tags">{tagsLabel(this.tags)}</div>, 100);
app.forum.attribute<boolean>('showTagsInSearchResults') &&
items.add('tags', <div className="SolutionSearchResult-tags">{tagsLabel(this.tags)}</div>, 100);

items.add('discussion-title', <div className="DiscussionSearchResult-title">{highlight(this.discussionTitle(), this.query)}</div>, 90);

Expand Down
8 changes: 7 additions & 1 deletion js/src/forum/components/SolutionSearchSource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ export default class SolutionSearchSource implements SearchSource {

this.results.set(query, []);

const queryString = query + ' ' + this.queryMutators().join(' ');

const params = {
filter: { q: query + ' is:solved' },
filter: { q: queryString },
page: { limit: this.limit() },
include: this.includes().join(','),
};
Expand Down Expand Up @@ -60,4 +62,8 @@ export default class SolutionSearchSource implements SearchSource {
limit(): number {
return 3;
}

queryMutators(): string[] {
return ['is:solved'];
}
}
48 changes: 48 additions & 0 deletions src/ForumAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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.
*/

namespace FoF\BestAnswer;

use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Settings\SettingsRepositoryInterface;

class ForumAttributes
{
/**
* @var SettingsRepositoryInterface
*/
protected $settings;

public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}

public function __invoke(ForumSerializer $serializer, $model, array $attributes): array
{
if ($value = $this->getBooleanSetting('fof-best-answer.search.solution_search')) {
$attributes['showTagsInSearchResults'] = $this->getBooleanSetting('fof-best-answer.search.display_tags');
}

$attributes['solutionSearchEnabled'] = $value;
$attributes['canSelectBestAnswerOwnPost'] = $this->getBooleanSetting('fof-best-answer.allow_select_own_post');
$attributes['useAlternativeBestAnswerUi'] = $this->getBooleanSetting('fof-best-answer.use_alternative_ui');
$attributes['showBestAnswerFilterUi'] = $this->getBooleanSetting('fof-best-answer.show_filter_dropdown');
$attributes['bestAnswerDiscussionSidebarJumpButton'] = $this->getBooleanSetting('fof-best-answer.discussion_sidebar_jump_button');

return $attributes;
}

protected function getBooleanSetting(string $key): bool
{
return (bool) $this->settings->get($key);
}
}

0 comments on commit 082b595

Please sign in to comment.