A platform for community discussion. Free, open, simple.
FIX: Background like count update didn't account for own user actions (#16688)
Rafael dos Santos Silva919f71537e1f
diff --git a/app/assets/javascripts/discourse/app/controllers/topic.js b/app/assets/javascripts/discourse/app/controllers/topic.jsindex f0fd230fcf9..ab42d954fa3 100644--- a/app/assets/javascripts/discourse/app/controllers/topic.js+++ b/app/assets/javascripts/discourse/app/controllers/topic.js@@ -1712,9 +1712,15 @@ export default Controller.extend(bufferedProperty("model"), {.then(() => refresh({ id: data.id, refreshLikes: true }));break;}- case "liked": {+ case "liked":+ case "unliked": {postStream- .triggerLikedPost(data.id, data.likes_count)+ .triggerLikedPost(+ data.id,+ data.likes_count,+ data.user_id,+ data.type+ ).then(() => refresh({ id: data.id, refreshLikes: true }));break;}diff --git a/app/assets/javascripts/discourse/app/models/post-stream.js b/app/assets/javascripts/discourse/app/models/post-stream.jsindex e85d80de64e..7c509f75d8f 100644--- a/app/assets/javascripts/discourse/app/models/post-stream.js+++ b/app/assets/javascripts/discourse/app/models/post-stream.js@@ -847,12 +847,12 @@ export default RestModel.extend({return resolved;},- triggerLikedPost(postId, likesCount) {+ triggerLikedPost(postId, likesCount, userID, eventType) {const resolved = Promise.resolve();const post = this.findLoadedPost(postId);if (post) {- post.updateLikeCount(likesCount);+ post.updateLikeCount(likesCount, userID, eventType);this.storePost(post);}diff --git a/app/assets/javascripts/discourse/app/models/post.js b/app/assets/javascripts/discourse/app/models/post.jsindex 16ea1d81532..0d52a034721 100644--- a/app/assets/javascripts/discourse/app/models/post.js+++ b/app/assets/javascripts/discourse/app/models/post.js@@ -355,21 +355,18 @@ const Post = RestModel.extend({}},- updateLikeCount(count) {+ updateLikeCount(count, userId, eventType) {+ let ownLike = User.current()?.id === userId && eventType === "liked";let current_actions_summary = this.get("actions_summary");let likeActionID = Site.current().post_action_types.find((a) => a.name_key === "like").id;+ const newActionObject = { id: likeActionID, count, acted: ownLike };if (!this.actions_summary.find((entry) => entry.id === likeActionID)) {let json = Post.munge({id: this.id,- actions_summary: [- {- id: likeActionID,- count,- },- ],+ actions_summary: [newActionObject],});this.set("actions_summary",@@ -378,11 +375,12 @@ const Post = RestModel.extend({this.set("actionByName", json.actionByName);this.set("likeAction", json.likeAction);} else {- this.actions_summary.find(- (entry) => entry.id === likeActionID- ).count = count;- this.actionByName["like"] = count;- this.likeAction.count = count;+ Object.assign(+ this.actions_summary.find((entry) => entry.id === likeActionID),+ newActionObject+ );+ Object.assign(this.actionByName["like"], newActionObject);+ Object.assign(this.likeAction, newActionObject);}},diff --git a/lib/post_action_creator.rb b/lib/post_action_creator.rbindex 910078766f8..7b8256b380f 100644--- a/lib/post_action_creator.rb+++ b/lib/post_action_creator.rb@@ -161,7 +161,7 @@ privatedef notify_subscribersif @post_action_name == :like- @post.publish_change_to_clients! :liked, { likes_count: @post.like_count + 1 }+ @post.publish_change_to_clients! :liked, { likes_count: @post.like_count + 1, user_id: @created_by.id }elsif self.class.notify_types.include?(@post_action_name)@post.publish_change_to_clients! :actedenddiff --git a/lib/post_action_destroyer.rb b/lib/post_action_destroyer.rbindex d215f1616e5..697047de906 100644--- a/lib/post_action_destroyer.rb+++ b/lib/post_action_destroyer.rb@@ -65,7 +65,7 @@ protecteddef notify_subscribersname = PostActionType.types[@post_action_type_id]if name == :like- @post.publish_change_to_clients!(:liked, { likes_count: @post.like_count })+ @post.publish_change_to_clients!(:unliked, { likes_count: @post.like_count, user_id: @destroyed_by.id })elsif self.class.notify_types.include?(name)@post.publish_change_to_clients!(:acted)enddiff --git a/spec/lib/post_action_creator_spec.rb b/spec/lib/post_action_creator_spec.rbindex 483fa9ffe1b..c5f201f2b59 100644--- a/spec/lib/post_action_creator_spec.rb+++ b/spec/lib/post_action_creator_spec.rb@@ -60,6 +60,19 @@ describe PostActionCreator doexpect(result.post_action.post_action_type_id).to eq(like_type_id)end+ it 'notifies subscribers' do+ expect(post.reload.like_count).to eq(0)++ messages = MessageBus.track_publish do+ PostActionCreator.new(user, post, like_type_id).perform+ end++ message = messages.last.data+ expect(message[:type]).to eq(:liked)+ expect(message[:likes_count]).to eq(1)+ expect(message[:user_id]).to eq(user.id)+ end+it 'does not create an invalid post action' doresult = PostActionCreator.new(user, nil, like_type_id).performexpect(result.failed?).to eq(true)diff --git a/spec/lib/post_action_destroyer_spec.rb b/spec/lib/post_action_destroyer_spec.rbindex 1b333f86a48..ad0d2f7db00 100644--- a/spec/lib/post_action_destroyer_spec.rb+++ b/spec/lib/post_action_destroyer_spec.rb@@ -26,8 +26,9 @@ describe PostActionDestroyer doendmessage = messages.last.data- expect(message[:type]).to eq(:liked)+ expect(message[:type]).to eq(:unliked)expect(message[:likes_count]).to eq(0)+ expect(message[:user_id]).to eq(user.id)endend