From fbdd9c00345f7df31fb194633923e242f037bbbf Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 30 Mar 2016 11:17:52 +1100 Subject: [PATCH] FIX: unread and new count not removing deleted topics on the fly FIX: unread PMs interfering with unread count --- .../models/topic-tracking-state.js.es6 | 46 ++++++++++++++++--- app/models/topic_tracking_state.rb | 38 +++++++++++++-- lib/post_destroyer.rb | 2 + 3 files changed, 77 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/discourse/models/topic-tracking-state.js.es6 b/app/assets/javascripts/discourse/models/topic-tracking-state.js.es6 index d1d80461461..9a3886b947e 100644 --- a/app/assets/javascripts/discourse/models/topic-tracking-state.js.es6 +++ b/app/assets/javascripts/discourse/models/topic-tracking-state.js.es6 @@ -57,9 +57,12 @@ const TopicTrackingState = Discourse.Model.extend({ tracker.notify(data); const old = tracker.states["t" + data.topic_id]; - if (!_.isEqual(old, data.payload)) { - tracker.states["t" + data.topic_id] = data.payload; - tracker.incrementMessageCount(); + // don't add tracking state for read stuff that was not tracked in first place + if (old || data.message_type !== "read") { + if (!_.isEqual(old, data.payload)) { + tracker.states["t" + data.topic_id] = data.payload; + tracker.incrementMessageCount(); + } } } }; @@ -69,6 +72,24 @@ const TopicTrackingState = Discourse.Model.extend({ if (this.currentUser) { this.messageBus.subscribe("/unread/" + this.currentUser.get('id'), process); } + + this.messageBus.subscribe("/delete", msg => { + const old = tracker.states["t" + msg.topic_id]; + debugger + if (old) { + old.deleted = true; + } + tracker.incrementMessageCount(); + }); + + this.messageBus.subscribe("/recover", msg => { + const old = tracker.states["t" + msg.topic_id]; + debugger + if (old) { + delete old.deleted; + } + tracker.incrementMessageCount(); + }); }, updateSeen(topicId, highestSeen) { @@ -82,6 +103,7 @@ const TopicTrackingState = Discourse.Model.extend({ notify(data) { if (!this.newIncoming) { return; } + if (data.archetype === "private_message") { return; } const filter = this.get("filter"); const filterCategory = this.get("filterCategory"); @@ -267,7 +289,13 @@ const TopicTrackingState = Discourse.Model.extend({ countNew(category_id) { return _.chain(this.states) .where(isNew) - .where(topic => topic.category_id === category_id || topic.parent_category_id === category_id || !category_id) + .where(topic => + topic.archetype !== "private_message" && + !topic.deleted && ( + topic.category_id === category_id || + topic.parent_category_id === category_id || + !category_id) + ) .value() .length; }, @@ -283,7 +311,13 @@ const TopicTrackingState = Discourse.Model.extend({ countUnread(category_id) { return _.chain(this.states) .where(isUnread) - .where(topic => topic.category_id === category_id || topic.parent_category_id === category_id || !category_id) + .where(topic => + topic.archetype !== "private_message" && + !topic.deleted && ( + topic.category_id === category_id || + topic.parent_category_id === category_id || + !category_id) + ) .value() .length; }, @@ -291,7 +325,7 @@ const TopicTrackingState = Discourse.Model.extend({ countCategory(category_id) { let sum = 0; _.each(this.states, function(topic){ - if (topic.category_id === category_id) { + if (topic.category_id === category_id && !topic.deleted) { sum += (topic.last_read_post_number === null || topic.last_read_post_number < topic.highest_post_number) ? 1 : 0; } diff --git a/app/models/topic_tracking_state.rb b/app/models/topic_tracking_state.rb index 7ffc6ad1e1a..97bd347aee7 100644 --- a/app/models/topic_tracking_state.rb +++ b/app/models/topic_tracking_state.rb @@ -27,7 +27,8 @@ class TopicTrackingState highest_post_number: 1, created_at: topic.created_at, topic_id: topic.id, - category_id: topic.category_id + category_id: topic.category_id, + archetype: topic.archetype } } @@ -46,7 +47,8 @@ class TopicTrackingState payload: { bumped_at: topic.bumped_at, topic_id: topic.id, - category_id: topic.category_id + category_id: topic.category_id, + archetype: topic.archetype } } @@ -74,7 +76,8 @@ class TopicTrackingState created_at: post.created_at, topic_id: post.topic_id, category_id: post.topic.category_id, - notification_level: tu.notification_level + notification_level: tu.notification_level, + archetype: post.topic.archetype } } @@ -83,6 +86,35 @@ class TopicTrackingState end + def self.publish_recover(topic) + group_ids = topic.category && topic.category.secure_group_ids + + message = { + topic_id: topic.id, + message_type: "recover", + payload: { + topic_id: topic.id, + } + } + + MessageBus.publish("/recover", message.as_json, group_ids: group_ids) + + end + + def self.publish_delete(topic) + group_ids = topic.category && topic.category.secure_group_ids + + message = { + topic_id: topic.id, + message_type: "delete", + payload: { + topic_id: topic.id, + } + } + + MessageBus.publish("/delete", message.as_json, group_ids: group_ids) + end + def self.publish_read(topic_id, last_read_post_number, user_id, notification_level=nil) highest_post_number = Topic.where(id: topic_id).pluck(:highest_post_number).first diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb index f0b188082ea..9d35572f0f9 100644 --- a/lib/post_destroyer.rb +++ b/lib/post_destroyer.rb @@ -65,6 +65,7 @@ class PostDestroyer def staff_recovered @post.recover! @post.publish_change_to_clients! :recovered + TopicTrackingState.publish_recover(@post.topic) if @post.topic && @post.post_number == 1 end # When a post is properly deleted. Well, it's still soft deleted, but it will no longer @@ -96,6 +97,7 @@ class PostDestroyer feature_users_in_the_topic if @post.topic @post.publish_change_to_clients! :deleted if @post.topic + TopicTrackingState.publish_delete(@post.topic) if @post.topic && @post.post_number == 1 end # When a user 'deletes' their own post. We just change the text.