From d3595b611889fe2b405a3a858bb26700968e9089 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Saquetim?= <1108771+megothss@users.noreply.github.com> Date: Thu, 28 Nov 2024 17:20:20 -0300 Subject: [PATCH] FIX: Reset likeAction when updating a cached post from JSON data (#29971) This commit addresses an issue where the like button would not be updated properly when reloading a post that lost the only like it had received. --- .../javascripts/discourse/app/models/post.js | 1 + .../discourse/tests/unit/models/post-test.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/app/assets/javascripts/discourse/app/models/post.js b/app/assets/javascripts/discourse/app/models/post.js index 00d16c92062..69cdd292f9b 100644 --- a/app/assets/javascripts/discourse/app/models/post.js +++ b/app/assets/javascripts/discourse/app/models/post.js @@ -63,6 +63,7 @@ function trackedPostProperty(target, propertyKey, descriptor) { export default class Post extends RestModel { static munge(json) { + json.likeAction = null; if (json.actions_summary) { const lookup = EmberObject.create(); diff --git a/app/assets/javascripts/discourse/tests/unit/models/post-test.js b/app/assets/javascripts/discourse/tests/unit/models/post-test.js index e9dd417967d..dc30ffca4a8 100644 --- a/app/assets/javascripts/discourse/tests/unit/models/post-test.js +++ b/app/assets/javascripts/discourse/tests/unit/models/post-test.js @@ -116,4 +116,21 @@ module("Unit | Model | post", function (hooks) { ); assert.strictEqual(post.version, 2, "the version number increased"); }); + + test("likeAction", function (assert) { + const post = this.store.createRecord("post", { + id: 1173, + }); + + post.likeAction = { count: 1 }; + assert.deepEqual(post.likeAction, { count: 1 }, "likeAction set"); + + // creating a new record with the same id should reset the likeAction in the original post because instance + // is cached and the information required to properly generate the field is not available in the new JSON data + this.store.createRecord("post", { + id: 1173, + }); + + assert.ok(post.likeAction === null, "likeAction was reset to null"); + }); });