mirror of
https://github.com/flarum/core.git
synced 2025-08-11 02:44:04 +02:00
Flag improvements/tweaks (#18)
* Option to allow users to flag their own post * Increase size of "Flag Post" modal * Allow optional reason detail for offtopic, inappropriate and spam reasons * For other reasons, detail text is now required * Switch to column type "text" to allow more details in reason
This commit is contained in:
@@ -10,9 +10,13 @@
|
||||
namespace Flarum\Flags\Command;
|
||||
|
||||
use Flarum\Flags\Flag;
|
||||
use Flarum\Foundation\ValidationException;
|
||||
use Flarum\Post\CommentPost;
|
||||
use Flarum\Post\PostRepository;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Flarum\User\AssertPermissionTrait;
|
||||
use Flarum\User\Exception\PermissionDeniedException;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Tobscure\JsonApi\Exception\InvalidParameterException;
|
||||
|
||||
class CreateFlagHandler
|
||||
@@ -25,17 +29,31 @@ class CreateFlagHandler
|
||||
protected $posts;
|
||||
|
||||
/**
|
||||
* @param PostRepository $posts
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
public function __construct(PostRepository $posts)
|
||||
protected $translator;
|
||||
|
||||
/**
|
||||
* @var SettingsRepositoryInterface
|
||||
*/
|
||||
protected $settings;
|
||||
|
||||
/**
|
||||
* @param PostRepository $posts
|
||||
* @param TranslatorInterface $translator
|
||||
*/
|
||||
public function __construct(PostRepository $posts, TranslatorInterface $translator, SettingsRepositoryInterface $settings)
|
||||
{
|
||||
$this->posts = $posts;
|
||||
$this->translator = $translator;
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CreateFlag $command
|
||||
* @return Flag
|
||||
* @throws InvalidParameterException
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function handle(CreateFlag $command)
|
||||
{
|
||||
@@ -51,6 +69,16 @@ class CreateFlagHandler
|
||||
|
||||
$this->assertCan($actor, 'flag', $post);
|
||||
|
||||
if ($actor->id === $post->user_id && ! $this->settings->get('flarum-flags.can_flag_own')) {
|
||||
throw new PermissionDeniedException();
|
||||
}
|
||||
|
||||
if (array_get($data, 'attributes.reason') === null && array_get($data, 'attributes.reasonDetail') === '') {
|
||||
throw new ValidationException([
|
||||
'message' => $this->translator->trans('flarum-flags.forum.flag_post.reason-needed')
|
||||
]);
|
||||
}
|
||||
|
||||
Flag::unguard();
|
||||
|
||||
$flag = Flag::firstOrNew([
|
||||
|
@@ -14,6 +14,7 @@ use Flarum\Api\Serializer\CurrentUserSerializer;
|
||||
use Flarum\Api\Serializer\ForumSerializer;
|
||||
use Flarum\Api\Serializer\PostSerializer;
|
||||
use Flarum\Flags\Flag;
|
||||
use Flarum\Post\CommentPost;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Flarum\User\User;
|
||||
|
||||
@@ -49,7 +50,7 @@ class AddFlagsApiAttributes
|
||||
}
|
||||
|
||||
if ($event->isSerializer(PostSerializer::class)) {
|
||||
$event->attributes['canFlag'] = $event->actor->can('flag', $event->model);
|
||||
$event->attributes['canFlag'] = $event->actor->can('flag', $event->model) && $this->checkFlagOwnPostSetting($event->actor, $event->model);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,4 +77,19 @@ class AddFlagsApiAttributes
|
||||
|
||||
return $query->distinct()->count('flags.post_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $actor
|
||||
* @param CommentPost $post
|
||||
* @return bool
|
||||
*/
|
||||
protected function checkFlagOwnPostSetting(User $actor, CommentPost $post): bool
|
||||
{
|
||||
if ($actor->id === $post->user_id) {
|
||||
// If $actor is the post author, check to see if the setting is enabled
|
||||
return (bool) $this->settings->get('flarum-flags.can_flag_own');
|
||||
}
|
||||
// $actor is not the post author
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user