From 2f187dfd6576ca18f9f8e97c7885eb5a195144fa Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Fri, 26 Feb 2016 13:26:09 +1030 Subject: [PATCH] Delete a discussion when its last post is deleted. fixes #823 --- framework/core/js/forum/dist/app.js | 19 +++++++++++++++++-- .../core/js/forum/src/utils/PostControls.js | 19 +++++++++++++++++-- .../Listener/DiscussionMetadataUpdater.php | 6 ++++++ framework/core/src/Core/Post.php | 8 -------- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/framework/core/js/forum/dist/app.js b/framework/core/js/forum/dist/app.js index 66626c763..8f1f2dc10 100644 --- a/framework/core/js/forum/dist/app.js +++ b/framework/core/js/forum/dist/app.js @@ -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(); diff --git a/framework/core/js/forum/src/utils/PostControls.js b/framework/core/js/forum/src/utils/PostControls.js index 69a62a757..461c9c6ac 100644 --- a/framework/core/js/forum/src/utils/PostControls.js +++ b/framework/core/js/forum/src/utils/PostControls.js @@ -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(() => { diff --git a/framework/core/src/Core/Listener/DiscussionMetadataUpdater.php b/framework/core/src/Core/Listener/DiscussionMetadataUpdater.php index 57e64ba06..6591a126a 100755 --- a/framework/core/src/Core/Listener/DiscussionMetadataUpdater.php +++ b/framework/core/src/Core/Listener/DiscussionMetadataUpdater.php @@ -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(); + } } /** diff --git a/framework/core/src/Core/Post.php b/framework/core/src/Core/Post.php index 082c7a8de..ed2f53f1d 100755 --- a/framework/core/src/Core/Post.php +++ b/framework/core/src/Core/Post.php @@ -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)); });