Delete a discussion when its last post is deleted. fixes #823

This commit is contained in:
Toby Zerner 2016-02-26 13:26:09 +10:30
parent f375fd9241
commit 2f187dfd65
4 changed files with 40 additions and 12 deletions

View File

@ -33836,7 +33836,7 @@ System.register('flarum/utils/PostControls', ['flarum/components/EditPostCompose
onclick: this.restoreAction.bind(post)
}));
}
if (post.canDelete() && post.number() !== 1) {
if (post.canDelete()) {
items.add('delete', Button.component({
icon: 'times',
children: app.translator.trans('core.forum.post_controls.delete_forever_button'),
@ -33893,7 +33893,22 @@ System.register('flarum/utils/PostControls', ['flarum/components/EditPostCompose
if (context) context.loading = true;
return this['delete']().then(function () {
_this2.discussion().removePost(_this2.id());
var discussion = _this2.discussion();
discussion.removePost(_this2.id());
// If this was the last post in the discussion, then we will assume that
// the whole discussion was deleted too.
if (!discussion.posts.length) {
// If there is a discussion list in the cache, remove this discussion.
if (app.cache.discussionList) {
app.cache.discussionList.removeDiscussion(discussion);
}
if (app.viewingDiscussion(discussion)) {
app.history.back();
}
}
})['catch'](function () {}).then(function () {
if (context) context.loading = false;
m.redraw();

View File

@ -97,7 +97,7 @@ export default {
onclick: this.restoreAction.bind(post)
}));
}
if (post.canDelete() && post.number() !== 1) {
if (post.canDelete()) {
items.add('delete', Button.component({
icon: 'times',
children: app.translator.trans('core.forum.post_controls.delete_forever_button'),
@ -149,7 +149,22 @@ export default {
return this.delete()
.then(() => {
this.discussion().removePost(this.id());
const discussion = this.discussion();
discussion.removePost(this.id());
// If this was the last post in the discussion, then we will assume that
// the whole discussion was deleted too.
if (!discussion.posts.length) {
// If there is a discussion list in the cache, remove this discussion.
if (app.cache.discussionList) {
app.cache.discussionList.removeDiscussion(discussion);
}
if (app.viewingDiscussion(discussion)) {
app.history.back();
}
}
})
.catch(() => {})
.then(() => {

View File

@ -51,6 +51,12 @@ class DiscussionMetadataUpdater
public function whenPostWasDeleted(PostWasDeleted $event)
{
$this->removePost($event->post);
$discussion = $event->post->discussion;
if ($discussion->comments_count === 0) {
$discussion->delete();
}
}
/**

View File

@ -88,14 +88,6 @@ class Post extends AbstractModel
$post->discussion->save();
});
// Don't allow the first post in a discussion to be deleted, because
// it doesn't make sense. The discussion must be deleted instead.
static::deleting(function (Post $post) {
if ($post->number == 1) {
throw new DomainException('Cannot delete the first post of a discussion');
}
});
static::deleted(function (Post $post) {
$post->raise(new PostWasDeleted($post));
});