Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds new private thread notification #25

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 32 additions & 1 deletion app/models/thredded/private_topic.rb
Expand Up @@ -36,7 +36,38 @@ def user_id=(ids)
end end


def users_to_sentence def users_to_sentence
users.map{ |user| user.to_s.capitalize }.to_sentence users.map { |user| user.to_s.capitalize }.to_sentence
end

def self.unread_privates?(user)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def self.unread_privates?(user)
  completely_unread_private_topics_exist || latest_private_topic_date_newer_than_last_read
end

HOW ABOUT THAT SHIT?! This throws a whole lot of the onus in private instance methods but I think that's worthwhile. It's easier to figure out the work being done in those methods with names like that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

completely_unread_private_topics? || latest_private_topic_unread?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

brand_new_private_topics?(user) || latest_private_topic_unread?(user)
end

private

def self.brand_new_private_topics?(user)
user_private_topic_count(user) > 0 && latest_private_topic_read_date(user).blank?
end

def self.latest_private_topic_unread?(user)
latest_private_topic_date(user) > latest_private_topic_read_date(user)
end

def self.latest_private_topic_date(user)
user.thredded_private_topics.maximum('updated_at')
end

def self.latest_private_topic_read_date(user)
user
.thredded_private_topics
.includes(:user_topic_reads)
.where('thredded_user_topic_reads.user_id = ?', user.id)
.references(:user_topic_reads)
.maximum('thredded_user_topic_reads.updated_at')
end

def self.user_private_topic_count(user)
user.thredded_private_topics.count
end end
end end
end end
9 changes: 4 additions & 5 deletions app/views/thredded/shared/_topic_nav.html.erb
Expand Up @@ -9,11 +9,10 @@
<% end %> <% end %>


<ul class="listings"> <ul class="listings">
<% cache ['topic-nav-list', current_user, messageboard] do %> <li class="topic_list"><%= link_to 'topics', messageboard_topics_path %></li>
<li class="topic_list"><%= link_to 'topics', messageboard_topics_path %></li> <% if can? :list, messageboard.private_topics.build %>
<% if can? :list, messageboard.private_topics.build %> <% unread = Thredded::PrivateTopic.unread_privates?(current_user) %>
<li class="private_topic_list"><%= link_to 'private topics', messageboard_private_topics_path %></li> <li class="private_topic_list"><%= link_to 'private topics', messageboard_private_topics_path, class: unread ? 'unread' : '' %></li>
<% end %>
<% end %> <% end %>


<%= render 'thredded/search/form' %> <%= render 'thredded/search/form' %>
Expand Down
23 changes: 23 additions & 0 deletions spec/models/thredded/private_topic_spec.rb
Expand Up @@ -50,5 +50,28 @@ module Thredded
@topic.users_to_sentence.should eq 'Privateuser1 and Privateuser2' @topic.users_to_sentence.should eq 'Privateuser1 and Privateuser2'
end end
end end

describe '.unread_privates?' do
context 'when a user sends another user a PM' do
it 'the user should have unread private topics' do
expect(PrivateTopic.unread_privates?(@user2)).to be_true
end
end

context 'when a user reads a private thread' do
it 'the user should not have unread private topics' do
create(:user_topic_read, user: @user2, topic: @topic)
expect(PrivateTopic.unread_privates?(@user2)).to be_false
end
end

context 'when a user reads a PM and gets another PM' do
it 'the user should have unread private topics' do
create(:user_topic_read, user: @user2, topic: @topic)
create(:private_topic, messageboard: @messageboard, users: [@user1, @user2])
expect(PrivateTopic.unread_privates?(@user2)).to be_true
end
end
end
end end
end end