Skip to content

A platform for community discussion. Free, open, simple.

FIX: Endless loading post history (#28425)

Jan Cernik437d7a0ad19e
diff --git a/app/assets/javascripts/discourse/app/components/modal/history.js b/app/assets/javascripts/discourse/app/components/modal/history.js
index 562a272c9c3..404781abd4c 100644
--- a/app/assets/javascripts/discourse/app/components/modal/history.js
+++ b/app/assets/javascripts/discourse/app/components/modal/history.js
@@ -24,6 +24,7 @@ export default class History extends Component {
@service site;
@service currentUser;
@service siteSettings;
+ @service appEvents;
@tracked loading;
@tracked postRevision;
@@ -169,13 +170,29 @@ export default class History extends Component {
}
}
- refresh(postId, postVersion) {
+ async refresh(postId, postVersion) {
this.loading = true;
- Post.loadRevision(postId, postVersion).then((result) => {
+ try {
+ const result = await Post.loadRevision(postId, postVersion);
this.postRevision = result;
+ } catch (error) {
+ this.args.closeModal();
+ this.dialog.alert(error.jqXHR.responseJSON.errors[0]);
+
+ const postStream = this.args.model.post?.topic?.postStream;
+ if (!postStream) {
+ return;
+ }
+
+ postStream
+ .triggerChangedPost(postId, this.args.model)
+ .then(() =>
+ this.appEvents.trigger("post-stream:refresh", { id: postId })
+ );
+ } finally {
this.loading = false;
this.initialLoad = false;
- });
+ }
}
hide(postId, postVersion) {
diff --git a/app/assets/javascripts/discourse/tests/acceptance/history-test.js b/app/assets/javascripts/discourse/tests/acceptance/history-test.js
index d65616c76f3..c69061c42f2 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/history-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/history-test.js
@@ -1,6 +1,8 @@
import { click, visit } from "@ember/test-helpers";
import { test } from "qunit";
+import topicFixtures from "discourse/tests/fixtures/topic";
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
+import { cloneJSON } from "discourse-common/lib/object";
const revisionResponse = {
created_at: "2021-11-24T10:59:36.163Z",
@@ -101,3 +103,34 @@ acceptance("History Modal - anonymous", function (needs) {
);
});
});
+
+acceptance("History Modal - not found", function (needs) {
+ needs.pretender((server, helper) => {
+ const json = cloneJSON(topicFixtures["/t/280/1.json"]);
+ json.post_stream.posts[0].version = 2;
+ json.post_stream.posts[0].can_view_edit_history = true;
+
+ server.get("/t/280.json", () => helper.response(json));
+ server.get("/t/280/:post_number.json", () => {
+ helper.response(json);
+ });
+
+ server.get("/posts/398/revisions/latest.json", () => {
+ return helper.response(404, {
+ errors: ["The requested URL or resource could not be found."],
+ error_type: "not_found",
+ });
+ });
+ });
+
+ test("try to view a nonexistent revision", async function (assert) {
+ await visit("/t/internationalization-localization/280");
+ await click("article[data-post-id='398'] .edits button");
+ assert.dom(".dialog-body").exists();
+ await click(".dialog-footer .btn-primary");
+
+ assert
+ .dom("article[data-post-id='398'] .edits button")
+ .doesNotExist("it should refresh the post to hide the revisions button");
+ });
+});
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
index 6585ed2ae4b..c1e00d44a62 100644
--- a/app/controllers/posts_controller.rb
+++ b/app/controllers/posts_controller.rb
@@ -469,6 +469,8 @@ class PostsController < ApplicationController
post.public_version -= 1
post.save
+ post.publish_change_to_clients!(:revised)
+
render body: nil
end
@@ -509,6 +511,8 @@ class PostsController < ApplicationController
post.public_version += 1
post.save
+ post.publish_change_to_clients!(:revised)
+
render body: nil
end
diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb
index 92491f4bcdd..c699b4cf7f9 100644
--- a/spec/requests/posts_controller_spec.rb
+++ b/spec/requests/posts_controller_spec.rb
@@ -2105,6 +2105,41 @@ RSpec.describe PostsController do
end
end
+ context "when the history on a specific post is hidden" do
+ it "works when hiding a revision" do
+ sign_in(admin)
+
+ message =
+ MessageBus
+ .track_publish("/topic/#{post.topic.id}") do
+ put "/posts/#{post_revision.post_id}/revisions/#{post_revision.number}/hide"
+ end
+ .first
+
+ expect(response.status).to eq(200)
+ expect(message.data[:type]).to eq(:revised)
+ expect(message.data[:version]).to eq(2)
+ expect(post_revision.reload[:hidden]).to eq(true)
+ end
+
+ it "works when showing a revision" do
+ post_revision.update!(hidden: true)
+ sign_in(admin)
+
+ message =
+ MessageBus
+ .track_publish("/topic/#{post.topic.id}") do
+ put "/posts/#{post_revision.post_id}/revisions/#{post_revision.number}/show"
+ end
+ .first
+
+ expect(response.status).to eq(200)
+ expect(message.data[:type]).to eq(:revised)
+ expect(message.data[:version]).to eq(2)
+ expect(post_revision.reload[:hidden]).to eq(false)
+ end
+ end
+
context "when post is hidden" do
before do
post.hidden = true