discourse/lib/unread.rb
Alan Guo Xiang Tan 37b8ce79c9
FEATURE: Add last visit indication to topic view page. (#13471)
This PR also removes grey old unread bubble from the topic badges by
dropping `TopicUser#highest_seen_post_number`.
2021-07-05 14:17:31 +08:00

38 lines
906 B
Ruby

# frozen_string_literal: true
class Unread
# This module helps us calculate unread post counts
def initialize(topic, topic_user, guardian)
@guardian = guardian
@topic = topic
@topic_user = topic_user
end
def unread_posts
return 0 if @topic_user.last_read_post_number.blank?
return 0 if do_not_notify?(@topic_user.notification_level)
highest_post_number = @guardian.is_staff? ? @topic.highest_staff_post_number : @topic.highest_post_number
return 0 if @topic_user.last_read_post_number > highest_post_number
unread = (highest_post_number - @topic_user.last_read_post_number)
unread = 0 if unread < 0
unread
end
protected
DO_NOT_NOTIFY_LEVELS = [
TopicUser.notification_levels[:muted],
TopicUser.notification_levels[:regular]
]
def do_not_notify?(notification_level)
DO_NOT_NOTIFY_LEVELS.include?(notification_level)
end
end