mirror of
https://github.com/flarum/framework.git
synced 2024-12-13 07:03:35 +08:00
Allow author to delete discussion if there are no replies
Also disallow the first post in a discussion to be deleted or hidden (thus preventing discussions with zero posts) closes flarum/core#90 closes flarum/core#92
This commit is contained in:
parent
dfe1a9bae5
commit
3223f65ce3
|
@ -37,11 +37,14 @@ export default function(app) {
|
||||||
items.add('restore', ActionButton.component({ icon: 'reply', label: 'Restore', onclick: restoreAction.bind(this) }));
|
items.add('restore', ActionButton.component({ icon: 'reply', label: 'Restore', onclick: restoreAction.bind(this) }));
|
||||||
} else {
|
} else {
|
||||||
items.add('edit', ActionButton.component({ icon: 'pencil', label: 'Edit', onclick: editAction.bind(this) }));
|
items.add('edit', ActionButton.component({ icon: 'pencil', label: 'Edit', onclick: editAction.bind(this) }));
|
||||||
items.add('hide', ActionButton.component({ icon: 'times', label: 'Delete', onclick: hideAction.bind(this) }));
|
|
||||||
|
if (this.number() != 1) {
|
||||||
|
items.add('hide', ActionButton.component({ icon: 'times', label: 'Delete', onclick: hideAction.bind(this) }));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((this.contentType() !== 'comment' || this.isHidden()) && this.canDelete()) {
|
if ((this.contentType() !== 'comment' || this.isHidden()) && this.canDelete() && this.number() != 1) {
|
||||||
items.add('delete', ActionButton.component({ icon: 'times', label: 'Delete', onclick: deleteAction.bind(this) }));
|
items.add('delete', ActionButton.component({ icon: 'times', label: 'Delete', onclick: deleteAction.bind(this) }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,16 +15,17 @@ class CreateDiscussionsTable extends Migration
|
||||||
public function up()
|
public function up()
|
||||||
{
|
{
|
||||||
Schema::create('discussions', function (Blueprint $table) {
|
Schema::create('discussions', function (Blueprint $table) {
|
||||||
|
|
||||||
$table->increments('id');
|
$table->increments('id');
|
||||||
$table->string('title', 200);
|
$table->string('title', 200);
|
||||||
$table->integer('comments_count')->unsigned()->default(0);
|
$table->integer('comments_count')->unsigned()->default(0);
|
||||||
|
$table->integer('participants_count')->unsigned()->default(0);
|
||||||
$table->integer('number_index')->unsigned()->default(0);
|
$table->integer('number_index')->unsigned()->default(0);
|
||||||
|
|
||||||
$table->dateTime('start_time');
|
$table->dateTime('start_time');
|
||||||
$table->integer('start_user_id')->unsigned()->nullable();
|
$table->integer('start_user_id')->unsigned()->nullable();
|
||||||
$table->integer('start_post_id')->unsigned()->nullable();
|
$table->integer('start_post_id')->unsigned()->nullable();
|
||||||
|
|
||||||
$table->dateTime('last_time')->nullable();
|
$table->dateTime('last_time')->nullable();
|
||||||
$table->integer('last_user_id')->unsigned()->nullable();
|
$table->integer('last_user_id')->unsigned()->nullable();
|
||||||
$table->integer('last_post_id')->unsigned()->nullable();
|
$table->integer('last_post_id')->unsigned()->nullable();
|
||||||
|
|
|
@ -16,16 +16,17 @@ class DiscussionSerializer extends DiscussionBasicSerializer
|
||||||
$state = $discussion->stateFor($user);
|
$state = $discussion->stateFor($user);
|
||||||
|
|
||||||
$attributes += [
|
$attributes += [
|
||||||
'commentsCount' => (int) $discussion->comments_count,
|
'commentsCount' => (int) $discussion->comments_count,
|
||||||
'startTime' => $discussion->start_time->toRFC3339String(),
|
'participantsCount' => (int) $discussion->participants_count,
|
||||||
'lastTime' => $discussion->last_time ? $discussion->last_time->toRFC3339String() : null,
|
'startTime' => $discussion->start_time->toRFC3339String(),
|
||||||
'lastPostNumber' => $discussion->last_post_number,
|
'lastTime' => $discussion->last_time ? $discussion->last_time->toRFC3339String() : null,
|
||||||
'canReply' => $discussion->can($user, 'reply'),
|
'lastPostNumber' => $discussion->last_post_number,
|
||||||
'canRename' => $discussion->can($user, 'rename'),
|
'canReply' => $discussion->can($user, 'reply'),
|
||||||
'canDelete' => $discussion->can($user, 'delete'),
|
'canRename' => $discussion->can($user, 'rename'),
|
||||||
|
'canDelete' => $discussion->can($user, 'delete'),
|
||||||
|
|
||||||
'readTime' => $state && $state->read_time ? $state->read_time->toRFC3339String() : null,
|
'readTime' => $state && $state->read_time ? $state->read_time->toRFC3339String() : null,
|
||||||
'readNumber' => $state ? (int) $state->read_number : 0
|
'readNumber' => $state ? (int) $state->read_number : 0
|
||||||
];
|
];
|
||||||
|
|
||||||
return $this->extendAttributes($discussion, $attributes);
|
return $this->extendAttributes($discussion, $attributes);
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
use Flarum\Core\Repositories\PostRepositoryInterface as PostRepository;
|
use Flarum\Core\Repositories\PostRepositoryInterface as PostRepository;
|
||||||
use Flarum\Core\Events\PostWillBeSaved;
|
use Flarum\Core\Events\PostWillBeSaved;
|
||||||
use Flarum\Core\Support\DispatchesEvents;
|
use Flarum\Core\Support\DispatchesEvents;
|
||||||
|
use Flarum\Core\Models\CommentPost;
|
||||||
|
|
||||||
class EditPostCommandHandler
|
class EditPostCommandHandler
|
||||||
{
|
{
|
||||||
|
@ -22,15 +23,17 @@ class EditPostCommandHandler
|
||||||
|
|
||||||
$post->assertCan($user, 'edit');
|
$post->assertCan($user, 'edit');
|
||||||
|
|
||||||
if (isset($command->data['content'])) {
|
if ($post instanceof CommentPost) {
|
||||||
$post->revise($command->data['content'], $user);
|
if (isset($command->data['content'])) {
|
||||||
}
|
$post->revise($command->data['content'], $user);
|
||||||
|
}
|
||||||
|
|
||||||
if (isset($command->data['isHidden'])) {
|
if (isset($command->data['isHidden'])) {
|
||||||
if ($command->data['isHidden']) {
|
if ($command->data['isHidden']) {
|
||||||
$post->hide($user);
|
$post->hide($user);
|
||||||
} else {
|
} else {
|
||||||
$post->restore($user);
|
$post->restore($user);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,7 @@ class DiscussionMetadataUpdater
|
||||||
|
|
||||||
$discussion->comments_count++;
|
$discussion->comments_count++;
|
||||||
$discussion->setLastPost($event->post);
|
$discussion->setLastPost($event->post);
|
||||||
|
$discussion->refreshParticipantsCount();
|
||||||
$discussion->save();
|
$discussion->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,6 +47,7 @@ class DiscussionMetadataUpdater
|
||||||
$discussion = $event->post->discussion;
|
$discussion = $event->post->discussion;
|
||||||
|
|
||||||
$discussion->refreshCommentsCount();
|
$discussion->refreshCommentsCount();
|
||||||
|
$discussion->refreshParticipantsCount();
|
||||||
$discussion->refreshLastPost();
|
$discussion->refreshLastPost();
|
||||||
$discussion->save();
|
$discussion->save();
|
||||||
}
|
}
|
||||||
|
@ -56,6 +58,7 @@ class DiscussionMetadataUpdater
|
||||||
|
|
||||||
if ($discussion->exists) {
|
if ($discussion->exists) {
|
||||||
$discussion->refreshCommentsCount();
|
$discussion->refreshCommentsCount();
|
||||||
|
$discussion->refreshParticipantsCount();
|
||||||
|
|
||||||
if ($discussion->last_post_id == $post->id) {
|
if ($discussion->last_post_id == $post->id) {
|
||||||
$discussion->refreshLastPost();
|
$discussion->refreshLastPost();
|
||||||
|
|
|
@ -5,6 +5,7 @@ use Flarum\Core\Events\PostWasPosted;
|
||||||
use Flarum\Core\Events\PostWasRevised;
|
use Flarum\Core\Events\PostWasRevised;
|
||||||
use Flarum\Core\Events\PostWasHidden;
|
use Flarum\Core\Events\PostWasHidden;
|
||||||
use Flarum\Core\Events\PostWasRestored;
|
use Flarum\Core\Events\PostWasRestored;
|
||||||
|
use Flarum\Core\Exceptions\ValidationFailureException;
|
||||||
|
|
||||||
class CommentPost extends Post
|
class CommentPost extends Post
|
||||||
{
|
{
|
||||||
|
@ -75,6 +76,10 @@ class CommentPost extends Post
|
||||||
*/
|
*/
|
||||||
public function hide($user)
|
public function hide($user)
|
||||||
{
|
{
|
||||||
|
if ($this->number == 1) {
|
||||||
|
throw new ValidationFailureException;
|
||||||
|
}
|
||||||
|
|
||||||
if (! $this->hide_time) {
|
if (! $this->hide_time) {
|
||||||
$this->hide_time = time();
|
$this->hide_time = time();
|
||||||
$this->hide_user_id = $user->id;
|
$this->hide_user_id = $user->id;
|
||||||
|
@ -93,6 +98,10 @@ class CommentPost extends Post
|
||||||
*/
|
*/
|
||||||
public function restore($user)
|
public function restore($user)
|
||||||
{
|
{
|
||||||
|
if ($this->number == 1) {
|
||||||
|
throw new ValidationFailureException;
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->hide_time !== null) {
|
if ($this->hide_time !== null) {
|
||||||
$this->hide_time = null;
|
$this->hide_time = null;
|
||||||
$this->hide_user_id = null;
|
$this->hide_user_id = null;
|
||||||
|
|
|
@ -20,15 +20,16 @@ class Discussion extends Model
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public static $rules = [
|
public static $rules = [
|
||||||
'title' => 'required',
|
'title' => 'required',
|
||||||
'start_time' => 'required|date',
|
'start_time' => 'required|date',
|
||||||
'comments_count' => 'integer',
|
'comments_count' => 'integer',
|
||||||
'start_user_id' => 'integer',
|
'participants_count' => 'integer',
|
||||||
'start_post_id' => 'integer',
|
'start_user_id' => 'integer',
|
||||||
'last_time' => 'date',
|
'start_post_id' => 'integer',
|
||||||
'last_user_id' => 'integer',
|
'last_time' => 'date',
|
||||||
'last_post_id' => 'integer',
|
'last_user_id' => 'integer',
|
||||||
'last_post_number' => 'integer'
|
'last_post_id' => 'integer',
|
||||||
|
'last_post_number' => 'integer'
|
||||||
];
|
];
|
||||||
|
|
||||||
protected static $relationships = [];
|
protected static $relationships = [];
|
||||||
|
@ -175,6 +176,18 @@ class Discussion extends Model
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refresh the discussion's participants count.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function refreshParticipantsCount()
|
||||||
|
{
|
||||||
|
$this->participants_count = $this->participants()->count('users.id');
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specify that a post was added to this discussion during this request
|
* Specify that a post was added to this discussion during this request
|
||||||
* for later retrieval.
|
* for later retrieval.
|
||||||
|
@ -255,6 +268,16 @@ class Discussion extends Model
|
||||||
return $this->posts()->where('type', 'comment')->whereNull('hide_time');
|
return $this->posts()->where('type', 'comment')->whereNull('hide_time');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define the relationship with the discussion's participants.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||||
|
*/
|
||||||
|
public function participants()
|
||||||
|
{
|
||||||
|
return User::join('posts', 'posts.user_id', '=', 'users.id')->where('posts.discussion_id', $this->id)->select('users.*')->distinct();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define the relationship with the discussion's first post.
|
* Define the relationship with the discussion's first post.
|
||||||
*
|
*
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use Flarum\Core\Events\PostWasDeleted;
|
use Flarum\Core\Events\PostWasDeleted;
|
||||||
use Flarum\Core\Support\Locked;
|
use Flarum\Core\Support\Locked;
|
||||||
|
use Flarum\Core\Exceptions\ValidationFailureException;
|
||||||
|
|
||||||
class Post extends Model
|
class Post extends Model
|
||||||
{
|
{
|
||||||
|
@ -74,6 +75,12 @@ class Post extends Model
|
||||||
$post->discussion->save();
|
$post->discussion->save();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
static::deleting(function ($post) {
|
||||||
|
if ($post->number == 1) {
|
||||||
|
throw new ValidationFailureException;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
static::deleted(function ($post) {
|
static::deleted(function ($post) {
|
||||||
$post->raise(new PostWasDeleted($post));
|
$post->raise(new PostWasDeleted($post));
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user