2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
class Unread
|
|
|
|
|
|
|
|
# This module helps us calculate unread and new post counts
|
|
|
|
|
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
|
|
|
|
return 0 if do_not_notify?(@topic_user.notification_level)
|
2014-10-30 17:40:35 -05:00
|
|
|
result = ((@topic_user.highest_seen_post_number || 0) - (@topic_user.last_read_post_number || 0))
|
2013-02-25 10:42:20 -06:00
|
|
|
result = 0 if result < 0
|
|
|
|
result
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def new_posts
|
2014-10-30 17:40:35 -05:00
|
|
|
return 0 if @topic_user.highest_seen_post_number.blank?
|
2013-02-05 13:16:51 -06:00
|
|
|
return 0 if do_not_notify?(@topic_user.notification_level)
|
|
|
|
|
2016-12-02 00:03:31 -06:00
|
|
|
highest_post_number = @guardian.is_staff? ? @topic.highest_staff_post_number : @topic.highest_post_number
|
|
|
|
|
|
|
|
return 0 if (@topic_user.last_read_post_number || 0) > highest_post_number
|
|
|
|
|
|
|
|
new_posts = (highest_post_number - @topic_user.highest_seen_post_number)
|
2013-02-05 13:16:51 -06:00
|
|
|
new_posts = 0 if new_posts < 0
|
2019-11-14 14:10:51 -06:00
|
|
|
new_posts
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def do_not_notify?(notification_level)
|
2013-03-06 14:17:07 -06:00
|
|
|
[TopicUser.notification_levels[:muted], TopicUser.notification_levels[:regular]].include?(notification_level)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|