FIX: Showing Unread(1) when you weren't tracking the topic. Also

includes performance fix when having MANY new or unread topics.
This commit is contained in:
Robin Ward 2014-02-26 15:37:42 -05:00
parent 1c51a613b2
commit 9267c162a1
4 changed files with 28 additions and 32 deletions

View File

@ -103,26 +103,8 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
}
}
if(filter === "new" && !list.more_topics_url){
// scrub all new rows and reload from list
_.each(this.states, function(state){
if(state.last_read_post_number === null) {
tracker.removeTopic(state.topic_id);
}
});
}
if(filter === "unread" && !list.more_topics_url){
// scrub all new rows and reload from list
_.each(this.states, function(state){
if(state.last_read_post_number !== null) {
tracker.removeTopic(state.topic_id);
}
});
}
_.each(list.topics, function(topic){
var row = {};
var row = tracker.states["t" + topic.id] || {};
row.topic_id = topic.id;
if(topic.unseen) {
@ -142,7 +124,6 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
}
tracker.states["t" + topic.id] = row;
});
this.incrementMessageCount();
@ -166,6 +147,7 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
return topic.last_read_post_number !== null &&
topic.last_read_post_number < topic.highest_post_number;
})
.where(function(topic) { return topic.notification_level >= Discourse.Topic.NotificationLevel.TRACKING})
.where(function(topic){ return topic.category_name === category_name || !category_name;})
.value()
.length;

View File

@ -9,7 +9,13 @@ class TopicTrackingState
CHANNEL = "/user-tracking"
attr_accessor :user_id, :topic_id, :highest_post_number, :last_read_post_number, :created_at, :category_name
attr_accessor :user_id,
:topic_id,
:highest_post_number,
:last_read_post_number,
:created_at,
:category_name,
:notification_level
def self.publish_new(topic)
@ -57,7 +63,7 @@ class TopicTrackingState
end
def self.publish_read(topic_id, last_read_post_number, user_id)
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
@ -67,7 +73,8 @@ class TopicTrackingState
payload: {
last_read_post_number: last_read_post_number,
highest_post_number: highest_post_number,
topic_id: topic_id
topic_id: topic_id,
notification_level: notification_level
}
}
@ -104,7 +111,13 @@ class TopicTrackingState
new = TopicQuery.new_filter(Topic, "xxx").where_values.join(" AND ").gsub!("'xxx'", treat_as_new_topic_clause)
sql = <<SQL
SELECT u.id AS user_id, topics.id AS topic_id, topics.created_at, highest_post_number, last_read_post_number, c.name AS category_name
SELECT u.id AS user_id,
topics.id AS topic_id,
topics.created_at,
highest_post_number,
last_read_post_number,
c.name AS category_name,
tu.notification_level
FROM users u
FULL OUTER JOIN topics ON 1=1
LEFT JOIN topic_users tu ON tu.topic_id = topics.id AND tu.user_id = u.id
@ -126,6 +139,7 @@ SQL
if topic_id
sql << " AND topics.id = :topic_id"
end
sql << " ORDER BY topics.bumped_at DESC LIMIT 500"
SqlBuilder.new(sql)
.map_exec(TopicTrackingState, user_ids: user_ids, topic_id: topic_id)

View File

@ -183,7 +183,7 @@ class TopicUser < ActiveRecord::Base
if before_last_read < post_number
# The user read at least one new post
TopicTrackingState.publish_read(topic_id, post_number, user.id)
TopicTrackingState.publish_read(topic_id, post_number, user.id, after)
user.update_posts_read!(post_number - before_last_read)
end
@ -194,15 +194,15 @@ class TopicUser < ActiveRecord::Base
if rows.length == 0
# The user read at least one post in a topic that they haven't viewed before.
TopicTrackingState.publish_read(topic_id, post_number, user.id)
args[:new_status] = notification_levels[:regular]
if (user.auto_track_topics_after_msecs || SiteSetting.auto_track_topics_after) == 0
args[:new_status] = notification_levels[:tracking]
end
TopicTrackingState.publish_read(topic_id, post_number, user.id, args[:new_status])
user.update_posts_read!(post_number)
args[:tracking] = notification_levels[:tracking]
args[:regular] = notification_levels[:regular]
args[:site_setting] = SiteSetting.auto_track_topics_after
exec_sql("INSERT INTO topic_users (user_id, topic_id, last_read_post_number, seen_post_count, last_visited_at, first_visited_at, notification_level)
SELECT :user_id, :topic_id, :post_number, ft.highest_post_number, :now, :now,
case when coalesce(u.auto_track_topics_after_msecs, :site_setting) = 0 then :tracking else :regular end
SELECT :user_id, :topic_id, :post_number, ft.highest_post_number, :now, :now, :new_status
FROM topics AS ft
JOIN users u on u.id = :user_id
WHERE ft.id = :topic_id

View File

@ -1,3 +1,3 @@
class TopicTrackingStateSerializer < ApplicationSerializer
attributes :topic_id, :highest_post_number, :last_read_post_number, :created_at, :category_name
attributes :topic_id, :highest_post_number, :last_read_post_number, :created_at, :category_name, :notification_level
end