mirror of
https://github.com/flarum/core.git
synced 2025-08-02 22:47:33 +02:00
Allow discussions to be hidden and restored
This commit is contained in:
@@ -49,6 +49,16 @@ class EditDiscussionHandler
|
||||
$discussion->rename($attributes['title'], $actor);
|
||||
}
|
||||
|
||||
if (isset($attributes['isHidden'])) {
|
||||
$discussion->assertCan($actor, 'hide');
|
||||
|
||||
if ($attributes['isHidden']) {
|
||||
$discussion->hide($actor);
|
||||
} else {
|
||||
$discussion->restore();
|
||||
}
|
||||
}
|
||||
|
||||
event(new DiscussionWillBeSaved($discussion, $actor, $data));
|
||||
|
||||
$discussion->save();
|
||||
|
@@ -14,6 +14,8 @@ use Flarum\Core\Model;
|
||||
use Flarum\Events\DiscussionWasDeleted;
|
||||
use Flarum\Events\DiscussionWasStarted;
|
||||
use Flarum\Events\DiscussionWasRenamed;
|
||||
use Flarum\Events\DiscussionWasHidden;
|
||||
use Flarum\Events\DiscussionWasRestored;
|
||||
use Flarum\Events\PostWasDeleted;
|
||||
use Flarum\Events\ScopePostVisibility;
|
||||
use Flarum\Core\Posts\Post;
|
||||
@@ -68,7 +70,7 @@ class Discussion extends Model
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $dates = ['start_time', 'last_time'];
|
||||
protected $dates = ['start_time', 'last_time', 'hide_time'];
|
||||
|
||||
/**
|
||||
* The user for which the state relationship should be loaded.
|
||||
@@ -147,6 +149,41 @@ class Discussion extends Model
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide the discussion.
|
||||
*
|
||||
* @param User $actor
|
||||
* @return $this
|
||||
*/
|
||||
public function hide(User $actor = null)
|
||||
{
|
||||
if (! $this->hide_time) {
|
||||
$this->hide_time = time();
|
||||
$this->hide_user_id = $actor ? $actor->id : null;
|
||||
|
||||
$this->raise(new DiscussionWasHidden($this));
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore the discussion.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function restore()
|
||||
{
|
||||
if ($this->hide_time !== null) {
|
||||
$this->hide_time = null;
|
||||
$this->hide_user_id = null;
|
||||
|
||||
$this->raise(new DiscussionWasRestored($this));
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the discussion's start post details.
|
||||
*
|
||||
|
@@ -15,7 +15,7 @@ use Flarum\Core\Users\User;
|
||||
use Flarum\Events\ModelAllow;
|
||||
use Flarum\Events\ScopeModelVisibility;
|
||||
use Flarum\Events\RegisterDiscussionGambits;
|
||||
use Flarum\Events\ScopeEmptyDiscussionVisibility;
|
||||
use Flarum\Events\ScopeHiddenDiscussionVisibility;
|
||||
use Flarum\Support\ServiceProvider;
|
||||
use Flarum\Extend;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
@@ -58,12 +58,15 @@ class DiscussionsServiceProvider extends ServiceProvider
|
||||
|
||||
$events->listen(ScopeModelVisibility::class, function (ScopeModelVisibility $event) {
|
||||
if ($event->model instanceof Discussion) {
|
||||
if (! $event->actor->hasPermission('discussion.editPosts')) {
|
||||
$event->query->where(function ($query) use ($event) {
|
||||
$query->where('comments_count', '>', '0')
|
||||
->orWhere('start_user_id', $event->actor->id);
|
||||
$user = $event->actor;
|
||||
|
||||
event(new ScopeEmptyDiscussionVisibility($query, $event->actor));
|
||||
if (! $user->hasPermission('discussion.hide')) {
|
||||
$event->query->where(function ($query) use ($user) {
|
||||
$query->whereNull('discussions.hide_time')
|
||||
->where('comments_count', '>', 0)
|
||||
->orWhere('start_user_id', $user->id);
|
||||
|
||||
event(new ScopeHiddenDiscussionVisibility($query, $user, 'discussion.hide'));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -86,8 +89,10 @@ class DiscussionsServiceProvider extends ServiceProvider
|
||||
->needs('Flarum\Core\Search\GambitManager')
|
||||
->give(function (Container $app) {
|
||||
$gambits = new GambitManager($app);
|
||||
|
||||
$gambits->setFulltextGambit('Flarum\Core\Discussions\Search\Gambits\FulltextGambit');
|
||||
$gambits->add('Flarum\Core\Discussions\Search\Gambits\AuthorGambit');
|
||||
$gambits->add('Flarum\Core\Discussions\Search\Gambits\HiddenGambit');
|
||||
$gambits->add('Flarum\Core\Discussions\Search\Gambits\UnreadGambit');
|
||||
|
||||
event(new RegisterDiscussionGambits($gambits));
|
||||
|
42
src/Core/Discussions/Search/Gambits/HiddenGambit.php
Normal file
42
src/Core/Discussions/Search/Gambits/HiddenGambit.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Core\Discussions\Search\Gambits;
|
||||
|
||||
use Flarum\Core\Discussions\Search\DiscussionSearch;
|
||||
use Flarum\Core\Search\RegexGambit;
|
||||
use Flarum\Core\Search\Search;
|
||||
use LogicException;
|
||||
|
||||
class HiddenGambit extends RegexGambit
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $pattern = 'is:hidden';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function conditions(Search $search, array $matches, $negate)
|
||||
{
|
||||
if (! $search instanceof DiscussionSearch) {
|
||||
throw new LogicException('This gambit can only be applied on a DiscussionSearch');
|
||||
}
|
||||
|
||||
$search->getQuery()->where(function ($query) use ($negate) {
|
||||
if ($negate) {
|
||||
$query->whereNull('hide_time')->where('comments_count', '>', 0);
|
||||
} else {
|
||||
$query->whereNotNull('hide_time')->orWhere('comments_count', 0);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user