From f217a5bc112b274affe5b4e7a44240aa946a228b Mon Sep 17 00:00:00 2001 From: Steffen van Bergerem Date: Thu, 13 Mar 2014 21:26:05 +0100 Subject: [PATCH] Add filters to notifications controller --- Changelog.md | 1 + app/controllers/application_controller.rb | 6 +++ app/controllers/conversations_controller.rb | 2 +- app/controllers/notifications_controller.rb | 13 ++++++ app/controllers/posts_controller.rb | 2 +- app/controllers/status_messages_controller.rb | 2 +- app/helpers/notifications_helper.rb | 5 ++- app/models/notification.rb | 11 +++++ .../notifications/_notify_popup_item.haml | 2 +- config/locales/diaspora/en.yml | 42 ++++++++++++------- config/locales/javascript/javascript.en.yml | 4 ++ .../jasmine_fixtures/notifications_spec.rb | 19 +++++++++ .../notifications_controller_spec.rb | 16 ++++++- spec/helpers/notifications_helper_spec.rb | 2 +- 14 files changed, 103 insertions(+), 24 deletions(-) create mode 100644 spec/controllers/jasmine_fixtures/notifications_spec.rb diff --git a/Changelog.md b/Changelog.md index 1f599fb0b..c2caa74f3 100644 --- a/Changelog.md +++ b/Changelog.md @@ -32,6 +32,7 @@ * Add permalinks for comments [#4577](https://github.com/diaspora/diaspora/pull/4577) * New menu for the mobile version [#4673](https://github.com/diaspora/diaspora/pull/4673) * Added comment count to statistic to enable calculations of posts/comments ratios [#4799](https://github.com/diaspora/diaspora/pull/4799) +* Add filters to notifications controller [#4814](https://github.com/diaspora/diaspora/pull/4814) # 0.3.0.3 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 780852ab4..320ce93f0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -151,4 +151,10 @@ class ApplicationController < ActionController::Base gon.preloads = {} end + def self.use_bootstrap_for *routes + before_filter -> { + @css_framework = :bootstrap + gon.bootstrap = true + }, only: routes.flatten + end end diff --git a/app/controllers/conversations_controller.rb b/app/controllers/conversations_controller.rb index acc024dca..180c42bf6 100644 --- a/app/controllers/conversations_controller.rb +++ b/app/controllers/conversations_controller.rb @@ -2,7 +2,7 @@ class ConversationsController < ApplicationController before_filter :authenticate_user! layout ->(c) { request.format == :mobile ? "application" : "with_header" } - before_filter -> { @css_framework = :bootstrap } + use_bootstrap_for :index, :show, :new respond_to :html, :mobile, :json, :js diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index cfdf2be95..85e951436 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -5,6 +5,9 @@ class NotificationsController < ApplicationController before_filter :authenticate_user! + layout ->(c) { request.format == :mobile ? "application" : "with_header_with_footer" } + use_bootstrap_for :index + def update note = Notification.where(:recipient_id => current_user.id, :id => params[:id]).first if note @@ -23,6 +26,10 @@ class NotificationsController < ApplicationController def index conditions = {:recipient_id => current_user.id} + if params[:type] && Notification.types.has_key?(params[:type]) + conditions[:type] = Notification.types[params[:type]] + end + if params[:show] == "unread" then conditions[:unread] = true end page = params[:page] || 1 per_page = params[:per_page] || 25 @notifications = WillPaginate::Collection.create(page, per_page, Notification.where(conditions).count ) do |pager| @@ -43,6 +50,12 @@ class NotificationsController < ApplicationController @unread_notification_count = current_user.unread_notifications.count + @grouped_unread_notification_counts = {} + + Notification.types.each_with_object(current_user.unread_notifications.group_by(&:type)) {|(name, type), notifications| + @grouped_unread_notification_counts[name] = notifications.has_key?(type) ? notifications[type].count : 0 + } + respond_to do |format| format.html format.xml { render :xml => @notifications.to_xml } diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index affb08f15..0539da8f7 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -9,7 +9,7 @@ class PostsController < ApplicationController before_filter :set_format_if_malformed_from_status_net, :only => :show before_filter :find_post, :only => [:show, :interactions] - before_filter -> { @css_framework = :bootstrap } + use_bootstrap_for :show respond_to :html, :mobile, diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index 772a80c2a..83f92f3ac 100644 --- a/app/controllers/status_messages_controller.rb +++ b/app/controllers/status_messages_controller.rb @@ -7,7 +7,7 @@ class StatusMessagesController < ApplicationController before_filter :remove_getting_started, :only => [:create] - before_filter -> { @css_framework = :bootstrap }, :only => [:bookmarklet] + use_bootstrap_for :bookmarklet respond_to :html, :mobile, diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb index 42bf14244..6db4e46e4 100644 --- a/app/helpers/notifications_helper.rb +++ b/app/helpers/notifications_helper.rb @@ -1,5 +1,6 @@ module NotificationsHelper include PeopleHelper + include PostsHelper def object_link(note, actors) target_type = note.popup_translation_key @@ -7,13 +8,13 @@ module NotificationsHelper if note.instance_of?(Notifications::Mentioned) if post = note.linked_object - translation(target_type, :actors => actors, :count => actors_count, :post_link => link_to(t('notifications.post'), post_path(post)).html_safe) + translation(target_type, :actors => actors, :count => actors_count, :post_link => link_to(post_page_title(post), post_path(post)).html_safe) else t(note.deleted_translation_key, :actors => actors, :count => actors_count).html_safe end elsif note.instance_of?(Notifications::CommentOnPost) || note.instance_of?(Notifications::AlsoCommented) || note.instance_of?(Notifications::Reshared) || note.instance_of?(Notifications::Liked) if post = note.linked_object - translation(target_type, :actors => actors, :count => actors_count, :post_author => h(post.author_name), :post_link => link_to(t('notifications.post'), post_path(post), 'data-ref' => post.id, :class => 'hard_object_link').html_safe) + translation(target_type, :actors => actors, :count => actors_count, :post_author => h(post.author_name), :post_link => link_to(post_page_title(post), post_path(post), 'data-ref' => post.id, :class => 'hard_object_link').html_safe) else t(note.deleted_translation_key, :actors => actors, :count => actors_count).html_safe end diff --git a/app/models/notification.rb b/app/models/notification.rb index a60d8cdb9..2db32f764 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -91,4 +91,15 @@ private def self.suppress_notification?(recipient, post) post.is_a?(Post) && recipient.is_shareable_hidden?(post) end + + def self.types + { + "also_commented" => "Notifications::AlsoCommented", + "comment_on_post" => "Notifications::CommentOnPost", + "liked" => "Notifications::Liked", + "mentioned" => "Notifications::Mentioned", + "reshared" => "Notifications::Reshared", + "started_sharing" => "Notifications::StartedSharing" + } + end end diff --git a/app/views/notifications/_notify_popup_item.haml b/app/views/notifications/_notify_popup_item.haml index 89da6fa53..10b502d6d 100644 --- a/app/views/notifications/_notify_popup_item.haml +++ b/app/views/notifications/_notify_popup_item.haml @@ -1,4 +1,4 @@ -.notification_element{:data=>{:guid => n.id}, :class => (n.unread ? "unread" : "read")} +.notification_element{:data=>{:guid => n.id, :type => (Notification.types.key(n.type) || '')}, :class => (n.unread ? "unread" : "read")} = person_image_tag n.actors.first, :thumb_small = notification_message_for(n) %div diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 84d64658d..728dfb449 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -637,25 +637,25 @@ en: one: "%{actors} sent you a message." other: "%{actors} sent you a message." comment_on_post: - zero: "%{actors} commented on your %{post_link}." - one: "%{actors} commented on your %{post_link}." - other: "%{actors} commented on your %{post_link}." + zero: "%{actors} commented on your post »%{post_link}«." + one: "%{actors} commented on your post »%{post_link}«." + other: "%{actors} commented on your post »%{post_link}«." also_commented: - zero: "%{actors} also commented on %{post_author}'s %{post_link}." - one: "%{actors} also commented on %{post_author}'s %{post_link}." - other: "%{actors} also commented on %{post_author}'s %{post_link}." + zero: "%{actors} also commented on %{post_author}'s post »%{post_link}«." + one: "%{actors} also commented on %{post_author}'s post »%{post_link}«." + other: "%{actors} also commented on %{post_author}'s post »%{post_link}«." mentioned: - zero: "%{actors} have mentioned you in a %{post_link}." - one: "%{actors} has mentioned you in a %{post_link}." - other: "%{actors} have mentioned you in a %{post_link}." + zero: "%{actors} have mentioned you in the post »%{post_link}«." + one: "%{actors} has mentioned you in the post »%{post_link}«." + other: "%{actors} have mentioned you in the »%{post_link}«." liked: - zero: "%{actors} have liked your %{post_link}." - one: "%{actors} has liked your %{post_link}." - other: "%{actors} have liked your %{post_link}." + zero: "%{actors} have liked your post »%{post_link}«." + one: "%{actors} has liked your post »%{post_link}«." + other: "%{actors} have liked your post »%{post_link}«." reshared: - zero: "%{actors} have reshared your %{post_link}." - one: "%{actors} has reshared your %{post_link}." - other: "%{actors} have reshared your %{post_link}." + zero: "%{actors} have reshared your post »%{post_link}«." + one: "%{actors} has reshared your post »%{post_link}«." + other: "%{actors} have reshared your post »%{post_link}«." post: "post" also_commented_deleted: zero: "%{actors} commented on a deleted post." @@ -675,8 +675,18 @@ en: other: "%{actors} mentioned you in a deleted post." index: notifications: "Notifications" - mark_all_as_read: "Mark All as Read" + mark_all_as_read: "Mark all as read" + mark_read: "Mark read" mark_unread: "Mark unread" + show_all: "show all" + show_unread: "show unread" + all_notifications: "All Notifications" + also_commented: "Also commented" + comment_on_post: "Comment on post" + liked: "Liked" + mentioned: "Mentioned" + reshared: "Reshared" + started_sharing: "Started sharing" and_others: zero: "and nobody else" one: "and one more" diff --git a/config/locales/javascript/javascript.en.yml b/config/locales/javascript/javascript.en.yml index 5d65beb6c..0cf179ba4 100644 --- a/config/locales/javascript/javascript.en.yml +++ b/config/locales/javascript/javascript.en.yml @@ -98,6 +98,10 @@ en: conversation: participants: "Participants" + notifications: + mark_read: "Mark read" + mark_unread: "Mark unread" + stream: hide: "Hide" public: "Public" diff --git a/spec/controllers/jasmine_fixtures/notifications_spec.rb b/spec/controllers/jasmine_fixtures/notifications_spec.rb new file mode 100644 index 000000000..8851eb02d --- /dev/null +++ b/spec/controllers/jasmine_fixtures/notifications_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper' + +describe NotificationsController do + describe '#index' do + before do + sign_in :user, alice + @post = FactoryGirl.create(:status_message) + FactoryGirl.create(:notification, :recipient => alice, :target => @post) + get :read_all + FactoryGirl.create(:notification, :recipient => alice, :target => @post) + eve.share_with(alice.person, eve.aspects.first) + end + + it "generates a jasmine fixture", :fixture => true do + get :index + save_fixture(html_for("body"), "notifications") + end + end +end diff --git a/spec/controllers/notifications_controller_spec.rb b/spec/controllers/notifications_controller_spec.rb index 256b7f465..5b2e37452 100644 --- a/spec/controllers/notifications_controller_spec.rb +++ b/spec/controllers/notifications_controller_spec.rb @@ -120,7 +120,21 @@ describe NotificationsController do Nokogiri(response.body).css('.aspect_membership').should_not be_empty end - + end + + describe "filter notifications" do + it "supports filtering by notification type" do + eve.share_with(alice.person, eve.aspects.first) + get :index, "type" => "started_sharing" + assigns[:notifications].count.should == 1 + end + + it "supports filtering by read/unread" do + get :read_all + 2.times { FactoryGirl.create(:notification, :recipient => alice, :target => @post) } + get :index, "show" => "unread" + assigns[:notifications].count.should == 2 + end end end end diff --git a/spec/helpers/notifications_helper_spec.rb b/spec/helpers/notifications_helper_spec.rb index 1015818eb..94817dd0a 100644 --- a/spec/helpers/notifications_helper_spec.rb +++ b/spec/helpers/notifications_helper_spec.rb @@ -77,7 +77,7 @@ describe NotificationsHelper do output.should include I18n.t("#{@notification.popup_translation_key}", :actors => notification_people_link(@notification), :count => @notification.actors.count, - :post_link => "#{t('notifications.post')}") + :post_link => link_to(post_page_title(@post), post_path(@post), 'data-ref' => @post.id, :class => 'hard_object_link').html_safe) end context 'when post is deleted' do