forum: fix topics being incorrectly marked as unread (again).
Second attempt at 71690cacc. Fix topics on page 2+ being still marked as
unread after the user has marked all topics as read.
This commit is contained in:
@@ -15,6 +15,7 @@ class ForumTopic < ApplicationRecord
|
||||
belongs_to_updater
|
||||
has_many :posts, -> {order("forum_posts.id asc")}, :class_name => "ForumPost", :foreign_key => "topic_id", :dependent => :destroy
|
||||
has_many :forum_topic_visits
|
||||
has_one :forum_topic_visit_by_current_user, -> { where(user_id: CurrentUser.id) }, class_name: "ForumTopicVisit"
|
||||
has_many :moderation_reports, through: :posts
|
||||
has_one :original_post, -> {order("forum_posts.id asc")}, class_name: "ForumPost", foreign_key: "topic_id", inverse_of: :topic
|
||||
|
||||
@@ -58,14 +59,18 @@ class ForumTopic < ApplicationRecord
|
||||
end
|
||||
|
||||
def read_by_user(user)
|
||||
return none if user.last_forum_read_at.nil? || user.last_forum_read_at < '2000-01-01'
|
||||
last_forum_read_at = user.last_forum_read_at || "2000-01-01".to_time
|
||||
|
||||
read_topics = user.visited_forum_topics.where("forum_topic_visits.last_read_at >= forum_topics.updated_at")
|
||||
old_topics = where("? >= forum_topics.updated_at", user.last_forum_read_at)
|
||||
old_topics = where("? >= forum_topics.updated_at", last_forum_read_at)
|
||||
|
||||
where(id: read_topics).or(where(id: old_topics))
|
||||
end
|
||||
|
||||
def unread_by_user(user)
|
||||
where.not(id: ForumTopic.read_by_user(user))
|
||||
end
|
||||
|
||||
def sticky_first
|
||||
order(is_sticky: :desc, updated_at: :desc)
|
||||
end
|
||||
@@ -110,17 +115,10 @@ class ForumTopic < ApplicationRecord
|
||||
ForumTopicVisit.create(:user_id => user.id, :forum_topic_id => id, :last_read_at => updated_at)
|
||||
end
|
||||
|
||||
has_unread_topics =
|
||||
ForumTopic
|
||||
.permitted
|
||||
.active
|
||||
.where("forum_topics.updated_at >= ?", user.last_forum_read_at)
|
||||
.joins("left join forum_topic_visits on (forum_topic_visits.forum_topic_id = forum_topics.id and forum_topic_visits.user_id = #{user.id})")
|
||||
.where("(forum_topic_visits.id is null or forum_topic_visits.last_read_at < forum_topics.updated_at)")
|
||||
.exists?
|
||||
unread_topics = ForumTopic.permitted.active.unread_by_user(user)
|
||||
|
||||
unless has_unread_topics
|
||||
user.update_attribute(:last_forum_read_at, Time.now)
|
||||
if !unread_topics.exists?
|
||||
user.update!(last_forum_read_at: Time.zone.now)
|
||||
ForumTopicVisit.prune!(user)
|
||||
end
|
||||
end
|
||||
@@ -138,6 +136,16 @@ class ForumTopic < ApplicationRecord
|
||||
user.level >= min_level
|
||||
end
|
||||
|
||||
# XXX forum_topic_visit_by_current_user is a hack to reduce queries on the forum index.
|
||||
def is_read?
|
||||
return true if CurrentUser.is_anonymous?
|
||||
|
||||
topic_last_read_at = forum_topic_visit_by_current_user&.last_read_at || "2000-01-01".to_time
|
||||
forum_last_read_at = CurrentUser.last_forum_read_at || "2000-01-01".to_time
|
||||
|
||||
(topic_last_read_at >= updated_at) || (forum_last_read_at >= updated_at)
|
||||
end
|
||||
|
||||
def create_mod_action_for_delete
|
||||
ModAction.log("deleted forum topic ##{id} (title: #{title})", :forum_topic_delete)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user