FIX: unread and new count not removing deleted topics on the fly

FIX: unread PMs interfering with unread count
This commit is contained in:
Sam 2016-03-30 11:17:52 +11:00
parent b7171154da
commit fbdd9c0034
3 changed files with 77 additions and 9 deletions

View file

@ -57,9 +57,12 @@ const TopicTrackingState = Discourse.Model.extend({
tracker.notify(data); tracker.notify(data);
const old = tracker.states["t" + data.topic_id]; const old = tracker.states["t" + data.topic_id];
if (!_.isEqual(old, data.payload)) { // don't add tracking state for read stuff that was not tracked in first place
tracker.states["t" + data.topic_id] = data.payload; if (old || data.message_type !== "read") {
tracker.incrementMessageCount(); 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) { if (this.currentUser) {
this.messageBus.subscribe("/unread/" + this.currentUser.get('id'), process); 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) { updateSeen(topicId, highestSeen) {
@ -82,6 +103,7 @@ const TopicTrackingState = Discourse.Model.extend({
notify(data) { notify(data) {
if (!this.newIncoming) { return; } if (!this.newIncoming) { return; }
if (data.archetype === "private_message") { return; }
const filter = this.get("filter"); const filter = this.get("filter");
const filterCategory = this.get("filterCategory"); const filterCategory = this.get("filterCategory");
@ -267,7 +289,13 @@ const TopicTrackingState = Discourse.Model.extend({
countNew(category_id) { countNew(category_id) {
return _.chain(this.states) return _.chain(this.states)
.where(isNew) .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() .value()
.length; .length;
}, },
@ -283,7 +311,13 @@ const TopicTrackingState = Discourse.Model.extend({
countUnread(category_id) { countUnread(category_id) {
return _.chain(this.states) return _.chain(this.states)
.where(isUnread) .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() .value()
.length; .length;
}, },
@ -291,7 +325,7 @@ const TopicTrackingState = Discourse.Model.extend({
countCategory(category_id) { countCategory(category_id) {
let sum = 0; let sum = 0;
_.each(this.states, function(topic){ _.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 || sum += (topic.last_read_post_number === null ||
topic.last_read_post_number < topic.highest_post_number) ? 1 : 0; topic.last_read_post_number < topic.highest_post_number) ? 1 : 0;
} }

View file

@ -27,7 +27,8 @@ class TopicTrackingState
highest_post_number: 1, highest_post_number: 1,
created_at: topic.created_at, created_at: topic.created_at,
topic_id: topic.id, topic_id: topic.id,
category_id: topic.category_id category_id: topic.category_id,
archetype: topic.archetype
} }
} }
@ -46,7 +47,8 @@ class TopicTrackingState
payload: { payload: {
bumped_at: topic.bumped_at, bumped_at: topic.bumped_at,
topic_id: topic.id, 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, created_at: post.created_at,
topic_id: post.topic_id, topic_id: post.topic_id,
category_id: post.topic.category_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 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) 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 highest_post_number = Topic.where(id: topic_id).pluck(:highest_post_number).first

View file

@ -65,6 +65,7 @@ class PostDestroyer
def staff_recovered def staff_recovered
@post.recover! @post.recover!
@post.publish_change_to_clients! :recovered @post.publish_change_to_clients! :recovered
TopicTrackingState.publish_recover(@post.topic) if @post.topic && @post.post_number == 1
end end
# When a post is properly deleted. Well, it's still soft deleted, but it will no longer # 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 feature_users_in_the_topic if @post.topic
@post.publish_change_to_clients! :deleted 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 end
# When a user 'deletes' their own post. We just change the text. # When a user 'deletes' their own post. We just change the text.