2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
class Unread
|
2021-07-05 01:17:31 -05:00
|
|
|
# This module helps us calculate unread post counts
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2016-12-02 00:03:31 -06:00
|
|
|
def initialize(topic, topic_user, guardian)
|
|
|
|
@guardian = guardian
|
2013-02-05 13:16:51 -06:00
|
|
|
@topic = topic
|
|
|
|
@topic_user = topic_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def unread_posts
|
2021-07-05 01:17:31 -05:00
|
|
|
return 0 if @topic_user.last_read_post_number.blank?
|
2013-02-05 13:16:51 -06:00
|
|
|
return 0 if do_not_notify?(@topic_user.notification_level)
|
|
|
|
|
2022-06-29 19:18:12 -05:00
|
|
|
highest_post_number =
|
|
|
|
@guardian.is_whisperer? ? @topic.highest_staff_post_number : @topic.highest_post_number
|
2016-12-02 00:03:31 -06:00
|
|
|
|
2021-07-05 01:17:31 -05:00
|
|
|
return 0 if @topic_user.last_read_post_number > highest_post_number
|
2016-12-02 00:03:31 -06:00
|
|
|
|
2021-07-05 01:17:31 -05:00
|
|
|
unread = (highest_post_number - @topic_user.last_read_post_number)
|
|
|
|
unread = 0 if unread < 0
|
|
|
|
unread
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2021-07-05 01:17:31 -05:00
|
|
|
DO_NOT_NOTIFY_LEVELS = [
|
|
|
|
TopicUser.notification_levels[:muted],
|
|
|
|
TopicUser.notification_levels[:regular],
|
|
|
|
]
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
def do_not_notify?(notification_level)
|
2021-07-05 01:17:31 -05:00
|
|
|
DO_NOT_NOTIFY_LEVELS.include?(notification_level)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|