Skip to content

Commit

Permalink
tests passing locally now
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Oct 29, 2023
1 parent bd4427d commit 388fdc2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
11 changes: 1 addition & 10 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,7 @@
(new Extend\ApiSerializer(Serializer\BasicDiscussionSerializer::class))
->hasOne('bestAnswerPost', Serializer\BasicPostSerializer::class)
->hasOne('bestAnswerUser', Serializer\BasicUserSerializer::class)
->attribute('hasBestAnswer', function (Serializer\BasicDiscussionSerializer $serializer, Discussion $discussion) {
return $discussion->bestAnswerPost ? $discussion->bestAnswerPost->id : false;
})
->attribute('bestAnswerSetAt', function (Serializer\BasicDiscussionSerializer $serializer, Discussion $discussion) {
if ($discussion->best_answer_set_at) {
return Carbon::createFromTimeString($discussion->best_answer_set_at)->format(DateTime::RFC3339);
}

return null;
}),
->attributes(BasicDiscussionAttributes::class),

(new Extend\ApiSerializer(Serializer\UserSerializer::class))
->attributes(UserBestAnswerCount::class),
Expand Down
17 changes: 17 additions & 0 deletions src/BasicDiscussionAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace FoF\BestAnswer;

use Flarum\Api\Serializer\BasicDiscussionSerializer;
use Flarum\Discussion\Discussion;

class BasicDiscussionAttributes
{
public function __invoke(BasicDiscussionSerializer $serializer, Discussion $discussion, array $attributes): array
{
$attributes['hasBestAnswer'] = $discussion->bestAnswerPost !== null ? $discussion->bestAnswerPost->id : false;
$attributes['bestAnswerSetAt'] = $discussion->best_answer_set_at ? $discussion->best_answer_set_at->toRFC3339String() : null;

return $attributes;
}
}
1 change: 1 addition & 0 deletions src/BestAnswerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function tagEnabledForBestAnswer(Discussion $discussion): bool
{
$enabled = false;

/** @phpstan-ignore-next-line */
$discussionTags = $discussion->tags;
foreach ($discussionTags as $discussionTag) {
if ((bool) $discussionTag->is_qna) {
Expand Down
16 changes: 13 additions & 3 deletions src/Listeners/SaveBestAnswerToDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Flarum\Notification\Notification;
use Flarum\Notification\NotificationSyncer;
use Flarum\Post\Post;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\Tags\Tag;
use Flarum\User\Exception\PermissionDeniedException;
use Flarum\User\User;
Expand Down Expand Up @@ -48,17 +49,23 @@ class SaveBestAnswerToDatabase
*/
private $translator;

/**
* @var SettingsRepositoryInterface
*/
private $settings;

/**
* @var BestAnswerRepository
*/
protected $bestAnswer;

public function __construct(NotificationSyncer $notifications, Dispatcher $bus, TranslatorInterface $translator, BestAnswerRepository $bestAnswer)
public function __construct(NotificationSyncer $notifications, Dispatcher $bus, TranslatorInterface $translator, BestAnswerRepository $bestAnswer, SettingsRepositoryInterface $settings)
{
$this->notifications = $notifications;
$this->bus = $bus;
$this->translator = $translator;
$this->bestAnswer = $bestAnswer;
$this->settings = $settings;
}

public function handle(Saving $event)
Expand Down Expand Up @@ -86,6 +93,7 @@ public function handle(Saving $event)

private function removeBestAnswer(Discussion $discussion, User $actor): void
{
/** @var Post|null $post */
$post = $discussion->bestAnswerPost;

if (!$post) {
Expand All @@ -106,7 +114,7 @@ private function removeBestAnswer(Discussion $discussion, User $actor): void

private function setBestAnswer(Discussion $discussion, User $actor, int $id): void
{
/** @var Post $post */
/** @var Post|null $post */
$post = $discussion->posts()->find($id);

if ($id && !$post) {
Expand Down Expand Up @@ -139,7 +147,7 @@ private function setBestAnswer(Discussion $discussion, User $actor, int $id): vo

protected function changeTags(Discussion $discussion, string $method)
{
$tagsToChange = @json_decode(resolve('flarum.settings')->get('fof-best-answer.select_best_answer_tags'));
$tagsToChange = @json_decode($this->settings->get('fof-best-answer.select_best_answer_tags'));

if (empty($tagsToChange)) {
return;
Expand All @@ -149,6 +157,7 @@ protected function changeTags(Discussion $discussion, string $method)

// Query errors if we try to attach tags that are already attached due to the unique constraint
if ($method === 'attach') {
/** @phpstan-ignore-next-line */
$existingTags = $discussion->tags()->pluck('id');
$validTags = $validTags->whereNotIn('id', $existingTags);
}
Expand All @@ -159,6 +168,7 @@ protected function changeTags(Discussion $discussion, string $method)
return;
}

/** @phpstan-ignore-next-line */
$discussion->tags()->$method($validTagsIds);
}
}

0 comments on commit 388fdc2

Please sign in to comment.